Exemplo n.º 1
0
 public void CombinePaintedMeshes(bool autoSelect, MeshFilter[] meshFilters)
 {
     if (meshFilters == null || meshFilters.Length == 0)
     {
         Debug.LogError((object)"MeshBrush: The meshFilters array you passed as an argument to the CombinePaintedMeshes function is empty or null... Combining action cancelled!");
     }
     else
     {
         this.localTransformationMatrix = ((Component)this).get_transform().get_worldToLocalMatrix();
         this.materialToMesh            = new Hashtable();
         int num = 0;
         for (long index = 0; index < meshFilters.LongLength; ++index)
         {
             this.currentMeshFilter = meshFilters[index];
             num += this.currentMeshFilter.get_sharedMesh().get_vertexCount();
             if (num > 64000)
             {
                 return;
             }
         }
         for (long index = 0; index < meshFilters.LongLength; ++index)
         {
             this.currentMeshFilter = meshFilters[index];
             this.currentRenderer   = (Renderer)((Component)meshFilters[index]).GetComponent <Renderer>();
             this.instance          = new CombineUtility.MeshInstance();
             this.instance.mesh     = this.currentMeshFilter.get_sharedMesh();
             if (Object.op_Inequality((Object)this.currentRenderer, (Object)null) && this.currentRenderer.get_enabled() && Object.op_Inequality((Object)this.instance.mesh, (Object)null))
             {
                 this.instance.transform = Matrix4x4.op_Multiply(this.localTransformationMatrix, ((Component)this.currentMeshFilter).get_transform().get_localToWorldMatrix());
                 this.materials          = this.currentRenderer.get_sharedMaterials();
                 for (int val1 = 0; val1 < this.materials.Length; ++val1)
                 {
                     this.instance.subMeshIndex = Math.Min(val1, this.instance.mesh.get_subMeshCount() - 1);
                     this.objects = (ArrayList)this.materialToMesh[(object)this.materials[val1]];
                     if (this.objects != null)
                     {
                         this.objects.Add((object)this.instance);
                     }
                     else
                     {
                         this.objects = new ArrayList();
                         this.objects.Add((object)this.instance);
                         this.materialToMesh.Add((object)this.materials[val1], (object)this.objects);
                     }
                 }
                 Object.DestroyImmediate((Object)((Component)this.currentRenderer).get_gameObject());
             }
         }
         foreach (DictionaryEntry dictionaryEntry in this.materialToMesh)
         {
             this.elements  = (ArrayList)dictionaryEntry.Value;
             this.instances = (CombineUtility.MeshInstance[]) this.elements.ToArray(typeof(CombineUtility.MeshInstance));
             GameObject gameObject = new GameObject("Combined mesh");
             gameObject.get_transform().set_parent(((Component)this).get_transform());
             gameObject.get_transform().set_localScale(Vector3.get_one());
             gameObject.get_transform().set_localRotation(Quaternion.get_identity());
             gameObject.get_transform().set_localPosition(Vector3.get_zero());
             gameObject.AddComponent <MeshFilter>();
             gameObject.AddComponent <MeshRenderer>();
             gameObject.AddComponent <SaveCombinedMesh>();
             ((Renderer)gameObject.GetComponent <Renderer>()).set_material((Material)dictionaryEntry.Key);
             gameObject.set_isStatic(true);
             this.currentMeshFilter = (MeshFilter)gameObject.GetComponent <MeshFilter>();
             this.currentMeshFilter.set_mesh(CombineUtility.Combine(this.instances, false));
         }
         ((Component)this).get_gameObject().set_isStatic(true);
     }
 }
Exemplo n.º 2
0
        public void CombinePaintedMeshes(bool autoSelect)
        {
            meshFilters    = GetComponentsInChildren <MeshFilter>();
            myTransform    = transform.worldToLocalMatrix;
            materialToMesh = new Hashtable();

            int totalVertCount = 0;

            for (long i = 0; i < meshFilters.LongLength; i++)
            {
                filter = (MeshFilter)meshFilters[i];

                totalVertCount += filter.sharedMesh.vertexCount;

                if (totalVertCount > 64000)
                {
                    #if UNITY_EDITOR
                    if (UnityEditor.EditorUtility.DisplayDialog("Warning!", "You are trying to combine a group of meshes whose total vertex count exceeds Unity's built-in limit.\n\nThe process has been aborted to prevent the accidental deletion of all painted meshes and numerous disturbing error messages printed to the console.\n\nConsider splitting your meshes into smaller groups and combining them separately.", "Okay"))
                    {
                        return;
                    }
                    #endif
                }
            }

            for (long i = 0; i < meshFilters.LongLength; i++)
            {
                filter      = (MeshFilter)meshFilters[i];
                curRenderer = meshFilters[i].GetComponent <Renderer>();

                instance      = new CombineUtility.MeshInstance();
                instance.mesh = filter.sharedMesh;

                if (curRenderer != null && curRenderer.enabled && instance.mesh != null)
                {
                    instance.transform = myTransform * filter.transform.localToWorldMatrix;

                    materials = curRenderer.sharedMaterials;
                    for (int m = 0; m < materials.Length; m++)
                    {
                        instance.subMeshIndex = System.Math.Min(m, instance.mesh.subMeshCount - 1);

                        objects = (ArrayList)materialToMesh[materials[m]];
                        if (objects != null)
                        {
                            objects.Add(instance);
                        }
                        else
                        {
                            objects = new ArrayList();
                            objects.Add(instance);
                            materialToMesh.Add(materials[m], objects);
                        }
                    }

                    DestroyImmediate(curRenderer.gameObject);
                }
            }

            foreach (DictionaryEntry de in materialToMesh)
            {
                elements  = (ArrayList)de.Value;
                instances = (CombineUtility.MeshInstance[])elements.ToArray(typeof(CombineUtility.MeshInstance));

                GameObject go = new GameObject("Combined mesh");
                go.transform.parent        = transform;
                go.transform.localScale    = Vector3.one;
                go.transform.localRotation = Quaternion.identity;
                go.transform.localPosition = Vector3.zero;
                go.AddComponent <MeshFilter>();
                go.AddComponent <MeshRenderer>();
                go.AddComponent <SaveCombinedMesh>();
                go.GetComponent <Renderer>().material = (Material)de.Key;
                go.isStatic = true;

                filter      = go.GetComponent <MeshFilter>();
                filter.mesh = CombineUtility.Combine(instances, false);

                #if UNITY_EDITOR
                if (autoSelect)
                {
                    UnityEditor.Selection.activeObject = go;
                }
                #endif
            }
            gameObject.isStatic = true;
        }
Exemplo n.º 3
0
        public void CombinePaintedMeshes(bool autoSelect, MeshFilter[] meshFilters)
        {
            if (meshFilters == null || meshFilters.Length == 0)
            {
                Debug.LogError("MeshBrush: The meshFilters array you passed as an argument to the CombinePaintedMeshes function is empty or null... Combining action cancelled!");
                return;
            }

            localTransformationMatrix = transform.worldToLocalMatrix;
            materialToMesh            = new Hashtable();

            int totalVertCount = 0;

            for (long i = 0; i < meshFilters.LongLength; i++)
            {
                currentMeshFilter = (MeshFilter)meshFilters[i];

                totalVertCount += currentMeshFilter.sharedMesh.vertexCount;

                if (totalVertCount > 64000)
                {
#if UNITY_EDITOR
                    if (UnityEditor.EditorUtility.DisplayDialog("Warning!", "You are trying to combine a group of meshes whose total vertex count exceeds Unity's built-in limit.\n\nThe process has been aborted to prevent the accidental deletion of all painted meshes and numerous disturbing error messages printed to the console.\n\nConsider splitting your meshes into smaller groups and combining them separately.\n\n=> You can do that for example based on the circle brush's area (press the combine meshes key in the scene view), or via multiple MeshBrush instances to form various painting sets and combine them individually; see the help section in the inspector for more detailed infos!", "Okay"))
                    {
                        return;
                    }
#endif
                    return;
                }
            }

            for (long i = 0; i < meshFilters.LongLength; i++)
            {
                currentMeshFilter = (MeshFilter)meshFilters[i];
                currentRenderer   = meshFilters[i].GetComponent <Renderer>();

                instance      = new CombineUtility.MeshInstance();
                instance.mesh = currentMeshFilter.sharedMesh;

                if (currentRenderer != null && currentRenderer.enabled && instance.mesh != null)
                {
                    instance.transform = localTransformationMatrix * currentMeshFilter.transform.localToWorldMatrix;

                    materials = currentRenderer.sharedMaterials;
                    for (int m = 0; m < materials.Length; m++)
                    {
                        instance.subMeshIndex = System.Math.Min(m, instance.mesh.subMeshCount - 1);

                        objects = (ArrayList)materialToMesh[materials[m]];
                        if (objects != null)
                        {
                            objects.Add(instance);
                        }
                        else
                        {
                            objects = new ArrayList();
                            objects.Add(instance);
                            materialToMesh.Add(materials[m], objects);
                        }
                    }

                    DestroyImmediate(currentRenderer.gameObject);
                }
            }

            foreach (DictionaryEntry de in materialToMesh)
            {
                elements  = (ArrayList)de.Value;
                instances = (CombineUtility.MeshInstance[])elements.ToArray(typeof(CombineUtility.MeshInstance));

                var go = new GameObject("Combined mesh");

                go.transform.parent        = transform;
                go.transform.localScale    = Vector3.one;
                go.transform.localRotation = Quaternion.identity;
                go.transform.localPosition = Vector3.zero;
                go.AddComponent <MeshFilter>();
                go.AddComponent <MeshRenderer>();
                go.AddComponent <SaveCombinedMesh>();
                go.GetComponent <Renderer>().material = (Material)de.Key;
                go.isStatic = true;

                currentMeshFilter      = go.GetComponent <MeshFilter>();
                currentMeshFilter.mesh = CombineUtility.Combine(instances, false);

#if UNITY_EDITOR
                if (autoSelect)
                {
                    UnityEditor.Selection.activeObject = go;
                }
#endif
            }
            gameObject.isStatic = true;
        }
Exemplo n.º 4
0
        public static Mesh Combine(CombineUtility.MeshInstance[] combines, bool generateStrips)
        {
            CombineUtility.vertexCount   = 0;
            CombineUtility.triangleCount = 0;
            CombineUtility.stripCount    = 0;
            foreach (CombineUtility.MeshInstance combine in combines)
            {
                if (Object.op_Inequality((Object)combine.mesh, (Object)null))
                {
                    CombineUtility.vertexCount += combine.mesh.get_vertexCount();
                    if (generateStrips)
                    {
                        CombineUtility.curStripCount = combine.mesh.GetTriangles(combine.subMeshIndex).Length;
                        if (CombineUtility.curStripCount != 0)
                        {
                            if (CombineUtility.stripCount != 0)
                            {
                                if ((CombineUtility.stripCount & 1) == 1)
                                {
                                    CombineUtility.stripCount += 3;
                                }
                                else
                                {
                                    CombineUtility.stripCount += 2;
                                }
                            }
                            CombineUtility.stripCount += CombineUtility.curStripCount;
                        }
                        else
                        {
                            generateStrips = false;
                        }
                    }
                }
            }
            if (!generateStrips)
            {
                foreach (CombineUtility.MeshInstance combine in combines)
                {
                    if (Object.op_Inequality((Object)combine.mesh, (Object)null))
                    {
                        CombineUtility.triangleCount += combine.mesh.GetTriangles(combine.subMeshIndex).Length;
                    }
                }
            }
            CombineUtility.vertices  = new Vector3[CombineUtility.vertexCount];
            CombineUtility.normals   = new Vector3[CombineUtility.vertexCount];
            CombineUtility.tangents  = new Vector4[CombineUtility.vertexCount];
            CombineUtility.uv        = new Vector2[CombineUtility.vertexCount];
            CombineUtility.uv1       = new Vector2[CombineUtility.vertexCount];
            CombineUtility.colors    = new Color[CombineUtility.vertexCount];
            CombineUtility.triangles = new int[CombineUtility.triangleCount];
            CombineUtility.strip     = new int[CombineUtility.stripCount];
            CombineUtility.offset    = 0;
            foreach (CombineUtility.MeshInstance combine in combines)
            {
                if (Object.op_Inequality((Object)combine.mesh, (Object)null))
                {
                    CombineUtility.Copy(combine.mesh.get_vertexCount(), combine.mesh.get_vertices(), CombineUtility.vertices, ref CombineUtility.offset, combine.transform);
                }
            }
            CombineUtility.offset = 0;
            foreach (CombineUtility.MeshInstance combine in combines)
            {
                if (Object.op_Inequality((Object)combine.mesh, (Object)null))
                {
                    CombineUtility.invTranspose = combine.transform;
                    Matrix4x4 inverse = ((Matrix4x4) ref CombineUtility.invTranspose).get_inverse();
                    CombineUtility.invTranspose = ((Matrix4x4) ref inverse).get_transpose();
                    CombineUtility.CopyNormal(combine.mesh.get_vertexCount(), combine.mesh.get_normals(), CombineUtility.normals, ref CombineUtility.offset, CombineUtility.invTranspose);
                }
            }
            CombineUtility.offset = 0;
            foreach (CombineUtility.MeshInstance combine in combines)
            {
                if (Object.op_Inequality((Object)combine.mesh, (Object)null))
                {
                    CombineUtility.invTranspose = combine.transform;
                    Matrix4x4 inverse = ((Matrix4x4) ref CombineUtility.invTranspose).get_inverse();
                    CombineUtility.invTranspose = ((Matrix4x4) ref inverse).get_transpose();
                    CombineUtility.CopyTangents(combine.mesh.get_vertexCount(), combine.mesh.get_tangents(), CombineUtility.tangents, ref CombineUtility.offset, CombineUtility.invTranspose);
                }
            }
            CombineUtility.offset = 0;
            foreach (CombineUtility.MeshInstance combine in combines)
            {
                if (Object.op_Inequality((Object)combine.mesh, (Object)null))
                {
                    CombineUtility.Copy(combine.mesh.get_vertexCount(), combine.mesh.get_uv(), CombineUtility.uv, ref CombineUtility.offset);
                }
            }
            CombineUtility.offset = 0;
            foreach (CombineUtility.MeshInstance combine in combines)
            {
                if (Object.op_Inequality((Object)combine.mesh, (Object)null))
                {
                    CombineUtility.Copy(combine.mesh.get_vertexCount(), combine.mesh.get_uv2(), CombineUtility.uv1, ref CombineUtility.offset);
                }
            }
            CombineUtility.offset = 0;
            foreach (CombineUtility.MeshInstance combine in combines)
            {
                if (Object.op_Inequality((Object)combine.mesh, (Object)null))
                {
                    CombineUtility.CopyColors(combine.mesh.get_vertexCount(), combine.mesh.get_colors(), CombineUtility.colors, ref CombineUtility.offset);
                }
            }
            CombineUtility.triangleOffset = 0;
            CombineUtility.stripOffset    = 0;
            CombineUtility.vertexOffset   = 0;
            foreach (CombineUtility.MeshInstance combine in combines)
            {
                if (Object.op_Inequality((Object)combine.mesh, (Object)null))
                {
                    if (generateStrips)
                    {
                        int[] triangles = combine.mesh.GetTriangles(combine.subMeshIndex);
                        if (CombineUtility.stripOffset != 0)
                        {
                            if ((CombineUtility.stripOffset & 1) == 1)
                            {
                                CombineUtility.strip[CombineUtility.stripOffset]     = CombineUtility.strip[CombineUtility.stripOffset - 1];
                                CombineUtility.strip[CombineUtility.stripOffset + 1] = triangles[0] + CombineUtility.vertexOffset;
                                CombineUtility.strip[CombineUtility.stripOffset + 2] = triangles[0] + CombineUtility.vertexOffset;
                                CombineUtility.stripOffset += 3;
                            }
                            else
                            {
                                CombineUtility.strip[CombineUtility.stripOffset]     = CombineUtility.strip[CombineUtility.stripOffset - 1];
                                CombineUtility.strip[CombineUtility.stripOffset + 1] = triangles[0] + CombineUtility.vertexOffset;
                                CombineUtility.stripOffset += 2;
                            }
                        }
                        for (int index = 0; index < triangles.Length; ++index)
                        {
                            CombineUtility.strip[index + CombineUtility.stripOffset] = triangles[index] + CombineUtility.vertexOffset;
                        }
                        CombineUtility.stripOffset += triangles.Length;
                    }
                    else
                    {
                        int[] triangles = combine.mesh.GetTriangles(combine.subMeshIndex);
                        for (int index = 0; index < triangles.Length; ++index)
                        {
                            CombineUtility.triangles[index + CombineUtility.triangleOffset] = triangles[index] + CombineUtility.vertexOffset;
                        }
                        CombineUtility.triangleOffset += triangles.Length;
                    }
                    CombineUtility.vertexOffset += combine.mesh.get_vertexCount();
                }
            }
            Mesh mesh = new Mesh();

            ((Object)mesh).set_name("Combined Mesh");
            mesh.set_vertices(CombineUtility.vertices);
            mesh.set_normals(CombineUtility.normals);
            mesh.set_colors(CombineUtility.colors);
            mesh.set_uv(CombineUtility.uv);
            mesh.set_uv2(CombineUtility.uv1);
            mesh.set_tangents(CombineUtility.tangents);
            if (generateStrips)
            {
                mesh.SetTriangles(CombineUtility.strip, 0);
            }
            else
            {
                mesh.set_triangles(CombineUtility.triangles);
            }
            return(mesh);
        }