[SOLVED] Creating my own custom buffers (for night and day prebaked light)

I’m looking to set multiple color buffers for prebaked light. I’m planning on setting 1 for the day level light and 1 as the night light level then setting a material level parameter of daylightIntensity and having the shader combine the two on the graphics card, for for example twilight. So I need to set this:

Mesh mapmesh=new Mesh();
mapmesh.setBuffer(VertexBuffer.Type.Color, 4, getVertexColourDay()); 
mapmesh.setBuffer(VertexBuffer.Type.Color2, 4, getVertexColourNight()); 

However there is no VertexBuffer.Type.Color2 so that’s obviously not going to work. There is Tangent which I could possibly misuse for this but it feels wrong and doesn’t really help if I need more buffers.

So my question is:
How do I define more buffers, in addition to the built in ones?

I don’t think you can define more buffers in jme, but you can use texCoord2 texCoord3, you pass 4 as lenght and you should be fine.

1 Like

Ah, I had assumed they had to be all 2 floats long. If I can pass 4 floats into them I doubt i’ll ever use up all the texCoords. Thanks!

1 Like

I’ve run through this and it all works very nicely. I’ll put the code for the java and shaders at the bottom of this post in case anyone else is trying to do this. I’ve also put in in a respository for the full working example Bitbucket. And I’ve done a more in the context of my game write up here https://richardonsoftware.wordpress.com/night-and-day-done-not-stupidly/

    package mygame;

    import com.jme3.app.SimpleApplication;
    import com.jme3.material.Material;
    import com.jme3.math.Vector2f;
    import com.jme3.math.Vector3f;
    import com.jme3.math.Vector4f;
    import com.jme3.scene.Geometry;
    import com.jme3.scene.Mesh;
    import com.jme3.scene.VertexBuffer;
    import com.jme3.shader.VarType;
    import com.jme3.util.BufferUtils;

    public class Main extends SimpleApplication {

        private Material mat;
        private float dayNightPeriod = 4;
        private float time;
        public static void main(String[] args) {
            Main app = new Main();

        public void simpleInitApp() {
            //the points in 3d space where the geometry will be
            Vector3f [] vertices = new Vector3f[8];
            //one square
            vertices[0] = new Vector3f(0,0,0);
            vertices[1] = new Vector3f(3,0,0);
            vertices[2] = new Vector3f(0,3,0);
            vertices[3] = new Vector3f(3,3,0);
            //a second square
            vertices[4] = new Vector3f(3,0,0);
            vertices[5] = new Vector3f(6,0,0);
            vertices[6] = new Vector3f(3,3,0);
            vertices[7] = new Vector3f(6,3,0);
            //combine those vetexes into triangles 
            int [] indexes = { 
                //first square
                //second square    
            //we're not using a texture but if we were this would define whtich parts of the image are where    
            Vector2f[] texCoord = new Vector2f[8];
            texCoord[0] = new Vector2f(0,0);
            texCoord[1] = new Vector2f(1,0);
            texCoord[2] = new Vector2f(0,1);
            texCoord[3] = new Vector2f(1,1);
            texCoord[4] = new Vector2f(0,0);
            texCoord[5] = new Vector2f(1,0);
            texCoord[6] = new Vector2f(0,1);
            texCoord[7] = new Vector2f(1,1);
            //Set the first colour buffer, this will be the sunlight color
            Vector4f[] sunlightColour = new Vector4f[8]; //these are Vector4f because we have a red, green, blue and transparency per vertex
            //both the squares at full brightness in sunlight
            sunlightColour[0] = new Vector4f(1,1,1,1);
            sunlightColour[1] = new Vector4f(1,1,1,1);
            sunlightColour[2] = new Vector4f(1,1,1,1);
            sunlightColour[3] = new Vector4f(1,1,1,1);
            sunlightColour[4] = new Vector4f(1,1,1,1);
            sunlightColour[5] = new Vector4f(1,1,1,1);
            sunlightColour[6] = new Vector4f(1,1,1,1);
            sunlightColour[7] = new Vector4f(1,1,1,1);
            Vector4f[] sourceLightColour = new Vector4f[8];
            sourceLightColour[0] = new Vector4f(0.9f,0.9f,0.9f,1); //first square at 90% brightness in sourcelight
            sourceLightColour[1] = new Vector4f(0.9f,0.9f,0.9f,1);
            sourceLightColour[2] = new Vector4f(0.9f,0.9f,0.9f,1);
            sourceLightColour[3] = new Vector4f(0.9f,0.9f,0.9f,1);
            sourceLightColour[4] = new Vector4f(0.3f,0.3f,0.3f,1);//second square at 30% brightness in sourcelight
            sourceLightColour[5] = new Vector4f(0.3f,0.3f,0.3f,1);
            sourceLightColour[6] = new Vector4f(0.3f,0.3f,0.3f,1);
            sourceLightColour[7] = new Vector4f(0.3f,0.3f,0.3f,1);
            //now we have all the data we create the mesh
            //for more details https://jmonkeyengine.github.io/wiki/jme3/advanced/custom_meshes.html
            Mesh mesh = new Mesh();
            mesh.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
            mesh.setBuffer(VertexBuffer.Type.Index,    3, BufferUtils.createIntBuffer(indexes));
            mesh.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
            mesh.setBuffer(VertexBuffer.Type.Color, 4, BufferUtils.createFloatBuffer(sunlightColour));
            //we're using the conventional Color buffer for sunlight and using one of the (usually) unused TexCoord5 to hold the source light
            mesh.setBuffer(VertexBuffer.Type.TexCoord5, 4, BufferUtils.createFloatBuffer(sourceLightColour));
            Geometry geom = new Geometry("mesh", mesh);

            mat = new Material(assetManager, "MatDefs/RepeatingUnshaded.j3md");
            //mat = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md"); 
            mat.setBoolean("VertexColor", true);


        public void simpleUpdate(float tpf) {
           time += tpf;
           if (time>dayNightPeriod){
              time = 0; 
           mat.setParam("SunlightIntensity", VarType.Float,time/dayNightPeriod); 

Vertex shader

    #import "Common/ShaderLib/GLSLCompat.glsllib"
    #import "Common/ShaderLib/Skinning.glsllib"
    #import "Common/ShaderLib/Instancing.glsllib"

    attribute vec3 inPosition;

#if defined(HAS_COLORMAP) || (defined(HAS_LIGHTMAP) && !defined(SEPARATE_TEXCOORD))
    #define NEED_TEXCOORD1

attribute vec2 inTexCoord;
attribute vec2 inTexCoord2;
attribute vec4 inColor;

varying vec2 texCoord1;
varying vec2 texCoord2;
attribute vec4 inTexCoord5; //!changed! this is the source light that is passed to us
varying vec4 texCoord5; //!changed! and this is how we'll pass it through to the fragment shader

varying vec4 vertColor;

void main(){
    #ifdef NEED_TEXCOORD1
        texCoord1 = inTexCoord;

        texCoord2 = inTexCoord2;

        vertColor = inColor;

    vec4 modelSpacePos = vec4(inPosition, 1.0);
    #ifdef NUM_BONES

    texCoord5 = inTexCoord5; //!changed! its the fragment shader that wants this so we just pass it on

    gl_Position = TransformWorldViewProjection(modelSpacePos);

Fragment shader
    #import "Common/ShaderLib/GLSLCompat.glsllib"

    #if defined(HAS_GLOWMAP) || defined(HAS_COLORMAP) || (defined(HAS_LIGHTMAP) && !defined(SEPARATE_TEXCOORD))
        #define NEED_TEXCOORD1

    #if defined(DISCARD_ALPHA)
        uniform float m_AlphaDiscardThreshold;

    uniform float m_SunlightIntensity; //!changed!
    uniform vec4 m_Color;
    uniform sampler2D m_ColorMap;
    uniform sampler2D m_LightMap;

    varying vec2 texCoord1;
    varying vec2 texCoord2;

    varying vec4 vertColor;

    varying vec4 texCoord5; //!changed!this is the source color passed through to us

    void main(){
        vec4 color = vec4(1.0);

        #ifdef HAS_COLORMAP
            color *= texture2D(m_ColorMap, texCoord1);     

        #ifdef HAS_VERTEXCOLOR
            //!changed! This is the actual combining of the two colours
            color.x *= max(vertColor.x * m_SunlightIntensity,texCoord5.x);
            color.y *= max(vertColor.y * m_SunlightIntensity,texCoord5.y);
            color.z *= max(vertColor.z * m_SunlightIntensity,texCoord5.z);
            color.a *= vertColor.a;

        #ifdef HAS_COLOR
            color *= m_Color;

        #ifdef HAS_LIGHTMAP
            #ifdef SEPARATE_TEXCOORD
                color.rgb *= texture2D(m_LightMap, texCoord2).rgb;
                color.rgb *= texture2D(m_LightMap, texCoord1).rgb;

        #if defined(DISCARD_ALPHA)
            if(color.a < m_AlphaDiscardThreshold){

        gl_FragColor = color;

Really liked your example richtea. Managed to adapt it to my own hacked version of Lighting.j3md, so now I can combine directional lights with both day and night baked lighting. Great stuff.

1 Like

I find your work inspiring.

And here’s the result: I live in the northern hemisphere.
Let us normalize 24 h as 2 Pi.
My day = my night = Pi only 2 times at year (beginning of spring and autumn) In winter my day is shorter, in summer longer than Pi.

Surfaces sloping to the east notice more dawn.
Surfaces sloping to the west get more of the sunset.
Surfaces sloping more south will get more of both.

So if you were not only using TexCoord5, but TexCoord4 for east-west brightness intensity correction and TexCoord5 for zenith-south brightness intensity correction, and in
SunlightIntensity4 = Math.max (0, sin (time) - season correction)
(Because the sinus curve is always flatter at the top, the redness also becomes longer / slower) and
SunlightIntensity5 = (1- cos (time)) / 2
(or vice versa) and in the shader would pass both as running variables, the time of day could become even more realistic.


Hi! I read this thread and I thought it could be done just by using predefined jme3 lighting instead of messing with shaders and mesh buffers. I got my hands dirty for a while and I’ve just implemented a simple similar effect with custom light colors depending on the time frame (maybe chosen the colors are not the better option).

In case you’re interested the code is at github: GitHub - joliver82/jME3-simple-sunlight and a demo video at youtube: https://youtu.be/57tMoVnATXo

I think that uses point light rather than pre baked light. A fair thing for some uses but prebaked allows for light to go round corners (or whatever the pre baking algorithm wants to do)

Ooo! I love that idea. I might have to look into implementing that

Hi @richtea! The point light is to replicate in a quick way the lighting of the same scene video you posted. About the sunlight, I thought it was cleaner and quicker to implement it with just lighting instead. As any other techniques, it depends on the result you want to achieve which one to use.

Offopic: I just saw you published a game on steam (One Million Worlds) which is great, congratulations! I’m planning on releasing my game for both mobiles and desktop, so steam is an option for me. Is it hard to get a game accepted in the platform? Are you using steamworks4j or any other steam wrapper for java I may not be aware of? Thanks

1 Like


It wasn’t too hard. They’ve changed the process so there’s a $100 application fee replacing the old greenlight program (but acceptance for a real game is much much more likely).

They go through the store page very thoroughly but less so the application itself (I think they just care that its not malware).

My game isn’t integrated with steam, just uses steam for distribution and launch (so no achievements or other steam stuff) so I’m not sure about support for that.

I use steam pipe to deploy to steam, which is pretty well documented. The only annoying thing is you can’t use an ephemeral build server to deploy to it because it has to be two factor authenticated, but I was able to get a persistent Jenkins to use it.


First of all, I would like to thank everyone who patiently answers my questions. Most of the time it’s Paul. The last time he wrote me:

I have so much fun with jME3, that i actually forgot why I was watching with jME3: to tinker a game.

So I couldn’t stop adding a prebaked light to the TestTextureArray. Here is today’s result:

Simplifications: sun at its zenith, no seasons.

In texCoord1.z there is at best an int, after the comma the float is unused. Here I could store the alignment within [0.1f, 0.9f] from east to west (not [0…0.999], because Paul already warned me in a different context about overflows with twisted triangles.)

		Mesh m = new Mesh();
	Vector3f[] vertices = new Vector3f[8];
	vertices[0] = new Vector3f(0, 0, -3);
	vertices[1] = new Vector3f(3, 0, 0);
	vertices[2] = new Vector3f(0, 3, 0);
	vertices[3] = new Vector3f(3, 3, 0);

	vertices[4] = new Vector3f(3, 0, 0);
	vertices[5] = new Vector3f(6, 0, 0);
	vertices[6] = new Vector3f(3, 3, 0);
	vertices[7] = new Vector3f(6, 3, -3);

	Vector3f[] texCoord = new Vector3f[8];
	texCoord[0] = new Vector3f(0, 0, 0.1f);
	texCoord[1] = new Vector3f(1, 0, 0.2f);
	texCoord[2] = new Vector3f(0, 1, 0.2f);
	texCoord[3] = new Vector3f(1, 1, 0.5f);

	texCoord[4] = new Vector3f(0, 0, 1.5f);
	texCoord[5] = new Vector3f(1, 0, 1.8f);
	texCoord[6] = new Vector3f(0, 1, 1.8f);
	texCoord[7] = new Vector3f(1, 1, 1.9f);

simpleUpdate code:

	private float myTime = 0.0f;

public void simpleUpdate(final float tpf) {
	myTime += tpf * 0.3f;
	if (myTime > FastMath.TWO_PI) {
		myTime -= FastMath.TWO_PI;
	if (mat != null) {
		float h = Math.max(0.0f, FastMath.sin(myTime));
		float ampm = FastMath.cos(myTime);
		float dawnsunset = Math.max(0.0f, (h - 0.2f) * (0.2f - h) * 25 + 1);
		float dawn = ampm > 0 ? dawnsunset : 0.0f;
		float sunset = ampm < 0 ? dawnsunset : 0.0f;
		float night = Math.max(0.0f, 0.2f - h);
		Vector4f colorDawn = new Vector4f(1.1f * dawn, 0.2f * dawn, 0.0f * dawn, dawn);
		mat.setVector4("ColorDawn", colorDawn);
		Vector4f colorNoonNight = new Vector4f(1.0f * h + 0.16f * dawn + 0.18f * sunset, 1.0f * h,
				1.0f * h + night * 1.3f, 1);
		mat.setVector4("ColorNoon", colorNoonNight);
		Vector4f colorSunset = new Vector4f(1.3f * sunset, 0.4f * sunset, 0.04f * sunset, sunset);
		mat.setVector4("ColorSunset", colorSunset);

		// System.out.println(colorDawn + " / " + colorNoon + " / " + colorSunset);

MaterialDef JaReUnshadedArray {

MaterialParameters {
    TextureArray ColorMap
    Texture2D LightMap
    Color Color (Color)
    Boolean VertexColor
    Boolean SeparateTexCoord
    Color ColorDawn    // !JARE!
    Color ColorNoon    // !JARE!
    Color ColorSunset  // !JARE!

FragmentShader GLSL300 GLSL100: assets/matdefs/JaReUnshadedArray.frag


void main(){

// !JARE! Begin
float arrIndex = float(int(texCoord1.z)); 
vec3 texCoord1a= vec3(texCoord1.x, texCoord1.y,  arrIndex);
float h = texCoord1.z - arrIndex;
float dawn = max(0.0,(h-0.1)*(0.1-h)*100.0 +1.0);
float sunset = max(0.0,(h-0.9)*(0.9-h)*100.0 +1.0);
vec4 colorSun = m_ColorDawn * dawn + m_ColorNoon + m_ColorSunset * sunset;
vec4 color = colorSun / (colorSun.a+0.001);
// !JARE! End

    color *= texture2DArray(m_ColorMap, texCoord1a);   // !JARE!

Ah yes: I use color.a to normalize the color according to the summation.

Okay, the numbers are still hipshots, but it works pretty nicely.

I am grateful for comments and corrections. This is only my second attempt to work on the shader.


I’m the “poster child” for this issue. I’m on year ten of my game project after all.

…of course, in the mean time I’ve produced like a dozen open source libraries. Still, if getting “productively side-tracked” were a sport, I might be able to make the Olympic team by now.

So it’s easy for me to spot in others. And it’s certainly a find mode of existence unless it’s by accident. :slight_smile:


My own landscape, day-night cycle:

@pspeed: I have dutifully switched all textures (except water) to texture array.

1 Like