x


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?

more ▼

asked Oct 29 '10 at 07:17 PM

Jeremy 6 gravatar image

Jeremy 6
193 8 8 16

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!

Feb 10 at 11:20 PM ZoomDomain
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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).

more ▼

answered Oct 29 '10 at 07:25 PM

felix. gravatar image

felix.
1.9k 18 23 47

"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. My 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?

Oct 29 '10 at 09:00 PM Jeremy 6
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 11 at 05:36 AM

ZoomDomain gravatar image

ZoomDomain
890 7 22 56

(comments are locked)
10|3000 characters needed characters left
more ▼

answered Oct 29 '10 at 10:20 PM

Jessy gravatar image

Jessy
15.6k 72 95 196

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.

Oct 30 '10 at 07:48 PM Jeremy 6

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.

Oct 30 '10 at 08:21 PM Jessy

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.

May 05 at 07:19 PM ZoomDomain
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x300
x139
x122
x61
x35

asked: Oct 29 '10 at 07:17 PM

Seen: 4287 times

Last Updated: May 05 at 07:19 PM