I have a problem when coming to handle faces in an unlit area. meaning that if im underground the top face is still being lit up more than the sides. Even when the sun is not on top. im using directional light and ambient light. this problem is coming from sun = new DirectionalLight(); and when i uncomment it it works (but then i dont have sunlight its just the ambient remoaning)
You will need to show the code that changes the direction of the directional light… because I think it’s not really being changed if the lighting isn’t changing.
Edit: in case someone else comes across this question in the future and wants a little more background into one of the fundamental possible confusions with lighting and shadow… here is what I wrote on discord:
“”“”
In 3D graphics, lighting and shadow are not the same thing.
With just a directional light and an object, the facing of the triangles (their normals) controls how bright that triangle will be. Facing the light directly = very bright. Facing away from the light = no light. This is not the same as shadodws.
Lighting is easy. Shadows are hard. Shadows require a lot of “extra stuff” to make it happen. Lighting is essentially nice and easy default behavior for any “lighting” shader. …to get shadows you have to have additional things setup like a shadow renderer and shadow processor.
“”“”
float tNorm = timeOfDay / cycleLength;
float sunAng = tNorm * FastMath.TWO_PI;
Vector3f sunDir = new Vector3f(0f, FastMath.sin(sunAng), -
FastMath.cos(sunAng)).normalizeLocal();
sun.setDirection(sunDir.negate());
this is the code where the directional light is changing. Also As you can see in the first picture shadows are being done correctly with the direction of the sun. But i still cant figure out why the top face is still lighting up even if the sun is not directly above
Have you set/generated tangents?
How will this remove the lighting of the top face of the block?
Tangents are required by the lighting calculations to be correct. And over the past 10 years missing tangents solved probably around 90% of “lighting does not work as expected” posts
It worked. experiencing some lag but it solved the issue. Thanks for your suggestion.
You probably can generate the tangents as part of your mesh generation code to not have to call the generator. I have no access to my code to give you the values required for axis aligend blocks, so others might be able to supply those.
Im creating them when im generating the chunk and im getting spikes when the new chunk generates. Generated chunks are being cached
Yeah, the generator has to loop over all vertices. It has to support any type of mesh, there are better solutions for block type meshes. They are constants depending on the face in the special case of a box.
Im now offloading the tangents and computing them on to a background thread. This seems to work fine now
However you set the normals on your mesh you can just set the tangents, too. For a block world they are essentially constants like the normals, depending on the orientation of your textures.
It’s a 1:1 mapping from normal to tangent for a block world.
Edit: also note that the only reason you would need tangents is if you are using normal maps or bump maps.
Tbh now im getting an issue about some light leaks throught the blocks and the shadows arent perfect.
“Real” shadows are a hacky trick in 3D graphics. Even in triple-A games, the shadows aren’t perfect.
That being said, there are plenty of tweaks you can do to improve some situations. Hard to diagnose from the description given, though.
Thanks for now il try some others things and will try to fix as much as i can
Do you have a code or docs example on how to block direct sunlight with a block? As now i fixed only then top face being lit up. now everything is being lit upunderground.
Block world games are the hardest types of game to write because you have to do everything different than a normal game. So standard techniques will not work well.
The only good thing is that there will be a shedload of articles on “how minecraft did it” that you can look at.
The short answer is:
place light=15 in a block.
place light = light-1 in the surrounding blocks.
repeat until 0.
Write a custom shader to mix sunlight with local lighting.
To expand a bit on what pspeed is saying.
In a typical blockworld there won’t be any true lights in the scene at all (No DirectionalLight, no PointLight). The lighting data is all precalculated and baked into the mesh itself. When a chunk of blockworld updates then the light is calculated and baked into the mesh; this is done by setting every sun visible block as max light and any light source (e.g. torches) as max light and then letting the light “flow” out from those points in a sweep.
Advantages
- Gets good enough light in the very weird and complex blockworld geometry
- Can have an unlimited number of virtual light sources with no performance implication
- Light “trails off” the way real light in a cave does from the entrance, getting a bit murkier the further into the cave you go (you’d need ray tracing with multiple bounces to get that in the real light model)
Downsides
- No shadows (you can’t have that sharp tree shadow with this approach), only gradual murkiness
- Completely different workflow from normal games
- You have to write a custom shader to get day/night cycles to work (your shader mixes two precalculated values, one for day with one for night)
Another down side is that dynamic lighting (like a fire bolt shooting through a cave) is more difficult.