# Algorithm to split a texture in two

Discussion in 'Scripting' started by Nick4, Aug 12, 2018.

1. ### Nick4

Joined:
Nov 27, 2013
Posts:
51
I have a texture and when I draw a line over the texture, the intersecting pixels become transparent.

However, now I want to create two separate textures by splitting the texture in 2 by the transparent pixels. I've been thinking really hard for the last couple of days but even if I do come up with something, a horizontal or vertical line messes things up. And I'm not really good at math, maybe there is something I could use in "Mathf" class but I don't know. Any ideas?

2. ### lordofduct

Joined:
Oct 3, 2011
Posts:
6,361
Copy the texture out, and set all pixels on one side of the line transparent.

You know what pixels are set transparent. They have an x,y coordinate in the texture. So for all pixels you just loop over getting its position in the texture and compare it to the line.

So say you're at pixels 32,12... you find out what point the line is at at x=32, say it's at 54. So you're < 54, so if you're filling where everything < the line is transparent, you set that pixel transparent.

Kurt-Dekker likes this.
3. ### Scabbage

Joined:
Dec 11, 2014
Posts:
266
That will break if the line is vertical, because it wont have a point on the line at x=32. There would also need to be a check for that condition, and then it would need to switch to using the y coordinate.

Last edited: Aug 13, 2018
4. ### lordofduct

Joined:
Oct 3, 2011
Posts:
6,361
Not necessarily.

Because your function for the line can return a value outside of the bounds of the image, such as a negative number for where the line is.

The only way it would fail is if the line is non-functional. Which only one line with a constant slope exists that is non-functional. And that's a perfectly vertical line. But of course that's going to have issues... it's sort of in its name, it's "non-functional".

5. ### Scabbage

Joined:
Dec 11, 2014
Posts:
266
That's my point. With the resolution he's using a totally vertical line looks very possible, in which case the axis would need to be swapped conceptually so it's a function again.

6. ### Kiwasi

Joined:
Dec 5, 2013
Posts:
16,323
Cheat. Create two new meshes with vertices at the endpoints of the lines. Its going to be much easier on performance then creating separate textures.

Hosnkobf and lordofduct like this.
7. ### jvo3dc

Joined:
Oct 11, 2013
Posts:
1,334
I agree that that would be a lot faster.

In terms of representing a line that can also be horizontal or vertical, use ax + by = c. (Where a and b together are normalized to 1, similar to the representation of a plane in 3D.) In a shader you can then clip the pixels on both sides using:
Code (csharp):
1.
2. float2 point; // Position of the current pixel
3. float3 line; // Representation of the line with a, b and c
4. clip(dot(point, line.xy) - line.z); // Clip one side
5. clip(line.z - dot(point, line.xy)); // Clip the other side
6.

8. ### Nick4

Joined:
Nov 27, 2013
Posts:
51
Thanks a lot. This will probably do it and it's at least 100 times more economic in terms of performance.