Render texture camera points at “dirt” texture in front of model texture. Alpha of dirt texture is manipulated to simulate cleaning of the model. I would like to keep track of the ratio of remaining dirt pixels to the starting dirt pixels; this would be easy if I could just use the texture dimensions - but a model usually does not take up 100% of its texture atlas. How would I find the coordinates of all pixels of a texture atlas used by a given model?
the mesh has UV coordiantes on each vertex pointing into the texture in a normalized way.
this means a UV.x of .5 points to 512th pixel if the texture is 1024 wide. the UVs are interpolated when covering the mesh surface
Hi
That’s an interesting problem. Before going into a solution, worth mentioning that reading from a render texture on CPU is very expensive (it flushes the GPU). If you are doing so, that’ll be a consideration you may need to take into account if doing it every frame.
Anyway, onto a solution. There’s no entirely simple way of doing this, however it is possible. You will have to iterate over all the triangles in each submesh of your mesh. This will basically mean reading the indices from each submesh. Then you’ll need to use those to read the UVs of each triangle on your mesh.
From that work you’ll be able to extract a list of triangles, and for each one the UV value assigned to each vertex. Now if you allocate an array integers of the same dimensions as your texture, you can use the UVs to iterate over each texel in each triangle, and increment its corresponding element in your array by 1.
Once complete, your array will be a count of how many triangles use each texel.
The tricky bit will probably be using the 3 UV values for your triangle to work out which texels it uses. This is a problem similar to rasterizing a triangle for which there’s lots of info online available.
One thing I would recommend is whilst you develop this, implement the ability to easily copy that array into a texture and render it into a quad on screen. That’ll help you visualise what’s going on and make it easier to debug.
-Chris