Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Algorithm to split a texture in two

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

  1. Nick4

    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.
    1.png 2.png
    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

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    5,863
    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

    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

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    5,863
    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

    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

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    15,660
    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

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,216
    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

    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.