[SOLVED] TerrainLighting.j3md NormalMaps

When I run this code for a terrain with diffusemap and normalmap I get this error:

Uncaught exception thrown in Thread[jME3 Main,5,main]
RendererException: compile error in: ShaderSource[name=Common/MatDefs/Terrain/TerrainLighting.frag, defines, type=Fragment, language=GLSL100]
0(720) : error C1008: undefined variable “calculateNormal”

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.renderer.RenderManager;
import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.terrain.heightmap.AbstractHeightMap;
import com.jme3.terrain.heightmap.ImageBasedHeightMap;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
//import com.jme3.util.TangentBinormalGenerator;

/**
 * This is the Main Class of your Game. You should only do initialization here.
 * Move your Logic into AppStates or Controls
 * @author normenhansen
 */
public class Main extends SimpleApplication {

     Material matTerrain;
     private float groundScale = 16;
     private TerrainQuad terrain;
    
    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        
         // TERRAIN TEXTURE material
        matTerrain = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
        matTerrain.setBoolean("useTriPlanarMapping", false);
        matTerrain.setFloat("Shininess", 0.0f);
        
        // HEIGHTMAP image (for the terrain heightmap)
        Texture heightMapImage = assetManager.loadTexture("Textures/heightmap.png");
        
        Texture ground = assetManager.loadTexture("Textures/Ground/Ground_04.png");
        ground.setWrap(WrapMode.Repeat);
        matTerrain.setTexture("DiffuseMap", ground);
        matTerrain.setFloat("DiffuseMap_0_scale", groundScale);
        
        Texture normalMap0 = assetManager.loadTexture("Textures/Ground/Ground_04_Nrm.png");
        normalMap0.setWrap(WrapMode.Repeat);
        matTerrain.setTexture("NormalMap", normalMap0);
        
        // CREATE HEIGHTMAP
        AbstractHeightMap heightmap = null;
        try {
            heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.5f);
            heightmap.load();
            heightmap.smooth(0.9f, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());
        terrain.setMaterial(matTerrain);
        terrain.setLocalTranslation(0, -100, 0);
        rootNode.attachChild(terrain);
    }

    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }
}

Can someone help me? I just want to try out a terrain with a normalmap.

It looks like your terrain is missing an alpha map.

This tutorial has the full example code for making a working terrain with some of the JME test assets.

But the important code from that tutorial that you are missing is:

/** 1.1) Add ALPHA map (for red-blue-green coded splat textures) */
mat_terrain.setTexture("Alpha", assetManager.loadTexture(
        "Textures/Terrain/splat/alphamap.png"));

If your’e not sure how the alphamap splatting system works, you may also want to look into that so you can paint more textures onto your terrain. But basically, each color channel of the alphamp (red/blue/green/alpha) corresponds to a texture, and the shader blends your textures depending on where that color is painted in the alpha map.

1 Like

I added the alpha map like you said and now the code works.
Thanks for the info.

2 Likes

Please note you also have wiki that can help you much: