# Does anyone have mesh normals calculation code?

Discussion in 'General Discussion' started by reefwirrax, Nov 6, 2013.

1. ### reefwirrax

Joined:
Sep 20, 2013
Posts:
138
Has anyone found some mesh-normals finding code that produces nice results in unity, as good as the recalculatenormals? Because the built-in function cannot be multi-cored, held in an array prior to writing, edited, it has bugs along seams, etc.

could you prevent me having to search around the Internet searching for and converting codes for many hours in order to find one? do you know a really good code for it in C++?

please post what you have it would be heavily of you

2. ### RvBGames

Joined:
Oct 22, 2013
Posts:
141
I'm not sure if I correctly understand your problem. . .but I think it may be a matter of averaging the normal when a vertex is shared by multiple faces. I may have C++ code lying around, I just have to look for it. Since DirectX has this I didn't have to use my own, OpenGL on the other hand didn't, and I did

That last bit is to see if you're following long.

Would you please post a picture of what you have.

Is this a preprocessing issue or something you are trying to do at runtime?

3. ### HolBol

Joined:
Feb 9, 2010
Posts:
2,884
Why C++? Unity doesn't use C++ for scripting.

4. ### BFGames

Joined:
Oct 2, 2012
Posts:
1,543
Create two vectors based on a triangles three vertices and then take the cross product of these two vectors and normalize it.

5. ### RvBGames

Joined:
Oct 22, 2013
Posts:
141
Although the method mentioned does produce a normal, it does not account for when the same vertex is shared among multiple faces that may be oriented differently.

6. ### Dantus

Joined:
Oct 21, 2009
Posts:
5,667
Take all the triangles around a vertex, do what BFGames wrote for each of and add them together. Now, normalize the result. Done.

7. ### halley

Joined:
Aug 26, 2013
Posts:
737
Do a first pass with what BFGames wrote for each, so that you can get naive normals. Then take all the triangles around a vertex that are not on the opposite side of a seam edge, or have a naive normal that is over the crease angle limit, and do what Dantus wrote, and add them together scaled with the edge weights specified. Now normalize the result. Done.

Last edited: Nov 6, 2013
8. ### BFGames

Joined:
Oct 2, 2012
Posts:
1,543
Do what halley wrote, enjoy, and then go buy an ice cream!

9. ### SmellyDogs

Joined:
Jul 16, 2013
Posts:
387
Also remember that order matters when calculating the cross product otherwise you could have inverse normal.

a x b != b x a (not commutative).

So if winding is anti-clockwise you swap around a and b terms.

unityunity