Search Unity

Cel-Shaded or Toon look

Discussion in 'Shaders' started by j_spencer93, Nov 8, 2018.

  1. j_spencer93

    j_spencer93

    Joined:
    Dec 17, 2015
    Posts:
    60
    I have never messed with the lighting in Unity or Shaders before. I want to make a Toon or Cel-Shaded look but what i am finding online says one of two options: use the built in Toon Shader or Make my own and apply it to each model individually. The question is, how do i change over to the Toon Shader? And is there a way to apply a custom shader to everything at once?
     
  2. Quakeulf

    Quakeulf

    Joined:
    Mar 26, 2013
    Posts:
    40
    You can play around with this shader I am working on. It might be working out for you:

    Code (CSharp):
    1. Shader "Toon/Lithe"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Colour", Color) = (1,1,1,1)
    6.         _RimColor ("Rimlight Colour", Color) = (0,0,0,0.0)
    7.           _RimPower ("Rimlight Power", Range(0,10)) = 3.0
    8.         _MainTex ("Main Texture (RGB)", 2D) = "gray" {}
    9.         _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
    10.         _MinDistance ("Minimum Distance", Float) = 0
    11.         _MaxDistance ("Maximum Distance", Float) = 100  
    12.         _Treshold ("Cel treshold", Range(1., 20.)) = 5.
    13.         _Ambient ("Ambient intensity", Range(0., 0.5)) = 0.1  
    14.     }
    15.  
    16.     SubShader
    17.     {
    18.         Tags { "RenderType"="Opaque" }
    19.         LOD 200
    20.         Cull Back
    21.        
    22.         CGPROGRAM
    23.         // custom lighting function that uses a texture ramp based
    24.         // on angle between light direction and normal
    25.         // THIS ONLY SEEMS TO WORK FOR SKINNED MESH RENDERERS IN FORWARD LIGHTING
    26.         // DEFERRED LIGHTING DOES NOT WORK
    27.         #pragma surface surf ToonRamp
    28.         #pragma lighting ToonRamp exclude_path:prepass
    29.         #pragma multi_compile_instancing
    30.  
    31.         sampler2D _Ramp;      
    32.         sampler2D _MainTex;
    33.  
    34.         UNITY_INSTANCING_BUFFER_START(Props)
    35.             UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
    36.             UNITY_DEFINE_INSTANCED_PROP(fixed4, _RimColor)
    37.             UNITY_DEFINE_INSTANCED_PROP(fixed, _RimPower)
    38.             UNITY_DEFINE_INSTANCED_PROP(fixed, _MinDistance)
    39.             UNITY_DEFINE_INSTANCED_PROP(fixed, _MaxDistance)
    40.             UNITY_DEFINE_INSTANCED_PROP(fixed, _Treshold)
    41.             UNITY_DEFINE_INSTANCED_PROP(fixed, _Ambient)
    42.         UNITY_INSTANCING_BUFFER_END(Props)
    43.  
    44.         struct Input
    45.         {
    46.             fixed2 uv_MainTex : TEXCOORD0;
    47.             float3 viewDir;
    48.             float3 worldPos;
    49.             UNITY_VERTEX_INPUT_INSTANCE_ID
    50.         };
    51.        
    52.         inline fixed4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
    53.         {          
    54.             #ifndef USING_DIRECTIONAL_LIGHT
    55.             lightDir = normalize(lightDir);
    56.             #endif
    57.            
    58.             fixed d = (dot (s.Normal, lightDir) * 0.5 + 0.5) * atten;
    59.             fixed3 ramp = lerp(tex2D (_Ramp, fixed2(d,d)).rgb, fixed3(1.0, 1.0, 1.0), 1.0 - s.Alpha);
    60.  
    61.             //ramp *= ramp;
    62.  
    63.             fixed4 c;
    64.             // Use s.Alpha here to get some sort of night-torch effect:
    65.             c.rgb = min(ramp, s.Albedo) * _LightColor0.rgb; // s.Albedo * (_LightColor0.rgb * _LightColor0.rgb) * ramp; // * s.Alpha; // * (atten * 2);
    66.             c.a = _LightColor0.a;
    67.             return c;
    68.         }
    69.  
    70.         float LightToonShading(float3 normal, float3 lightDir)
    71.         {
    72.             fixed treshold = UNITY_ACCESS_INSTANCED_PROP(Props, _Treshold);
    73.  
    74.             fixed NdotL = max(0.0, dot(normalize(normal), normalize(lightDir)));
    75.             return floor(NdotL * treshold) / (treshold - 0.5);
    76.         }
    77.  
    78.         void surf (Input IN, inout SurfaceOutput o)
    79.         {
    80.             fixed4 colour = UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
    81.             fixed startFade = UNITY_ACCESS_INSTANCED_PROP(Props, _MinDistance);
    82.             fixed endFade = UNITY_ACCESS_INSTANCED_PROP(Props, _MaxDistance);
    83.  
    84.             fixed dist = distance(_WorldSpaceCameraPos, IN.worldPos);
    85.             fixed weight = saturate( (dist - startFade) / (endFade - startFade));
    86.  
    87.             fixed4 rimColour = UNITY_ACCESS_INSTANCED_PROP(Props, _RimColor);
    88.             fixed rimPower = UNITY_ACCESS_INSTANCED_PROP(Props, _RimPower);
    89.            
    90.             fixed rim = 1.0 - saturate(dot (normalize(IN.viewDir), normalize(o.Normal)));
    91.  
    92.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * colour;
    93.             //fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    94.  
    95.             //c.rgb *= saturate(LightToonShading(o.Normal, _WorldSpaceLightPos0.xyz) + UNITY_ACCESS_INSTANCED_PROP(Props, _Ambient)) * _LightColor0.rgb;
    96.  
    97.             o.Albedo = c.rgb + (rimColour.rgb * pow(rim, rimPower));
    98.             o.Alpha = c.a * ((weight * weight));
    99.  
    100.             //o.Emission = o.Normal * c.rgb * IN.viewDir;
    101.               //o.Emission = c.rgb * c.rgb; // * _LightColor0.rgb; // rimColour.rgb * pow(rim, rimPower) * c.rgb;
    102.         }
    103.         ENDCG
    104.     }
    105.  
    106.     Fallback "Diffuse"
    107. }
    108.  
    This is the effect it gets:

     
  3. j_spencer93

    j_spencer93

    Joined:
    Dec 17, 2015
    Posts:
    60
    Um...once i applied this to a model to see how it looked it just turned them white.
     
  4. Quakeulf

    Quakeulf

    Joined:
    Mar 26, 2013
    Posts:
    40
    Did you apply a texture and a toonramp? The toonramp is usually a horizontal gradient from left to right being dark to light. It is based on ToonLitOutline which has been a part of the Unity files you can add to a project.
     
  5. j_spencer93

    j_spencer93

    Joined:
    Dec 17, 2015
    Posts:
    60
    Lol when i said i was new at this, i meant i knew nothing about shading. Thank you