Voxel shader question

I’m trying to implement day/night lighting in my voxel game. To do this I need to implement a shader that scales the light based on the time of day. I have made a copy of the Unshaded.j3md and Lighting.j3md and their corresponding .vert and .frag files to start with.

I’ve never written a shader but I get the general idea. I think I’m going to need to use a modified version of the Lighting.j3md, and I can probably cut out some complexity in there.

Each of my vertices have color values set for local lighting and an alpha value that I’m not currently using(just setting to 1.0) so I could put the data that I need in the alpha value to be interpreted by the shader as a scale value.

My question is, how do I update my time value so the shader can scale the color accordingly? It seems like every loop I will have to set a new value on the material and re-render the mesh which would cut out any performance benefits of using a shader in the first place.

To get an idea of where I am, I have included a day and night screenshot of my current game.

Thank you for any help.



Maybe this is a silly question, but have you take a look on SkyControl? I think it can help you w/o creating a custom shader.

Set a material parameter.

Your mesh is already rendered every frame… so not sure why you are worried about that unless you mistyped.

If I set a material parameter in the game loop does that immediately become available to the shader?

@afonsolage I’m using the SkyControl. I’m using custom baked lighting so the lights managed by the SkyControl won’t help me here. Thanks for the suggestion though.

@pspeed, I guess I was under the impression that setting a Material parameter in the game loop would require me building a new mesh to be sent to the GPU for the shader to have access.

I didn’t think JME was magical enough to send the material parameters as they were set. But if so then that’s awesome! Problem solved(that is, now I have to actually write the shader…)

When else would they be sent? Material parameters are ‘always there’ else nothing would work because that’s how the shader knows about the projection matrix, etc…

In fact, if you set a Vector3f or a ColorRGBA then you can set the fields of it without even having to reset the material parameter. The shader will see the updated values.

1 Like

Like I said, I thought those were final variables set when the Mesh gets built.

Now I can understand how some of these advanced shaders get built. That clears up a TON in my mental model of how this works.

@pspeed you da man! Thank you


Got it working! And it’s glorious!

Congrats on entering the scary/strange/awesome/daunting new world of shader programming. :slight_smile:

1 Like

Would you mind giving me an idea of what to research for implementing smooth lighting? The “glsl smooth lighting” comes up with a large variety of things and I’m not sure which one applies to what I’m trying to do. As you can see from my screenshots, my lighting is blocky. I found this link but I’m not sure ambient occlusion is exactly what I’m looking for, although it would be nice as well. http://0fps.net/2013/07/03/ambient-occlusion-for-minecraft-like-worlds/

Just looking for a couple of pointers(not null pointers :slight_smile: ).

Thanks for your time.

1 Like

Those articles on 0fps are awesome. What makes me crazy is he saying “This is easy to understand, just read those formulas:”…I think my math base isn’t that good… :confounded:

Well it certainly isn’t “easy”. If it were easy everyone would do it. :smile:

I think the ambient occlusion concept is fairly straight forward. You are really just dimming the light of a vertex based on how many neighboring voxels are around that vertex. I’m just not sure that’s going to give me the smooth lighting effect. I think there must be gradients or averaging of vertex lights going on.