Advertisement
Guest User

Untitled

a guest
Jul 26th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Roystan/Toon"
  2. {
  3.     Properties
  4.     {
  5.         _Color("Color", Color) = (1,1,1,1)
  6.         _MainTex("Main Texture", 2D) = "white" {}
  7.         // Ambient light is applied uniformly to all surfaces on the object.
  8.         [HDR]
  9.         _AmbientColor("Ambient Color", Color) = (0.4,0.4,0.4,1)
  10.         [HDR]
  11.         _SpecularColor("Specular Color", Color) = (0.9,0.9,0.9,1)
  12.         // Controls the size of the specular reflection.
  13.         _Glossiness("Glossiness", Float) = 32
  14.         [HDR]
  15.         _RimColor("Rim Color", Color) = (1,1,1,1)
  16.         _RimAmount("Rim Amount", Range(0, 1)) = 0.716
  17.         // Control how smoothly the rim blends when approaching unlit
  18.         // parts of the surface.
  19.         _RimThreshold("Rim Threshold", Range(0, 1)) = 0.1      
  20.     }
  21.     SubShader
  22.     {
  23.         Tags
  24.         {
  25.             "Queue" = "Transparent"
  26.         }
  27.  
  28.         Pass
  29.         {
  30.             // Setup our pass to use Forward rendering, and only receive
  31.             // data on the main directional light and ambient light.
  32.             Tags
  33.             {
  34.                 "LightMode" = "ForwardBase"
  35.                 "PassFlags" = "OnlyDirectional"
  36.             }
  37.  
  38.             Blend SrcAlpha OneMinusSrcAlpha
  39.             ZWrite Off
  40.  
  41.             CGPROGRAM
  42.             #pragma vertex vert
  43.             #pragma fragment frag
  44.             // Compile multiple versions of this shader depending on lighting settings.
  45.             #pragma multi_compile_fwdbase
  46.            
  47.             #include "UnityCG.cginc"
  48.             // Files below include macros and functions to assist
  49.             // with lighting and shadows.
  50.             #include "Lighting.cginc"
  51.             #include "AutoLight.cginc"
  52.  
  53.             struct appdata
  54.             {
  55.                 float4 vertex : POSITION;              
  56.                 float4 uv : TEXCOORD0;
  57.                 float3 normal : NORMAL;
  58.             };
  59.  
  60.             struct v2f
  61.             {
  62.                 float4 pos : SV_POSITION;
  63.                 float3 worldNormal : NORMAL;
  64.                 float2 uv : TEXCOORD0;
  65.                 float3 viewDir : TEXCOORD1;
  66.                 // Macro found in Autolight.cginc. Declares a vector4
  67.                 // into the TEXCOORD2 semantic with varying precision
  68.                 // depending on platform target.
  69.                 SHADOW_COORDS(2)
  70.             };
  71.  
  72.             sampler2D _MainTex;
  73.             float4 _MainTex_ST;
  74.            
  75.             v2f vert (appdata v)
  76.             {
  77.                 v2f o;
  78.                 o.pos = UnityObjectToClipPos(v.vertex);
  79.                 o.worldNormal = UnityObjectToWorldNormal(v.normal);    
  80.                 o.viewDir = WorldSpaceViewDir(v.vertex);
  81.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  82.                 // Defined in Autolight.cginc. Assigns the above shadow coordinate
  83.                 // by transforming the vertex from world space to shadow-map space.
  84.                 TRANSFER_SHADOW(o)
  85.                 return o;
  86.             }
  87.            
  88.             float4 _Color;
  89.  
  90.             float4 _AmbientColor;
  91.  
  92.             float4 _SpecularColor;
  93.             float _Glossiness;     
  94.  
  95.             float4 _RimColor;
  96.             float _RimAmount;
  97.             float _RimThreshold;   
  98.  
  99.             float4 frag (v2f i) : SV_Target
  100.             {
  101.                 float3 normal = normalize(i.worldNormal);
  102.                 float3 viewDir = normalize(i.viewDir);
  103.  
  104.                 // Lighting below is calculated using Blinn-Phong,
  105.                 // with values thresholded to creat the "toon" look.
  106.                 // https://en.wikipedia.org/wiki/Blinn-Phong_shading_model
  107.  
  108.                 // Calculate illumination from directional light.
  109.                 // _WorldSpaceLightPos0 is a vector pointing the OPPOSITE
  110.                 // direction of the main directional light.
  111.                 float NdotL = dot(_WorldSpaceLightPos0, normal);
  112.  
  113.                 // Samples the shadow map, returning a value in the 0...1 range,
  114.                 // where 0 is in the shadow, and 1 is not.
  115.                 float shadow = SHADOW_ATTENUATION(i);
  116.                 // Partition the intensity into light and dark, smoothly interpolated
  117.                 // between the two to avoid a jagged break.
  118.                 float lightIntensity = smoothstep(0, 0.01, NdotL * shadow);
  119.                 // Multiply by the main directional light's intensity and color.
  120.                 float4 light = lightIntensity * _LightColor0;
  121.  
  122.                 // Calculate specular reflection.
  123.                 float3 halfVector = normalize(_WorldSpaceLightPos0 + viewDir);
  124.                 float NdotH = dot(normal, halfVector);
  125.                 // Multiply _Glossiness by itself to allow artist to use smaller
  126.                 // glossiness values in the inspector.
  127.                 float specularIntensity = pow(NdotH * lightIntensity, _Glossiness * _Glossiness);
  128.                 float specularIntensitySmooth = smoothstep(0.005, 0.01, specularIntensity);
  129.                 float4 specular = specularIntensitySmooth * _SpecularColor;            
  130.  
  131.                 // Calculate rim lighting.
  132.                 float rimDot = 1 - dot(viewDir, normal);
  133.                 // We only want rim to appear on the lit side of the surface,
  134.                 // so multiply it by NdotL, raised to a power to smoothly blend it.
  135.                 float rimIntensity = rimDot * pow(NdotL, _RimThreshold);
  136.                 rimIntensity = smoothstep(_RimAmount - 0.01, _RimAmount + 0.01, rimIntensity);
  137.                 float4 rim = rimIntensity * _RimColor;
  138.  
  139.                 float4 sample = tex2D(_MainTex, i.uv);
  140.  
  141.                 float4 color = (light + _AmbientColor + specular + rim) * _Color * sample;
  142.                 color.a = saturate(color.a);
  143.  
  144.                 return color;
  145.             }
  146.             ENDCG
  147.         }
  148.  
  149.         // Shadow casting support.
  150.         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement