Advertisement
tonynogo

Demo 77 - Cel shading formula

Jul 6th, 2017
9,844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Lighting/CelShader"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Texture", 2D) = "white" {}
  6.         _Treshold ("Cel treshold", Range(1., 20.)) = 5.
  7.         _Ambient ("Ambient intensity", Range(0., 0.5)) = 0.1
  8.     }
  9.     SubShader
  10.     {
  11.         Tags { "RenderType"="Opaque" "LightMode"="ForwardBase" }
  12.  
  13.         Pass
  14.         {
  15.             CGPROGRAM
  16.             #pragma vertex vert
  17.             #pragma fragment frag
  18.            
  19.             #include "UnityCG.cginc"
  20.  
  21.             struct v2f
  22.             {
  23.                 float4 pos : SV_POSITION;
  24.                 float2 uv : TEXCOORD0;
  25.                 float3 worldNormal : NORMAL;
  26.             };
  27.  
  28.             float _Treshold;
  29.  
  30.             float LightToonShading(float3 normal, float3 lightDir)
  31.             {
  32.                 float NdotL = max(0.0, dot(normalize(normal), normalize(lightDir)));
  33.                 return floor(NdotL * _Treshold) / (_Treshold - 0.5);
  34.             }
  35.  
  36.             sampler2D _MainTex;
  37.             float4 _MainTex_ST;
  38.  
  39.             v2f vert (appdata_full v)
  40.             {
  41.                 v2f o;
  42.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  43.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  44.                 o.worldNormal = mul(v.normal.xyz, (float3x3) unity_WorldToObject);
  45.                 return o;
  46.             }
  47.  
  48.             fixed4 _LightColor0;
  49.             half _Ambient;
  50.  
  51.             fixed4 frag (v2f i) : SV_Target
  52.             {
  53.                 fixed4 col = tex2D(_MainTex, i.uv);
  54.                 col.rgb *= saturate(LightToonShading(i.worldNormal, _WorldSpaceLightPos0.xyz) + _Ambient) * _LightColor0.rgb;
  55.                 return col;
  56.             }
  57.             ENDCG
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement