torch_in_sky

Cel-shader

Jan 12th, 2020
2,885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.20 KB | None | 0 0
  1. Shader "Unlit/CelShading"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Texture", 2D) = "white" {}
  6.         _ShadowTint("Shadow Tint", Color) = (1,1,1,1)
  7.         _OutlineWidth ("Outline Width", Range(0, 0.1)) = 0.01
  8.     }
  9.  
  10.     SubShader
  11.     {
  12.         Tags { "RenderType"="Opaque" }
  13.         LOD 100
  14.  
  15.         Pass
  16.         {
  17.             Tags
  18.             {
  19.                 "LightMode" = "ForwardBase"
  20.                 "PassFlags" = "OnlyDirectional"
  21.             }
  22.            
  23.             Cull Off
  24.  
  25.             Stencil
  26.             {
  27.                 Ref 1
  28.                 Comp Always
  29.                 Pass Replace
  30.             }
  31.  
  32.             CGPROGRAM
  33.             #pragma vertex vert
  34.             #pragma fragment frag
  35.  
  36.             #include "UnityCG.cginc"
  37.  
  38.             struct appdata
  39.             {
  40.                 float4 vertex : POSITION;
  41.                 float2 uv : TEXCOORD0;
  42.                 float3 normal : NORMAL;
  43.                 half4 color : COLOR;
  44.             };
  45.  
  46.             struct v2f
  47.             {
  48.                 float2 uv : TEXCOORD0;
  49.                 float4 vertex : SV_POSITION;
  50.                 float3 worldNormal : NORMAL;
  51.                 half4 color : COLOR;
  52.                 float3 viewDir : TEXCOORD1;
  53.             };
  54.  
  55.             sampler2D _MainTex;
  56.             float4 _MainTex_ST;
  57.             half4 _ShadowTint;
  58.  
  59.             v2f vert (appdata v)
  60.             {
  61.                 v2f o;
  62.                 o.vertex = UnityObjectToClipPos(v.vertex);
  63.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  64.                 o.worldNormal = UnityObjectToWorldNormal(v.normal);
  65.                 o.color = v.color;
  66.                 o.viewDir = WorldSpaceViewDir(v.vertex);
  67.                 return o;
  68.             }
  69.  
  70.             static const half4 SPECULAR_COLOR = half4(1, 1, 1, 1);
  71.  
  72.             fixed4 frag (v2f i, half facing : VFACE) : SV_Target
  73.             {
  74.                 float3 normal = normalize(i.worldNormal);
  75.                 half sign = facing > 0.5 ? 1.0 : -1.0;
  76.                 normal *= sign;
  77.                 float NdotL = dot(_WorldSpaceLightPos0, normal);
  78.                 float NdotL01 = NdotL * 0.5 + 0.5;
  79.                 half shadowMask = 1 - step(1 - i.color.r, NdotL01);
  80.                 float3 viewDir = normalize(i.viewDir);
  81.                 float3 halfVector = normalize(_WorldSpaceLightPos0 + viewDir);
  82.                 float NdotH = saturate(dot(halfVector, normal));
  83.                 float specularIntensity = i.color.g == 0 ? 0 : pow(NdotH, i.color.g * 500);
  84.                 half specularMask = step(0.5, specularIntensity);
  85.                 specularMask *= (1 - shadowMask);
  86.                 fixed4 texCol = tex2D(_MainTex, i.uv);
  87.                 half4 shadowCol = texCol * shadowMask * _ShadowTint;
  88.                 half4 col = lerp(texCol, shadowCol, shadowMask);
  89.                 col = lerp(col, SPECULAR_COLOR, specularMask);
  90.                 return col;
  91.             }
  92.             ENDCG
  93.         }
  94.  
  95.         Pass
  96.         {  
  97.             Cull Off
  98.  
  99.             Stencil
  100.             {
  101.                 Ref 1
  102.                 Comp Greater
  103.             }
  104.  
  105.             CGPROGRAM
  106.             #pragma vertex vert
  107.             #pragma fragment frag
  108.  
  109.             #include "UnityCG.cginc"
  110.  
  111.             struct appdata
  112.             {
  113.                 float4 vertex : POSITION;
  114.                 float3 normal : NORMAL;
  115.                 half4 color : COLOR;
  116.             };
  117.  
  118.             struct v2f
  119.             {
  120.                 float4 vertex : SV_POSITION;
  121.             };
  122.  
  123.             half _OutlineWidth;
  124.             static const half4 OUTLINE_COLOR = half4(0,0,0,0);
  125.  
  126.             v2f vert (appdata v)
  127.             {
  128.                 float4 clipPosition = UnityObjectToClipPos(v.vertex);
  129.                 float3 clipNormal = mul((float3x3) UNITY_MATRIX_VP, mul((float3x3) UNITY_MATRIX_M, v.normal));
  130.                 float2 offset = normalize(clipNormal.xy) * _OutlineWidth * clipPosition.w * v.color.b;
  131.                 float aspect = _ScreenParams.x / _ScreenParams.y;
  132.                 offset.y *= aspect;
  133.                 clipPosition.xy += offset;
  134.                 v2f o;
  135.                 o.vertex = clipPosition;
  136.                 return o;
  137.             }
  138.  
  139.             fixed4 frag () : SV_Target
  140.             {
  141.                 return OUTLINE_COLOR;
  142.             }
  143.             ENDCG
  144.         }
  145.  
  146.         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment