Advertisement
Guest User

Untitled

a guest
Oct 4th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Toony Colors Pro+Mobile Shaders
  2. // (c) 2014,2015 Jean Moreno
  3.  
  4. #ifndef TOONYCOLORS_INCLUDED
  5.     #define TOONYCOLORS_INCLUDED
  6.    
  7.     #if TCP2_RAMPTEXT
  8.         //Lighting Ramp
  9.         sampler2D _Ramp;
  10.     #else
  11.         float _RampThreshold;
  12.         float _RampSmooth;
  13.     #endif
  14.    
  15.     #if TCP2_SPEC_TOON
  16.         fixed _SpecSmooth;
  17.     #endif
  18.    
  19.     //Highlight/Shadow Colors
  20.     fixed4 _HColor;
  21.     fixed4 _SColor;
  22.    
  23. #endif
  24.  
  25. //================================================================================================================================
  26. // FORWARD PATH
  27. //--------------------------------------------------------------------------------------------------------------------------------
  28. // TOONY COLORS -- REGULAR
  29. inline half4 LightingToonyColors (SurfaceOutput s, half3 lightDir, half atten)
  30. {
  31.     fixed ndl = max(0, dot(s.Normal, lightDir)*0.5 + 0.5);
  32. #if TCP2_RAMPTEXT
  33.     fixed3 ramp = tex2D(_Ramp, fixed2(ndl,ndl));
  34. #else
  35.     fixed3 ramp = smoothstep(_RampThreshold-_RampSmooth*0.5, _RampThreshold+_RampSmooth*0.5, ndl);
  36. #endif
  37. #if !(POINT) && !(SPOT)
  38.     ramp *= atten;
  39. #endif
  40.     _SColor = lerp(_HColor, _SColor, _SColor.a);    //Shadows intensity through alpha
  41.     ramp = lerp(_SColor.rgb,_HColor.rgb,ramp);
  42.     fixed4 c;
  43.     c.rgb = s.Albedo * _LightColor0.rgb * ramp;
  44. #if (POINT || SPOT)
  45.     c.rgb *= atten;
  46. #endif
  47.     c.a = s.Alpha;
  48.     return c;
  49. }
  50. //--------------------------------------------------------------------------------------------------------------------------------
  51. // TOONY COLORS -- REGULAR + SPECULAR
  52. inline half4 LightingToonyColorsSpec (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
  53. {
  54.     s.Normal = normalize(s.Normal);
  55.     fixed ndl = max(0, dot(s.Normal, lightDir)*0.5 + 0.5);
  56. #if TCP2_RAMPTEXT
  57.     fixed3 ramp = tex2D(_Ramp, fixed2(ndl,ndl));
  58. #else
  59.     fixed3 ramp = smoothstep(_RampThreshold-_RampSmooth*0.5, _RampThreshold+_RampSmooth*0.5, ndl);
  60. #endif
  61. #if !(POINT) && !(SPOT)
  62.     ramp *= atten;
  63. #endif
  64.     _SColor = lerp(_HColor, _SColor, _SColor.a);    //Shadows intensity through alpha
  65.     ramp = lerp(_SColor.rgb,_HColor.rgb,ramp);
  66.     //Specular
  67.     half3 h = normalize(lightDir + viewDir);
  68.     float ndh = max(0, dot(s.Normal, h));
  69.     float spec = pow(ndh, s.Specular*128.0) * s.Gloss * 2.0;
  70. #if TCP2_SPEC_TOON
  71.     spec = smoothstep(0.5-_SpecSmooth*0.5, 0.5+_SpecSmooth*0.5, spec);
  72. #endif
  73.     spec *= atten;
  74.     fixed4 c;
  75.     c.rgb = s.Albedo * _LightColor0.rgb * ramp;
  76. #if (POINT || SPOT)
  77.     c.rgb *= atten;
  78. #endif
  79.     c.rgb += _LightColor0.rgb * _SpecColor.rgb * spec;
  80.     c.a = s.Alpha + _LightColor0.a * _SpecColor.a * spec;
  81.    
  82.     return c;
  83. }
  84. //--------------------------------------------------------------------------------------------------------------------------------
  85. // TOONY COLORS -- REGULAR LIGHTMAPS
  86. #if TCP2_LIGHTMAP
  87.     inline fixed4 LightingToonyColors_SingleLightmap (SurfaceOutput s, fixed4 color)
  88.     {
  89.         half3 lm = DecodeLightmap(color);
  90.        
  91.         float lum = Luminance(lm);
  92.     #if TCP2_RAMPTEXT
  93.         fixed3 ramp = tex2D(_Ramp, fixed2(lum,lum));
  94.     #else
  95.         fixed3 ramp = smoothstep(_RampThreshold-_RampSmooth*0.5, _RampThreshold+_RampSmooth*0.5, lum);
  96.     #endif
  97.         _SColor = lerp(_HColor, _SColor, _SColor.a);    //Shadows intensity through alpha
  98.         ramp = lerp(_SColor.rgb,_HColor.rgb,ramp);
  99.         lm *= ramp * 2;
  100.        
  101.         return fixed4(lm, 0);
  102.     }
  103.  
  104.     inline fixed4 LightingToonyColors_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade)
  105.     {
  106.         half3 lm = lerp(DecodeLightmap(indirectOnlyColor), DecodeLightmap(totalColor), indirectFade);
  107.        
  108.         float lum = Luminance(lm);
  109.     #if TCP2_RAMPTEXT
  110.         fixed3 ramp = tex2D(_Ramp, fixed2(lum,lum));
  111.     #else
  112.         fixed3 ramp = smoothstep(_RampThreshold-_RampSmooth*0.5, _RampThreshold+_RampSmooth*0.5, lum);
  113.     #endif
  114.         _SColor = lerp(_HColor, _SColor, _SColor.a);    //Shadows intensity through alpha
  115.         ramp = lerp(_SColor.rgb,_HColor.rgb,ramp);
  116.         lm *= ramp * 2;
  117.        
  118.         return fixed4(lm, 0);
  119.     }
  120.  
  121.     inline fixed4 LightingToonyColors_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, bool surfFuncWritesNormal)
  122.     {
  123.         UNITY_DIRBASIS
  124.         half3 scalePerBasisVector;
  125.        
  126.         half3 lm = DirLightmapDiffuse(unity_DirBasis, color, scale, s.Normal, surfFuncWritesNormal, scalePerBasisVector);
  127.        
  128.         float lum = Luminance(lm);
  129.     #if TCP2_RAMPTEXT
  130.         fixed3 ramp = tex2D(_Ramp, fixed2(lum,lum));
  131.     #else
  132.         fixed3 ramp = smoothstep(_RampThreshold-_RampSmooth*0.5, _RampThreshold+_RampSmooth*0.5, lum);
  133.     #endif
  134.         _SColor = lerp(_HColor, _SColor, _SColor.a);    //Shadows intensity through alpha
  135.         ramp = lerp(_SColor.rgb,_HColor.rgb,ramp);
  136.         lm *= ramp * 2;
  137.        
  138.         return half4(lm, 0);
  139.     }
  140. #endif
  141. //--------------------------------------------------------------------------------------------------------------------------------
  142. // TOONY COLORS -- SPECULAR LIGHTMAPS
  143. #if TCP2_LIGHTMAP
  144.     inline fixed4 LightingToonyColorsSpec_SingleLightmap (SurfaceOutput s, fixed4 color)
  145.     {
  146.         half3 lm = DecodeLightmap(color);
  147.        
  148.         float lum = Luminance(lm);
  149.     #if TCP2_RAMPTEXT
  150.         fixed3 ramp = tex2D(_Ramp, fixed2(lum,lum));
  151.     #else
  152.         fixed3 ramp = smoothstep(_RampThreshold-_RampSmooth*0.5, _RampThreshold+_RampSmooth*0.5, lum);
  153.     #endif
  154.         _SColor = lerp(_HColor, _SColor, _SColor.a);    //Shadows intensity through alpha
  155.         ramp = lerp(_SColor.rgb,_HColor.rgb,ramp);
  156.         lm *= ramp * 2;
  157.        
  158.         return fixed4(lm, 0);
  159.     }
  160.  
  161.     inline fixed4 LightingToonyColorsSpec_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade)
  162.     {
  163.         half3 lm = lerp(DecodeLightmap(indirectOnlyColor), DecodeLightmap(totalColor), indirectFade);
  164.        
  165.         float lum = Luminance(lm);
  166.     #if TCP2_RAMPTEXT
  167.         fixed3 ramp = tex2D(_Ramp, fixed2(lum,lum));
  168.     #else
  169.         fixed3 ramp = smoothstep(_RampThreshold-_RampSmooth*0.5, _RampThreshold+_RampSmooth*0.5, lum);
  170.     #endif
  171.         _SColor = lerp(_HColor, _SColor, _SColor.a);    //Shadows intensity through alpha
  172.         ramp = lerp(_SColor.rgb,_HColor.rgb,ramp);
  173.         lm *= ramp * 2;
  174.        
  175.         return fixed4(lm, 0);
  176.     }
  177.  
  178.     inline fixed4 LightingToonyColorsSpec_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, half3 viewDir, bool surfFuncWritesNormal, out half3 specColor)
  179.     {
  180.         UNITY_DIRBASIS
  181.         half3 scalePerBasisVector;
  182.        
  183.         half3 lm = DirLightmapDiffuse(unity_DirBasis, color, scale, s.Normal, surfFuncWritesNormal, scalePerBasisVector);
  184.        
  185.         half3 lightDir = normalize(scalePerBasisVector.x * unity_DirBasis[0] + scalePerBasisVector.y * unity_DirBasis[1] + scalePerBasisVector.z * unity_DirBasis[2]);
  186.         half3 h = normalize(lightDir + viewDir);
  187.        
  188.         float nh = max(0, dot(s.Normal, h));
  189.         float spec = pow(nh, s.Specular * 128.0);
  190.        
  191.         // specColor used outside in the forward path, compiled out in prepass
  192.         specColor = lm * _SpecColor.rgb * s.Gloss * spec;
  193.        
  194.         float lum = Luminance(lm);
  195.     #if TCP2_RAMPTEXT
  196.         fixed3 ramp = tex2D(_Ramp, fixed2(lum,lum));
  197.     #else
  198.         fixed3 ramp = smoothstep(_RampThreshold-_RampSmooth*0.5, _RampThreshold+_RampSmooth*0.5, lum);
  199.     #endif
  200.         _SColor = lerp(_HColor, _SColor, _SColor.a);    //Shadows intensity through alpha
  201.         ramp = lerp(_SColor.rgb,_HColor.rgb,ramp);
  202.         lm *= ramp * 2;
  203.        
  204.         // spec from the alpha component is used to calculate specular
  205.         // in the Lighting*_Prepass function, it's not used in forward
  206.         return half4(lm, spec);
  207.     }
  208. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement