Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JoelAtMoodkie · Oct 06, 2010 at 12:29 PM · functionlibrarysource

Source code for library function "RecalculateNormals()"?

Hiya all,

Is there any way I can get the source code for the "RecalculateNormals()" library function? The function is found in the Mesh class.

I want to make a version which is optimized for what I require it for; it would save me re-writing it all from scratch if I could get the source code!

Regards, Joel.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brian-Kehrer · Oct 06, 2010 at 01:54 PM 0
Share

What are you looking to do?

avatar image JoelAtMoodkie · Oct 06, 2010 at 02:50 PM 0
Share

I'm doing some simple mesh deformation, but I'd like to only calculate the vertex normals for vertices that have been modified.

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Jesse Anders · Oct 06, 2010 at 05:05 PM

I doubt anyone is going to post the engine source code for recalculating mesh normals, but here's a couple of things to consider.

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

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:

  • Add up the unit-length normals of all the triangles incident on each vertex and normalize the result
  • 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)

So, you might try both of those methods and see if one matches Unity's results better than the other.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoelAtMoodkie · Oct 07, 2010 at 10:52 AM 0
Share

Thanks for the response. I deter$$anonymous$$ed 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.

avatar image
1

Answer by Mike 3 · Oct 06, 2010 at 01:37 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoelAtMoodkie · Oct 06, 2010 at 03:02 PM 0
Share

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?

avatar image
1

Answer by MountDoomTeam · Apr 28, 2013 at 05:09 PM

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;
    }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by frogsbo · Jan 24, 2014 at 10:54 AM

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


nmls.jpg (427.0 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Why pass parameters into a function? 2 Answers

Camera Move 1 Answer

Array Problem - Error Code BCE0022 1 Answer

Is the scripting API source code available? 2 Answers

Help with function code, passing Vector3 info 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges