Advertisement
Guest User

CharacterToon.shader

a guest
Nov 13th, 2019
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Shader "Toon/Character" {
  3.     Properties{
  4.         _Color("Main Color", Color) = (0.5,0.5,0.5,1)
  5.         _MainTex("Base (RGB)", 2D) = "white" {}
  6.         _Offset("Toon Ramp Blur",  Range(0, 1)) = 0
  7.         _Mask("R = Emis, G = Spec", 2D) = "black" {}
  8.         _SpecTint("Spec Tint", Color) = (1,1,1,1)
  9.         _HitTint("Hit Tint", Color) = (1,1,1,1)
  10.         [PerRendererData] _Hit("Hit", Range(0, 1)) = 0 // hide in inspector
  11.  
  12.     }
  13.  
  14.         SubShader{
  15.             Tags{ "RenderType" = "Opaque" }
  16.             LOD 200
  17.             Cull Off
  18.  
  19.             CGPROGRAM
  20.  
  21.         #pragma surface surf ToonRamp fullforwardshadows addshadow
  22.  
  23.             float _Offset;
  24.            
  25.             // custom lighting function based
  26.             // on angle between light direction and normal
  27.         #pragma lighting ToonRamp //exclude_path:prepass
  28.             inline half4 LightingToonRamp(SurfaceOutput s, half3 lightDir, half atten)
  29.             {
  30.         #ifndef USING_DIRECTIONAL_LIGHT
  31.                 lightDir = normalize(lightDir);
  32.         #endif
  33.                 float d = dot(s.Normal, lightDir);
  34.                 float3 lightIntensity = smoothstep(0 , fwidth(d) + _Offset , d);
  35.  
  36.                 half4 c;
  37.                 c.rgb = s.Albedo * _LightColor0.rgb * lightIntensity * (atten * 2);
  38.                 c.a = s.Alpha;
  39.                 return c;
  40.             }
  41.  
  42.  
  43.             sampler2D _MainTex;
  44.             float4 _Color, _HitTint;
  45.             sampler2D _Mask;
  46.             float4 _SpecTint;
  47.  
  48.             struct Input {
  49.                 float2 uv_MainTex : TEXCOORD0;
  50.                 float3 viewDir;
  51.             };
  52.  
  53.             // property blocks for hit effects
  54.             UNITY_INSTANCING_BUFFER_START(Props)
  55.                 UNITY_DEFINE_INSTANCED_PROP(float, _Hit)
  56.             UNITY_INSTANCING_BUFFER_END(Props)
  57.  
  58.  
  59.             void surf(Input IN, inout SurfaceOutput o) {
  60.                 // main texture
  61.                 half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
  62.  
  63.                 // effects mask
  64.                 half4 e = tex2D(_Mask, IN.uv_MainTex);
  65.  
  66.                 // spec
  67.                 float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
  68.                 float spec = dot(IN.viewDir, o.Normal);// specular based on view and light direction
  69.                 float cutOff = step(saturate(spec), 0.8); // cutoff for where base color is
  70.                 float3 specularMain = c.rgb * (1 - cutOff) * e.g * _SpecTint * 4;// inverted base cutoff times specular color
  71.  
  72.                 // highlight
  73.                 float highlight = saturate(dot(normalize(lightDir + (IN.viewDir * 0.5)), o.Normal)); // highlight based on light direction
  74.                 float3 highlightMain = (step(0.9,highlight) * c.rgb *_SpecTint * 2) * e.g; //glowing highlight
  75.  
  76.                 // rim
  77.                 half rim = 1 - saturate(dot(normalize(IN.viewDir), o.Normal));// standard rim calculation  
  78.  
  79.                 // emissive glow based on red channel
  80.                 o.Emission = e.r * (c.rgb * 2);
  81.  
  82.                 // add a glow via the specular green channel as well
  83.                 o.Emission += (pow(rim, 7) * e.g * c.rgb* _SpecTint * 5);
  84.                 // hit effect, power 2 is how much model is covered, higher number is less coverage
  85.                 o.Emission += (pow(rim, 2) * (UNITY_ACCESS_INSTANCED_PROP(Props, _Hit) * 2 * _HitTint));
  86.                
  87.                 // glow of dissolve
  88.                 o.Emission += highlightMain;
  89.  
  90.                 // final color
  91.                 o.Albedo = c.rgb + specularMain;
  92.  
  93.                 // main rim
  94.                 // rim on the lit side
  95.                 float DotLight = dot(lightDir, o.Normal);
  96.                 // blend with normal rim
  97.                 float rimIntensity = rim * pow(DotLight, 0.1);
  98.                 // cutoff
  99.                 rimIntensity = smoothstep(0.7,fwidth(rimIntensity) + 0.7, rimIntensity);
  100.  
  101.                 // add strong rim
  102.                 o.Albedo += (rimIntensity * 2 * c.rgb);
  103.  
  104.        
  105.             }
  106.             ENDCG
  107.  
  108.         }
  109.  
  110.             Fallback "Diffuse"
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement