Esempio n. 1
0
        void proceedCutResult(CutData cutData)
        {
            if (cutData == null || cutData.Parent == null || cutData.Child == null || cutData.CutResult == null)
            {
                return;
            }

            Cuttable parent       = cutData.Parent;
            Cuttable child        = cutData.Child;
            Plane    cuttingPlane = cutData.Plane;
            Vector3  cutDirection = cutData.CutDirection;

            Mesh      mesh          = parent.GetMeshFilter().mesh;
            CutResult cutResult     = cutData.CutResult;
            Mesh      aMesh         = cutResult.CreateMeshA();
            Mesh      bMesh         = cutResult.CreateMeshB();
            Mesh      newParentMesh = aMesh;
            Mesh      newChildMesh  = bMesh;

            if (newParentMesh.vertexCount > 0 && newChildMesh.vertexCount > 0)
            {
                //original object base should stay in place so I switch meshes position in case
                //bMesh is closer to an origin point
                flipMeshesPositionIfNeeded(ref newParentMesh, ref newChildMesh, parent.transform);

                parent.SetNewMesh(newParentMesh);
                child.SetNewMesh(newChildMesh);

                //set mesh and position for child
                child.transform.position   = parent.transform.position;
                child.transform.rotation   = parent.transform.rotation;
                child.transform.localScale = parent.transform.localScale;
                child.GetMeshFilter().mesh = newChildMesh;
                child.gameObject.SetActive(true);
                activatedChildren.Add(child);

                //add torque and force to child object
                Rigidbody rb = child.GetComponent <Rigidbody> ();
                addForceAndTorqueToAChildObject(rb, cutDirection);

                //shoot particles
                if (cutResult.EdgeVertices.Count > 0)
                {
                    Vector3 pos = cutResult.EdgeVertices [0];
                    pos = parent.transform.TransformPoint(pos);
                    particleManager.ShootParticles(pos, cutDirection);
                }
            }
            else
            {
                child.gameObject.SetActive(false);
                deactivatedChildrenPool.Enqueue(child);
            }

            parent.IsBusy = false;
            child.IsBusy  = false;
        }