Custom Mesh baked in lighting

Ok. So day and night, or in general time of day would be best handled in shader?

I guess that’s what you were talking about in your first post. I would have to use a custom material if I wanted directional lighting.

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.

1 Like

Thanks. I think I’ll start simple and ignore night/day to get my feet wet.

I kinda got the same issue, and i’m not understanding how you guys manage to get the light baked :confused:
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 ?

As last hint to what i’m looking for : baking lightmap unity 3D

Thanks for any help.

I’m also considering using static lighting for a custom mesh.
Looking around I found people suggesting (1, 2) using Sunflow to bake the lightmap.

Is it feasible?

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 :smile:

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.

1 Like

@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);
    setTexture("ColorMap", texture);

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:

    private static Mesh generateMesh(){
    Mesh mesh = new Mesh();
    mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(positions));
    mesh.setBuffer(Type.Index, 1, BufferUtils.createShortBuffer(indices));
    mesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals));
    mesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(textureCoordinates));
    mesh.setBuffer(Type.Color, 4, BufferUtils.createFloatBuffer(color));
    return mesh;

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:

    private void addLighting(BlockChunkControl chunk, Vector3Int location, Block.Face face)
	LightMap lights = chunk.getLights();

	float light = lights.getNormalizedLight(location);




I don’t care about color so I just set the light value the same for RGB. Also I’m adding RGBA values for each vertex in the block shape.

Hope this helps.

@xuan I got no idea on how to use SunFlow…

@jchappelle Thx, time to read :blush:

Oh, my bad. I forgot minecraft style was to have the whole face with the same light.

My bad, I didn’t noticed you where talking about “minecraft lightmap style”…
Anyway, I’ve just finished reading all the stuff and i need to say that’s pretty interesting, and now i understand why i couldn’t figure out some stuff :smiley:

Well sorry for that. Good luck.

Plenty of bugs still but I actually have basic light working. In a business application I can pump out functionality like a machine but in games it took me a solid week and a half to figure out a buggy version of lighting…lol.

I am still not including day/night cycles. @pspeed why is the shader the way to go for day/night lights? My understanding is that the shader code written is compiled by the Open GL framework and sent down to the GPU to run on the chip itself without having to travel so far. So it is able to change the look of the world without me having to rebuild the mesh. Is that correct?

Here are some screenshots of where I am.

1 Like

Yes, setting one uniform is cheaper than rebuilding and resending a whole mesh every time the sun moves a little.

Ok. So is a shader like a mini call back function running on the GPU that gets called during the rendering pipeline?

The reason I am asking is because I have not read anything that explains it that way, but from what I have read, it seems to be true.

Just not sure.

It’s like a program that runs on the GPU. One transforms raw mesh data into something that can be turned into pixels. The other calculates the pixel values.

You should really really really do some googling on this topic as it’s too huge to cover properly here.

1 Like

I have done some reading but none that explained it in the way my mental model described it.

You seem to explain things to me in a very understandable way, so that’s why I asked.

Thank you!

By the way, you confirmed what I was thinking. No need to explain anything else.

Looking at a more simple shader like JME’s Unshaded would be educational most likely.

1 Like

Will do thank you.