Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Azial · Dec 05, 2015 at 08:42 PM · shadersnormalsblend

Shader: blending between normals not working

Hi,

in the following shader i try to tone down the bump for the physically based lightning, so the first pass (the refracted "background") is better visible.

 Shader "Custom/WaterRefl" {
     Properties {
         _BumpMap ("Normalmap", 2D) = "bump" {}
         _Refraction  ("Refraction", range (0, 128)) = 10
         _Reflection  ("Reflection", range (0, 1)) = 0.5
         _Glossiness ("Smoothness", Range(0, 1)) = 0.5
         _Metallic ("Metallic", Range(0, 1)) = 0.0
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
         
         GrabPass { }
         
         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard fullforwardshadows
 
         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0
 
         sampler2D _BumpMap;
         float _Refraction;
         float _Reflection;
         half _Glossiness;
         half _Metallic;
         sampler2D _GrabTexture;
         float4 _GrabTexture_TexelSize;
 
         struct Input {
             float2 uv_BumpMap;
             float4 screenPos;
             float3 worldRefl;
             INTERNAL_DATA
         };
 
         void surf (Input IN, inout SurfaceOutputStandard o) {
             half3 bump = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
             float2 offset = bump * _Refraction * _GrabTexture_TexelSize.xy;
             IN.screenPos.xy += offset * IN.screenPos.z;
             float3 refractColor = tex2Dproj( _GrabTexture, IN.screenPos);
             
             half3 origNorm = o.Normal;
             o.Normal = origNorm * (1-_Reflection) + bump * _Reflection; // this does not work as expected
             o.Albedo = refractColor;
             o.Metallic = _Metallic;
             o.Smoothness = _Glossiness;
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }


The problem is this: o.Normal = origNorm (1-_Reflection) + bump _Reflection; It does not work as expected (with _Reflection, I want to control how much the skybox is distorted by the bump texture).

The strange thing is, when I force it to take only the bump OR the original normals, it works as expected (too much distortion or no distortion at all):

o.Normal = origNorm; // skybox is not distorted OR o.Normal = bump; // skybox it way too much distorted

With o.Normal = bump * _Reflection, I get just a flat grey plane for low values of _Reflection. I think that's because it does not matter what size normals have, the direction is important. So I need a way to tone down the strength of the normal map somehow...

too-low.jpeg (158.6 kB)
too-much.jpeg (280.6 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
1
Best Answer

Answer by JonathanCzeck · Dec 05, 2015 at 09:43 PM

A handy thing if you're not sure what's going on in a surface shader is to look at the generated code by selecting the shader in your Project and hitting the "Show generated code" button in the Inspector. Skimming around for stuff about o.Normal, you'll find this line right before the surface shader is called:

 o.Normal = IN.worldNormal;

So you're linearly interpolating between a world space normal and a tangent space normal. (Your unpacked _BumpMap texture). That's almost surely not what you want.

Just write your scaled tangent space normal into o.Normal. You don't have to take into account what was there before.

Comment
Add comment · Show 2 · 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 Azial · Dec 05, 2015 at 10:31 PM 0
Share

Ok, I didn't know about the world and tangent space, thanks!

Just write your scaled tangent space normal into o.Normal

You mean o.Normal = bump * _Reflection? But then, I get a really strange result (image attached), which is nowhere between the two extrema described above. [1]: /storage/temp/59427-too-dark.jpg
too-dark.jpg (188.2 kB)
avatar image Azial · Dec 05, 2015 at 10:57 PM 0
Share

Ok, I made it:

 half3 n = bump;
 n.z = _Reflection;
 o.Normal = normalize(n);

where _Reflection must be set to a value around 5 or so for nice "blending".

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

32 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is vertices.normals face- or vertex-normals? 1 Answer

Double sided geometry. 3 Answers

Uncorrect normals tri-planer shader 0 Answers

I'm computing my surface normals in the fragment program - so why does Unity still require per-vertex normals/tangents? 1 Answer

Can I retrieve per face normals instead of per vertex normals in vertex shader? 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