Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to get worldspace (or localspace) positions of green pixels on a texture?

Discussion in 'General Graphics' started by Sarrivin, Oct 4, 2023.

  1. Sarrivin

    Sarrivin

    Joined:
    Dec 18, 2015
    Posts:
    24
    Hello,

    Like in the topic title. I need to get the worldspace (or localspace) positions of green pixels on a texture on a plane, so I could instantiate prefabs there. I loop through the pixels and find them, but how to convert their index positions to worldspace or localspace?

    Code (CSharp):
    1. int textureWidth = textureToSpawnOn.width;
    2. int textureHeight = textureToSpawnOn.height;
    3.  
    4. for (int x = 0; x < textureWidth; x++)
    5. {
    6.      for (int y = 0; y < textureHeight; y++)
    7.      {
    8.          Color pixelColor = textureToSpawnOn.GetPixel(x, y);
    9.          // Check if the pixel color is green
    10.          if (pixelColor == Color.green)
    11.          {
    12.              // TODO: Find each pixel in worldspace (or localspace)
    13.               Vector3 spawnPosition =
    14.          }
    15.      }
    16. }
    Tried many things, but still not there. Anyone could do this?

    Edit: Figured out it's impossible for anything with variable height, so I guess I need some other solution.
     
    Last edited: Oct 4, 2023
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,837
    You already got close to the UV. This can show how to get the world pos. You can try to load UV info, or just divide the found pixel by the total size (if it's pixel 512 of a 1025 pixel, the value would be 0.5).
    If it's on a plane and the green pixel is on 0.5, 0.5, you can use the plane width and height in world space to calculate the position of the pixel. For variable height you can sample the heightmap to add that to the height. You're basically doing the reverse of rendering
     
    Sarrivin likes this.
  3. Sarrivin

    Sarrivin

    Joined:
    Dec 18, 2015
    Posts:
    24
    Thank you for the response. Yeah, you've confirmed exactly what I've since figured out. Have no idea why I forgot that (probably a break I had from graphics). I've remembered UVs are basically percentages, so it's easy. There could be also scale involved and that heightmap sampling. Weird, I was confused it would be harder for some reason.
    Maybe because it would be harder if there was no heightmap for some irregular objects. Guess there'd be some hardcore number crunching with triangles involved. Wonder if there's a function for that - I bet there should be if the texture is painted all over such object... :)
    Don't have time to reconstruct it on triangles and UVs now though, maybe later.

    ps. oh, and it's not a performant approach, but needed to reconstruct the concept for some spatial understanding - off cpu we go.
     
    Last edited: Oct 7, 2023
    DevDunk likes this.