• 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 SvenEV · Aug 08, 2013 at 09:10 PM · shadernormalmapbumpmaptangentsnow

Shader issue: Normal map from tangent to world space - Textures flipping?

I'm currently trying to write a snow shader. I'd like the snow to appear on top of a mesh, of course. The snow covered areas should also depend on the material's normal (bump) map so that it appears on top of the details provided by the normal map as well.

To ac$$anonymous$$eve t$$anonymous$$s I convert the normal map from tangent to world space in the pixel shader. The amount of snow then corresponds to the normal map's y component.

T$$anonymous$$s works quite well so far except for one issue: When rotating the GameObject at certain angles the textures seem to flip so that the snow is no longer on top of the details provided by the normal map but beneath them.

image

Here's a video illustrating the issue (the snow is painted blue for testing purposes): http://www.youtube.com/watch?v=6VG-yoeqqWk. As you can see the blue snow at first appears on top of the rocks "sticking out" of the cube. But at 45° the textures flip (they flip again at 135°, 225° and 315°).

What could be wrong with my shader?

 Shader "SnowMaterial"
 {
     Properties 
     {
         _SnowColor("_SnowColor", Color) = (1,1,1,1)
         _SnowBump("_SnowBump", 2D) = "bump" {}
         _MainTex("Base (RGB) Gloss (A)", 2D) = "black" {}
         _BumpMap("Normalmap", 2D) = "bump" {}
     }
     
     SubShader 
     {
         Tags
         {
             "Queue" = "Geometry"
             "IgnoreProjector" = "False"
             "RenderType" = "Opaque"
         }
 
         Cull Back
         ZWrite On
         ZTest LEqual
         ColorMask RGBA
         Fog { }
 
         CGPROGRAM
             #pragma surface surf BlinnPhong vertex:vert
             #pragma target 3.0
 
             float4 _SnowColor;
             sampler2D _SnowBump;
             sampler2D _MainTex;
             sampler2D _BumpMap;
 
             struct Input
             {
                 float3 sWorldNormal;
                 float2 uv_SnowBump;
                 float2 uv_MainTex;
                 float2 uv_BumpMap;
                 float4 tangent;
             };
 
             void vert(inout appdata_full v, out Input o)
             {
                 v.normal = (float4( v.normal.x, v.normal.y, v.normal.z, 1.0)).xyz;
 
                 o.sWorldNormal = mul((float3x3)_Object2World, SCALED_NORMAL);
                 o.tangent = mul(v.tangent, _Object2World);
             }
 
             void surf(Input IN, inout SurfaceOutput o)
             {                
                 float4 mainTex = tex2D(_MainTex, IN.uv_MainTex);
                 float3 mainBump = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
                 float3 snowBump = UnpackNormal(tex2D(_SnowBump, IN.uv_SnowBump));
                 
                 float3 tangent = normalize(IN.tangent.xyz);
                 float3 normal = normalize(IN.sWorldNormal);
                 float3 binormal = normalize(cross(normal, tangent) * IN.tangent.w);
                 
                 float3x3 tangentToWorld = transpose(float3x3(tangent, binormal, normal));
                 
                 float3 amount = saturate(mul(tangentToWorld, normalize(mainBump)));
                 o.Albedo = lerp(mainTex, _SnowColor, amount.y);
                 o.Normal = lerp(mainBump, snowBump, amount.y);
             }
         ENDCG
     }
     Fallback "Diffuse"
 }


shaderissue.jpg (184.1 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
2
Best Answer

Answer by SvenEV · Aug 11, 2013 at 03:03 PM

Looks like

 o.tangent = mul(v.tangent, _Object2World);

should actually be

 o.tangent = mul(_Object2World, v.tangent);

Now the snow is always on top of the rocks at any rotation.

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 ggzerosum · Jul 30, 2019 at 06:54 AM 0
Share

It's very long time to expect relpy.. but i got a one question.

line 62 : float3x3 tangentToWorld = transpose(float3x3(tangent, binormal, normal));

why did you transpose matrix?

As i know, float3x3(tangent, binormal, normal) is computed to


[ [ Tx, Ty, Tz ] [ Bx, By, Bz ] [ Nx, Ny, Nz ] ]


and that is the result of tangentToWorld Matrix..?

I'm confused cause i learned tangent space few days ago. Please let me know your idea!

Thx

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

16 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

Related Questions

Normal mapping mirrored UVs? 2 Answers

can you make a surface shader be drawn before or after a certain pass? 1 Answer

Mobile bump shader not working 1 Answer

Normal map mask 2 Answers

Mobile Bumped Diffuse problem 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