Advertisement
Guest User

2DToon Shader

a guest
Jan 15th, 2018
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Shader "2DToon"
  3. {
  4.     Properties
  5.     {
  6.     [TCP2HeaderHelp(BASE, Base Properties)]
  7.         //TOONY COLORS
  8.         _Color ("Color", Color) = (1,1,1,1)
  9.         _HColor ("Highlight Color", Color) = (0.785,0.785,0.785,1.0)
  10.         _SColor ("Shadow Color", Color) = (0.195,0.195,0.195,1.0)
  11.  
  12.         //DIFFUSE
  13.         _MainTex ("Main Texture", 2D) = "white" {}
  14.     [TCP2Separator]
  15.  
  16.         //TOONY COLORS RAMP
  17.         [TCP2Header(RAMP SETTINGS)]
  18.  
  19.         _RampThreshold ("Ramp Threshold", Range(0,1)) = 0.5
  20.         _RampSmooth ("Ramp Smoothing", Range(0.001,1)) = 0.1
  21.     [TCP2Separator]
  22.  
  23.     [TCP2HeaderHelp(NORMAL MAPPING, Normal Bump Map)]
  24.         //BUMP
  25.         _BumpMap ("Normal map (RGB)", 2D) = "bump" {}
  26.     [TCP2Separator]
  27.  
  28.     [TCP2HeaderHelp(TRANSPARENCY)]
  29.         //Alpha Testing
  30.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
  31.     [TCP2Separator]
  32.  
  33.  
  34.         //Avoid compile error if the properties are ending with a drawer
  35.         [HideInInspector] __dummy__ ("unused", Float) = 0
  36.     }
  37.  
  38.     SubShader
  39.     {
  40.  
  41.         Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
  42.         Cull Off
  43.  
  44.         CGPROGRAM
  45.  
  46.         #pragma surface surf ToonyColorsCustom  exclude_path:deferred exclude_path:prepass
  47.         #pragma target 3.0
  48.  
  49.         //================================================================
  50.         // VARIABLES
  51.  
  52.         fixed4 _Color;
  53.         sampler2D _MainTex;
  54.         sampler2D _BumpMap;
  55.         fixed _Cutoff;
  56.  
  57.         struct Input
  58.         {
  59.             half2 uv_MainTex;
  60.             half2 uv_BumpMap;
  61.         };
  62.  
  63.         //================================================================
  64.         // CUSTOM LIGHTING
  65.  
  66.         //Lighting-related variables
  67.         fixed4 _HColor;
  68.         fixed4 _SColor;
  69.         half _RampThreshold;
  70.         half _RampSmooth;
  71.  
  72.         // Instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
  73.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
  74.         // #pragma instancing_options assumeuniformscaling
  75.         UNITY_INSTANCING_BUFFER_START(Props)
  76.             // put more per-instance properties here
  77.         UNITY_INSTANCING_BUFFER_END(Props)
  78.  
  79.         //Custom SurfaceOutput
  80.         struct SurfaceOutputCustom
  81.         {
  82.             half atten;
  83.             fixed3 Albedo;
  84.             fixed3 Normal;
  85.             fixed3 Emission;
  86.             half Specular;
  87.             fixed Gloss;
  88.             fixed Alpha;
  89.         };
  90.  
  91.         inline half4 LightingToonyColorsCustom (inout SurfaceOutputCustom s, half3 viewDir, UnityGI gi)
  92.         {
  93.         #define IN_NORMAL s.Normal
  94.    
  95.             half3 lightDir = gi.light.dir;
  96.         #if defined(UNITY_PASS_FORWARDBASE)
  97.             half3 lightColor = _LightColor0.rgb;
  98.             half atten = s.atten;
  99.         #else
  100.             half3 lightColor = gi.light.color.rgb;
  101.             half atten = 1;
  102.         #endif
  103.  
  104.             IN_NORMAL = normalize(IN_NORMAL);
  105.             fixed ndl = max(0, dot(IN_NORMAL, lightDir));
  106.             #define NDL ndl
  107.  
  108.             #define     RAMP_THRESHOLD  _RampThreshold
  109.             #define     RAMP_SMOOTH     _RampSmooth
  110.  
  111.             fixed3 ramp = smoothstep(RAMP_THRESHOLD - RAMP_SMOOTH*0.5, RAMP_THRESHOLD + RAMP_SMOOTH*0.5, NDL);
  112.         #if !(POINT) && !(SPOT)
  113.             ramp *= atten;
  114.         #endif
  115.         #if !defined(UNITY_PASS_FORWARDBASE)
  116.             _SColor = fixed4(0,0,0,1);
  117.         #endif
  118.             _SColor = lerp(_HColor, _SColor, _SColor.a);    //Shadows intensity through alpha
  119.             ramp = lerp(_SColor.rgb, _HColor.rgb, ramp);
  120.             fixed4 c;
  121.             c.rgb = s.Albedo * lightColor.rgb * ramp;
  122.             c.a = s.Alpha;
  123.  
  124.         #ifdef UNITY_LIGHT_FUNCTION_APPLY_INDIRECT
  125.             c.rgb += s.Albedo * gi.indirect.diffuse;
  126.         #endif
  127.  
  128.             return c;
  129.         }
  130.  
  131.         void LightingToonyColorsCustom_GI(inout SurfaceOutputCustom s, UnityGIInput data, inout UnityGI gi)
  132.         {
  133.             gi = UnityGlobalIllumination(data, 1.0, IN_NORMAL);
  134.  
  135.             s.atten = data.atten;   //transfer attenuation to lighting function
  136.             gi.light.color = _LightColor0.rgb;  //remove attenuation
  137.         }
  138.  
  139.         //================================================================
  140.         // SURFACE FUNCTION
  141.  
  142.         void surf(Input IN, inout SurfaceOutputCustom o)
  143.         {
  144.             fixed4 mainTex = tex2D(_MainTex, IN.uv_MainTex);
  145.             o.Albedo = mainTex.rgb * _Color.rgb;
  146.             o.Alpha = mainTex.a * _Color.a;
  147.    
  148.             //Cutout (Alpha Testing)
  149.             clip (o.Alpha - _Cutoff);
  150.  
  151.             //Normal map
  152.             half4 normalMap = tex2D(_BumpMap, IN.uv_BumpMap.xy);
  153.             o.Normal = UnpackNormal(normalMap);
  154.         }
  155.  
  156.         ENDCG
  157.     }
  158.  
  159.     Fallback "Diffuse"
  160.     CustomEditor "TCP2_MaterialInspector_SG"
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement