[SOLVED] Mesh + Material with Lighting.j3md throws error

Hello everybody, i am a total beginner in JME and have a Problem with Materials based on …Lighting.j3md.

The following Codes works fine and results in the following Output

… vertices = new Vector3f[4];
System.out.println("i = " + i);
vertices[0] = new Vector3f(100,100,100);
vertices[0] = new Vector3f((float)punkte[i].getX(),(float)punkte[i].getY(),(float)punkte[i].getZ());
vertices[1] = new Vector3f((float)punkte[i+1].getX(),(float)punkte[i+1].getY(),(float)punkte[i+1].getZ());
vertices[2] = new Vector3f((float)punkte[i].getX(),(float)punkte[i].getY(),(float)(punkte[i].getZ()+profil.getLaenge()));
vertices[3] = new Vector3f((float)punkte[i+1].getX(),(float)punkte[i+1].getY(),(float)(punkte[i+1].getZ()+profil.getLaenge()));

            m = new Mesh(); 

            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);

            // Setting buffers
            m.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
            m.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
            m.setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createShortBuffer(indexes));
            m.updateBound();

// I think the interesting part starts here
// Creating a geometry, and apply a single color material to it
Geometry geom = new Geometry(“MeshProfil”, m);
Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

            mat.setColor("Color", Profil.getFarbe());                          
            geom.setMaterial(mat);

            nodeProfil.attachChild(geom);

See Pic 1

But when i want to use Lighting the following Code results in an Error

            Geometry geom = new Geometry("MeshProfil", m);
            Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");       
            mat.setBoolean("UseMaterialColors",true);
            mat.setColor("Ambient", ColorRGBA.White);  
            mat.setColor("Diffuse", ColorRGBA.White);  
            mat.setColor("Color", Profil.getFarbe());                           

            geom.setMaterial(mat);

            nodeProfil.attachChild(geom);

See Pic2

When i do it without the line “mat.setColor(“Color”, Profil.getFarbe()); " the output looks like this (just Focus on the “invisible” parts”)

See Pic3

What am i doing wrong? i have set ambient light an 4 Point Lights around the objects.

Just use Diffuse Instead of Color. It is a bit bad but every shader has it’s own name for the base color: color in unshaded, diffuse in lighting and Albedo for PBR

Edit: and use the three backticks as code Formatation

As Darkchaos said, the texture names are sadly not standardized between different material definitions. You can check out what parameters lighting.j3md has here (under defines section): https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md

Which btw is because shaded already has multiple colors so there cant be that one color

Hello, thank you very much for your fast reply, but i don’t understand where exactly I have to use Diffuse instead of Color. Can yout tell me which line you ment?

You have a line mat.setColor(“Color”, Profil.getFarbe()); You’d have to change “Color” to “Diffuse”. And probably delete the line where you already set diffuse to white.

This is not working. It results in invisible (or black) Output (Pic3). Even when I use mat.setColor(“Diffuse”, ColorRGBA.White);
Are there other settings that have to be made?

Can you get us your light setup code? Since pic3 looks like a lack of light problem. In jme lights only affect the node they’re attached to and their children.

    DirectionalLight sun = new DirectionalLight();
    sun.setDirection(new Vector3f(1,0,-2).normalizeLocal());
    sun.setColor(ColorRGBA.White);
    rootNode.addLight(sun);
    
    // Es werden vier "Lampen" um das Zentrum des Gerät herum gesetzt
    float x =nodeGeraet.getWorldBound().getCenter().x;
    float y =nodeGeraet.getWorldBound().getCenter().y;
    float z =nodeGeraet.getWorldBound().getCenter().z;
    
    PointLight lampeLV = new PointLight();          // Lampe Links/Vorne
    lampeLV.setColor(ColorRGBA.Yellow);
    lampeLV.setRadius(4f);   
    lampeLV.setPosition(new Vector3f(x-10000f,y+5000,z+10000));
    lampeLV.setRadius(50000f);
    rootNode.addLight(lampeLV);
    
    PointLight lampeRV = new PointLight();          // Lampe Rechts/Vorne
    lampeRV.setColor(ColorRGBA.Blue);
    lampeRV.setRadius(4f);   
    lampeRV.setPosition(new Vector3f(x+10000f,y+5000,z+10000));
    lampeRV.setRadius(50000f);
    rootNode.addLight(lampeRV);
    
    PointLight lampeLH = new PointLight();          // Lampe Links/Hinten
    lampeLH.setColor(ColorRGBA.Red);
    lampeLH.setRadius(4f);   
    lampeLH.setPosition(new Vector3f(x-10000f,y+5000,z-10000));
    lampeLH.setRadius(50000f);
    rootNode.addLight(lampeLH);
    
    PointLight lampeRH = new PointLight();          // Lampe Rechts/Vorne
    lampeRH.setColor(ColorRGBA.Green);
    lampeRH.setRadius(4f);   
    lampeRH.setPosition(new Vector3f(x+10000f,y+5000,z-10000));
    lampeRH.setRadius(50000f);
    rootNode.addLight(lampeRH);

// The lamps are at the four corners around the center of the objects

// Just recognized that the Line "lampeLV.setRadius(4f); " is crap. But it doesn’t Change anything

Is your model really a couple thousand units big? Even though, the directional light should be visible. Maybe you have flipped normals?
From the custom mesh manual:

Remember that you must specify the vertices counter-clockwise.

If that was ignored it could result in flipped normals.

As you can see in pic 1 the normals are correct. And the model ist this big because 1 wu ist one Millimeter. I hope this is not a Problem. Zhe Software i want to write ist not a game but meant for the visualisation of technical Equipment.

Unshaded.j3md ignores normals, so pic1 doesn’t really prove the normals are correct.

I have checked that the normals are ok. And “Unshaded.j3md” Does NOT ignore the normals on my System.

Normals are used in lighting calculations so it wouldn’t make sense that they affect an unshaded material. From memory the unshaded material doesn’t even query normals.

To debug normals you can follow this post

Verify the lines are pointing in the directions they should be.

Could you prepare some TestCase?

we dont see all Code, even one reason could be you created “overlay” mesh there with different material(and you dont noticed it). So i think it could be best it we can See TestCase.

You can also add an ambient light which is unaffected by normals. But the ambient color must not be black then (on the model)

Here is a Test where the same Material ist used for different objects. One Looks ok (blue boxes), the other one stays black

            vertices = new Vector3f[4];
            vertices[0] = new Vector3f((float)punkte[i].getX(),(float)punkte[i].getY(),(float)punkte[i].getZ());
            vertices[1] = new Vector3f((float)punkte[i+1].getX(),(float)punkte[i+1].getY(),(float)punkte[i+1].getZ());
            vertices[2] = new Vector3f((float)punkte[i].getX(),(float)punkte[i].getY(),(float)(punkte[i].getZ()+profil.getLaenge()));
            vertices[3] = new Vector3f((float)punkte[i+1].getX(),(float)punkte[i+1].getY(),(float)(punkte[i+1].getZ()+profil.getLaenge()));
                            
            m = new Mesh(); 

            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);

            m.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
            m.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
            m.setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createShortBuffer(indexes));
            m.updateBound();
            
            Geometry geom = new Geometry("MeshProfil", m);
            
            Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"); 
            mat.setBoolean("UseMaterialColors",true);
            mat.setColor("Ambient", ColorRGBA.Blue); 
            mat.setColor("Diffuse", ColorRGBA.Blue); 
            
            geom.setMaterial(mat);
            nodeProfil.attachChild(geom);            // doesn't work (black mesh-objects)      
            
            Box box = new Box(100f,100f,100f);
            Geometry myGeometry = new Geometry("Box", box);
            myGeometry.setMaterial(mat);            
            nodeProfil.attachChild(myGeometry);      // works (blue boxes)  

TestCase is a test that we can Run, here its just part of code. You dont need provide all your code, just create Simple Test that can be run and demonstrate issue.

Also i see you use Lighting.j3md again instead of Unshaded. Lighting need normals/light/etc and you dont provide them.(normals at least)

Please provide TestCase.

1 Like

Maybe there is an easier way. I have read the Topic “Custom Mesh Shapes” in the JME-help. I would like to use the QuadMesh that is described there with a Material that can be shaded (Lighting.j3md?). What has to be changed about the mesh object to get this done?