- Home /
How do I manually calculate Normals for my Meshes?
I'm creating my own terrain tiling system for the iPhone so I don't have "SetNeighbours" or anything like that. I'm working in raw meshes. As such the normals near the edge of my meshes are screwed up.
Any way I can set the normals for my triangles manually?
I am working on this at the moment, if anyone has a solution to the edges please squawk about them now. also, if you have procedural mesh, you can use the procedural function to calculate the normals uniformly rather than using the actual mesh. I don't know how to do that exactly also!
Answer by fherbst · Oct 29, 2010 at 07:25 PM
Something like that should do it if you have a script-made mesh and want to recalculate the normals.
EDIT: Yes, you can edit the normals by hand, too:
function Update () { var mesh : Mesh = GetComponent(MeshFilter).mesh; var normals : Vector3[] = mesh.normals;
// do custom normal calculations
// (e.g. looking at the surrounding faces and their normals
// and interpolating them - thats the vertex normal)
for (var i = 0; i < normals.Length; i++)
normals[i] = myCustomRecalculatedNormal;
mesh.normals = normals;
}
I hope that helps - if you need help for the calculation itself, just comment; I will see what I can do (you could also take a look at this first - does exactly what you want, I think).
"Imported meshes sometimes don't share all vertices. For example a vertex at a uv seam will be split into two vertices. Thus the RecalculateNormals function will create normals that are not smooth at the uv seam."
This is the exact problem I'm running into. $$anonymous$$y terrain tiles are separate meshes so the vertices on the edges of the tiles don't share the same meshes. Hence "manually" updating normals.
I can retrieve the common vertices.. can I hand set the normals?
Answer by MountDoomTeam · Feb 11, 2013 at 05:36 AM
edit
Best way i found using procedural terrains, just get 2 points near the vertex using the same info you had to get the vertex height, it ll give you 3 points to cross product normal height and smooth shadows also. if you use 4 points around the vertex it may give you better results on peaks normals. works with hi res terrain height data. seamless. better quality normals.
there are various ways to calculate normals on the edges of the tiles. The question is, which one is easier, and which one is faster.
You can use the height map/procedural data to sample the points around the edge vertices and construct a normal from that. slow method.
You can use a separate mesh that is not visible and that is larger so that the visible tiles, and then you apply all the vertex/normal calculations to the hidden tile, and then copy the normals and the vertices from the hidden tile to the visible ones. I expect this is the slowest method.
The fastest by far method, I'm not sure how accurate it is I haven't tried it it should be hundred percent accurate, is that you go through the vertices of edges, and you will find that for a 10 x 10 plane, vertex 1 is on the same point as vertex 91 for the plane next to it, because vertices 0 to 10 is the south edge, and 90 to 100 is the North edge, and all the East ones all finish with 0, and the West ones all finish with 9. So when you have to vertices on the same points you can add the normal of both and divide by 2 to average it and get the actual normal of that point which I think should be correct. it should be true for the edges and corners.
What you can also do is rotate each tile by 90 so that all the edge vertices correspond one-to-one with each other in the array indices, and generate the height map data using transform point.
...
also go through edge vertices' normals for adjoining mesh and average normals that share same vertex position.
Answer by Fehr · Dec 20, 2019 at 10:52 AM
Thought this thread is now nearly ten years old, I recently had to solve this problem and wanted to share an amazing explanation and solution by Sebastian Lague on his YouTube channel as part of his procedural land mass generation tutorial:
https://www.youtube.com/watch?v=NpeYTcS7n-M&list=PLFt_AvWsXl0eBW2EiBtl_sxmDtSgZBxB3∈dex=12
Hopefully this helps someone in the future who needs to solve the same problem one day.
Answer by Jessy · Oct 29, 2010 at 10:20 PM
Triangles don't have normals. Vertices do.
http://unity3d.com/support/documentation/ScriptReference/Mesh-normals.html
Strictly speaking triangles have normals because a point in space cannot have a face (unlike a plane). However, in this context, the normals of vertices is the average of the triangle normals that share the vertex.
Thanks for the info though. Now I just have to figure out how to do the normal calculation myself.
No. The normal of the triangle itself isn't used for anything, except mesh colliders, which you're not talking about. There's no reason that the normal of a vertex has to have anything to do with the triangles that surround it; it just makes sense for realistic lighting, which is their intended purpose.
in unity vertices have their own normals. in some code, each vertex of a triangle has the same normal for all 3 points of the triangle.
Your answer
Follow this Question
Related Questions
Holes in procedural mesh 0 Answers
Problem drawing a mesh with Graphics.DrawMeshNow 1 Answer
How to make sure two meshes have the same vertex count 0 Answers
Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer
Procedural cube code 0 Answers