That’s because we forced the color to red to make sure the shader was working.
For the next step:
How big is your map in world space? Where is it positioned?
Update: the basic approach is as follows… I’m going to assume you have the values I asked for above. For the sake of clarity, X_MIN, Y_MIN will be the values for the lowest corner of your map in world space. X_SIZE, Y_SIZE will be the size of the map in world space.
Overview:
- setup a FOW texture in Java and get it setup to write to
- add a material parameter for the FOW texture
- modify the .vert shader to modify lighting based on the world space of the vertex
Edit FowLighting.j3md and add:
Texture2D FowMap -LINEAR
to the MaterialParameters section.
Scroll down to " Technique {" and then to " Defines { " and add:
HAS_FOW
That adds a new FowMap material parameter we can set in Java code and it adds a #define to the shader code so that we can easily turn on/off code.
Edit FowLighting.vert…
Somewhere near the top add:
#ifdef HAS_FOW
uniform sampler2D m_FowMap;
#endif
…that allows the .vert shader to use the new FowMap texture that was added to the material.
Remove this line we added before:
DiffuseSum = vec4(1.0, 0.0, 0.0, 1.0);
Replace it with:
#ifdef HAS_FOW
// Get the x,z location of the vertex in world space
vec2 worldXz = (TransformWorld(modelSpacePos)).xz
// Change the world space coordinate to texture space 0..1
vec2 fowUv = worldXz - vec2(MIN_X, MIN_Z); // use your actual values here in format like 0.0
fowUv *= vec2(X_SIZE, Z_SIZE);
// Sample the texture
vec4 fow = texture2D(m_FowMap, worldXz);
// adjust lighting by the fog of war value
DiffuseSum *= fow;
AmbientSum *= fow;
SpecularSum *= fow;
#endif
…that turns the model position of the vertex into world space and then converts that into values between 0 and 1 for sampling the FOW texture we will create next.
Setup the texture in Java code:
Add this somewhere central:
// Create the raw FOW overlay image
int size = 1024;
ByteBuffer data = BufferUtils.createByteBuffer(size * size * 4); // square image, four bytes per color
Image image = new Image(Image.Format.ABGR8, size, size, data, ColorSpace.Linear);
// Create a raster that we can draw to
ImageRaster raster = ImageRaster.create(image);
// Create the texture to set to the FowMap on materials
Texture2D fowMap = new Texture2D(raster);
fowMap is what you will set on your materials.
material.setTexture(“FowMap”, fowMap);
raster is where you will draw “unfogged” areas.
As an example, I’ll show a gradient from 0…128
for( int i = 0; i < 128; i++ ) {
for( int j = 0; j < 128; j++ ) {
float f = (1f - i/128) * (1f - j/128);
ColorRGBA color = new ColorRGBA(f, f, f, 1.0);
raster.setPixel(i, j, color);
}
}
That should partially reveal a corner of the map with the “brightest” being right at the corner.
How best to paint into the raster is something we can cover once all of the above is working.
GIANT CAVEAT: I have not tested any of the above code and just typed it right into the forum by hand. It may have syntax errors, etc.