• 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 MylesLambert · Feb 10, 2013 at 09:37 PM · vertexheightnormal

Calculate Vertex Normals in shader with heightmap.

Hey! I've been attempting to calculate the vertex normals in a shader due to the mesh being dynamic. I figured I could do this by grabbing 4 heightmap colour offsets and cross product/ averaging them, but this is giving me just a single normal direction over the entire mesh.

Here is my shader code :

             float2 v1UVs = v.texcoord;
                 v1UVs.y -= 0.001;
                 
             float2 v3UVs = v.texcoord;
                 v3UVs.y += 0.001;
                 
             float2 v4UVs = v.texcoord;
                 v4UVs.x -= 0.001;
                 
             float2 v2UVs = v.texcoord;
                 v2UVs.x += 0.001;
                 
             float4 heightMapv1 = tex2Dlod (_HeightMap, float4(v1UVs,0,0));
             float4 heightMapv2 = tex2Dlod (_HeightMap, float4(v2UVs,0,0));
             float4 heightMapv3 = tex2Dlod (_HeightMap, float4(v3UVs,0,0));
             float4 heightMapv4 = tex2Dlod (_HeightMap, float4(v4UVs,0,0));
             
             float heightv1 = ((heightMapv1.r - 0.5) *2) * 10;
             float heightv2 = ((heightMapv2.r - 0.5) *2) * 10;
             float heightv3 = ((heightMapv3.r - 0.5) *2) * 10;
             float heightv4 = ((heightMapv4.r - 0.5) *2) * 10;
             
             float3 vectorv1 = (0,heightv1,1);
             float3 vectorv3 = (0,heightv3,-1);
             float3 vectorv4 = (-1,heightv4,0);
             float3 vectorv2 = (1,heightv2,0);
             
             float3 crossv12 = normalize(cross(vectorv1, vectorv2));
             float3 crossv23 = normalize(cross(vectorv2, vectorv3));
             float3 crossv34 = normalize(cross(vectorv3, vectorv4));
             float3 crossv41 = normalize(cross(vectorv4, vectorv1));
             
             float3 average12 = normalize((crossv12 + crossv23) * 0.5);
             float3 average34 = normalize((crossv34 + crossv41) * 0.5);
             
             float3 average = normalize((average12 + average34) * 0.5);
             
             v.normal = average;

I do get some strange warnings on the shader too :

implicit truncation of vector type, comma expression used where a vector constructor may have been intended, potentially unintended use of a comma expression in a variable initializer, floating point division by zero.

Thanks!

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

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

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

Related Questions

Color depending on vertex normal 1 Answer

How to get to know a mesh' faces normals? 0 Answers

Getting vertex data from lighting function in shaders. 1 Answer

Problem of color in custom vertex shader 0 Answers

Why does my shader only shade objects based on their normals in the X axis? 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