Beispiel #1
0
        // Constructor
        public RFSnapshot(GameObject go, bool compress)
        {
            nm    = go.name;
            oldId = go.GetInstanceID();
            if (go.transform.parent != null)
            {
                parentOldId = go.transform.parent.gameObject.GetInstanceID();
            }
            pos   = go.transform.position;
            rot   = go.transform.rotation;
            scale = go.transform.localScale;

            // Mesh
            MeshFilter mf = go.GetComponent <MeshFilter>();

            if (mf != null)
            {
                mesh = new RFMesh(mf.sharedMesh, compress);
            }

            // Materials
            mats = new List <string>();
            MeshRenderer mr = go.GetComponent <MeshRenderer>();

            if (mr != null)
            {
                foreach (var mat in mr.sharedMaterials)
                {
                    mats.Add(AssetDatabase.GetAssetPath(mat));
                }
            }
        }
Beispiel #2
0
 // Get demolition mesh
 static Mesh GetDemolitionMesh(RayfireShatter scr)
 {
     if (scr.skinnedMeshRend != null)
     {
         return(RFMesh.BakeMesh(scr.skinnedMeshRend));
     }
     return(scr.meshFilter.sharedMesh);
 }
Beispiel #3
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);
 }
Beispiel #4
0
        // Create object
        public static GameObject Create(RFSnapshot cap, float sizeFilter)
        {
            // Mesh and size filtering
            Mesh mesh = null;

            if (cap.mesh.subMeshCount > 0)
            {
                // Size filtering
                if (cap.mesh.bounds.size.magnitude < sizeFilter)
                {
                    return(null);
                }

                // Get mesh
                mesh = cap.mesh.GetMesh();
            }

            // Object
            GameObject go = new GameObject();

            go.name = cap.nm;
            go.transform.position   = cap.pos;
            go.transform.rotation   = cap.rot;
            go.transform.localScale = cap.scale;

            // Mesh
            if (mesh != null)
            {
                MeshFilter mf = go.AddComponent <MeshFilter>();
                mf.sharedMesh = mesh;
            }

            // Materials
            if (cap.mats.Count > 0)
            {
                MeshRenderer    mr        = go.AddComponent <MeshRenderer>();
                List <Material> materials = new List <Material>();
                foreach (var matPath in cap.mats)
                {
                    materials.Add((Material)AssetDatabase.LoadAssetAtPath(matPath, typeof(Material)));
                }
                mr.sharedMaterials = materials.ToArray();
            }

            cap.newId = go.GetInstanceID();
            cap.newTm = go.transform;

            return(go);
        }
Beispiel #5
0
        // Precache meshes for prefab in editor
        public void PrefabPrecache()
        {
            if (demolitionType == DemolitionType.ManualPrefabPrecache && objectType == ObjectType.Mesh)
            {
                // Set components for mesh / skinned mesh / clusters
                SetComponentsBasic();

                // Cache meshes
                RFDemolitionMesh.CacheInstant(this);

                // Convert meshes to RFmeshes
                if (HasMeshes == true)
                {
                    rfMeshes = new RFMesh[meshes.Length];
                    for (int i = 0; i < meshes.Length; i++)
                    {
                        rfMeshes[i] = new RFMesh(meshes[i], meshDemolition.compressPrefab);
                    }
                }
                meshes = null;
            }
        }
Beispiel #6
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);
            }
        }
Beispiel #7
0
        /// /////////////////////////////////////////////////////////
        /// Static
        /// /////////////////////////////////////////////////////////

        // Demolish single mesh to fragments
        public static bool DemolishMesh(RayfireRigid scr)
        {
            // Object demolition
            if (scr.objectType != ObjectType.Mesh && scr.objectType != ObjectType.SkinnedMesh)
            {
                return(true);
            }

            // Skip if reference
            if (scr.demolitionType == DemolitionType.ReferenceDemolition)
            {
                return(true);
            }

            // Already has fragments
            if (scr.HasFragments == true)
            {
                // Set tm
                scr.rootChild.position         = scr.transForm.position;
                scr.rootChild.rotation         = scr.transForm.rotation;
                scr.rootChild.transform.parent = RayfireMan.inst.transForm;

                // Activate root and fragments
                scr.rootChild.gameObject.SetActive(true);

                // Start all coroutines
                for (int i = 0; i < scr.fragments.Count; i++)
                {
                    scr.fragments[i].StartAllCoroutines();
                }

                scr.limitations.demolished = true;
                return(true);
            }

            // Has serialized meshes but has no Unity meshes - convert to unity meshes
            if (scr.HasRfMeshes == true && scr.HasMeshes == false)
            {
                RFMesh.ConvertRfMeshes(scr);
            }

            // Has unity meshes - create fragments
            if (scr.HasMeshes == true)
            {
                scr.fragments = CreateFragments(scr);
                scr.limitations.demolished = true;
                return(true);
            }

            // Still has no Unity meshes - cache Unity meshes
            if (scr.HasMeshes == false)
            {
                // Cache unity meshes
                CacheRuntime(scr);

                // Caching in progress. Stop demolition
                if (scr.meshDemolition.runtimeCaching.inProgress == true)
                {
                    return(false);
                }

                // Has unity meshes - create fragments
                if (scr.HasMeshes == true)
                {
                    scr.fragments = CreateFragments(scr);
                    scr.limitations.demolished = true;
                    return(true);
                }
            }

            return(false);
        }
Beispiel #8
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);
            }
        }