@@ -34,7 +34,6 @@
#endregion
using System .Collections .Generic ;
using System .Linq ;
using UnityEngine ;
@@ -215,167 +214,6 @@ public static Mesh MakePlane(int w, int h, PLANE plane, bool _01, bool cw, bool
return mesh ;
}
public static Mesh MakeSphere (float radius , int divr , int divh )
{
float a , da , yp , ya , yda , yf ;
float U , V , dU , dV ;
if (divr < 3 ) divr = 3 ;
if (divh < 3 ) divh = 3 ;
int numVerts = (divh - 2 ) * divr + 2 + (divh - 2 );
int numInds = (divr * 2 + divr * 2 * (divh - 3 )) * 3 ;
Mesh mesh = new Mesh ();
Vector3 [] vertices = new Vector3 [numVerts ];
Vector2 [] uvs = new Vector2 [numVerts ];
int [] inds = new int [numInds ];
int vidx = 0 ; // current vertex
int fidx = 0 ; // current index
// top and bottom vertices
vertices [vidx ++ ] = new Vector3 (0 , radius , 0 );
vertices [vidx ++ ] = new Vector3 (0 , - radius , 0 );
ya = 0 ;
yda = Mathf .PI / (divh - 1 );
da = (2 * Mathf .PI ) / divr ;
// all other vertices
for (int y = 0 ; y < divh - 2 ; y ++ )
{
ya += yda ;
yp = Mathf .Cos (ya ) * radius ;
yf = Mathf .Sin (ya ) * radius ;
a = 0 ;
for (int x = 0 ; x < divr ; x ++ )
{
vertices [vidx ++ ] = new Vector3 (Mathf .Cos (a ) * yf , yp , Mathf .Sin (a ) * yf );
if (x == divr - 1 )
{
// add an extra vertex in the end of each longitudinal circunference
Vector3 tmp = vertices [y * (divr + 1 ) + 2 ];
vertices [vidx ++ ] = new Vector3 (tmp .x , tmp .y , tmp .z );
}
a += da ;
}
}
a = 0 ;
U = 0 ;
dU = 1 . 0 f / divr ;
dV = V = 1 . 0 f / divh ;
// top indices
for (int x = 0 ; x < divr ; x ++ )
{
int [] v = { 0 , 2 + x + 1 , 2 + x };
inds [fidx ++ ] = v [0 ];
inds [fidx ++ ] = v [1 ];
inds [fidx ++ ] = v [2 ];
uvs [v [0 ]].x = U ;
uvs [v [0 ]].y = 0 ;
uvs [v [1 ]].x = U + dU ;
uvs [v [1 ]].y = V ;
uvs [v [2 ]].x = U ;
uvs [v [2 ]].y = V ;
U += dU ;
}
da = 1 . 0 f / (divr + 1 );
int offv = 2 ;
// create main body faces
for (int x = 0 ; x < divh - 3 ; x ++ )
{
U = 0 ;
for (int y = 0 ; y < divr ; y ++ )
{
int [] v = { offv + y , offv + (divr + 1 ) + y + 1 , offv + (divr + 1 ) + y };
inds [fidx ++ ] = v [0 ];
inds [fidx ++ ] = v [1 ];
inds [fidx ++ ] = v [2 ];
uvs [v [0 ]].x = U ;
uvs [v [0 ]].y = V ;
uvs [v [1 ]].x = U + dU ;
uvs [v [1 ]].y = V + dV ;
uvs [v [2 ]].x = U ;
uvs [v [2 ]].y = V + dV ;
int [] vv = { offv + y , offv + y + 1 , offv + y + 1 + (divr + 1 ) };
inds [fidx ++ ] = vv [0 ];
inds [fidx ++ ] = vv [1 ];
inds [fidx ++ ] = vv [2 ];
uvs [vv [0 ]].x = U ;
uvs [vv [0 ]].y = V ;
uvs [vv [1 ]].x = U + dU ;
uvs [vv [1 ]].y = V ;
uvs [vv [2 ]].x = U + dU ;
uvs [vv [2 ]].y = V + dV ;
U += dU ;
}
V += dV ;
offv += divr + 1 ;
}
int s = numVerts - divr - 1 ;
U = 0 ;
// bottom faces
for (int x = 0 ; x < divr ; x ++ )
{
int [] v = { 1 , s + x , s + x + 1 };
inds [fidx ++ ] = v [0 ];
inds [fidx ++ ] = v [1 ];
inds [fidx ++ ] = v [2 ];
uvs [v [0 ]].x = U ;
uvs [v [0 ]].y = 1 . 0 f ;
uvs [v [1 ]].x = U ;
uvs [v [1 ]].y = V ;
uvs [v [2 ]].x = U + dU ;
uvs [v [2 ]].y = V ;
U += dU ;
}
mesh .vertices = vertices ;
mesh .uv = uvs ;
mesh .triangles = inds ;
;
mesh .RecalculateBounds ();
mesh .RecalculateNormals ();
mesh .hideFlags = HideFlags .DontSave ;
SolveTangents (mesh );
return mesh ;
}
public static Mesh SetupRingSegmentMesh (int SegmentCount , int SegmentDetail , float InnerRadius , float OuterRadius , float BoundsShift )
{
var mesh = new Mesh ();
@@ -487,35 +325,29 @@ public static Vector3 SolveNormal(Vector3 v1, Vector3 v2, Vector3 v3)
return Vector3 .Cross (vt1 , vt2 ).normalized ;
}
public static void SolveTangents (Mesh theMesh )
public static void SolveTangents (this Mesh theMesh , ref int [] indices , ref List < Vector3 > vertices , ref List < Vector3 > normals , ref List < Vector2 > uvs )
{
int vertexCount = theMesh .vertexCount ;
Vector3 [] vertices = theMesh .vertices ;
Vector3 [] normals = theMesh .normals ;
Vector2 [] texcoords = theMesh .uv ;
int vertexCount = vertices .Count ;
int triangleCount = indices .Length / 3 ;
int [] triangles = theMesh .triangles ;
int triangleCount = triangles .Length / 3 ;
Vector4 [] tangents = new Vector4 [vertexCount ];
List <Vector4 > tangents = new List <Vector4 >(vertexCount );
Vector3 [] tan1 = new Vector3 [vertexCount ];
Vector3 [] tan2 = new Vector3 [vertexCount ];
int tri = 0 ;
int triangle = 0 ;
for (int i = 0 ; i < ( triangleCount ) ; i ++ )
for (int i = 0 ; i < triangleCount ; i ++ )
{
int i1 = triangles [ tri ];
int i2 = triangles [ tri + 1 ];
int i3 = triangles [ tri + 2 ];
int i1 = indices [ triangle ];
int i2 = indices [ triangle + 1 ];
int i3 = indices [ triangle + 2 ];
Vector3 v1 = vertices [i1 ];
Vector3 v2 = vertices [i2 ];
Vector3 v3 = vertices [i3 ];
Vector2 w1 = texcoords [i1 ];
Vector2 w2 = texcoords [i2 ];
Vector2 w3 = texcoords [i3 ];
Vector2 w1 = uvs [i1 ];
Vector2 w2 = uvs [i2 ];
Vector2 w3 = uvs [i3 ];
float x1 = v2 .x - v1 .x ;
float x2 = v3 .x - v1 .x ;
@@ -542,7 +374,7 @@ public static void SolveTangents(Mesh theMesh)
tan2 [i2 ] += tdir ;
tan2 [i3 ] += tdir ;
tri += 3 ;
triangle += 3 ;
}
for (int i = 0 ; i < (vertexCount ); i ++ )
@@ -553,14 +385,9 @@ public static void SolveTangents(Mesh theMesh)
// Gram-Schmidt orthogonalize
Vector3 .OrthoNormalize (ref n , ref t );
tangents [i ].x = t .x ;
tangents [i ].y = t .y ;
tangents [i ].z = t .z ;
// Calculate handedness
tangents [i ].w = (Vector3 .Dot (Vector3 .Cross (n , t ), tan2 [i ]) < 0 . 0 f ) ? - 1 . 0 f : 1 . 0 f ;
tangents .Add (VectorHelper .MakeFrom (t , (Vector3 .Dot (Vector3 .Cross (n , t ), tan2 [i ]) < 0 . 0 f ) ? - 1 . 0 f : 1 . 0 f ));
}
theMesh .SetTangents (tangents . ToList () );
theMesh .SetTangents (tangents );
}
}