- Home /
Make mesh triangles have a single normal vector to make them stay triangles??
Hello guys! So here's my problem: My general idea was to make an endless low poly world generator but i can figure something out, in order for the terrain to be low poly i need each triangle of the mesh to have only one vector facing toward the normal vector of the triangle, but i don't know how to do that, Is there a way to give/apply a mesh's triangles a single vector (In my case its the normal vector of that triangle)? or alternitavely prevent the unity engine from caculating the avarage vectors of the triangle apon rnadering the mesh and just leave them as triangles?
Thanks in advance, any help much appreciated!
Answer by IgorAherne · Sep 13, 2017 at 01:15 AM
I think you want to explicitly assign normals to the Mesh.normals array. One normal per your vertex. So for 1 triangle you would have 3 vertices and 3 normals
yes, you will have to work with MeshFilter.mesh or .sharedMesh
you can point it at a new instance of Mesh, for example,
Mesh sharpCornersMesh = new Mesh();
sharpCornersMesh.normals = new Vector3[]{ normalDir, normalDir, normalDir }; /make sure normalDir is .normalized
sharpCornersMesh.vertices = new Vector3[]{ coordA, coordB, coordC }
sharpCornersMesh.triangles = new int[]{0, 1, 2}
myMeshFilter.sharedMesh = sharpCornersMesh;
you could compute normalDir per each triangle as a normalDir = Cross(coordA-CoordB, coordA-coordC).normalized;
depending on the order in which you supply coordA, coordB, coordC into your mesh you may wish to swap the order of arguments in the Cross() function, else your normals will point in the opposite direction
Also, make sure to save your mesh as an asset, else you will loose it the next time you open Unity; just google about it
But what about vertices that are being used for multiple triangles, it would be good for meshes where each triangle has its own 3 vertices cause all of them whould have the same normal vector of the triangle but when some vertices are connected to a number of triangles it could be problomatic couldn't it? or is there something i don't know?
Answer by Orr10c · Sep 13, 2017 at 05:18 AM
Thank you very much! I'll take a look at this :)
But what about vertices that are being used for multiple triangles, it would be good for meshes where each triangle has its own 3 vertices cause all of them whould have the same normal vector of the triangle but when some vertices are connected to a number of triangles it could be problomatic couldn't it? or is there something i don't know?
yes, having a mesh with 3 vertices per each triangle (non-connected) is the only option in your case, otherwise, if you share vertices you won't have the required normals to define a sharp edge.
Having 3 vertices per triangle does increase memory requirement, and you need to ensure the neighboring vertices from neighboring triangles have the same coordinate. Otherwise, you will have visual gaps between your triangles
Your answer
Follow this Question
Related Questions
Modify a mesh at runtime 0 Answers
How to get direct access to a terrain's vertices? 1 Answer
Can't paint detail on terrain. 1 Answer
Lighting Banding Artifact 0 Answers
Questions about render distance in an open world game 0 Answers