[SOLVED] A few issues with my day/night lighting system

Hello all.

I have implemented, as part of the day/night system in my project, an “orbiting” PointLight, without attenuation, and which orbits the player’s position at a great distance. This way the player can never reach the “sun”. I have also tried setting the lighting influence radius of the PointLight to something very large, (which turns on attenuation).

The problems I have run into:

  • The lighting on the terrain does not look exactly correct. I would expect this setup to light the terrain as if there were nearly parallel rays of light hitting the surface. It does not appear so, based on the shadowing. I have tried using a DirectionalLight, but I do not like how the light looks. I prefer being able to attach the “sun” light to the entity which holds the particle emitter which serves as the visual for the “sun”. This way I can easily update the position.

  • When the “sun” sets, and is therefore below the surface of the terrain, I still see light under overhangs, and on the ceilings of caves. How do I prevent light from shining through the terrain mesh from behind? I assume that is what is happening.

@adrian-maggio said: Hello all.

I have implemented, as part of the day/night system in my project, an “orbiting” PointLight, without attenuation, and which orbits the player’s position at a great distance. This way the player can never reach the “sun”. I have also tried setting the lighting influence radius of the PointLight to something very large, (which turns on attenuation).

The problems I have run into:

  • The lighting on the terrain does not look exactly correct. I would expect this setup to light the terrain as if there were nearly parallel rays of light hitting the surface. It does not appear so, based on the shadowing. I have tried using a DirectionalLight, but I do not like how the light looks. I prefer being able to attach the “sun” light to the entity which holds the particle emitter which serves as the visual for the “sun”. This way I can easily update the position.

You will need to explain more about this because it seems like you are asking how to make PointLight behave exactly like DirectionalLight… but then don’t like DirectionalLight for some reason. DirectionalLight is actually what you want here because the sun is so far away that the rays are essentially parallel. Any other difference will be in the setup.

@adrian-maggio said: - When the "sun" sets, and is therefore below the surface of the terrain, I still see light under overhangs, and on the ceilings of caves. How do I prevent light from shining through the terrain mesh from behind? I assume that is what is happening.

When the sun goes below the horizon, turn it off… or stop moving it… or flip it to the other horizon and set it like a moon… there are a few options. But if you have a light pointing up then the underside of things will be lit. No way around that.

Have you considered using the SkyControl plugin instead of writing your own?

The first issue is no longer a problem.

@sgold, Are you talking about SkyFactory? I am already using that.

@pspeed, You’re correct, I do have some options for when the sun sets. However, there is still the problem of light shining down into caves and through overhangs when the sun is still above the horizon. Do you know is a shadow renderer will alleviate this?

@sgold, Never mind. I found SkyControl. I will try it out soon. It may make my life a lot easier!

2 Likes

Good. You’ll still need a shadow renderer, however. Here is a good introduction:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:light_and_shadow#casting_shadows

@sgold, I integrated SkyControl into my project. It is quite nice. However, I needed to modify the code at Updater.java, line 401, to this:

[java]Vector3f propagationDirection = direction; //.negate();[/java]

Maybe it is a problem in my code, but SkyControl was shining the light in the opposite direction in which I needed it to. In effect, the light was shining up through the bottom, in a direction 180 degrees from where the sun/moon was.

Any idea what could cause this?

I’m not sure. Perhaps your scene is “upside down”: uses -Y for the “up” direction instead of +Y.

Not to my knowledge. When I check the camera location, it is positive Y. PhysicsCharacter and gravity behave properly. My normal vectors are correct. It is all very odd.

Could you provide me with a small app which demonstrates the issue?

I will when i get a second to work more on this. I do not rule out that it is a problem in my code. When I enable shadows, I get weird results as well… as if the shadows are being cast by geometry from the underside of the terrain. Does it help to tell you that I am generating procedural terrain? A lot of things could go wrong. Thanks in advance for your help!

@adrian-maggio said: I will when i get a second to work more on this. I do not rule out that it is a problem in my code. When I enable shadows, I get weird results as well... as if the shadows are being cast by geometry from the underside of the terrain. Does it help to tell you that I am generating procedural terrain? A lot of things could go wrong. Thanks in advance for your help!

Sounds like either your normals are backwards or your winding is backwards. Did you have to change your material’s RenderState face culling for your stuff to show up by any chance?

I’m culling the back-faces:

[java]rs.setFaceCullMode( RenderState.FaceCullMode.Back );[/java]

@adrian-maggio said: I'm culling the back-faces:

[java]rs.setFaceCullMode( RenderState.FaceCullMode.Back );[/java]

Then I don’t know… it seemed like a vertex winding issue.

Sounds like something is up with your mesh, though. Try any of the standard JME shapes and I suspect they will look right.

@pspeed said: Then I don't know... it seemed like a vertex winding issue.

Sounds like something is up with your mesh, though. Try any of the standard JME shapes and I suspect they will look right.

You are correct. Standard JME primitives behave as they should.
Is it possible my vertex winding is backwards, and my normals are also inverted?

@adrian-maggio said: You are correct. Standard JME primitives behave as they should. Is it possible my vertex winding is backwards, and my normals are also inverted?

Either is possible… but what you’ve described so far seems to indicate that your winding is not backwards. You may need to reduce things to a simple test case.

Edit: just to clarify, your original problem sounds exactly like what would happen if the winding was backwards. Your description of how you’ve set the material up leads me to believe otherwise.

It seems I had the normals negated. It works properly now.

1 Like