Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static Vector3 GetSurfaceNormal(FLVER.Vertex[] face)
- {
- Vector3 v1 = face[2].Position - face[1].Position;
- Vector3 v2 = face[1].Position - face[0].Position;
- return Vector3.Normalize(Vector3.Cross(v1, v2));
- }
- private static Vector4 MakeTangent(Vector3 normal)
- {
- Vector3 t1 = Vector3.Cross(normal, Vector3.UnitX);
- Vector3 t2 = Vector3.Cross(normal, Vector3.UnitY);
- if (t1.Length() > t2.Length())
- return new Vector4(t1, 1);
- else
- return new Vector4(t2, 1);
- }
- private static void FlattenMesh(FLVER2.Mesh mesh)
- {
- var faceGroups = new List<List<FLVER.Vertex[]>>();
- foreach (var face in mesh.GetFaces())
- {
- Vector3 normal = GetSurfaceNormal(face);
- var group = faceGroups.Find(fg => (normal - GetSurfaceNormal(fg[0])).Length() < 0.0001f); // Heuristic: my ass
- if (group == null)
- {
- group = new List<FLVER.Vertex[]>();
- faceGroups.Add(group);
- }
- group.Add(face);
- }
- var newVertices = new List<FLVER.Vertex>();
- var newIndices = new List<int>();
- foreach (var group in faceGroups)
- {
- var indices = new Dictionary<FLVER.Vertex, int>();
- int addVertex(FLVER.Vertex vert, Vector3 norm, Vector4 tang)
- {
- vert.Normal = norm;
- for (int i = 0; i < vert.Tangents.Count; i++)
- vert.Tangents[i] = tang;
- if (!indices.ContainsKey(vert))
- {
- indices[vert] = newVertices.Count;
- newVertices.Add(new FLVER.Vertex(vert));
- }
- return indices[vert];
- }
- Vector3 normal = GetSurfaceNormal(group[0]);
- Vector4 tangent = MakeTangent(normal);
- foreach (var face in group)
- {
- newIndices.Add(addVertex(face[0], normal, tangent));
- newIndices.Add(addVertex(face[1], normal, tangent));
- newIndices.Add(addVertex(face[2], normal, tangent));
- }
- }
- mesh.Vertices = newVertices;
- mesh.FaceSets.ForEach(fs => fs.Indices = newIndices);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement