- Home /
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!
He is trying to "conduct physical simulations on it"(or not) depending upon this value. Apologies for before I failed to spot this vital bit of info in one of you later comments, in part due to my tiredness and in part down to your repetition.
I never said I would be able to solve your problem, I just wanted you to give the community that little bit of vital information so they are better equipped to help you out... The people on the forums may be able to offer you a variety of ways to achieve your goal with this extra little bit of info. (without it you could have been trying to do anything...)
PS :Please try and be polite to people when they are genuinely trying to help you :)
Oh and post your other pics again in conjunction with this, they do help explain what you are doing :D
In all the moment I've been polite with you, but taking into account your continuous repetitions, it seemed that you were kidding me. Thank you.
Now, I'd need any help on this, I'm quite confused :).
No problem 7toni7 :D .I'm not like that, I always try my best to help. I hope someone thinks of some suitable way for you to achieve your goal, I know you have been struggling with this for a while now and it must be frustrating. All the best and good luck :)
Answer by thef1chesser · Oct 16, 2014 at 02:50 PM
Well searching this site gives me this: http://answers.unity3d.com/questions/24662/detect-lightshadow-falling-on-object.html
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)
Answer by Bunny83 · Oct 16, 2014 at 04:37 PM
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.
Easy, I've solved the problem placing the main camera on the normal vector of each facet of the mesh, and making the camera point to that facet and rendering the color of that facet (using a very low value of the field of view of the camera to constraint the camera pointing). Consequently, I can obtain a rendered image of each facet in the mesh, thus allowing me to compute the final brightness of each one ;).