How to divide the sprite into 2 parts from code

Hello,

I have some sprite and I want to divide it into two parts by randomly line (or polyline) like at picture.

But without MeshFilter I have no idea how to do it.

Help me please.

Sorry for my English and Thanks!

1434387--76509--$divsp.png

Do you need this in edit mode or runtime?

Runtime.

Thats a little bit difficult. You can do square chunks of code at runtime fairly easily, but as for dynamic angled shapes like that, its going to take a home-rolled solution to fit your exact needs for sure. You’d need something that iterates through the whole image, checking a couple points (randomly selected? depending on “sword” slices?), doing some math to get a line between them, and to determine which pixels are on which side of said line, then copying pixels from side A and side B into their own new sprites… so yeah not gonna be easy, lol be sure to share it with us when you wrap that up :stuck_out_tongue:

I can imagine something like that will turn up on the asset store soon though, with the new 2D tools kinda being so popular.

I was thinking about copying pixels via GetPixel/SetPixel from Texture2D. But I think it is very difficult for productivity. Or is there a quick way to copy the pixels?

In the first step, I want to learn how to cut a straight line.

You can use Texture2D.ReadPixels and PolygonCollider2D.SetPath(if you use physics) for that.

hmm This function gets a portion the screen?
How can I ReadPixels from some Sprite?

with get pixels 32 and set pixels 32, that’s the fastest way to grab those pixels to work with. add them to a Color array, and iterate through working some magic.