Get normal vectors for custom mesh

I’ve created a custom mesh (triangle), how to get normals for all 3 vertices? (It must looks just like flat triangle with Lighting.j3md material)

The short answer is that JME provides a method to calculate a normal vector of a 3-D triangle:


Keep in mind that (like any 2-D figure) a triangle has two normal vectors, one being the negative of the other. Your choice of sign may affect the triangle’s visibility.

This is not a com.jme3.math.Triangle, this is vustom mesh created from vetrices, and I need to set normals to this custom mesh

You mean setting and getting mesh buffers?

mesh.setBuffer(Type.Normal, 3, float[] { ... });

VertexBuffer vb = getBuffer(Type.Normal);

or do you mean getting the normal vector of a triangle, like this?

Create it facing upward and set 0,1,0 as normal… or multiply 0,1,0 for the rotation ¯\(ツ)

I mean setting and getting mesh buffers. I have only 3 points coordinates, and i need to set normals for Lighting material.

It isn’t facing upward because vertices data is loaded from file.

Ok then, use the @sgold method

It doesn’t matter if you don’t have a Triangle object, you can create it with the vertices you load.

 new Triangle((Vector3f) p1, (Vector3f) p2, (Vector3f) p3);

Then you write the result in the normal buffer once per vertex.

1 Like

I did it, but it works strange with SpotLight, it looks like ON/OFF mode (0% and 100% of brightness) when I rotate mesh. Here are screenshots:

Only those 2 states.