public static VNormal[] ComputeNormals(IMesh mesh, bool optimize) { var vnorms = new VNormal[mesh.NumVerts]; var fnorms = new Vector3[mesh.NumFaces]; for (var index = 0; index < mesh.NumVerts; index++) { vnorms[index] = new VNormal(); } for (var index = 0; index < mesh.NumFaces; index++) { var face = mesh.Faces[index]; Vector3 v0 = mesh.Verts[(int)face.V[0]].ToVector3(); Vector3 v1 = mesh.Verts[(int)face.V[1]].ToVector3(); Vector3 v2 = mesh.Verts[(int)face.V[2]].ToVector3(); fnorms[index] = Vector3.Cross((v1 - v0), (v2 - v1)); for (var j = 0; j < 3; j++) { vnorms[(int)face.V[j]].AddNormal(fnorms[index], optimize ? 1 : face.SmGroup); } fnorms[index].Normalize(); } for (var index = 0; index < mesh.NumVerts; index++) { vnorms[index].Normalize(); } return(vnorms); }
// Normalize each normal in the list public void Normalize() { VNormal ptr = next; VNormal prev = this; while (ptr != null) { if ((ptr.smooth & smooth) != 0) { norm += ptr.norm; prev.next = ptr.next; ptr = prev.next; } else { prev = ptr; ptr = ptr.next; } } norm.Normalize(); if (next != null) { next.Normalize(); } }
public static VNormal[] ComputeNormals(IMesh mesh) { var vnorms = new VNormal[mesh.NumVerts]; var fnorms = new Vector3[mesh.NumFaces]; for (var index = 0; index < mesh.NumVerts; index++) { vnorms[index] = new VNormal(); } for (var index = 0; index < mesh.NumFaces; index++) { var face = mesh.Faces[index]; Vector3 v0 = mesh.Verts[(int)face.V[0]].ToVector3(); Vector3 v1 = mesh.Verts[(int)face.V[1]].ToVector3(); Vector3 v2 = mesh.Verts[(int)face.V[2]].ToVector3(); fnorms[index] = Vector3.Cross((v1 - v0), (v2 - v1)); for (var j = 0; j < 3; j++) { vnorms[(int)face.V[j]].AddNormal(fnorms[index], face.SmGroup); } fnorms[index].Normalize(); } for (var index = 0; index < mesh.NumVerts; index++) { vnorms[index].Normalize(); } return vnorms; }
public VNormal(Vector3 n, uint s) { next = null; init = true; norm = n; smooth = s; }
public VNormal() { smooth = 0; next = null; init = false; norm = new Vector3(0, 0, 0); }
public void AddNormal(Vector3 n, uint s) { if (((s & smooth) == 0) && init) { if (next != null) next.AddNormal(n, s); else { next = new VNormal(n, s); } } else { norm += n; smooth |= s; init = true; } }
public void AddNormal(Vector3 n, uint s) { if (((s & smooth) == 0) && init) { if (next != null) { next.AddNormal(n, s); } else { next = new VNormal(n, s); } } else { norm += n; smooth |= s; init = true; } }
int CreateGlobalVertex(IMesh mesh, int face, int facePart, List<GlobalVertex> vertices, bool hasUV, bool hasUV2, VNormal[] vnorms, List<GlobalVertex>[] verticesAlreadyExported, IISkinContextData skinContextData) { var faceObject = mesh.Faces[face]; var vertexIndex = (int)faceObject.V[facePart]; var vertex = new GlobalVertex { BaseIndex = vertexIndex, Position = mesh.Verts[vertexIndex], Normal = vnorms[vertexIndex].GetNormal(verticesAlreadyExported != null ? 1 : faceObject.SmGroup) }; if (hasUV) { var tvertexIndex = (int)mesh.TvFace[face].T[facePart]; vertex.UV = Loader.Global.Point2.Create(mesh.TVerts[tvertexIndex].X, mesh.TVerts[tvertexIndex].Y); } if (hasUV2) { var tvertexIndex = (int)mesh.MapFaces(2)[face].T[facePart]; vertex.UV2 = Loader.Global.Point2.Create(mesh.MapVerts(2)[tvertexIndex].X, mesh.MapVerts(2)[tvertexIndex].Y); } if (skinContextData != null) { float weight0 = 0; float weight1 = 0; float weight2 = 0; int bone0 = bonesCount; int bone1 = bonesCount; int bone2 = bonesCount; int bone3 = bonesCount; int nbBones = skinContextData.GetNumAssignedBones(vertexIndex); if (nbBones > 0) { bone0 = skinContextData.GetAssignedBone(vertexIndex, 0); weight0 = skinContextData.GetBoneWeight(vertexIndex, 0); } if (nbBones > 1) { bone1 = skinContextData.GetAssignedBone(vertexIndex, 1); weight1 = skinContextData.GetBoneWeight(vertexIndex, 1); } if (nbBones > 2) { bone2 = skinContextData.GetAssignedBone(vertexIndex, 2); weight2 = skinContextData.GetBoneWeight(vertexIndex, 2); } if (nbBones > 3) { bone3 = skinContextData.GetAssignedBone(vertexIndex, 3); } if (nbBones == 0) { weight0 = 1.0f; bone0 = bonesCount; } if (nbBones > 4) { RaiseError("Too many bones influences per vertex: " + nbBones + ". Babylon.js only support 4 bones influences per vertex.", 2); } vertex.Weights = Loader.Global.Point4.Create(weight0, weight1, weight2, 1.0 - weight0 - weight1 - weight2); vertex.BonesIndices = (bone3 << 24) | (bone2 << 16) | (bone1 << 8) | bone0; } if (verticesAlreadyExported != null) { if (verticesAlreadyExported[vertexIndex] != null) { var index = verticesAlreadyExported[vertexIndex].IndexOf(vertex); if (index > -1) { return verticesAlreadyExported[vertexIndex][index].CurrentIndex; } } else { verticesAlreadyExported[vertexIndex] = new List<GlobalVertex>(); } vertex.CurrentIndex = vertices.Count; verticesAlreadyExported[vertexIndex].Add(vertex); } vertices.Add(vertex); return vertices.Count - 1; }