Directional Lights influence the wrong normals [updated]

Discussion in 'Shaders' started by ciprianbiris, Aug 8, 2014.

1. ciprianbiris

Joined:
Oct 11, 2013
Posts:
26
Hi,

I have a problem with the directional lights. I try to set up a "sun" so I use 6 directional lights, one for each direction. All my objects are cubes and the normales are set up correctly. If I use a Point Light and turn off different normals for different cube faces, it works properly and I see black for 0.
But if I use the 6 directional lights, which are all perpendicular to each other and perpendicular to the cubes, the light adds up and for example, a light which is oriented on the ground, will add light also to the other faces.
Why does this happen? Even if I turn normals to 0, I don't get black anymore because the light is influenced.

Hoe can I have a correct light like a point light but formed from directional lights?

Unity Technologies

Joined:
Aug 11, 2006
Posts:
3,356
I'm having difficulty understanding your setup/question. A picture might help. Why do you need more than one directional light for a sun?

3. ciprianbiris

Joined:
Oct 11, 2013
Posts:
26
I need more than one directional light because my game is made of cubes. So all surfaces are perpendicular to each other. Imagine a game like Minecraft.
The problem is that even if I set the normals of the top surface to 0,0,0, it will still receive light.
I need 6 directional lights because each "face" of the world would respond to one directional light. This way also my Ambient Occlusion works but the problems is that lights act crazy.
So why did a surface with normal set at 0,0,0 receive light ?

so I want each directional light to light only the corresponding surface. Imagine a cube and each face of the cube would be influenced by only one directional light.

4. metaleap

Joined:
Oct 3, 2012
Posts:
589
This sounds so incredibly strange I have no idea what kind of visuals this is supposed to produce. Minecraft screenshots I've seen so far use standard lighting -- 1 directional for sun or moon, point lights where applicable for dunno candles/campfires/torches or whatever...

5. ciprianbiris

Joined:
Oct 11, 2013
Posts:
26
Nope, it does not use point lights. Light information is stored in a light matrix and spread across the normals. I will upload some photos though it is kind of hard to explain what happens even with photos. One directional light would influence max 3 planes at a time out of 6. there would be parts of the scene pitch dark
So here's an example:
One directional light pointing down - everything is okay - only top surfaces are lit up no matter what intensity the light has

Multiple directional lights pointing down - not okay - other surfaces different from the top surfaces also become lit up

Point light - acts correctly - this is what I kind of want to achieve

Last edited: Aug 10, 2014
6. mouurusai

Joined:
Dec 2, 2011
Posts:
309
<<So why did a surface with normal set at 0,0,0 receive light ?>>
It's because vertex lights calculates not per each light source, all lights packs into spherical harmonics. I don't understand math behind, maybe someone else can explain why is never became to black or maybe it's bug.
-------
You can force per pixel light calculations be turn light source options to "important" or write custom shader to take that effect.
-------
Here sphere lit with vertex light. It exactly looks wrong, it seems it is still a bug.

7. ciprianbiris

Joined:
Oct 11, 2013
Posts:
26
So I declare it as a bug.
Easy to replicate:
Create an empty scene
Add one Directional Light with rotation 90,0,0 - everything is okay - only top surface receives light
Add one more Directional Light with rotation 90,0,0 - not okay - other surfaces receive light too

Last edited: Aug 11, 2014
8. ciprianbiris

Joined:
Oct 11, 2013
Posts:
26
So is it a bug or not?

9. Farfarer

Joined:
Aug 17, 2010
Posts:
2,249

I created a cube, a downwards facing directional light and then duplicated the light. Top face gets brighter, nothing else changes.

How are you combining the cubes together (I assume you are or your draw calls would be mental)?

10. ciprianbiris

Joined:
Oct 11, 2013
Posts:
26
Wait what? it doesn't happen for you? I have Unity 4.5.1f3
How I combine cubes? well, 16x16x256 cubes form one mesh..
I get around 100 draw calls

11. Farfarer

Joined:
Aug 17, 2010
Posts:
2,249
I mean, what do you do to combine them? I suspect you're averaging the vertex normals at the joins or the normals at the joins are taking one value and using that for everything.

But no, the lighting doesn't add on areas that are not facing the light. Are you using a custom shader? If so, what's the code for it?

12. mouurusai

Joined:
Dec 2, 2011
Posts:
309
Farfarer, switch light source render mode to "not important", it's appear only for vertex lightings.

13. Farfarer

Joined:
Aug 17, 2010
Posts:
2,249
Ah, right. I see it now... could well be to do with the approximation of Spherical Harmonics.

14. ciprianbiris

Joined:
Oct 11, 2013
Posts:
26
but it is still a bug. I can't do my lighting model because the lighting is bugged..

15. Farfarer

Joined:
Aug 17, 2010
Posts:
2,249
It's not a bug, it's just how spherical harmonics work. They're designed to be a fast approximation, not accurate.

16. Flailer

Joined:
Apr 1, 2014
Posts:
66
It might be worth spending a little more time working out how a game like minecraft is rendered. I would imagine the set up to be more along the lines of ambient light to get an overall feel of global illumination and a single directional light for shadows and direct lighting from the sun position.

Try getting two lamps and a cube of paper and watch how they behave on a table in your room, you might be surprised at how light actually reacts when you start stacking them together.

Annnnd I wrote a nice little paragraph here about how spherical harmonics work and why you're receiving the results you are but then.... woops, all got deleted.

To try and sum up what I said, imagine an infinitely small point in space, being made into a sphere... (SPHERICAL harmonics) - casts out inifinite rays, in all directions. More than one dir light... and BAM infinite casting sphere. Worth reading up on all this stuff as well as diving in. At least you have a few phrases to google now.

17. ciprianbiris

Joined:
Oct 11, 2013
Posts:
26
I don't want to use the ambient light because I can't use the Ambient Occlusion (which is in the normals). I understand now the limitation of the Spherical Harmonics. Though it seems I can't use two lights in "important" render mode .
Is Unity Free able to provide 2 per-pixel-lights? It seems only the brightest one is shown and if both have same brightness, a random one is chosen.
2 per-pixel lights would satisfy my design..

18. Farfarer

Joined:
Aug 17, 2010
Posts:
2,249
You would probably do well to just have 6 colour values in your shader, one per direction and blend based normal components (much like monument valley does it).