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

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;

@Override
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);
	}
}

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

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

#ifdef HAS_COLORMAP
    color *= texture2DArray(m_ColorMap, texCoord1a);   // !JARE!
#endif


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.

4 Likes