Is there a way to get the texture color of a raycast result?
Currently, I’m raycasting on a textured cube with camera.screentoworld, which works just fine, but the raycast result only returns the entity, position and normal. Is there a conversion from world space-location to UV coordinates or something similar? I’m looking for what in Unity would be,
There isn’t a built in way to do that, I can think of two ways in doing that:
- One is as you said to get the UV coordinates on the picked point, here is a helpful post on the subject:
And this example does something similar to paint damage: https://developer.playcanvas.com/en/tutorials/character-damage-demo/
- The other way would be to read pixels from the frame buffer at the 2D coordinates of the mouse click and get directly the pixel color. Similar to what the pc.Picker class does to pick models.
Note though that this method has a performance hit since it stalls the GPU while the CPU reads the pixels.
I took a look at it but it’s note very reliable the way it’s working … On every frame to do the raycast, extract uvs, create a canvas, add the image, extract context then get the coordinates … especially every frame.
I took a different approach raycasting on a generated mesh and using those values