• 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 ricardo_arango · Feb 17, 2014 at 09:34 PM · normalmapnormal map

How are normal maps imported?

How does Unity save the normal vectors in a Normal Map? How do I get the normal vectors from a Normal Map texture in a shader?

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
6

Answer by ricardo_arango · Feb 17, 2014 at 09:54 PM

Unity packs the normal vectors into Normal Map Texture2D assets using two encoding methods, depending on the platform.

For mobile platforms (or platforms that don't support DXT compression), Unity saves the coordinates of the normal vectors normalized:

 c[0] = 1.0f;   // A = W
 c[1] = (normal.x + 1.0F) * 0.5;  // R = X
 c[2] = (normal.y + 1.0F) * 0.5;  // G = Y
 c[3] = (normal.z + 1.0F) * 0.5;  // B = Z

A normal vector of magnitude one can have negative and positive values, between -1 and 1. In the code above, the normal is being transformed into the 0 to 1 space.

The opposite transformation needs to be done in the shader, to decode the normal vector. Unity includes in the UnityCG.inccg file a helper function called UnpackNormal to perform this operation:

 inline fixed3 UnpackNormal(fixed4 packednormal)
 {
 #if (defined(SHADER_API_GLES) || defined(SHADER_API_GLES3)) && defined(SHADER_API_MOBILE)
     return packednormal.xyz * 2 - 1;
 #else
     return UnpackNormalDXT5nm(packednormal);
 #endif
 }

In your shader code you would write something like this:

 fixed3 normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));

For other platforms, Unity will encode the NormalMap in the DXT5nm format. You can find information online about this format, for example here : http://tech-artists.org/wiki/Normal_map_compression. To summarize, DXT5 compression has more bits to store the information of the Green and Alpha channels. Therefore, it's possible to store two of the components using those channels and calculate the third one.

 c[0] = red; // A = W
 c[1] = c[2] = c[3] = green; // RGB = XYZ

To obtain the normal vector from the NormalMap, you can also use UnpackNormal. The code for the DXT5nm specific unpacking is this:

 inline fixed3 UnpackNormalDXT5nm (fixed4 packednormal)
 {
     fixed3 normal;
     normal.xy = packednormal.wy * 2 - 1;
 #if defined(SHADER_API_FLASH)
     // Flash does not have efficient saturate(), and dot() seems to require an extra register.
     normal.z = sqrt(1 - normal.x*normal.x - normal.y*normal.y);
 #else
     normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
 #endif
     return normal;
 }

The UnpackNormal and UnpackNormalDXT5nm functions are found in UnityCG.cginc. That file is included with the Editor when installed, you can find it in the "CGIncludes" folder.

Alternatively you can download the shader source code from the Unity Download Archive : https://unity3d.com/unity/download/archive

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

18 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

Related Questions

a shader or decal that only renders the normal map 0 Answers

How can I fix a normalmap procedurally generated? 2 Answers

Export Normal Map from Selected Mesh 0 Answers

Unity2D Tilemap Add Sprites with Normal Maps to tilePallet 2 Answers

Why do my normal maps pop in and out? 2 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