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; }
void onCutTriggered(Transform planeTransform) { if (threadedCutter.IsBusy) { return; } List <CutData> cutData = new List <CutData> (); int activadedChildrenCount = activatedChildren.Count; for (int i = 0; i < activadedChildrenCount; i++) { if (activatedChildren [i].IsTouchingBlade && deactivatedChildrenPool.Count > 0) { Plane cuttingPlane = new Plane(planeTransform, activatedChildren [i].transform); CutData data = new CutData(); SimpleMesh simpleMesh = new SimpleMesh(); simpleMesh.Create(activatedChildren [i].GetMeshFilter().mesh, cuttingPlane); data.Parent = activatedChildren [i]; data.Child = deactivatedChildrenPool.Dequeue(); data.Plane = cuttingPlane; data.SimpleMesh = simpleMesh; data.CutDirection = planeTransform.right; cutData.Add(data); data.Parent.IsBusy = true; data.Child.IsBusy = true; } } if (cutData.Count > 0) { StartCoroutine(cutInThreadAndWaitToEnd(cutData)); } }