Esempio n. 1
0
        private List <MeshData> GetMeshData(GameObject obj)
        {
            var renderers   = obj.GetComponentsInChildren <MeshRenderer>();
            var meshFilters = obj.GetComponentsInChildren <MeshFilter>();

            ExploderUtils.Warning(renderers.Length == meshFilters.Length, "Renderers and meshes don't match!");

            if (renderers.Length != meshFilters.Length)
            {
                return(new List <MeshData>());
            }

            var outList = new List <MeshData>(renderers.Length);

            for (int i = 0; i < renderers.Length; i++)
            {
                if (meshFilters[i].sharedMesh == null)
                {
                    ExploderUtils.Log("Missing shared mesh in " + meshFilters[i].name);
                    continue;
                }

                if (!meshFilters[i].sharedMesh || !meshFilters[i].sharedMesh.isReadable)
                {
                    UnityEngine.Debug.LogWarning("Mesh is not readable: " + meshFilters[i].name);
                    continue;
                }

                if (/*IsExplodable(meshFilters[i].gameObject)*/ true)
                {
                    outList.Add(new MeshData
                    {
                        sharedMesh     = meshFilters[i].sharedMesh,
                        sharedMaterial = renderers[i].sharedMaterial,
                        gameObject     = renderers[i].gameObject,
                        centroid       = renderers[i].bounds.center,
                        parentObject   = obj,
                    });
                }
            }

            // find skinned mesh
            var renderersSkinned = obj.GetComponentsInChildren <SkinnedMeshRenderer>();

            for (int i = 0; i < renderersSkinned.Length; i++)
            {
                var bakeMesh = new Mesh();
                renderersSkinned[i].BakeMesh(bakeMesh);
                var bakeObj    = core.bakeSkinManager.CreateBakeObject(obj.name);
                var meshFilter = bakeObj.AddComponent <MeshFilter>();
                meshFilter.sharedMesh = bakeMesh;
                var meshRenderer = bakeObj.AddComponent <MeshRenderer>();
                meshRenderer.sharedMaterial = renderersSkinned[i].material;
                bakeObj.transform.position  = renderersSkinned[i].gameObject.transform.position;
                bakeObj.transform.rotation  = renderersSkinned[i].gameObject.transform.rotation;
                ExploderUtils.SetVisible(bakeObj, false);

                outList.Add(new MeshData
                {
                    sharedMesh          = bakeMesh,
                    sharedMaterial      = meshRenderer.sharedMaterial,
                    gameObject          = bakeObj,
                    centroid            = meshRenderer.bounds.center,
                    parentObject        = bakeObj,
                    skinnedBakeOriginal = obj,
                });
            }

            return(outList);
        }