Strange shader/lightning behavior in generated mesh

For some reason this happens with my generated mesh:

This is what it looks like if all squares are the same size:

This is the same done with block objects and also what I would like it to look like:

All pictures use the same material, so I don’t think that is the casue.

Any idea what could cause this?

I am not sure what your mesh generation code is doing, but I suspect it is sharing vertices between faces in some way, and this is causing a problem if the normals to the faces are not the same.

The issue you show looks like the sort of problem you get with cubes when you don’t give each face its own vertices - it takes on a ‘curved’ effect from a lighting point of view, as the normals point out diagonally from each corner and blend across the cube.

If you are currently trying to share vertices between triangles in any way, I would avoid it for the moment. Start with mesh generation that:

  • Just creates 3 new vertices for every triangle
  • Calculate the normal to the triangle using the cross product of the 2 edges
  • Store that same normal for all 3 vertices

This is not optimal, but should give you a correct mesh.

Once you’ve got that, you could look at more efficient use of vertices - for example you could easily share vertices between the 2 triangles on a single face of a block, and if you’re careful perhaps more. Start simple though - and only make it better if it isn’t good enough! :slight_smile:

(Edit: Just remembered the ‘RecalculateNormals’ function on a mesh. Provided you do my first step - 3 vertices for each triangle, and they have the correct winding order, recalculate normals should do the rest if you’re using it).


I figured out why this happens, yet to fix it though.

The difference between blocks and my mesh was, that the blocks were always size of (1,1,1), while my mesh was a lot bigger. Using same size squares didn’t work because the overall mesh is still bigger and applying the material on it does weird things apparently.