TerrainLighting does not appear to render with lighting

Hi all,

I am trying to use TerrainLighting.j3md to add lighting to my originally-Terrain.j3md terrain.

So, after converting it to TerrainLighting, I got… Exactly the same result:

The plane has got diffuse lighting, as you can see on it’s left side. However the terrain does not seem to have any lighting at all, and looks exactly the same as Terrain.j3md did.

  • Terrain is definitely TerrainLighting.j3md, as I can enable options like WardIso and TriPlanarMapping.
  • Terrain has three diffuse textures, with one alpha map.
  • This occur occurs even if there is no AmbientLight at all.
  • If I change the diffuse lighting direction to be more horizontal, the ENTIRE terrain will look uniformally darker, as if all the normals were pointing in the +Y direction.

As for my code.

Lighting:

  DirectionalLight dl;
private void setUpLight() {

//Even when removing an ambient light, issue still occurs.
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(0.1f));
rootNode.addLight(al);


//If I change the Y to something like -0.1, the entire terrain is UNIFORMALLY dark.
dl = new DirectionalLight();
dl.setColor(ColorRGBA.White);
dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
rootNode.addLight(dl);
}

Terrain code:

mat_terrain = new Material(assetManager, 
        "Common/MatDefs/Terrain/TerrainLighting.j3md");
mat_terrain.setColor("Diffuse", ColorRGBA.White);
mat_terrain.setColor("Ambient", ColorRGBA.Black);
mat_terrain.setBoolean("WardIso", true);

//mat_terrain.setFloat("Shininess", 5f);
/** 1.1) Add ALPHA map (for red-blue-green coded splat textures) */
mat_terrain.setTexture("AlphaMap", assetManager.loadTexture(
        "Textures/Terrain/splat/Alpha.jpg"));
int texMult = 3;
/** 1.2) Add GRASS texture into the red layer (Tex1). */
Texture grass = assetManager.loadTexture(
        "Textures/Terrain/splat/Grass.png");
grass.setWrap(WrapMode.Repeat);
grass.setAnisotropicFilter(16);
mat_terrain.setTexture("DiffuseMap", grass);
mat_terrain.setFloat("DiffuseMap_0_scale", 32f * texMult);

/** 1.3) Add DIRT texture into the green layer (Tex2) */
Texture dirt = assetManager.loadTexture(
        "Textures/Terrain/splat/Sand.png");
dirt.setWrap(WrapMode.Repeat);
dirt.setAnisotropicFilter(16);
mat_terrain.setTexture("DiffuseMap_1", dirt);
mat_terrain.setFloat("DiffuseMap_1_scale", 32f * texMult);

/** 1.4) Add ROAD texture into the blue layer (Tex3) */
Texture rock = assetManager.loadTexture(
        "Textures/Terrain/splat/Dirt2.png");
rock.setAnisotropicFilter(16);
rock.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("DiffuseMap_2", rock);
mat_terrain.setFloat("DiffuseMap_2_scale", 24f * texMult);

/** 2. Create the height map */
AbstractHeightMap heightmap = null;
Texture heightMapImage = assetManager.loadTexture(
        "Textures/Terrain/splat/Map.bmp");
heightmap = new ImageBasedHeightMap(heightMapImage.getImage());
heightmap.setHeightScale(2.5f);
heightmap.load();

/** 3. We have prepared material and heightmap. 
 * Now we create the actual terrain:
 * 3.1) Create a TerrainQuad and name it "my terrain".
 * 3.2) A good value for terrain tiles is 64x64 -- so we supply 64+1=65.
 * 3.3) We prepared a heightmap of size 512x512 -- so we supply 512+1=513.
 * 3.4) As LOD step scale we supply Vector3f(1,1,1).
 * 3.5) We supply the prepared heightmap itself.
 */
int patchSize = 65;
terrain = new TerrainQuad("Map", patchSize, 513, heightmap.getHeightMap());

/** 4. We give the terrain its material, position & scale it, and attach it. */
terrain.setMaterial(mat_terrain);
terrain.setLocalTranslation(0, -300, 0);
terrain.setLocalScale(6 * 4f, 2f, 6 * 4f);
rootNode.attachChild(terrain);

/** 5. The LOD (level of detail) depends on were the camera is: */
TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
terrain.addControl(control);

CollisionShape sceneShape =
        CollisionShapeFactory.createMeshShape(terrain);
landscape = new RigidBodyControl(sceneShape, 0f);
terrain.addControl(landscape);
landscape.setFriction(0.5f);
landscape.setRestitution(0.4f);

Has anyone got any clue what is wrong with this?

Okay, I have solved the problem.

Basically the problem was this:

heightmap.setHeightScale(2.5f);
terrain.setLocalScale(6 * 4f, 2f, 6 * 4f);

Scaling it like that, for some reason, made the normals very upward-facing and uniform.

To solve this, I instead used the following values:

heightmap.setHeightScale(0.2f);
terrain.setLocalScale(6 * 4f, 6 * 4f, 6 * 4f);

Which gives about the same terrain.

I would like to mark this as solved, however if anyone could please give me an explanation as to why this occurred, I would really appreciate it. Will then mark as solved.

2 Likes