- Home /
Question by
IslamUni · Aug 26, 2017 at 11:17 AM ·
spritepositioningcutting
How to position Sprite sliced/cut parts?
Hello everyone, I'm working on this sprite slicer mechanics and everything is finished except for the positioning of the two cut objects so help with this please. The code is too much to place here so I'm going to share what's relevant to the issue, This is the main function to get an idea of the workflow.
public void SliceSprite(Vector2 hitPointA, Vector2 hitPointB, GameObject obj)
{
SpriteRenderer spriteR = obj.GetComponent<SpriteRenderer>();
Vertices originCornerPoints;
Vertices newCornerPoints;
Sprite originSp, newSp;
// Transforming points from global to local;
hitPointA = obj.transform.InverseTransformPoint(hitPointA);
hitPointB = obj.transform.InverseTransformPoint(hitPointB);
// Get vertex set for both old and new sprite AND create sprite pieces from the original.
FindPathWithRectangleCorners(hitPointA, hitPointB, out originCornerPoints, out newCornerPoints, out originSp, out newSp, spriteR);
// Closing the polygon with the line that forms a convex.
SealPolygon(ref originCornerPoints, hitPointA, hitPointB, spriteR.sprite, originSp);
SealPolygon(ref newCornerPoints, hitPointA, hitPointB, spriteR.sprite, newSp);
// Sort vertices to shape outlines
originCornerPoints = SortVertices(originCornerPoints);
newCornerPoints = SortVertices(newCornerPoints);
// Build the required texture polygon based on the slice line.
OverrideSpriteGeometry(originCornerPoints, originSp);
OverrideSpriteGeometry(newCornerPoints, newSp);
// Creat sliced sprites
DeploySlices(obj, spriteR.sprite, originSp, newSp);
}
and here is where the problem occurs (testing on vertical cuts only).
private void DeploySlices(GameObject obj, Sprite mainSprite, Sprite originSp, Sprite newSp)
{
int flipX = obj.GetComponent<SpriteRenderer>().flipX ? -1 : 1;
int flipY = obj.GetComponent<SpriteRenderer>().flipY ? -1 : 1;
// Create a new gameobject to carry the smallest slice of the sprites.
GameObject go = new GameObject("Slice");
//go.transform.SetParent(obj.transform, false);
go.transform.localScale = obj.transform.localScale;
go.transform.position = obj.transform.position;
go.AddComponent<SpriteRenderer>();
go.GetComponent<SpriteRenderer>().flipX = obj.GetComponent<SpriteRenderer>().flipX;
go.GetComponent<SpriteRenderer>().flipY = obj.GetComponent<SpriteRenderer>().flipY;
float value1 = 0.0f, value2 = 0.0f;
value1 = mainSprite.rect.width - originSp.rect.width;
value1 *= flipX;
go.transform.position += new Vector3(value1, 0);
go.GetComponent<SpriteRenderer>().sprite = originSp;
mainSprite = null;
}
a display sample of the current positioning approach drawn on top of the original sprite for comparison.
ask if there is any ambiguity, thank you.
capture.jpg
(24.1 kB)
Comment