Esempio n. 1
0
 // Get demolition mesh
 static Mesh GetDemolitionMesh(RayfireShatter scr)
 {
     if (scr.skinnedMeshRend != null)
     {
         return(RFMesh.BakeMesh(scr.skinnedMeshRend));
     }
     return(scr.meshFilter.sharedMesh);
 }
Esempio n. 2
0
 // Get demolition mesh
 static Mesh GetDemolitionMesh(RayfireRigid scr)
 {
     if (scr.skinnedMeshRend != null)
     {
         //return scr.skinnedMeshRend.sharedMesh;
         return(RFMesh.BakeMesh(scr.skinnedMeshRend));
     }
     return(scr.meshFilter.sharedMesh);
 }
Esempio n. 3
0
        // Set data
        void SetData()
        {
            transForm = GetComponent <Transform>();

            // Reset mesh
            mFilter = GetComponent <MeshFilter>();
            if (mFilter == null)
            {
                mFilter = gameObject.AddComponent <MeshFilter>();
            }
            mFilter.sharedMesh = null;

            // Reset mesh renderer
            meshRenderer = GetComponent <MeshRenderer>();
            if (meshRenderer == null)
            {
                meshRenderer = gameObject.AddComponent <MeshRenderer>();
            }
            meshRenderer.sharedMaterials = new Material[] {};

            // Get targets
            if (type == CombType.Children)
            {
                if (meshFilters == true)
                {
                    mFilters = GetComponentsInChildren <MeshFilter>().ToList();
                }
                if (skinnedMeshes == true)
                {
                    skinnedMeshRends = GetComponentsInChildren <SkinnedMeshRenderer>().ToList();
                }
                if (particleSystems == true)
                {
                    particleRends = GetComponentsInChildren <ParticleSystemRenderer>().ToList();
                }
            }
            if (type == CombType.ObjectsList)
            {
                mFilters = new List <MeshFilter>();
                if (meshFilters == true)
                {
                    foreach (var obj in objects)
                    {
                        MeshFilter mf = obj.GetComponent <MeshFilter>();
                        if (mf != null)
                        {
                            if (mf.sharedMesh != null)
                            {
                                mFilters.Add(mf);
                            }
                        }
                    }
                }

                skinnedMeshRends = new List <SkinnedMeshRenderer>();
                if (skinnedMeshes == true)
                {
                    foreach (var obj in objects)
                    {
                        SkinnedMeshRenderer sk = obj.GetComponent <SkinnedMeshRenderer>();
                        if (sk != null)
                        {
                            if (sk.sharedMesh != null)
                            {
                                skinnedMeshRends.Add(sk);
                            }
                        }
                    }
                }

                particleRends = new List <ParticleSystemRenderer>();
                if (particleSystems == true)
                {
                    foreach (var obj in objects)
                    {
                        ParticleSystemRenderer pr = obj.GetComponent <ParticleSystemRenderer>();
                        if (pr != null)
                        {
                            particleRends.Add(pr);
                        }
                    }
                }
            }

            // Clear mesh filters without mesh
            for (int i = mFilters.Count - 1; i >= 0; i--)
            {
                if (mFilters[i].sharedMesh == null)
                {
                    mFilters.RemoveAt(i);
                }
            }

            // Clear skinned meshes without mesh
            for (int i = skinnedMeshRends.Count - 1; i >= 0; i--)
            {
                if (skinnedMeshRends[i].sharedMesh == null)
                {
                    skinnedMeshRends.RemoveAt(i);
                }
            }

            // Get meshes and tms
            meshList   = new List <Mesh>();
            transForms = new List <Transform>();
            matList    = new List <List <Material> >();

            // Collect mesh, tm and mats for meshfilter
            foreach (var mf in mFilters)
            {
                // Filters
                if (mf.sharedMesh.vertexCount < vertexThreshold)
                {
                    continue;
                }
                MeshRenderer mr = mf.GetComponent <MeshRenderer>();
                if (mr != null && mr.bounds.size.magnitude < sizeThreshold)
                {
                    continue;
                }

                meshList.Add(mf.sharedMesh);
                transForms.Add(mf.transform);

                // Collect mats
                List <Material> mats = new List <Material>();
                if (mr != null)
                {
                    mats = mr.sharedMaterials.ToList();
                }
                matList.Add(mats);
            }

            // Collect mesh, tm and mats for skinned mesh
            foreach (var sk in skinnedMeshRends)
            {
                // SKip by vertex amount
                if (sk.sharedMesh.vertexCount < vertexThreshold)
                {
                    continue;
                }
                if (sk.bounds.size.magnitude < sizeThreshold)
                {
                    continue;
                }

                meshList.Add(RFMesh.BakeMesh(sk));
                transForms.Add(sk.transform);
                matList.Add(sk.sharedMaterials.ToList());
            }

            // Particle system
            #if UNITY_2018_2_OR_NEWER
            {
                if (particleRends.Count > 0)
                {
                    GameObject g = new GameObject();
                    foreach (var pr in particleRends)
                    {
                        Mesh m = new Mesh();
                        pr.BakeMesh(m, true);
                        if (m.vertexCount > 3)
                        {
                            meshList.Add(m);
                            transForms.Add(g.transform);
                            matList.Add(pr.sharedMaterials.ToList());
                        }
                    }

                    DestroyImmediate(g);
                }
            }
            #endif

            // No meshes
            if (meshList.Count == 0)
            {
                Debug.Log("No meshes to combine");
                return;
            }

            // Get mesh data
            invertNormals = new List <bool>();
            allMaterials  = new List <Material>();
            matIdList     = new List <List <int> >();
            for (int f = 0; f < transForms.Count; f++)
            {
                // Collect uniq material list
                foreach (var material in matList[f])
                {
                    if (allMaterials.Contains(material) == false)
                    {
                        allMaterials.Add(material);
                    }
                }

                // Collect material ids per submesh
                matIdList.Add(matList[f].Select(t => allMaterials.IndexOf(t)).ToList());

                // Get invert normals because of negative scale
                bool invert = false;
                if (transForms[f].localScale.x < 0)
                {
                    invert = !invert;
                }
                if (transForms[f].localScale.y < 0)
                {
                    invert = !invert;
                }
                if (transForms[f].localScale.z < 0)
                {
                    invert = !invert;
                }
                invertNormals.Add(invert);
            }
        }
Esempio n. 4
0
        // Set data
        void SetData()
        {
            transForm = GetComponent <Transform>();

            // Reset mesh
            meshFilter = GetComponent <MeshFilter>();
            if (meshFilter == null)
            {
                meshFilter = gameObject.AddComponent <MeshFilter>();
            }
            meshFilter.sharedMesh = null;

            // Reset mesh renderer
            meshRenderer = GetComponent <MeshRenderer>();
            if (meshRenderer == null)
            {
                meshRenderer = gameObject.AddComponent <MeshRenderer>();
            }
            meshRenderer.sharedMaterials = new Material[] {};

            // Get targets
            if (type == CombType.Children)
            {
                meshFilters      = GetComponentsInChildren <MeshFilter>().ToList();
                skinnedMeshRends = GetComponentsInChildren <SkinnedMeshRenderer>().ToList();
            }
            if (type == CombType.ObjectsList)
            {
                meshFilters = new List <MeshFilter>();
                foreach (var obj in objects)
                {
                    MeshFilter mf = obj.GetComponent <MeshFilter>();
                    if (mf != null)
                    {
                        meshFilters.Add(mf);
                    }
                }
                skinnedMeshRends = new List <SkinnedMeshRenderer>();
                foreach (var obj in objects)
                {
                    SkinnedMeshRenderer sk = obj.GetComponent <SkinnedMeshRenderer>();
                    if (sk != null)
                    {
                        skinnedMeshRends.Add(sk);
                    }
                }
            }

            // Clear mesh filters without mesh
            for (int i = meshFilters.Count - 1; i >= 0; i--)
            {
                if (meshFilters[i].sharedMesh == null)
                {
                    meshFilters.RemoveAt(i);
                }
            }

            // Clear skinned meshes without mesh
            for (int i = skinnedMeshRends.Count - 1; i >= 0; i--)
            {
                if (skinnedMeshRends[i].sharedMesh == null)
                {
                    skinnedMeshRends.RemoveAt(i);
                }
            }

            // Get meshes and tms
            meshes     = new List <Mesh>();
            transForms = new List <Transform>();
            matList    = new List <List <Material> >();

            // Collect mesh, tm and mats for meshfilter
            foreach (var mf in meshFilters)
            {
                meshes.Add(mf.sharedMesh);
                transForms.Add(mf.transform);

                // Collect mats
                MeshRenderer    mr   = mf.GetComponent <MeshRenderer>();
                List <Material> mats = new List <Material>();
                if (mr != null)
                {
                    mats = mr.sharedMaterials.ToList();
                }
                matList.Add(mats);
            }

            // Collect mesh, tm and mats for skinned mesh
            foreach (var sk in skinnedMeshRends)
            {
                meshes.Add(RFMesh.BakeMesh(sk));
                transForms.Add(sk.transform);
                matList.Add(sk.sharedMaterials.ToList());
            }

            // No meshes
            if (meshes.Count == 0)
            {
                Debug.Log("No meshes to combine");
                return;
            }

            // Get mesh data
            invertNormals = new List <bool>();
            allMaterials  = new List <Material>();
            matIdList     = new List <List <int> >();
            for (int f = 0; f < transForms.Count; f++)
            {
                // Collect uniq material list
                foreach (var material in matList[f])
                {
                    if (allMaterials.Contains(material) == false)
                    {
                        allMaterials.Add(material);
                    }
                }

                // Collect material ids per submesh
                matIdList.Add(matList[f].Select(t => allMaterials.IndexOf(t)).ToList());

                // Get invert normals because of negative scale
                bool invert = false;
                if (transForms[f].localScale.x < 0)
                {
                    invert = !invert;
                }
                if (transForms[f].localScale.y < 0)
                {
                    invert = !invert;
                }
                if (transForms[f].localScale.z < 0)
                {
                    invert = !invert;
                }
                invertNormals.Add(invert);
            }
        }