Exemplo n.º 1
0
    public virtual void ExplodePartial(int start)
    {
        for (int submesh = 0; submesh < M.subMeshCount; submesh++)
        {
            int[] indices = M.GetTriangles(submesh);

            for (int i = start; i < indices.Length - grandStep - 2; i += grandStep)
            {
                for (int n = 0; n < 3; n++)
                {
                    int index = indices[i + n];
                    newVerts[n]   = verts[index];
                    newUvs[n]     = uvs[index];
                    newNormals[n] = normals[index];
                }
                mesh          = new Mesh();
                mesh.vertices = newVerts;
                mesh.normals  = newNormals;
                mesh.uv       = newUvs;

                mesh.triangles = triangles;

                GO = ObjectsPool.Spawn(pooledObjectName, Vector3.zero, Quaternion.identity);

                if (GO != null)
                {
                    GO.SetActive(true);

                    Deactivator deactivator = GO.GetComponent <Deactivator>();

                    GO.layer = LayerMask.NameToLayer(layerName);

                    deactivator.attachedRenderer.material = MR.materials[submesh];
                    deactivator.attachedFilter.mesh       = mesh;

                    GO.transform.position   = transform.position;
                    GO.transform.rotation   = transform.rotation;
                    GO.transform.localScale = new Vector3(MR.transform.localScale.x * scaleFactor, MR.transform.localScale.y, MR.transform.localScale.z * scaleFactor);

                    if (changeForwardVector)
                    {
                        GO.transform.forward = transform.up;
                    }

                    GO.AddComponent <BoxCollider>();

                    deactivator.attachedRigid.AddExplosionForce(explosionForce, explosionOrigin, 50, upwardsModifier);
                    deactivator.attachedRigid.useGravity  = useGravity;
                    deactivator.attachedRigid.drag        = drag;
                    deactivator.attachedRigid.angularDrag = angularDrag;

                    deactivator.TriggerDeactivation(Random.Range(minimumAliveTime, maximumAliveTime));
                }
            }
        }
        Destroy(this);
    }
Exemplo n.º 2
0
    private void SplitInTwo()
    {
        int above = 0;
        int below = 0;

        Vector3[] vertices = M.vertices;

        for (int submesh = 0; submesh < M.subMeshCount; submesh++)
        {
            int[] subMeshIndices = M.GetIndices(submesh);

            for (int i = 0; i < subMeshIndices.Length; i += 3)
            {
                above = 0;
                below = 0;

                if (vertices[subMeshIndices[i]].y <= cutHeight)
                {
                    below++;
                }
                else
                {
                    above++;
                }

                if (vertices[subMeshIndices[i + 1]].y <= cutHeight)
                {
                    below++;
                }
                else
                {
                    above++;
                }
                if (vertices[subMeshIndices[i + 2]].y <= cutHeight)
                {
                    below++;
                }
                else
                {
                    above++;
                }

                if (above == 3 || below == 3)
                {
                    if (above == 3)
                    {
                        upIndices[submesh].Add(subMeshIndices[i]);
                        upIndices[submesh].Add(subMeshIndices[i + 1]);
                        upIndices[submesh].Add(subMeshIndices[i + 2]);

                        upIndices[upIndices.Length - 1].Add(subMeshIndices[i + 2]);
                        upIndices[upIndices.Length - 1].Add(subMeshIndices[i + 1]);
                        upIndices[upIndices.Length - 1].Add(subMeshIndices[i]);
                    }
                    else
                    {
                        lowIndices[submesh].Add(subMeshIndices[i]);
                        lowIndices[submesh].Add(subMeshIndices[i + 1]);
                        lowIndices[submesh].Add(subMeshIndices[i + 2]);

                        lowIndices[lowIndices.Length - 1].Add(subMeshIndices[i + 2]);
                        lowIndices[lowIndices.Length - 1].Add(subMeshIndices[i + 1]);
                        lowIndices[lowIndices.Length - 1].Add(subMeshIndices[i]);
                    }
                }
            }
        }
        DetermineVertexPositions(vertices);

        if (upperVertices != 0)
        {
            upper = ObjectsPool.Spawn(pooledObjectName, Vector3.zero, Quaternion.identity);

            upper.layer = LayerMask.NameToLayer("Fragments");
            upper.transform.position = transform.position;

            upper.transform.rotation   = SMR.transform.rotation;
            upper.transform.localScale = transform.localScale;

            upper.SetActive(true);
            upper.name = "upper";

            Mesh mesh = new Mesh();
            mesh.SetVertices(upperVertexList);

            mesh.subMeshCount = upIndices.Length;
            for (int i = 0; i < upIndices.Length; i++)
            {
                mesh.SetIndices(ApplyIndexChange(upIndices[i], vertexPosChange), MeshTopology.Triangles, i);
            }

            mesh.SetNormals(upNormals);
            mesh.SetUVs(0, upperUVs);

            Deactivator deactivator = upper.GetComponent <Deactivator>();
            deactivator.TriggerDeactivation(deactivateTime);

            Destroy(upper.GetComponent <MeshFilter>());
            Destroy(upper.GetComponent <MeshRenderer>());
            SkinnedMeshRenderer upSMR = upper.AddComponent <SkinnedMeshRenderer>();
            upSMR.sharedMesh      = mesh;
            upSMR.sharedMaterials = upMaterials;

            upSMR.sharedMesh.RecalculateBounds();
            upSMR.rootBone = upper.transform;

            BoxCollider box = upper.AddComponent <BoxCollider>();
            box.center = upSMR.sharedMesh.bounds.center;
            box.size   = upSMR.sharedMesh.bounds.size;

            deactivator.attachedRigid.AddForceAtPosition(upper.transform.forward * 0.5f, upper.transform.position, ForceMode.Impulse);
        }

        if (lowerVertices != 0)
        {
            lower = ObjectsPool.Spawn(pooledObjectName, Vector3.zero, Quaternion.identity);

            lower.layer = LayerMask.NameToLayer("Fragments");
            lower.transform.position = transform.position;

            lower.transform.rotation   = SMR.transform.rotation;
            lower.transform.localScale = transform.localScale;

            lower.SetActive(true);
            lower.name = "lower";

            Mesh meshLow = new Mesh();
            meshLow.SetVertices(lowerVertexList);
            meshLow.subMeshCount = lowIndices.Length;


            for (int i = 0; i < lowIndices.Length; i++)
            {
                meshLow.SetIndices(ApplyIndexChange(lowIndices[i], vertexPosChange), MeshTopology.Triangles, i);
            }

            meshLow.SetNormals(lowNormals);
            meshLow.SetUVs(0, lowUVs);

            Deactivator deactivatorLow = lower.GetComponent <Deactivator>();
            deactivatorLow.TriggerDeactivation(deactivateTime);

            Destroy(lower.GetComponent <MeshFilter>());
            Destroy(lower.GetComponent <MeshRenderer>());
            SkinnedMeshRenderer lowSMR = lower.AddComponent <SkinnedMeshRenderer>();
            lowSMR.sharedMaterials = lowMaterials;

            lowSMR.sharedMesh = meshLow;
            lowSMR.sharedMesh.RecalculateBounds();

            BoxCollider boxLow = lower.AddComponent <BoxCollider>();
            boxLow.center = lowSMR.sharedMesh.bounds.center;
            boxLow.size   = lowSMR.sharedMesh.bounds.size * 0.95f;

            deactivatorLow.attachedRigid.AddForceAtPosition(lower.transform.forward * 0.5f, lower.transform.position, ForceMode.Impulse);
        }
    }