[SOLVED] Custom coloured mesh invisible with DirectionalLight

Hi all,

I’m currently trying to add some lighting to my scene so have put in a DirectionalLight and have converted my Material that gets applied to my Mesh Geometry (which represents a single tile of my terrain) such that it should reflect the light, by following tutorials such as http://wiki.jmonkeyengine.org/doku.php/jme3:advanced:custom_meshes and http://wiki.jmonkeyengine.org/doku.php/jme3:advanced:light_and_shadow. However unfortunately I’m still in the dark (pun most definitely intended) about what I might be missing. Here’s a small runnable example which demonstrates the issue:

public class TestApplication extends SimpleApplication {

public static void main(String[] args) {
    new TestApplication().start();
}

@Override
public void simpleInitApp() {
    flyCam.setDragToRotate(true);
    
    Vector3f[] vertices = new Vector3f[4];
    vertices[0] = new Vector3f(0, -1, 0);
    vertices[1] = new Vector3f(0, -1, 1);
    vertices[2] = new Vector3f(1, -1, 0);
    vertices[3] = new Vector3f(1, -1, 1);

    Vector2f[] texCoord = new Vector2f[4];
    texCoord[0] = new Vector2f(0, 0);
    texCoord[1] = new Vector2f(1, 0);
    texCoord[2] = new Vector2f(0, 1);
    texCoord[3] = new Vector2f(1, 1);

    Vector3f[] normals = new Vector3f[4];
    normals[0] = new Vector3f(0, 1, 0);
    normals[1] = new Vector3f(0, 1, 0);
    normals[2] = new Vector3f(0, 1, 0);
    normals[3] = new Vector3f(0, 1, 0);

    int[] indexes = {2, 0, 1, 1, 3, 2};

    Mesh m = new Mesh();
    m.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
    m.setBuffer(VertexBuffer.Type.Normal, 3, BufferUtils.createFloatBuffer(normals));
    m.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
    m.setBuffer(VertexBuffer.Type.Index, 1, BufferUtils.createIntBuffer(indexes));

    Geometry g = new Geometry("terrainSquare", m);
    
//        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
//        mat.setColor("Color", new ColorRGBA(
//                40 / 255f,
//                80 / 255f,
//                40 / 255f,
//                1));
    Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
    mat.setBoolean("UseMaterialColors", true);
    mat.setBoolean("UseVertexColor", true);
    mat.setBoolean("VertexLighting", true);
    mat.setColor("Ambient", new ColorRGBA(
            40 / 255f,
            80 / 255f,
            40 / 255f,
            1));
    mat.setColor("Diffuse", new ColorRGBA(
            40 / 255f,
            80 / 255f,
            40 / 255f,
            1));
    mat.setColor("Specular", new ColorRGBA(
            40 / 255f,
            80 / 255f,
            40 / 255f,
            1));
    mat.setFloat("Shininess", 50f);
            mat.getAdditionalRenderState().setWireframe(true);
    g.setMaterial(mat);
    g.updateModelBound();
    rootNode.attachChild(g);

    DirectionalLight sun = new DirectionalLight();
    sun.setColor(ColorRGBA.White);
    sun.setDirection(new Vector3f(5f, 5f, 0f));
    rootNode.addLight(sun);
}

}

I’m thinking it’s something to do with the normal values but everything looks consistent with the articles I’ve read - any ideas what I’m missing?

Thanks hugely in advance,

Ian

First, this is not a valid direction vector as it’s not normalized.

Second, it points up instead of down so is likely shining on the bottom of your plane.

Thank you very much pspeed, that’s done the trick! For some reason I was thinking the setDirection method was defining the source of the light, but after re-reading the documentation, I see that a DirectionalLight doesn’t have a position so that assumption didn’t make a whole lot of sense :stuck_out_tongue: Thanks again!

1 Like