public static void RecalculateNormals(MeshDeformer deformer) { for (int i = 0; i < deformer.Scaffolds.Length; ++i) { ScaffoldWrapper scaffold = deformer.Scaffolds[i]; if (scaffold.Obj == null) { continue; } MeshFilter filter = scaffold.Obj.GetComponent <MeshFilter>(); MeshCollider collider = scaffold.Obj.GetComponent <MeshCollider>(); if (filter != null) { Mesh colliderMesh = null; if (collider != null) { collider.sharedMesh = Object.Instantiate(deformer.ColliderOriginal); collider.sharedMesh.name = deformer.ColliderOriginal.name + " Deformed"; colliderMesh = collider.sharedMesh; } filter.sharedMesh = Object.Instantiate(deformer.Original); filter.sharedMesh.name = deformer.Original.name + " Deformed"; scaffold.Wrap(filter.sharedMesh, colliderMesh, deformer.Axis, scaffold.CurveIndices, scaffold.SliceCount); scaffold.Deform(deformer, deformer.Original, deformer.ColliderOriginal); scaffold.RecalculateNormals(); } } ScaffoldWrapper prev = null; if (deformer.Loop) { prev = deformer.Scaffolds[deformer.Scaffolds.Length - 1]; } for (int i = 0; i < deformer.Scaffolds.Length; ++i) { ScaffoldWrapper scaffold = deformer.Scaffolds[i]; scaffold.SlerpContacts(deformer, deformer.Original, deformer.ColliderOriginal, prev, null); scaffold = prev; } }
public static void Subdivide() { GameObject selected = Selection.activeObject as GameObject; MeshDeformer deformer = selected.GetComponentInParent <MeshDeformer>(); MeshFilter meshFilter = null; if (deformer != null && deformer.Original != null) { if (!EditorUtility.DisplayDialog("Are you sure?", "This action is irreversible. Are you sure you want to subdivide mesh?", "Ok", "Cancel")) { return; } string name = deformer.Original.name; deformer.Original = MeshSubdivider.Subdivide4(deformer.Original); deformer.Original.name = name; if (deformer.ColliderOriginal != null) { name = deformer.ColliderOriginal.name; deformer.ColliderOriginal = MeshSubdivider.Subdivide4(deformer.ColliderOriginal); deformer.ColliderOriginal.name = name; } for (int i = 0; i < deformer.Scaffolds.Length; ++i) { ScaffoldWrapper scaffold = deformer.Scaffolds[i]; if (scaffold.Obj == null) { continue; } MeshFilter filter = scaffold.Obj.GetComponent <MeshFilter>(); MeshCollider collider = scaffold.Obj.GetComponent <MeshCollider>(); if (filter != null) { Mesh colliderMesh = null; if (collider != null) { collider.sharedMesh = Object.Instantiate(deformer.ColliderOriginal); collider.sharedMesh.name = deformer.ColliderOriginal.name + " Deformed"; colliderMesh = collider.sharedMesh; } filter.sharedMesh = Object.Instantiate(deformer.Original); filter.sharedMesh.name = deformer.Original.name + " Deformed"; scaffold.Wrap(filter.sharedMesh, colliderMesh, deformer.Axis, scaffold.CurveIndices, scaffold.SliceCount); scaffold.Deform(deformer, deformer.Original, deformer.ColliderOriginal); scaffold.RecalculateNormals(); } } ScaffoldWrapper prev = null; if (deformer.Loop) { prev = deformer.Scaffolds[deformer.Scaffolds.Length - 1]; } for (int i = 0; i < deformer.Scaffolds.Length; ++i) { ScaffoldWrapper scaffold = deformer.Scaffolds[i]; scaffold.SlerpContacts(deformer, deformer.Original, deformer.ColliderOriginal, prev, null); scaffold = prev; } } else { meshFilter = selected.GetComponent <MeshFilter>(); Mesh mesh = meshFilter.sharedMesh; Undo.RecordObject(selected, "Battlehub.MeshDeformer Subdivide"); Undo.RecordObject(meshFilter, "Battlehub.MeshDeformer Subdivide"); meshFilter.sharedMesh = MeshSubdivider.Subdivide4(mesh); meshFilter.sharedMesh.name = mesh.name; MeshCollider collider = selected.GetComponent <MeshCollider>(); if (collider != null) { Mesh colliderMesh = collider.sharedMesh; Undo.RecordObject(selected, "Battlehub.MeshDeformer Subdivide"); Undo.RecordObject(collider, "Battlehub.MeshDeformer Subdivide"); collider.sharedMesh = MeshSubdivider.Subdivide4(colliderMesh); collider.sharedMesh.name = colliderMesh.name; } } }