Exemple #1
0
        public static Mesh FixMesh(Mesh originalMesh, Vector3 extents)
        {
            Bounds bounds      = originalMesh.bounds;
            Bounds fixBounds   = new Bounds(bounds.center, extents * 2.0f);
            Mesh   fixVertices = HBUtils.BoundsToMesh(fixBounds);

            CombineInstance fixInstance = new CombineInstance();

            fixInstance.mesh         = fixVertices;
            fixInstance.subMeshIndex = 0;
            fixInstance.transform    = Matrix4x4.identity;

            CombineInstance[] combineInstances = new CombineInstance[originalMesh.subMeshCount];
            for (int i = 0; i < originalMesh.subMeshCount; ++i)
            {
                CombineInstance submeshInstance = new CombineInstance();
                submeshInstance.mesh      = Subdivider.ExtractSubmesh(originalMesh, i);
                submeshInstance.transform = Matrix4x4.identity;
                CombineInstance[] submeshInstances = new[] { fixInstance, submeshInstance };
                Mesh fixedSubmesh = new Mesh();
                fixedSubmesh.CombineMeshes(submeshInstances, true);

                CombineInstance meshInstance = new CombineInstance();
                meshInstance.mesh         = fixedSubmesh;
                meshInstance.transform    = Matrix4x4.identity;
                meshInstance.subMeshIndex = 0;
                combineInstances[i]       = meshInstance;
            }

            Mesh fixedMesh = new Mesh();

            fixedMesh.CombineMeshes(combineInstances, false);

            return(fixedMesh);
        }
Exemple #2
0
        private static void Subdivide()
        {
            if (Application.isPlaying)
            {
                EditorUtility.DisplayDialog("Unable to Subdivide Mesh", "Application.isPlaying == true", "OK");
                return;
            }

            foreach (Transform transform in Selection.transforms)
            {
                MeshFilter meshFilter = transform.GetComponent <MeshFilter>();
                if (meshFilter == null)
                {
                    Debug.LogWarning("Select object with MeshFilter component attached");
                    return;
                }

                if (meshFilter.sharedMesh == null)
                {
                    Debug.LogError("meshFilter.sharedMesh is null");
                    return;
                }

                Undo.RecordObject(meshFilter, "Subdivide");
                HBFixBounds hbFixBounds = transform.GetComponent <HBFixBounds>();
                if (hbFixBounds == null)
                {
                    Mesh mesh = Subdivider.Subdivide4(meshFilter.sharedMesh);
                    if (!meshFilter.sharedMesh.name.Contains("HBSubdivided"))
                    {
                        mesh.name = meshFilter.sharedMesh.name + " HBSubdivided";
                    }
                    else
                    {
                        mesh.name = meshFilter.sharedMesh.name;
                    }
                    meshFilter.sharedMesh = mesh;
                }
                else
                {
                    if (hbFixBounds.OriginalMesh != meshFilter.sharedMesh)
                    {
                        hbFixBounds.Rollback();
                    }

                    Mesh mesh = Subdivider.Subdivide4(meshFilter.sharedMesh);
                    if (!hbFixBounds.OriginalMesh.name.Contains("HBSubdivided"))
                    {
                        mesh.name = hbFixBounds.OriginalMesh.name + " HBSubdivided";
                    }
                    else
                    {
                        mesh.name = hbFixBounds.OriginalMesh.name;
                    }

                    hbFixBounds.OriginalMesh = mesh;
                    hbFixBounds.FixBounds(false);
                }
                Undo.RecordObject(meshFilter, "Subdivide");
            }
        }