I am trying to understand how (and if) I can convert from UV coordinates in the range of 0f to 1f to an array of pixel indices so I can modify the colors that fall within the bounds of the UV coordinate directly and apply them using ‘Texture2D.SetPixels32’… as far as I understand I should be able to convert from UV coordinates to texel coordinates like this:
// Convert UV coordinates to texel coordinates.
float u = pUVCoords.x * (pTextureWidth - 1);
float v = pUVCoords.y * (pTextureHeight - 1);
Considering my texture is bilinear (and has mipmapping disabled) I thought I could get the relevant indices like this:
pixelIndices.Add(y0 * pTextureWidth + x0);
pixelIndices.Add(y0 * pTextureWidth + x1);
pixelIndices.Add(y1 * pTextureWidth + x0);
pixelIndices.Add(y1 * pTextureWidth + x1);
but it’s missing many pixel indices (about half) leading me to believe I’m missing something or not understanding in general.
For more context I am iterating over all Mesh.uv elements so in my test it should be changing the color of the entire mesh if I’ve accomplished my goal.
I was hoping someone could help me understand how Unity internally converts from UV coordinates to pixel indices when sampling textures and how I could (if possible) accomplish my goal of getting an array of all indices for pixels in a Texture2D that fall under a given UV coordinate.
Thank you in advance for any information that helps me understand how I’m misunderstanding things, and/or how to solve this.