public void OnCollisionEnter(Collision col) { if (ignoreColliders.Contains(col.collider)) { ignoreColliders.Remove(col.collider); return; } Cuttable cuttable; if (!col.gameObject.TryGetComponent <Cuttable>(out cuttable)) { return; } Vector3 cutDir = directionsAreNormals ? TransformNormal(cutDirection, transform) : transform.TransformDirection(cutDirection); float relVel = (col.relativeVelocity - Vector3.Project(col.relativeVelocity, cutDir)).magnitude; // Debug.Log("vel: "+relVel); // Debug.DrawRay(col.GetContact(0).point,col.relativeVelocity-Vector3.Project(col.relativeVelocity,cutDir)/relVel,Color.blue,1); if (minimumVelocity > relVel) { return; } Vector3 dir = omnidirectionalMode ? -col.relativeVelocity : cutDir; Vector3 edge = directionsAreNormals ? TransformNormal(edgeDirection, transform) : transform.TransformDirection(edgeDirection); Vector3 angleProjection = Vector3.ProjectOnPlane(gameObject.GetComponentInParent <Rigidbody>().velocity, edge); // Debug.Log("angle: "+Vector3.Angle(angleProjection,cutDir)); // if (Vector3.Angle(angleProjection,cutDir) > 70) Debug.Break(); // Debug.DrawRay(col.GetContact(0).point,angleProjection,Color.red,1); // Debug.DrawRay(col.GetContact(0).point,cutDir,Color.green,1); // Debug.DrawRay(col.GetContact(0).point,-col.relativeVelocity,Color.blue,1); if (Vector3.Angle(angleProjection, cutDir) > maxAngle) { return; } Vector3 normal = Vector3.Cross(dir, edge).normalized; Vector3 pointInPlane = useContactPoint ? col.GetContact(0).point : transform.position; CuttingPlane plane = CuttingPlane.InWorldSpace(normal, pointInPlane); CutParams param = new CutParams( cuttable.checkForHoles, true, cuttable.closeOpenSurfaces, cuttable.allowOpenSurfaces, Vector3.zero, float.PositiveInfinity, 0, cuttable.innerTextureCoordinate ); CutResult result = PerformCut(col.gameObject, plane, param); if (result != null) { if (cuttable.polySeparate) { result.PolySeparate(); } result.DestroyObject(); foreach (CutObj res in result.Results) { GameObject resObj = res .UseDefaults() .WithDriftVelocity(driftVelocity) .WithSeperationDistance(seperationDistance) .WithRingWidth(cuttable.highlightWidth) .WithRingColor(cuttable.highLightColor) .FallbackToColor(new Color(1, 0.1f, 0.1f)) .Instantiate(); cuttable.CopyTo(resObj); ignoreColliders.Add(resObj.GetComponent <Collider>()); } } }
List <GameObject> IterativeCut(GameObject obj, int count) { CutParams param = new CutParams(true, false, false, false, Vector3.zero, float.PositiveInfinity, 0, Vector3.zero); try { Vector3 pos = obj.transform.position; Collider col; if (obj.TryGetComponent <Collider>(out col)) { pos = col.bounds.center; } CutResult res = API.PerformCut(obj, CuttingPlane.RandomInWorldSpace(obj.transform.position), param); if (res == null) { return new List <GameObject>() { obj } } ; res.DestroyObject(); res.PolySeparate(); if (count > 1) { List <GameObject> ret = new List <GameObject>(); foreach (CutObj robj in res.Results) { ret.AddRange( IterativeCut( robj .CopyParent() .CopyMaterial() .CopyVelocity() .CopyDensity() .WithCollider() .WithDriftVelocity(0.2f) .Instantiate(), count - 1 ) ); } return(ret); } else { return(res.ConvertAll( robj => robj .CopyParent() .CopyMaterial() .WithCollider() .WithRenderer() .CopyVelocity() .CopyDensity() .WithDriftVelocity(0.2f) .Instantiate() )); } } catch (MeshUtilsException e) { Debug.LogWarning(e); obj.AddComponent <Rigidbody>(); obj.AddComponent <MeshRenderer>(); obj.AddComponent <MeshCollider>(); return(new List <GameObject>() { obj }); } }