Guest User

Untitled

a guest
Feb 12th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. Shader "CubedParadox/Flat Lit Toon stencil"
  2. {
  3. Properties
  4. {
  5. _MainTex("MainTex", 2D) = "white" {}
  6. _Color("Color", Color) = (1,1,1,1)
  7. _ColorMask("ColorMask", 2D) = "black" {}
  8. _Shadow("Shadow", Range(0, 1)) = 0.4
  9. _outline_width("outline_width", Float) = 0.2
  10. _outline_color("outline_color", Color) = (0.5,0.5,0.5,1)
  11. _outline_tint("outline_tint", Range(0, 1)) = 0.5
  12. _EmissionMap("Emission Map", 2D) = "white" {}
  13. [HDR]_EmissionColor("Emission Color", Color) = (0,0,0,1)
  14. _BumpMap("BumpMap", 2D) = "bump" {}
  15. _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
  16.  
  17. // Blending state
  18. [HideInInspector] _Mode ("__mode", Float) = 0.0
  19. [HideInInspector] _OutlineMode("__outline_mode", Float) = 0.0
  20. [HideInInspector] _SrcBlend ("__src", Float) = 1.0
  21. [HideInInspector] _DstBlend ("__dst", Float) = 0.0
  22. [HideInInspector] _ZWrite ("__zw", Float) = 1.0
  23. }
  24.  
  25. SubShader
  26. {
  27. Tags
  28. {
  29. "RenderType" = "Opaque"
  30. }
  31.  
  32. Pass
  33. {
  34.  
  35. Stencil
  36. {
  37. Ref 1
  38. Comp equal
  39. Pass keep
  40. }
  41.  
  42. Name "FORWARD"
  43. Tags { "LightMode" = "ForwardBase" }
  44.  
  45. Blend [_SrcBlend] [_DstBlend]
  46. ZWrite [_ZWrite]
  47.  
  48. CGPROGRAM
  49. #include "FlatLitToonCore.cginc"
  50. #pragma shader_feature NO_OUTLINE TINTED_OUTLINE COLORED_OUTLINE
  51. #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
  52. #pragma vertex vert
  53. #pragma geometry geom
  54. #pragma fragment frag
  55.  
  56. #pragma only_renderers d3d11 glcore gles
  57. #pragma target 4.0
  58.  
  59. #pragma multi_compile_fwdbase
  60. #pragma multi_compile_fog
  61.  
  62. float4 frag(VertexOutput i) : COLOR
  63. {
  64. float4 objPos = mul(unity_ObjectToWorld, float4(0,0,0,1));
  65. i.normalDir = normalize(i.normalDir);
  66. float3x3 tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.normalDir);
  67. float3 _BumpMap_var = UnpackNormal(tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap)));
  68. float3 normalDirection = normalize(mul(_BumpMap_var.rgb, tangentTransform)); // Perturbed normals
  69. float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
  70.  
  71. float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
  72. float3 lightColor = _LightColor0.rgb;
  73. UNITY_LIGHT_ATTENUATION(attenuation, i, i.posWorld.xyz);
  74.  
  75. float4 _EmissionMap_var = tex2D(_EmissionMap,TRANSFORM_TEX(i.uv0, _EmissionMap));
  76. float3 emissive = (_EmissionMap_var.rgb*_EmissionColor.rgb);
  77. float4 _ColorMask_var = tex2D(_ColorMask,TRANSFORM_TEX(i.uv0, _ColorMask));
  78. float4 baseColor = lerp((_MainTex_var.rgba*_Color.rgba),_MainTex_var.rgba,_ColorMask_var.r);
  79. baseColor *= float4(i.col.rgb, 1);
  80.  
  81. #if COLORED_OUTLINE
  82. if(i.is_outline)
  83. {
  84. baseColor.rgb = i.col.rgb;
  85. }
  86. #endif
  87.  
  88. #if defined(_ALPHATEST_ON)
  89. clip (baseColor.a - _Cutoff);
  90. #endif
  91.  
  92. float3 lightmap = float4(1.0,1.0,1.0,1.0);
  93. #ifdef LIGHTMAP_ON
  94. lightmap = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1 * unity_LightmapST.xy + unity_LightmapST.zw));
  95. #endif
  96.  
  97. float3 reflectionMap = DecodeHDR(UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, normalize((_WorldSpaceCameraPos - objPos.rgb)), 7), unity_SpecCube0_HDR)* 0.02;
  98.  
  99. float grayscalelightcolor = dot(_LightColor0.rgb, grayscale_vector);
  100. float bottomIndirectLighting = grayscaleSH9(float3(0.0, -1.0, 0.0));
  101. float topIndirectLighting = grayscaleSH9(float3(0.0, 1.0, 0.0));
  102. float grayscaleDirectLighting = dot(lightDirection, normalDirection)*grayscalelightcolor*attenuation + grayscaleSH9(normalDirection);
  103.  
  104. float lightDifference = topIndirectLighting + grayscalelightcolor - bottomIndirectLighting;
  105. float remappedLight = (grayscaleDirectLighting - bottomIndirectLighting) / lightDifference;
  106.  
  107. float3 indirectLighting = saturate((ShadeSH9(half4(0.0, -1.0, 0.0, 1.0)) + reflectionMap));
  108. float3 directLighting = saturate((ShadeSH9(half4(0.0, 1.0, 0.0, 1.0)) + reflectionMap + _LightColor0.rgb));
  109. float3 directContribution = saturate((1.0 - _Shadow) + floor(saturate(remappedLight) * 2.0));
  110. float3 finalColor = emissive + (baseColor * lerp(indirectLighting, directLighting, directContribution));
  111. fixed4 finalRGBA = fixed4(finalColor * lightmap, baseColor.a);
  112. UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
  113. return finalRGBA;
  114. }
  115. ENDCG
  116. }
  117.  
  118. Pass
  119. {
  120. Name "FORWARD_DELTA"
  121. Tags { "LightMode" = "ForwardAdd" }
  122. Blend [_SrcBlend] One
  123.  
  124. CGPROGRAM
  125. #pragma shader_feature NO_OUTLINE TINTED_OUTLINE COLORED_OUTLINE
  126. #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
  127. #include "FlatLitToonCore.cginc"
  128. #pragma vertex vert
  129. #pragma geometry geom
  130. #pragma fragment frag
  131.  
  132. #pragma only_renderers d3d11 glcore gles
  133. #pragma target 4.0
  134.  
  135. #pragma multi_compile_fwdadd_fullshadows
  136. #pragma multi_compile_fog
  137.  
  138. float4 frag(VertexOutput i) : COLOR
  139. {
  140. float4 objPos = mul(unity_ObjectToWorld, float4(0,0,0,1));
  141. i.normalDir = normalize(i.normalDir);
  142. float3x3 tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.normalDir);
  143. float3 _BumpMap_var = UnpackNormal(tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap)));
  144. float3 normalDirection = normalize(mul(_BumpMap_var.rgb, tangentTransform)); // Perturbed normals
  145. float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
  146.  
  147. float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
  148. float3 lightColor = _LightColor0.rgb;
  149. UNITY_LIGHT_ATTENUATION(attenuation, i, i.posWorld.xyz);
  150.  
  151. float4 _ColorMask_var = tex2D(_ColorMask,TRANSFORM_TEX(i.uv0, _ColorMask));
  152. float4 baseColor = lerp((_MainTex_var.rgba*_Color.rgba),_MainTex_var.rgba,_ColorMask_var.r);
  153. baseColor *= float4(i.col.rgb, 1);
  154.  
  155. #if COLORED_OUTLINE
  156. if(i.is_outline) {
  157. baseColor.rgb = i.col.rgb;
  158. }
  159. #endif
  160.  
  161. #if defined(_ALPHATEST_ON)
  162. clip (baseColor.a - _Cutoff);
  163. #endif
  164.  
  165. float lightContribution = dot(normalize(_WorldSpaceLightPos0.xyz - i.posWorld.xyz),normalDirection)*attenuation;
  166. float3 directContribution = floor(saturate(lightContribution) * 2.0);
  167. float3 finalColor = baseColor * lerp(0, _LightColor0.rgb, saturate(directContribution + ((1 - _Shadow) * attenuation)));
  168. fixed4 finalRGBA = fixed4(finalColor,1) * i.col;
  169. UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
  170. return finalRGBA;
  171. }
  172. ENDCG
  173. }
  174.  
  175. Pass
  176. {
  177. Name "SHADOW_CASTER"
  178. Tags{ "LightMode" = "ShadowCaster" }
  179.  
  180. ZWrite On ZTest LEqual
  181.  
  182. CGPROGRAM
  183. #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
  184. #include "FlatLitToonShadows.cginc"
  185.  
  186. #pragma multi_compile_shadowcaster
  187. #pragma fragmentoption ARB_precision_hint_fastest
  188.  
  189. #pragma only_renderers d3d11 glcore gles
  190. #pragma target 4.0
  191.  
  192. #pragma vertex vertShadowCaster
  193. #pragma fragment fragShadowCaster
  194. ENDCG
  195. }
  196.  
  197. }
  198. FallBack "Diffuse"
  199. CustomEditor "FlatLitToonInspector"
  200. }
Advertisement
Add Comment
Please, Sign In to add comment