@@ -48,7 +48,7 @@ public static int[] ToInt(this ushort[] triangles2D)
4848 {
4949 return Array . ConvertAll ( triangles2D , i => ( int ) i ) ;
5050 }
51-
51+
5252 public static ushort [ ] ToUShort ( this int [ ] triangles3D )
5353 {
5454 return Array . ConvertAll ( triangles3D , i => ( ushort ) i ) ;
@@ -63,7 +63,7 @@ public static string GetAreaInfo(Vector2[] vertices2D, ushort[] triangles2D, Tex
6363 Vector2 a = vertices2D [ triangles2D [ i ] ] ;
6464 Vector2 b = vertices2D [ triangles2D [ i + 1 ] ] ;
6565 Vector2 c = vertices2D [ triangles2D [ i + 2 ] ] ;
66- area += ( a . x * ( b . y - c . y ) + b . x * ( c . y - a . y ) + c . x * ( a . y - b . y ) ) ;
66+ area += a . x * ( b . y - c . y ) + b . x * ( c . y - a . y ) + c . x * ( a . y - b . y ) ;
6767 }
6868
6969 area *= 0.5f * textureInfo . pixelPerUnit * textureInfo . pixelPerUnit ;
@@ -73,14 +73,7 @@ public static string GetAreaInfo(Vector2[] vertices2D, ushort[] triangles2D, Tex
7373 return $ "{ vertices2D . Length } verts, { triangles2D . Length / 3 } tris, { meshAreaRatio : F2} % overdraw";
7474 }
7575
76- public static Mesh Create ( Vector3 [ ] v , int [ ] t , TextureInfo textureInfo , bool splitVertices )
77- {
78- Mesh mesh = new Mesh ( ) ;
79- Update ( mesh , v , t , textureInfo , splitVertices ) ;
80- return mesh ;
81- }
82-
83- public static void Update ( Mesh mesh , Vector3 [ ] v , int [ ] t , TextureInfo textureInfo , bool splitVertices )
76+ public static Mesh Update ( Mesh mesh , Vector3 [ ] v , int [ ] t , TextureInfo textureInfo , bool splitVertices )
8477 {
8578 Vector2 [ ] uv = new Vector2 [ v . Length ] ;
8679
@@ -91,6 +84,11 @@ public static void Update(Mesh mesh, Vector3[] v, int[] t, TextureInfo textureIn
9184 uv [ i ] . y /= textureInfo . rect . size . y ;
9285 }
9386
87+ if ( mesh == null )
88+ {
89+ mesh = new Mesh ( ) ;
90+ }
91+
9492 mesh . Clear ( ) ;
9593 mesh . SetVertices ( v ) ;
9694 mesh . SetUVs ( 0 , uv ) ;
@@ -102,6 +100,8 @@ public static void Update(Mesh mesh, Vector3[] v, int[] t, TextureInfo textureIn
102100 {
103101 mesh . SplitVertices ( ) ;
104102 }
103+
104+ return mesh ;
105105 }
106106
107107 //reference 'https://github.com/sr4dev/Unity-SpriteAssist/issues/38 by MateuszRe'
@@ -129,7 +129,7 @@ public static void SplitVertices(this Mesh mesh)
129129 {
130130 var triangles = mesh . GetTriangles ( s ) ;
131131 int tCnt = triangles . Length ;
132-
132+
133133 for ( int t = 0 ; t < tCnt ; t ++ )
134134 {
135135 int index = triangles [ t ] ;
@@ -138,8 +138,10 @@ public static void SplitVertices(this Mesh mesh)
138138 vertices . Add ( vertices [ index ] ) ;
139139 if ( hasUvs )
140140 uvs . Add ( uvs [ index ] ) ;
141+
141142 if ( hasColors )
142143 colors . Add ( colors [ index ] ) ;
144+
143145 if ( hasTangents )
144146 tangents . Add ( tangents [ index ] ) ;
145147
@@ -158,8 +160,10 @@ public static void SplitVertices(this Mesh mesh)
158160
159161 if ( hasUvs )
160162 mesh . SetUVs ( 0 , uvs ) ;
163+
161164 if ( hasColors )
162165 mesh . SetColors ( colors ) ;
166+
163167 if ( hasTangents )
164168 mesh . SetTangents ( tangents ) ;
165169
@@ -228,9 +232,9 @@ public static void WeldVertices(Mesh mesh, float threshold = 0.005f, float bucke
228232 {
229233 tempIndex = tempList [ j ] ;
230234 if ( Vector3 . SqrMagnitude ( vertBuffer [ tempIndex ] - vertices [ i ] ) < threshold
231- && ( ! hasUVs || uvs [ i ] == uvsBuffer [ tempIndex ] )
232- && ( ! hasColors || colors [ i ] == colorBuffer [ tempIndex ] )
233- && ( ignoreNormals || normals [ i ] == normalBuffer [ tempIndex ] ) )
235+ && ( ! hasUVs || uvs [ i ] == uvsBuffer [ tempIndex ] )
236+ && ( ! hasColors || colors [ i ] == colorBuffer [ tempIndex ] )
237+ && ( ignoreNormals || normals [ i ] == normalBuffer [ tempIndex ] ) )
234238 {
235239 indexBuffer . Add ( tempList [ j ] ) ;
236240 goto skip ;
@@ -240,16 +244,18 @@ public static void WeldVertices(Mesh mesh, float threshold = 0.005f, float bucke
240244 vertBuffer . Add ( vertices [ i ] ) ;
241245 if ( hasColors )
242246 colorBuffer . Add ( colors [ i ] ) ;
247+
243248 if ( hasUVs )
244249 uvsBuffer . Add ( uvs [ i ] ) ;
250+
245251 if ( ! ignoreNormals )
246252 normalBuffer . Add ( normals [ i ] ) ;
247253
248254 tempList . Add ( newSize ) ;
249255 indexBuffer . Add ( newSize ) ;
250256 newSize ++ ;
251257
252- skip : ;
258+ skip : ;
253259 }
254260
255261 for ( int i = 0 ; i < triangles . Length ; i ++ )
@@ -263,6 +269,7 @@ public static void WeldVertices(Mesh mesh, float threshold = 0.005f, float bucke
263269
264270 if ( hasColors )
265271 mesh . SetColors ( colorBuffer ) ;
272+
266273 if ( hasUVs )
267274 mesh . SetUVs ( 0 , uvsBuffer ) ;
268275
@@ -273,6 +280,5 @@ public static void WeldVertices(Mesh mesh, float threshold = 0.005f, float bucke
273280
274281 if ( printResults ) Debug . Log ( "Reduced " + mesh . name + " from " + vertices . Length + " to " + vertBuffer . Count ) ;
275282 }
276-
277283 }
278- }
284+ }
0 commit comments