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 PolygoneHome · Mar 16, 2014 at 11:24 AM · meshgenerationnormal

Generating Normals

I've generated a mesh, and now I want to generate its normals for a smooth shading effect. The problem is that it doesn't seem to be working as I'd expect, it's still very blocky. The below is the model with a simple diffuse shader:

alt text

And my current code:

 public Vector3[] CalculateNormals(Vector3[] vertices, int[] triangles)
     {
         Vector3[] normals = new Vector3[vertices.Length];
         for(int i = 0; i < triangles.Length; i = i + 3)
         {
             Vector3[] storedPoints = new Vector3[3];
             storedPoints[0] = vertices[triangles[i]];
             storedPoints[1] = vertices[triangles[i + 1]];
             storedPoints[2] = vertices[triangles[i + 2]];
             Vector3 normal = Vector3.Cross(storedPoints[1] - storedPoints[0], storedPoints[2] - storedPoints[0]);
 
             for(int j = 0; j < 3; ++j)
             {
                 Vector3 a = storedPoints[(j+1) % 3] - storedPoints[j];
                 Vector3 b = storedPoints[(j+2) % 3] - storedPoints[j];
                 float weight = Mathf.Acos(Vector3.Dot(a, b) / (Vector3.Distance(Vector3.zero, a) * Vector3.Distance(Vector3.zero, b)));
                 normals[triangles[i + j]] += weight * normal;
             }
         }
 
         foreach(Vector3 normal in normals)
         {
             normal.Normalize();
         }
         return normals;
     }

I've been really tearing my hair out over this for the past week or so, any help would be immensely appreciated!

block.png (15.3 kB)
Comment
Add comment
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

1 Reply

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

Answer by Eric5h5 · Mar 16, 2014 at 11:26 AM

You can make sure the vertices are shared for all the quads, and use Mesh.RecalculateNormals, which will do it for you.

Comment
Add comment · Show 3 · 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 PolygoneHome · Mar 16, 2014 at 11:38 AM 0
Share

$$anonymous$$esh.RecalculateNormals results in blocky shading does it not? Or is that only the case if vertices are not shared?

Thanks :)

avatar image Eric5h5 · Mar 16, 2014 at 11:43 AM 0
Share

It's not even possible to have blocky shading if the vertices are shared.

avatar image PolygoneHome · Mar 16, 2014 at 11:46 AM 0
Share

Damn...you're right. Thanks a ton!

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I "generate a mesh" for a projected image or decal? 0 Answers

Changes to a mesh via code don't render until I interact with them through manual editor tools. 1 Answer

How to set up UV for lightmaps for generated meshes? 4 Answers

Procedurally Generated Cube Mesh 3 Answers

Mesh visible in editor but not build 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