Lighting a custom mesh


I’m quite new to the JME, just working on my first real project. First: Thanks to the team for all this. Nice job. My question is about custom meshes. I’d like to use them and took the sample “Creating a Quad Mesh” from the custom meshes documentation as my starting point. Everything’s working fine as long as I stay with unshaded material. But I’m not able to correctly light the mesh. When switching to the “Lighting” material definition, the triangles are no longer fully visible. One of them is always “cut” somewhere in the middle (means it’s partly dark), sometimes both are. It looks different after every start. The picture is not even stable, it’s partly flickering. When I remove one of the triangles from the scene, the other one becomes completely dark. I think something important is missing or wrong in my code.

Also, I’m not sure about the face normal vector calculation. Do you have to do this manually for every face, as I do here? If so, am I doing it right?

The code is:

[java]Vector3f[] vertices = new Vector3f[] { new Vector3f(0,0,0), new Vector3f(3,0,0), new Vector3f(0,3,0), new Vector3f(3,3,2), new Vector3f(3,0,2), new Vector3f(0,3,2) };

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

Vector3f[] normals = new Vector3f[indexes.length / 3];

for (int i = 0; i < indexes.length / 3; i++)

normals = new Triangle(vertices[indexes[i*3]], vertices[indexes[i*3+1]], vertices[indexes[i*3+2]]).getNormal();

Mesh m = new Mesh();

m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));

m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));

m.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals));


Geometry geom = new Geometry(“OurMesh”, m);

Material mat = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

mat.setBoolean(“UseMaterialColors”, true);

mat.setColor(“Ambient”, ColorRGBA.Black);

mat.setColor(“Diffuse”, ColorRGBA.Red);

mat.setColor(“Specular”, ColorRGBA.White);

mat.setFloat(“Shininess”, 0);



DirectionalLight sun = new DirectionalLight();

sun.setDirection(new Vector3f(1,0,-2).normalizeLocal());



Any idea what’s wrong here? (BTW, I think it would be a great help if the custom mesh sample continued to a lit object. The mesh created there now isn’t ready for lighting, is it?)


you didnt flip the indices e.g 1,2,3 becomes 3,2,1.

You can use this : instead of re-inverting the wheel.

you didnt flip the indices e.g 1,2,3 becomes 3,2,1.

Do you mean for the normal calculation? To change the code line to:
[java] normals = new Triangle(vertices[indexes[i*3+2]], vertices[indexes[i*3+1]], vertices[indexes[i*3]]).getNormal();[/java]
I already tried this, it doesn't change much. The triangles are still partly visible only and flickering.

You can use this : ...

Thanks a lot. What this code does, if I get it right, is to load a model and to create a transformed (mirrored) copy of it. It doesn't create a completely new mesh by code (from bare int and float arrays), so it doesn't show how to do that, does it?

My question is: Does JME support custom meshes at the moment? If so, how are you doing this? Is it possible to have a sample code, the simplest is fine, that goes the complete way to lighting?


Mesh mesh = new Mesh();

mesh = new MeshData(mesh).flipIndexes().createMesh();


  1. of course jme supports custom meshes and it works as intented. Lighting works fine. Its simple you gave jme wrong data !!!

I’m sure the indices are in the right order for face definition, I can see it in the unshaded version. When I flip them as you suggest, the triangles disappear there, and I can see the backside when moving the cam. So then it would be wrong.

Ok, I found my mistake now. I had a look at the sources of the Box, which is created from int and float arrays and therefore nothing else but a special custom mesh and obviously working. The normal vectors are not per-face, as I thought, but per-vertex. Of course they are. For sharp edges, each vertex has to appear multiple times in the list, once for each “true” face it’s part of. specifies each corner of the cube 3 times, so it has a 24 vertices list and a 24 normals list for an 8 vertex / 6 face mesh.

After increasing the number of normals to the number of vertices, my code works as expected. Thanks anyway.