/// <summary> /// /// </summary> /// <param name="p_spriteObj"></param> /// <param name="p_sprite"></param> /// <param name="p_point1"></param> /// <param name="p_point2"></param> /// <returns></returns> public OneCut.CutResult CutSpriteToMesh(OneCutObject p_spriteObj, Vector2 selfPos, Vector2 p_point1, Vector2 p_point2) { List <Triangle> exp_trig = new List <Triangle>(p_spriteObj.triangles); List <Vector2> intersectionPoints = new List <Vector2>(); Sprite sprite = p_spriteObj.sprite; Vector2[] vertices = p_spriteObj.meshVert; VerticesSegmentor verticesSegmentor = new VerticesSegmentor(p_point1, p_point2); for (int i = 0; i < exp_trig.Count; i++) { if (exp_trig[i].pairs.Count == 3) { HandleTrigIntersection(intersectionPoints, exp_trig[i], verticesSegmentor, selfPos, p_point1, p_point2); } } List <Triangle> newTrigCol = TrigBuilder.Build(exp_trig); //always contain 2 trig result var segmentResult = ResegmentTriangle(newTrigCol, verticesSegmentor, selfPos); return(new OneCut.CutResult( GetSprite(segmentResult[0]), GetSprite(segmentResult[1]), GetSprite(exp_trig), intersectionPoints )); //ResegmentVertices(vertices, verticesSegmentor); }
/// <summary> /// Cut object with line given /// </summary> /// <param name="p_spriteObj">Object to cut</param> /// <param name="p_point1">Line header</param> /// <param name="p_point2">Line footer</param> /// <param name="p_callback">Callback after calculation</param> public void Cut(OneCutObject p_spriteObj, Vector2 p_point1, Vector2 p_point2, System.Action <CutResult, bool> p_callback) { Vector2 objectPos = p_spriteObj.transform.position; #if !UNITY_WEBGL RunCutAyns(delegate { RunCut(p_spriteObj, objectPos, p_point1, p_point2, p_callback); }); #else RunCut(p_spriteObj, objectPos, p_point1, p_point2, p_callback); #endif }
/// <summary> /// Actual cutting mehtod /// </summary> /// <param name="p_spriteObj"></param> /// <param name="objectPos">Transform.positon couldn;t be obtain inside Threading, so give it individually here</param> /// <param name="p_point1"></param> /// <param name="p_point2"></param> /// <param name="p_callback"></param> private void RunCut(OneCutObject p_spriteObj, Vector2 objectPos, Vector2 p_point1, Vector2 p_point2, System.Action <CutResult, bool> p_callback) { List <Triangle> originalTrig = p_spriteObj.triangles; CutResult cutResult = mainAlgorithm.CutSpriteToMesh(p_spriteObj, objectPos, p_point1, p_point2); var newArea = System.Math.Round(cutResult.mainSprite.area + cutResult.subSprite.area, 2); var oriArea = System.Math.Round(cutResult.originSprite.area, 2); bool isSuccess = (newArea >= oriArea); lock (results) { results.Enqueue(new TaskResult(cutResult, isSuccess, p_callback)); } if (debugMode) { p_spriteObj.SetDebugVaraible(cutResult.intersectionPoints, cutResult.mainSprite.meshVert, cutResult.subSprite.meshVert); } }