• 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
1
Question by Fingar · Dec 22, 2013 at 06:01 PM · shadersverticesnormalsfaces

Is vertices.normals face- or vertex-normals?

Hey, guys! Merry Christmas!

I'm working on a little holidays-project for a prototype of my game. And now I'm digging down into the nitty-gritty; making shaders! I'm not VERY familiar with all the components of a 3D mesh (or applying the correct math to the algorithm), so I need a little guidance.

I am making a deform vertex-shader for a sphere where i need to work with normals. Just to make sure that I'm on the right track, I'm trying to use a script to draw debug-rays from the vertices in the direction of each vertices normal.

 private Mesh mesh;
 public float normalLength = 1;

 // Use this for initialization
 void Start () {
         mesh = GetComponent<MeshFilter>().mesh;
 }
 
 // Update is called once per frame
 void Update () {
         foreach (Vector3 v3 in mesh.normals)
         {
             Debug.DrawRay(transform.position + v3, v3 * normalLength, Color.red);
         }
 }

However... This seems only to give me the face-normals. Am I doing something wrong in the script? What the hell is going on?!?!

Comment
Add comment · Show 1
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 Owen-Reynolds · Dec 22, 2013 at 09:31 PM 1
Share

Your drawRay is shooting from the center of the object (yes, the first v3 is invisible, but the 2nd visible part is still the same line.) So it "randomly" goes through a face or any vert.

As aldonaletto has in the code below, start each normal at it's vertex.

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by aldonaletto · Dec 22, 2013 at 06:18 PM

mesh.normals are the normals associated to the vertices: usually the shader calculates each pixel's normal by interpolating among the normals. Flat surfaces have equal normals in their vertices (like if they were surface normals), but curved surfaces have different normals associated to each vertex.

Anyway, keep in mind that all data in a mesh is in local space, which doesn't take the transform settings into account (rotation, scale, position). You should convert each vertex and its normal to world space in order to correctly display them:

 void Update () {
     for (int i=0; i<mesh.vertices.Length; i++){
         Vector3 norm = transform.TransformDirection(mesh.normals[i]);
         Vector3 vert = transform.TransformPoint(mesh.vertices[i]);
         Debug.DrawRay(vert, norm * normalLength, Color.red);
     }
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Change vertex colours based on steepness 1 Answer

How can I calculate the normals of the edges of a 2D plane? 0 Answers

How to set mesh normals per face? 2 Answers

Double sided geometry. 3 Answers

Uncorrect normals tri-planer shader 0 Answers


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