I’m trying to implement lighting in my Minecraft clone game. Everything I have read about lighting in this type of game suggests “baked in lighting”. I’m not sure how to do that. I know there is a Color buffer that can be set on the mesh but I have not had any luck in setting that and I don’t know how that interacts with the material’s texture. I’m also unsure if I need to use Lighting.j3md or Unshaded.j3md for the material.
I’m not looking for a lighting algorithm, I’ll figure that out once I know how to pass lighting values to JME.
Here are my code snippets of what I have now that I think are relevant. Please let me know if I should include something else:
public class BlockChunk_Material extends Material{
public BlockChunk_Material(AssetManager assetManager, String blockTextureFilePath){
super(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
Texture texture = assetManager.loadTexture(blockTextureFilePath);
texture.setMagFilter(Texture.MagFilter.Nearest);
texture.setMinFilter(Texture.MinFilter.NearestNoMipMaps);
setTexture("ColorMap", texture);
getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
}
Probably you will need your own material if you still want directional (ie: sunlight) style lighting.
Else if you just want purely static lighting then use Unshaded, set a color buffer, and turn vertex colors on for the material. See docs for Unshaded’s material parameters.
He wants to propagate lighting like in Minecraft (as stated in the first post) and would like to bake that lighting in (as stated in the first post). This is the more performance option as it can handle an infinite number of lights where as Lighting.j3md will not give you such lighting propagation and will render the scene once per light, limiting the number of lights that one can support.
@pspeed if you don’t mind me asking, for Mythruna are you rebuilding the chunk meshes every time lighting changes? Or are you somehow just updating the color buffer?
Also, are there any resources you could suggest for lighting algorithms of this type?
I recalculate it every time since removing/adding a light is also changing the blocks anyway.
The algorithm isn’t hard… it’s like cellular automata or a flood fill. Plop a light value down, plop light value - 1 into all of its neighboring cells, repeat for unvisited cells.
But are you rebuilding the Mesh every time? In other words using new Mesh() or just updating the buffer? Seems like new Mesh() would be expensive considering how much light changes.
Yep. In that case, your colors would represent ‘local’ lighting (rgb if you want colored lights) and use the alpha value for “sun/moon” brightness. The shader would calculate directional lighting just like Lighting.j3md but then scale it based on the color alpha. You could treat the rgb portion as ambient but I found that to be a bit lame and did something more complicated personally… but that’s the easiest way.
I kinda got the same issue, and i’m not understanding how you guys manage to get the light baked
To be precise i’m looking for a static lightning, If you can help me i would be thanksfull.
From what i’ve understand I need :
A custom unshaded Material with the vertex color set to “On”.
A color buffer on the generated mesh.
Where i struggle if i got the way to go above right is :
How do i generate the color buffer ?
About the baking himself :
Does object on the scene not being part of the mesh will be baked to it ? (i mean the custom mesh is the floor, i got a house on it, does the house shadow will be baked to the floor ?)
If the point above does not work, how can i manage to get another object be baked to the floor ?
We are talking about minecraft style light baking. You both are talking about lightmaps… which is something entirely different and something you will generate in your 3D modeler and use as a second texture.
For anyone interested in lights being manually baked into the mesh here is a good resource I found for a light propagation algorithm.
I’ve been able to get some of it going. Still having issues with sunlight but at least now I understand somewhat how this stuff works. I think the value in learning how to write a voxel game like a minecraft clone is having to struggle through the low level details so that later when using tools like blender or maybe sunflow, you have an idea of what’s going on behind the scenes. At least that’s one of my goals in writing this game. Plus it’s just fun
I hope to open source the project in the form of a framework for kids to get involved in programming, including my own.
So far the community has been tremendously helpful, especially @pspeed and I’m really grateful.
@roroah if you are truly wanting to bake lights into a custom mesh then I will share how I am doing it.
My material:
public class BlockChunk_Material extends Material{
public BlockChunk_Material(AssetManager assetManager, String blockTextureFilePath){
super(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
Texture texture = assetManager.loadTexture(blockTextureFilePath);
texture.setMagFilter(Texture.MagFilter.Nearest);
texture.setMinFilter(Texture.MinFilter.NearestNoMipMaps);
setTexture("ColorMap", texture);
setBoolean("VertexColor",true);
getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
}
}
The key thing there is setBoolean(“VertexColor”, true). This tells the material to look at the color buffer of the mesh. This is how you set the color buffer:
The color buffer is made up of 4 float values(RGBA). I have a lighting algorithm that generates a value between 0 and 15 for each of my block locations then I set that value in my color array as follows: