Picking texture pixel with a ray cast (example)

It was mentioned a few times in the forum about a technique of casting a ray to read a pixel from a texture.
I wanted to try this old trick to simulate some rudimentary lighting by picking the static lightmap below an object and change its light intensity accordingly.
Another use case would be to get pixel perfect collisions on sprites (check if alpha > 0).

I couldn’t find any complete example in the forum so here is mine:

Sample screenshot:
Imgur
The box below the crosshair shows the color of the lightmap.

Any feedback is welcome.

5 Likes

Well, for terrain i just calculate x/y percentage coords and then i know texture x/y percentage too.

But for more advanced models i belive you would need pick triangle(from raycast), get its 3 points of mesh, know their UV coords, then based on raypick position between this 3 points calculate real UV coord of raypick, then use this UV coord and get pixel from texture.

i dont know any better solution, but maybe there is.

That’s exactly what I did.
The idea of my post is to provide a reference to anyone wanting to accomplish this in the future.

ah sorry, didnt noticed you finished it :slight_smile: Thought it was some problematic example.

Glad you share :+1: