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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED] Problem adding NormalMap to ToonShader

Discussion in 'Scripting' started by Yearl, Feb 22, 2016.

  1. Yearl

    Yearl

    Joined:
    Jun 26, 2015
    Posts:
    33
    Hi everyones !

    So I'm very weak in shaders script and so I take the Standart ToonShader and try to insert a NormalMap.

    There is the Shader code :

    Code (CSharp):
    1. Shader "Toon/Lit" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (0.5,0.5,0.5,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _BumpMap ("Normalmap", 2D) = "bump" {}
    6.         _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
    7.     }
    8.  
    9.     SubShader {
    10.         Tags { "RenderType"="Opaque" }
    11.        
    12.         Cull Off
    13.        
    14. CGPROGRAM
    15. #pragma surface surf ToonRamp
    16.  
    17. sampler2D _Ramp;
    18.  
    19. // custom lighting function that uses a texture ramp based
    20. // on angle between light direction and normal
    21. #pragma lighting ToonRamp exclude_path:prepass
    22. inline half4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
    23. {
    24.     #ifndef USING_DIRECTIONAL_LIGHT
    25.     lightDir = normalize(lightDir);
    26.     #endif
    27.    
    28.     half d = dot (s.Normal, lightDir)*0.5 + 0.5;
    29.     half3 ramp = tex2D (_Ramp, float2(d,d)).rgb;
    30.    
    31.     half4 c;
    32.     c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);
    33.     c.a = 0;
    34.     return c;
    35. }
    36.  
    37.  
    38. sampler2D _MainTex;
    39. sampler2D _BumpMap;
    40. float4 _Color;
    41.  
    42. struct Input {
    43.     float2 uv_MainTex : TEXCOORD0;
    44.     float2 uv_BumpMap : TEXCOORD0;
    45. };
    46.  
    47. void surf (Input IN, inout SurfaceOutput o) {
    48.     half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    49.     o.Albedo = c.rgb;
    50.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    51.     o.Alpha = c.a;
    52. }
    53. ENDCG
    54.  
    55.     }
    56.  
    57.     Fallback "Diffuse"
    58. }
    59.  

    Just modify properties and theses lines :

    Code (CSharp):
    1. sampler2D _MainTex;
    2. sampler2D _BumpMap;
    3. float4 _Color;
    4.  
    5. struct Input {
    6.     float2 uv_MainTex : TEXCOORD0;
    7.     float2 uv_BumpMap : TEXCOORD0;
    8. };
    9.  
    10. void surf (Input IN, inout SurfaceOutput o) {
    11.     half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    12.     o.Albedo = c.rgb;
    13.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    14.     o.Alpha = c.a;
    15. }
    To add NormalMap (Don't know if it's the good way)

    Then my normal don't have the good effect on the object ...

    My sword rendered :
    http://postimg.org/image/faaz3revb/

    And then my Nmap :
    http://postimg.org/image/7gyyazanr/

    An idea of why the Nmap doesnt stick correctly to object ?
     
  2. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    I guess your sword mesh does not contain a secondary UV channel dedicated for the normal map. Though, it's fine (and simpler) to use the same UVs as the "Main Color" texture:
    Code (CSharp):
    1. sampler2D _MainTex;
    2. sampler2D _BumpMap;
    3. float4 _Color;
    4. struct Input {
    5.     float2 uv_MainTex : TEXCOORD0;
    6. };
    7. void surf (Input IN, inout SurfaceOutput o) {
    8.     half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    9.     o.Albedo = c.rgb;
    10.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
    11.     o.Alpha = c.a;
    12. }
     
  3. Yearl

    Yearl

    Joined:
    Jun 26, 2015
    Posts:
    33
    Thank You ! Works great !!