Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to control normal map strength by a variable

Discussion in 'Shaders' started by Mhmd-Subhi, Nov 5, 2015.

  1. Mhmd-Subhi

    Mhmd-Subhi

    Joined:
    Mar 13, 2015
    Posts:
    28
    Hi,
    I have a shader that uses a (Damage) normal map, and I want to control this map's strength with a float between 1 and 0, 0 = no strength and 1 = full strength (Just like the standard shader),

    How can I do it?

    This is the normal map lines :

    Code (CSharp):
    1. _DamageMap ("Damagemap", 2D) = "bump" {}
    2. _Damage ("Damage Value", Range(0.0,10.0)) = 0.0
    3.  
    4. sampler2D _DamageMap;
    5. half _Damage;
    6.  
    7. void surf (Input IN, inout SurfaceOutput o)
    8. {
    9.  
    10. fixed4 Dmg = tex2D(_DamageMap, IN.uv_MainTex);
    11.  
    12. o.Normal = UnpackNormal (Dmg);
    13. }
     
    SamFernGamer4k likes this.
  2. alienheretic

    alienheretic

    Joined:
    Oct 15, 2008
    Posts:
    60
    _DamageMap ("Damagemap", 2D) = "bump" {}
    _Damage ("Damage Value", Range(0.0,10.0)) = 0.0

    sampler2D _DamageMap;
    half _Damage;

    void surf (Input IN, inout SurfaceOutput o)
    {
    fixed4 Dmg = UnpackScaleNormal(tex2D(_DamageMap, IN.uv_MainTex),_Damage);
    o.Normal =Dmg;
    }
     
    Yanomry and bjackyb12 like this.
  3. Mhmd-Subhi

    Mhmd-Subhi

    Joined:
    Mar 13, 2015
    Posts:
    28
    unfortunately, It didn't work, but i have got the method from the UnityStandardUtils.cginc file itself form the UnpackScaleNormal function inside it :

    Code (CSharp):
    1. fixed3 Dmg = UnpackNormal(tex2D(_DamageMap, IN.uv_MainTex));
    2.            
    3. Dmg.xy *= _Damage;
    4.            
    5. o.Normal = normalize(Dmg);
    thanks for your reply.
     
  4. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,445
    Just lerp between your texture and (0.5, 0.5, 1):

    float3 normal = lerp(float3(0.5, 0.5, 1), tex2D(_DamageMap, IN.uv_MainTex), _Damage);
    o.Normal = UnpackNormal(normal);
     
  5. Brookener

    Brookener

    Joined:
    Jan 15, 2019
    Posts:
    1
    6 years later, jbooth, thank you...
     
  6. Danielsantalla

    Danielsantalla

    Joined:
    Jan 7, 2015
    Posts:
    75
    7 years later, @jbooth , thanks!
     
  7. JDB-Artist

    JDB-Artist

    Joined:
    Dec 5, 2012
    Posts:
    41
    8 years later, @jbooth thank you.
     
  8. VZPX

    VZPX

    Joined:
    Sep 5, 2021
    Posts:
    14
    9 years later, @jbooth sheesh!
     
  9. Morseliot

    Morseliot

    Joined:
    Jan 10, 2015
    Posts:
    67
    2023 here, cheers.
     
    Mhmd-Subhi likes this.