How to obtain the brightness/color of a triangle?

Hi all,

I’m confused about how to obtain/compute the brightness of a set of triangles, when a directional light acts on them. My scene is composed of a directional light and a mesh. consider the following image:

I just want to know a way to obtain a brightness value (for each of the triangles) that indicates me if that triangles is illuminated, partially occluded by other triangles or shadows, or completely occluded. For example, using a brightness value.

Could I access to the texture attached to the my mesh’s material to know these values? theoretically I think so, because Unity has already calculated this brightness caused by the directional light.

Thank you!

Well searching this site gives me this: Detect light/shadow falling on object? - Unity Answers

So this a very complex issue within Unity apparently, which I am surprised by for the same reason as you state that this has to be calculated by Unity anyway.

It seems that in the more recent version they’ve added Getpixel() (see link, 2nd answer, for how to use it)

First of all, why did you post a new question with the actual same question when your other question isn’t answered yet?

I think the main issue you have is to understand how the actual brightness value of a “visible pixel” is calculated. Realtime lighting is calculated exclusively on your GPU in a shader. This information can’t be simply “accessed” as it is calculated per pixel of your visual screen. Things not visible by the camera aren’t even drawn / calculated. The GetPixel approach in the answer of thef1chesser does only work for baked lightmaps. So this is static information which doesn’t change. It’s not realtime lighting.

The calculation of the brightness value works like JonnyHilly explained in your 3rd question on the same topic.

Shadows are a way more complex topic. Shadows are extremly difficult to calculate. Most lighting systems use shadowmaps (which contains information how the scene is seen by a lightsource) or create shadow geometry (a mesh that represents the shadow volume ). However both approaches to simulate shadows calculate the final pixel color in the screen space of the camera. The triangles of the mesh have no idea how they might get shaded on the GPU.

The closest you could get by just calculating would be something like:

  • calculate manually the average brightness of the triangle by calculating the brightness of each vertex of the triangle and take the average.
  • To implement rudimentary shadowing you could simply raycast from the light source to your 3 vertices to see if something is in the way. Of course for directional lights you could just use the inverse light direction and start the ray at the vertex to see if there’s something in the direction where the light comes from. This will of course only detect closed objects as your ray would hit the backside of the occluder. The other way is to simply raycast from a distant point offset by the inv light direction towards your vertex. This of course only gives you a rough guide if the triangle receives shadow or not.

Another way could be to render the scene from the light position (for directional light you would use an orthographic camera) and render a modified version of the object where each triangle would be rendered with a different color (of course without lighting). That way you can use the pixel color to determine to which triangle the pixel belongs. Since you render from the light source only those parts are visible that receive light from this light source.

Any of these approaches would be quite heavy and i wouldn’t expect good performance. Keep in mind that Unity is a game engine. It’s whole purpose is to create a visual image for humans to watch. It’s not a scientific simulation software. If you want to calculate exact values based on a real-world-physics light model Unity will not be much helpful as that’s not what it’s made for.