Comments and answers for "Source code for library function "RecalculateNormals()"?"
http://answers.unity.com/questions/29286/source-code-for-library-function-recalculatenormal.html
The latest comments and answers for the question "Source code for library function "RecalculateNormals()"?"Answer by frogsbo
http://answers.unity.com/answers/624168/view.html
solution!
same as u3d for shared verts:
function simple_normals (go: GameObject ){
var mesh : Mesh = go.GetComponent(MeshFilter).mesh;
var indicies: int[] = mesh.triangles;
var myvertices : Vector3[] = mesh.vertices;
var mynormals: Vector3[] = mesh.vertices;
mynormals = new Vector3[mesh.vertices.length];
for(var i :int= 0; i < indicies.Length; i += 3)
{
var v0: Vector3= myvertices[indicies[i]];
var v1 :Vector3= myvertices[indicies[i+1]];
var v2 :Vector3= myvertices[indicies[i+2]];
var normal : Vector3= Vector3.Normalize(Vector3.Cross(v1 - v0, v2 - v0));
mynormals[indicies[i]] += normal;
mynormals[indicies[i+1]] += normal;
mynormals[indicies[i+2]] += normal;
}
for(i = 0; i < myvertices.Length; i++)
{
mynormals[i] = Vector3.Normalize(mynormals[i]);
}
mesh.normals = mynormals ;
}
COMPARISON: donate frogsbo.com /aka mtdoom plz (site not finished, this on sale in a week:
![alt text][1]
[1]: /storage/temp/21094-nmls.jpgFri, 24 Jan 2014 10:54:23 GMTfrogsboAnswer by MountDoomTeam
http://answers.unity.com/answers/446393/view.html
good question there, it's also useful for multithreading.
I found this which is a basic example, there is lots of different code for this online, if you use every triangle it gives you two neighbouring points to do a crossproduct from and then normalise.
// Some standard mesh information that you should have lying around.
// vertex is your vertex structure that just contains a normal and position here.
// vertices is a pointer to the first vertex
// indices is a pointer to the first index
// num_verts is number of vertices
// num_indices is number of indices
// each face of the mesh is made up of three vertices.
std::vector<Vector3>* normal_buffer = new std::vector<Vector3>[num_vertices];
for( int i = 0; i < num_indices; i += 3 )
{
// get the three vertices that make the faces
Vector3 p1 = vertices[indices[i+0]];
Vector3 p2 = vertices[indices[i+1]];
Vector3 p3 = vertices[indices[i+2]];
Vector3 v1 = p2 - p1;
Vector3 v2 = p3 - p1;
Vector3 normal = v1.Cross( v2 );
normal.Normalize();
// Store the face's normal for each of the vertices that make up the face.
normal_buffer[indices[i+0]].push_back( normal );
normal_buffer[indices[i+1]].push_back( normal );
normal_buffer[indices[i+2]].push_back( normal );
}
// Now loop through each vertex vector, and avarage out all the normals stored.
for( int i = 0; i < num_vertices; ++i )
{
for( int j = 0; j < normal_buffer[i].size(); ++j )
vertices[i].normal += normal_buffer[i][j];
vertices[i].normal /= normal_buffer[i].size();
--------------------------------------- - - - - -
This works for any triangle based mesh. works but doesn't make the same normals I think they are less good:
function simple_normals ( ){
var mesh : Mesh = gameObject.GetComponent(MeshFilter).mesh;
var indicies: int[] = mesh.triangles;
var vertices : Vector3[] = mesh.vertices;
var normals: Vector3[] = mesh.vertices;
for(var i :int= 0; i < indicies.Length; i += 3)
{
var v0: Vector3= vertices[indicies[i]];
var v1 :Vector3= vertices[indicies[i+1]];
var v2 :Vector3= vertices[indicies[i+2]];
var normal : Vector3= Vector3.Normalize(Vector3.Cross(v2 - v0, v1 - v0));
normals[indicies[i]] += normal;
normals[indicies[i+1]] += normal;
normals[indicies[i+2]] += normal;
}
for(i = 0; i < vertices.Length; i++)
{
normals[i] = Vector3.Normalize(normals[i]);
}
mesh.normals = normals ;
}
Heres a stripped down version of the Vector3 struct
public struct Vector3
{
float X, Y, Z;
public Vector3(float x, float y, float z)
{
X = x; Y = y; Z = z;
}
public static Vector3 Cross(Vector3 v1, Vector3 v2)
{
Vector3 result;
result.X = (v1.Y * v2.Z) - (v1.Z * v2.Y);
result.Y = (v1.Z * v2.X) - (v1.X * v2.Z);
result.Z = (v1.X * v2.Y) - (v1.Y * v2.X);
return result;
}
public static Vector3 Normalize(Vector3 v1)
{
float length = v1.X * v1.X + v1.Y * v1.Y + v1.Z * v1.Z;
length = Math.Sqrt(length);
v1.X /= length; v1.Y /= length; v1.Z /= length;
}
}Sun, 28 Apr 2013 17:09:00 GMTMountDoomTeamComment by JoelAtMoodkie on JoelAtMoodkie's answer
http://answers.unity.com/comments/83956/view.html
Thanks for the response. I determined it visually (and by using Debug.DrawLine() to view the normals). And I've used both of those methods you mentioned above; I don't think that the new normals are affecting the lighting. I'll post my code in another thread and see if I'm doing anything obviously wrong.Thu, 07 Oct 2010 10:52:10 GMTJoelAtMoodkieAnswer by Jesse Anders
http://answers.unity.com/answers/29310/view.html
<p>I doubt anyone is going to post the engine source code for recalculating mesh normals, but here's a couple of things to consider.</p>
<p>First, you said your code isn't producing the same results as RecalculateNormals(). How are you determining this? If you're looking at the numerical values for the normals in text form, don't forget to take floating-point error into account. (Even if you're using basically the same algorithm that Unity is, the results are unlikely to be exactly the same due to operation order and so forth.)</p>
<p>Second, how are you computing the normals? One possible reason why your normals wouldn't match Unity's would be that there's a bug in your code. If that isn't the case, it may be that you're using a different method than Unity is. The most common methods that I'm aware of are:</p>
<ul>
<li>Add up the unit-length normals of all the triangles incident on each vertex and normalize the result</li>
<li>Add up the unnormalized normals of all the triangles incident on each vertex and normalize the result (this will weight the normals by triangle area)</li>
</ul>
<p>So, you might try both of those methods and see if one matches Unity's results better than the other.</p>Wed, 06 Oct 2010 17:05:10 GMTJesse AndersComment by JoelAtMoodkie on JoelAtMoodkie's answer
http://answers.unity.com/comments/83857/view.html
The C++ code might be useful in seeing how Unity calculates vertex normals. At the moment my own function for calculating vertex normals doesn't produce the same results as RecalculateNormals(). Could anyone provide any info on how RecalculateNormals() works?Wed, 06 Oct 2010 15:02:19 GMTJoelAtMoodkieComment by JoelAtMoodkie
http://answers.unity.com/comments/83854/view.html
I'm doing some simple mesh deformation, but I'd like to only calculate the vertex normals for vertices that have been modified.Wed, 06 Oct 2010 14:50:30 GMTJoelAtMoodkieComment by Brian-Kehrer
http://answers.unity.com/comments/83850/view.html
What are you looking to do?Wed, 06 Oct 2010 13:54:44 GMTBrian-KehrerAnswer by Mike 3
http://answers.unity.com/answers/29291/view.html
<p>There isn't actually any code in the Mesh class really (besides extern function definitions) - it's all done via native code in the engine itself. The c++ code itself probably won't be of any use to you, as it'll hook into code you can't use yourself</p>Wed, 06 Oct 2010 13:37:22 GMTMike 3