public long Explode()
        {
            int count = this.pool.Count;
            int index = 0;

            if (count == 0)
            {
                return(0);
            }
            this.watch.Start();
            if (this.parameters.Callback != null)
            {
                this.parameters.Callback(0.0f, ExploderObject.ExplosionState.ExplosionStarted);
            }
            Vector3    vector3    = Vector3.get_zero();
            Quaternion quaternion = Quaternion.get_identity();

            if (Object.op_Implicit((Object)this.originalObject))
            {
                vector3    = Vector3.op_Subtraction(this.originalObject.get_transform().get_position(), this.initPos);
                quaternion = Quaternion.op_Multiply(this.originalObject.get_transform().get_rotation(), Quaternion.Inverse(this.initRot));
            }
            while (index < count)
            {
                Fragment fragment = this.pool[index];
                ++index;
                if (Object.op_Inequality((Object)this.originalObject, (Object)this.parameters.ExploderGameObject))
                {
                    ExploderUtils.SetActiveRecursively(this.originalObject, false);
                }
                else
                {
                    ExploderUtils.EnableCollider(this.originalObject, false);
                    ExploderUtils.SetVisible(this.originalObject, false);
                }
                Transform transform1 = ((Component)fragment).get_transform();
                transform1.set_position(Vector3.op_Addition(transform1.get_position(), vector3));
                Transform transform2 = ((Component)fragment).get_transform();
                transform2.set_rotation(Quaternion.op_Multiply(transform2.get_rotation(), quaternion));
                fragment.Explode(this.parameters);
            }
            if (this.parameters.DestroyOriginalObject && Object.op_Implicit((Object)this.originalObject) && !Object.op_Implicit((Object)this.originalObject.GetComponent <Fragment>()))
            {
                Object.Destroy((Object)this.originalObject);
            }
            if (this.parameters.ExplodeSelf && !this.parameters.DestroyOriginalObject)
            {
                ExploderUtils.SetActiveRecursively(this.parameters.ExploderGameObject, false);
            }
            if (this.parameters.HideSelf)
            {
                ExploderUtils.SetActiveRecursively(this.parameters.ExploderGameObject, false);
            }
            this.watch.Stop();
            return(this.watch.ElapsedMilliseconds);
        }
Esempio n. 2
0
        public long Explode()
        {
            var count   = pool.Count;
            var poolIdx = 0;

            if (count == 0)
            {
                return(0);
            }

            watch.Start();

            if (parameters.Callback != null)
            {
                parameters.Callback(0, ExploderObject.ExplosionState.ExplosionStarted);
            }

            var diffPos = Vector3.zero;
            var diffRot = Quaternion.identity;

            if (originalObject)
            {
                diffPos = originalObject.transform.position - initPos;
                diffRot = originalObject.transform.rotation * Quaternion.Inverse(initRot);
            }

            while (poolIdx < count)
            {
                var fragment = pool[poolIdx];

                poolIdx++;

                if (originalObject != parameters.ExploderGameObject)
                {
                    ExploderUtils.SetActiveRecursively(originalObject, false);
                }
                else
                {
                    ExploderUtils.EnableCollider(originalObject, false);
                    ExploderUtils.SetVisible(originalObject, false);
                }

                fragment.transform.position += diffPos;
                fragment.transform.rotation *= diffRot;

                fragment.Explode();
            }

            if (parameters.DestroyOriginalObject)
            {
                if (originalObject && !originalObject.GetComponent <Fragment>())
                {
                    GameObject.Destroy(originalObject);
                }
            }

            if (parameters.ExplodeSelf)
            {
                if (!parameters.DestroyOriginalObject)
                {
                    ExploderUtils.SetActiveRecursively(parameters.ExploderGameObject, false);
                }
            }

            if (parameters.HideSelf)
            {
                ExploderUtils.SetActiveRecursively(parameters.ExploderGameObject, false);
            }

            watch.Stop();
            return(watch.ElapsedMilliseconds);
        }
Esempio n. 3
0
        public override bool Run(float frameBudget)
        {
            var count = core.pool.Count;

            while (core.poolIdx < count)
            {
                var fragment = core.pool[core.poolIdx];
                var mesh     = core.postList[core.poolIdx];

                core.poolIdx++;

                if (!mesh.original)
                {
                    continue;
                }

                var unityMesh = mesh.mesh.ToUnityMesh();

                fragment.meshFilter.sharedMesh = unityMesh;

                // choose proper material

                if (mesh.option && mesh.option.FragmentMaterial)
                {
                    fragment.meshRenderer.sharedMaterial = mesh.option.FragmentMaterial;
                }
                else
                {
                    if (core.parameters.FragmentOptions.FragmentMaterial != null)
                    {
                        fragment.meshRenderer.sharedMaterial = core.parameters.FragmentOptions.FragmentMaterial;
                    }
                    else
                    {
                        fragment.meshRenderer.sharedMaterial = mesh.material;
                    }
                }

                unityMesh.RecalculateBounds();

                var oldParent = fragment.transform.parent;
                fragment.transform.parent     = mesh.parent;
                fragment.transform.position   = mesh.position;
                fragment.transform.rotation   = mesh.rotation;
                fragment.transform.localScale = mesh.localScale;
                fragment.transform.parent     = null;
                fragment.transform.parent     = oldParent;

                if (core.parameters.PartialExplosion)
                {
                }
                else
                {
                    if (mesh.original != core.parameters.ExploderGameObject)
                    {
                        ExploderUtils.SetActiveRecursively(mesh.original, false);
                    }
                    else
                    {
                        ExploderUtils.EnableCollider(mesh.original, false);
                        ExploderUtils.SetVisible(mesh.original, false);
                    }

                    if (mesh.skinnedOriginal && mesh.skinnedOriginal != core.parameters.ExploderGameObject)
                    {
                        ExploderUtils.SetActiveRecursively(mesh.skinnedOriginal, false);
                    }
                    else
                    {
                        ExploderUtils.EnableCollider(mesh.skinnedOriginal, false);
                        ExploderUtils.SetVisible(mesh.skinnedOriginal, false);
                    }

                    if (mesh.skinnedOriginal && mesh.bakeObject)
                    {
                        GameObject.DestroyObject(mesh.bakeObject, 1);
                    }
                }

                var plane = mesh.option && mesh.option.Plane2D;

                var use2d = core.parameters.Use2DCollision;

                if (!core.parameters.FragmentOptions.DisableColliders)
                {
                    if (core.parameters.MeshColliders && !use2d)
                    {
                        // dont use mesh colliders for 2d plane
                        if (!plane)
                        {
                            fragment.meshCollider.sharedMesh = unityMesh;
                        }
                    }
                    else
                    {
                        if (core.parameters.Use2DCollision)
                        {
                            MeshUtils.GeneratePolygonCollider(fragment.polygonCollider2D, unityMesh);
                        }
                        else
                        {
                            fragment.boxCollider.center = unityMesh.bounds.center;
                            fragment.boxCollider.size   = unityMesh.bounds.extents;
                        }
                    }
                }

                if (mesh.option)
                {
                    mesh.option.DuplicateSettings(fragment.options);
                }

                fragment.Explode();

                var force = core.parameters.Force;
                if (mesh.option && mesh.option.UseLocalForce)
                {
                    force = mesh.option.Force;
                }

                // apply force to rigid body
                fragment.ApplyExplosion(mesh.transform, mesh.mesh.centroid, core.parameters.Position, core.parameters.FragmentOptions, core.parameters.UseForceVector,
                                        core.parameters.ForceVector, force, mesh.original, core.parameters.TargetFragments);

#if SHOW_DEBUG_LINES
                UnityEngine.Debug.DrawLine(settings.Position, forceVector * settings.Force, Color.yellow, 3);
#endif

                if (Watch.ElapsedMilliseconds > frameBudget)
                {
                    return(false);
                }
            }

            if (core.parameters.DestroyOriginalObject)
            {
                foreach (var mesh in core.postList)
                {
                    if (mesh.original && !mesh.original.GetComponent <Fragment>())
                    {
                        Object.Destroy(mesh.original);
                    }

                    if (mesh.skinnedOriginal)
                    {
                        Object.Destroy(mesh.skinnedOriginal);
                    }
                }
            }

            if (core.parameters.ExplodeSelf)
            {
                if (!core.parameters.DestroyOriginalObject)
                {
                    ExploderUtils.SetActiveRecursively(core.parameters.ExploderGameObject, false);
                }
            }

            if (core.parameters.HideSelf)
            {
                ExploderUtils.SetActiveRecursively(core.parameters.ExploderGameObject, false);
            }

#if DBG
            ExploderUtils.Log("Explosion finished! " + postList.Count + postList[0].original.transform.gameObject.name);
#endif
//            core.exploder.OnExplosionFinished(true);

            Watch.Stop();

            return(true);
        }
        public override bool Run(float frameBudget)
        {
            int count = this.core.pool.Count;

            while (this.core.poolIdx < count)
            {
                Fragment   fragment = this.core.pool[this.core.poolIdx];
                MeshObject post     = this.core.postList[this.core.poolIdx];
                ++this.core.poolIdx;
                if (Object.op_Implicit((Object)post.original))
                {
                    Mesh unityMesh = post.mesh.ToUnityMesh();
                    fragment.AssignMesh(unityMesh);
                    if (Object.op_Implicit((Object)post.option) && Object.op_Implicit((Object)post.option.FragmentMaterial))
                    {
                        ((Renderer)fragment.meshRenderer).set_sharedMaterial(post.option.FragmentMaterial);
                    }
                    else if (Object.op_Inequality((Object)this.core.parameters.FragmentOptions.FragmentMaterial, (Object)null))
                    {
                        ((Renderer)fragment.meshRenderer).set_sharedMaterial(this.core.parameters.FragmentOptions.FragmentMaterial);
                    }
                    else
                    {
                        ((Renderer)fragment.meshRenderer).set_sharedMaterial(post.material);
                    }
                    unityMesh.RecalculateBounds();
                    Transform parent = ((Component)fragment).get_transform().get_parent();
                    ((Component)fragment).get_transform().set_parent(post.parent);
                    ((Component)fragment).get_transform().set_position(post.position);
                    ((Component)fragment).get_transform().set_rotation(post.rotation);
                    ((Component)fragment).get_transform().set_localScale(post.localScale);
                    ((Component)fragment).get_transform().set_parent((Transform)null);
                    ((Component)fragment).get_transform().set_parent(parent);
                    if (Object.op_Inequality((Object)post.original, (Object)this.core.parameters.ExploderGameObject))
                    {
                        ExploderUtils.SetActiveRecursively(post.original, false);
                    }
                    else
                    {
                        ExploderUtils.EnableCollider(post.original, false);
                        ExploderUtils.SetVisible(post.original, false);
                    }
                    if (Object.op_Implicit((Object)post.skinnedOriginal) && Object.op_Inequality((Object)post.skinnedOriginal, (Object)this.core.parameters.ExploderGameObject))
                    {
                        ExploderUtils.SetActiveRecursively(post.skinnedOriginal, false);
                    }
                    else
                    {
                        ExploderUtils.EnableCollider(post.skinnedOriginal, false);
                        ExploderUtils.SetVisible(post.skinnedOriginal, false);
                    }
                    if (Object.op_Implicit((Object)post.skinnedOriginal) && Object.op_Implicit((Object)post.bakeObject))
                    {
                        Object.Destroy((Object)post.bakeObject, 1f);
                    }
                    bool flag           = Object.op_Implicit((Object)post.option) && post.option.Plane2D;
                    bool use2Dcollision = this.core.parameters.Use2DCollision;
                    if (!this.core.parameters.FragmentOptions.DisableColliders)
                    {
                        if (this.core.parameters.FragmentOptions.MeshColliders && !use2Dcollision)
                        {
                            if (!flag)
                            {
                                fragment.meshCollider.set_sharedMesh(unityMesh);
                            }
                        }
                        else if (this.core.parameters.Use2DCollision)
                        {
                            MeshUtils.GeneratePolygonCollider(fragment.polygonCollider2D, unityMesh);
                        }
                        else
                        {
                            BoxCollider boxCollider1 = fragment.boxCollider;
                            Bounds      bounds1      = unityMesh.get_bounds();
                            Vector3     center       = ((Bounds) ref bounds1).get_center();
                            boxCollider1.set_center(center);
                            BoxCollider boxCollider2 = fragment.boxCollider;
                            Bounds      bounds2      = unityMesh.get_bounds();
                            Vector3     extents      = ((Bounds) ref bounds2).get_extents();
                            boxCollider2.set_size(extents);
                        }
                    }
                    fragment.Explode(this.core.parameters);
                    float force = this.core.parameters.Force;
                    if (Object.op_Implicit((Object)post.option) && post.option.UseLocalForce)
                    {
                        force = post.option.Force;
                    }
                    fragment.ApplyExplosion(post.transform, post.mesh.centroid, force, post.original, this.core.parameters);
                    if ((double)this.Watch.ElapsedMilliseconds > (double)frameBudget)
                    {
                        return(false);
                    }
                }
            }
            if (this.core.parameters.DestroyOriginalObject)
            {
                foreach (MeshObject post in this.core.postList)
                {
                    if (Object.op_Implicit((Object)post.original) && !Object.op_Implicit((Object)post.original.GetComponent <Fragment>()))
                    {
                        Object.Destroy((Object)post.original);
                    }
                    if (Object.op_Implicit((Object)post.skinnedOriginal))
                    {
                        Object.Destroy((Object)post.skinnedOriginal);
                    }
                }
            }
            if (this.core.parameters.ExplodeSelf && !this.core.parameters.DestroyOriginalObject)
            {
                ExploderUtils.SetActiveRecursively(this.core.parameters.ExploderGameObject, false);
            }
            if (this.core.parameters.HideSelf)
            {
                ExploderUtils.SetActiveRecursively(this.core.parameters.ExploderGameObject, false);
            }
            this.Watch.Stop();
            return(true);
        }
Esempio n. 5
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);
        }