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

Specular(rgb) map and emissive(rgb) map on my shader

Discussion in 'Shaders' started by Yearl, Mar 18, 2016.

  1. Yearl

    Yearl

    Joined:
    Jun 26, 2015
    Posts:
    33
    Hi everybody,
    I'm trying to add a specular map on my modified ToonShader based on rgb,
    so if I put a rainbow map on specular my mesh will shine multicolored !

    Here my script (specular doesnt working ...)

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

    Anyone can help ?
    Thx in advance !!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,248
    Because your custom light function doesn't use specular. Usually Unity's lighting functions handle the specular color and emission color, but since you're using a custom one you have to implement that yourself.

    In the case of emission, that's easy. Just add your emission map, set it to o.Emissive, then c.rgb += s.Emissive; in the light function.

    Specular is a little harder since you actually have to decide how you want that to work for your toon shader. In a normal blinn-phong you would be using pow() on a half angle dot product to create the specular highlight and multiplying that by your specular color, but I have no idea what look you want.
     
  3. Yearl

    Yearl

    Joined:
    Jun 26, 2015
    Posts:
    33
    Ok ... So for emissive I think I can do it, but for specular, I don't did the toon part and I'm a big beginner in shaders ...
    Can you give me an exemple to set the specMap ?