Search Unity

Question Retrieve a texture's pixel world position

Discussion in 'Testing & Automation' started by gabrimo, May 28, 2022.

  1. gabrimo

    gabrimo

    Joined:
    May 2, 2015
    Posts:
    92
    Hi there, I've been expanding the color detection system of my project. My current setup consist in a series of quads of the same size in front of each other. Those quads act like image layers, meaning that after some images are hierarchically arranged on them, a composite image is produced, which can then be exported as a texture using a camera and a render texture...

    Sometimes a given image layer needs to check it's background color(s) (the color(s) of a previous layer), for that, I'm generating some raycasts at specific vector2 positions within that quad's surface, if the pixel hit is transparent, that layer is moved to the "ignore raycast" layer and the next one is tested until a valid color is found. This core behavior is working, my problem regards in the raycast origin positions. Currently, those positions need to be manually filled by me considering each image shape, which isn't practical at all considering I'll have hundreds of them in the future. That's why I need to improve that, filling those positions "automatically" considering each image differences.

    To achieve that, my idea is to read the valid (non transparent) pixels of each image and iterate over those using a interleave limit. So for example, if my image has 5000 valid pixels, with a interleave limit of 500, that would result in 10 pixels apart from each other. The thing is, I don't know how to retrieve these "10 pixels" positions in order to store that to be used in my color detection module. I used GetPixels() to retrieve the pixels as a Color array, but in a second look I noticed that this class doesn't offer a way to access the pixels' positions (as far as I know at least). So I would like to know if someone here has a good suggestion to help me out.

    Hopefully my doubt is clear enough, express this using text may not be that simple to grasp, but I can provide further explanation and even produce some simple drawings in paint to explain better if needed...


    PS - If this is the wrong session, feel free to move my thread to the right one.