public static VertexColorTexture Convert( MeshVertex meshVertex ) { VertexColorTexture v; v.Position = meshVertex.Position; v.Color = meshVertex.Color0; v.TexCoord = meshVertex.TexCoord0; return v; }

void AddSpan(MeshConstructor mc, MeshVertex[,] mx, int latitudeSpanIndex, int longitudeSpanIndex, int submesh = 0) { MeshVertex topLeft = mx[latitudeSpanIndex + 1, longitudeSpanIndex]; MeshVertex topRight = mx[latitudeSpanIndex + 1, longitudeSpanIndex + 1]; MeshVertex bottomLeft = mx[latitudeSpanIndex, longitudeSpanIndex]; MeshVertex bottomRight = mx[latitudeSpanIndex, longitudeSpanIndex + 1]; mc.AddTriangle(topLeft, topRight, bottomLeft, submesh); mc.AddTriangle(bottomRight, bottomLeft, topRight, submesh); }

public void AddTriangle(MeshVertex a, MeshVertex b, MeshVertex c, int submeshIndex = 0) { AddVertex(a); AddVertex(b); AddVertex(c); triangles[submeshIndex].Add(a.index); triangles[submeshIndex].Add(b.index); triangles[submeshIndex].Add(c.index); }

public void AddVertex(MeshVertex v) { if (v.index != -1) { return; } vertices.Add(v.position); uv.Add(v.uv); v.index = vertices.Count - 1; normals.Add(v.normal); }

public static VertexColorTextureTBN Convert( MeshVertex meshVertex ) { VertexColorTextureTBN v; v.Position = meshVertex.Position; v.Tangent = MathUtil.ToHalf4( meshVertex.Tangent, 0 ); v.Binormal = MathUtil.ToHalf4( meshVertex.Binormal, 0 ); v.Normal = MathUtil.ToHalf4( meshVertex.Normal, 0 ); v.Color = meshVertex.Color0; v.TexCoord = meshVertex.TexCoord0; return v; }

public static VertexColorTextureTBNSkinned Convert ( MeshVertex meshVertex ) { VertexColorTextureTBNSkinned v; v.Position = meshVertex.Position; v.Tangent = MathUtil.ToHalf4( meshVertex.Tangent, 0 ); v.Binormal = MathUtil.ToHalf4( meshVertex.Binormal, 0 ); v.Normal = MathUtil.ToHalf4( meshVertex.Normal, 0 ); v.Color = meshVertex.Color0; v.TexCoord = meshVertex.TexCoord0; v.SkinIndices = meshVertex.SkinIndices; v.SkinWeights = meshVertex.SkinWeights; return v; }

Mesh GenerateMesh() { var mc = new MeshConstructor(); mc.mesh.name = name; mc.SetSubmeshCount(2); latitudes = latitudeSpans + 1; longitudes = longitudeSpans + 1; pictureLatitudeSpans = 2 * halfLatitudePictureSpans; pictureLongitudeSpans = 2 * halfLongitudePictureSpans; pictureLatitudes = pictureLatitudeSpans + 1; pictureLongitudes = pictureLongitudeSpans + 1; pictureSpans = new bool[latitudeSpans, longitudeSpans]; baseMatrix = new MeshVertex[latitudes, longitudes]; pictureMatrix = new MeshVertex[latitudes, longitudes]; for (int latitudePictureSpanIndex = 0; latitudePictureSpanIndex < pictureLatitudeSpans; latitudePictureSpanIndex++) { for (int longitudePictureSpanIndex = 0; longitudePictureSpanIndex < pictureLongitudeSpans; longitudePictureSpanIndex++) { int latitudeSpanIndex = latitudes/2-halfLatitudePictureSpans+latitudePictureSpanIndex; int longitudeSpanIndex = Extensions.Modulo(-halfLongitudePictureSpans+longitudePictureSpanIndex, longitudeSpans); pictureSpans[latitudeSpanIndex, longitudeSpanIndex] = true; } } for (int latitudeIndex = 0; latitudeIndex <= latitudeSpans; latitudeIndex++) { for (int longitudeIndex = 0; longitudeIndex <= longitudeSpans; longitudeIndex++) { baseMatrix[latitudeIndex, longitudeIndex] = new MeshVertex( position: getSpherePoint(latitudeIndex, longitudeIndex), uv: new Vector2(1f * longitudeIndex / longitudeSpans, 1f * latitudeIndex / latitudeSpans), normal: getSpherePoint(latitudeIndex, longitudeIndex).normalized ); } } for (int pictureLatitudeIndex = 0; pictureLatitudeIndex <= 2 * halfLatitudePictureSpans; pictureLatitudeIndex++) { for (int pictureLongitudeIndex = 0; pictureLongitudeIndex <= 2 * halfLongitudePictureSpans; pictureLongitudeIndex++) { int latitudeIndex = latitudes/2 -halfLatitudePictureSpans + pictureLatitudeIndex; int longitudeIndex = Extensions.Modulo(-halfLongitudePictureSpans + pictureLongitudeIndex, longitudeSpans); //Debug.LogFormat("Adding picture vertex {0}, {1}", latitudeIndex, longitudeIndex); pictureMatrix[latitudeIndex, longitudeIndex] = new MeshVertex( position: getSpherePoint(latitudeIndex, longitudeIndex), uv: new Vector2(1f * pictureLongitudeIndex / pictureLongitudeSpans, 1f * pictureLatitudeIndex / pictureLatitudeSpans), normal: getSpherePoint(latitudeIndex, longitudeIndex).normalized ); if (longitudeIndex == 0) { longitudeIndex = longitudes - 1; //Debug.LogFormat("Adding picture vertex {0}, {1}", latitudeIndex, longitudeIndex); pictureMatrix[latitudeIndex, longitudeIndex] = new MeshVertex( position: getSpherePoint(latitudeIndex, longitudeIndex), uv: new Vector2(1f * pictureLongitudeIndex / pictureLongitudeSpans, 1f * pictureLatitudeIndex / pictureLatitudeSpans), normal: getSpherePoint(latitudeIndex, longitudeIndex).normalized ); } } } for (int latitudeSpanIndex = 0; latitudeSpanIndex < latitudeSpans; latitudeSpanIndex++) { for (int longitudeSpanIndex = 0; longitudeSpanIndex < longitudeSpans; longitudeSpanIndex++) { if (isPictureSpan(latitudeSpanIndex, longitudeSpanIndex)) { AddSpan(mc, pictureMatrix, latitudeSpanIndex, longitudeSpanIndex, 1); } AddSpan(mc, baseMatrix, latitudeSpanIndex, longitudeSpanIndex); } } return mc.Done(); }

public Meshold() { Edges = new List<HalfEdge>(); Faces = new List<HalfEdgeFace>(); Vertices = new List<MeshVertex>(); double size = 1.0; var v0 = new MeshVertex(new Vect3(size / 2.0, -size / 2.0, size / 2.0)); var v1 = new MeshVertex(new Vect3(-size / 2.0, -size / 2.0, size / 2.0)); var v2 = new MeshVertex(new Vect3(-size / 2.0, -size / 2.0, -size / 2.0)); var v3 = new MeshVertex(new Vect3(size / 2.0, -size / 2.0, -size / 2.0)); var v4 = new MeshVertex(new Vect3(size / 2.0, size / 2.0, size / 2.0)); var v5 = new MeshVertex(new Vect3(-size / 2.0, size / 2.0, size / 2.0)); var v6 = new MeshVertex(new Vect3(-size / 2.0, size / 2.0, -size / 2.0)); var v7 = new MeshVertex( new Vect3(0, 0, 0)); var face1 = new HalfEdgeFace(); var face2 = new HalfEdgeFace(); var face3 = new HalfEdgeFace(); var face4 = new HalfEdgeFace(); var face5 = new HalfEdgeFace(); var face6 = new HalfEdgeFace(); Faces.AddRange(new[]{face1,face2,face3,face4,face5,face6}); var he1 = new HalfEdge { ToVertex = v1, HalfEdgeFace = face1 }; var he2 = new HalfEdge { ToVertex = v2, HalfEdgeFace = face1 }; var he3 = new HalfEdge { ToVertex = v3, HalfEdgeFace = face1 }; var he4 = new HalfEdge { ToVertex = v0, HalfEdgeFace = face1 }; he1.Next = he2; he2.Next = he3; he3.Next = he4; he4.Next = he1; face1.HalfEdge = v0.Edge = he1; var he5 = new HalfEdge { ToVertex = v1, HalfEdgeFace = face2 }; var he6 = new HalfEdge { ToVertex = v5, HalfEdgeFace = face2 }; var he7 = new HalfEdge { ToVertex = v6, HalfEdgeFace = face2 }; var he8 = new HalfEdge { ToVertex = v2, HalfEdgeFace = face2 }; he5.Next = he6; he6.Next = he7; he7.Next = he8; he8.Next = he5; face2.HalfEdge = v2.Edge = he5; var he9 = new HalfEdge { ToVertex = v5, HalfEdgeFace = face3 }; var he10 = new HalfEdge { ToVertex = v4, HalfEdgeFace = face3 }; var he11 = new HalfEdge { ToVertex = v7, HalfEdgeFace = face3 }; var he12 = new HalfEdge { ToVertex = v6, HalfEdgeFace = face3 }; he9.Next = he10; he10.Next = he11; he11.Next = he12; he12.Next = he9; face3.HalfEdge = v6.Edge = he9; var he13 = new HalfEdge { ToVertex = v4, HalfEdgeFace = face4 }; var he14 = new HalfEdge { ToVertex = v0, HalfEdgeFace = face4 }; var he15 = new HalfEdge { ToVertex = v3, HalfEdgeFace = face4 }; var he16 = new HalfEdge { ToVertex = v7, HalfEdgeFace = face4 }; he13.Next = he14; he14.Next = he15; he15.Next = he16; he16.Next = he13; face4.HalfEdge = v7.Edge = he13; var he17 = new HalfEdge { ToVertex = v2, HalfEdgeFace = face5 }; var he18 = new HalfEdge { ToVertex = v6, HalfEdgeFace = face5 }; var he19 = new HalfEdge { ToVertex = v7, HalfEdgeFace = face5 }; var he20 = new HalfEdge { ToVertex = v3, HalfEdgeFace = face5 }; he17.Next = he18; he18.Next = he19; he19.Next = he20; he20.Next = he17; face5.HalfEdge = v3.Edge = he17; var he21 = new HalfEdge { ToVertex = v1, HalfEdgeFace = face6 }; var he22 = new HalfEdge { ToVertex = v5, HalfEdgeFace = face6 }; var he23 = new HalfEdge { ToVertex = v4, HalfEdgeFace = face6 }; var he24 = new HalfEdge { ToVertex = v0, HalfEdgeFace = face6 }; he21.Next = he22; he22.Next = he23; he23.Next = he24; he24.Next = he21; face6.HalfEdge = v0.Edge = he21; MapPair(he1, he21); MapPair(he2, he5); MapPair(he3, he17); MapPair(he4, he15); //MapPair(he5, he2); MapPair(he6, he24); MapPair(he7, he9); MapPair(he8, he18); //MapPair(he9, he7); MapPair(he10, he23); MapPair(he11, he13); MapPair(he12, he19); //MapPair(he13, he11); MapPair(he14, he22); //MapPair(he15, he4); MapPair(he16, he20); //MapPair(he17, he3); //MapPair(he18, he8); //MapPair(he19, he12); //MapPair(he20, he16); //MapPair(he21, he1); //MapPair(he22, he14); //MapPair(he23, he10); //MapPair(he24, he6); }