Lux URP Fast Outline AlphaTested.shader 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Shader uses custom editor to set double sided GI
  2. // Needs _Culling to be set properly
  3. Shader "Lux URP/Fast Outline AlphaTested"
  4. {
  5. Properties
  6. {
  7. [HeaderHelpLuxURP_URL(uj834ddvqvmq)]
  8. [Header(Surface Options)]
  9. [Space(8)]
  10. [Enum(UnityEngine.Rendering.CompareFunction)]
  11. _ZTest ("ZTest", Int) = 4
  12. [Enum(UnityEngine.Rendering.CullMode)]
  13. _Cull ("Culling", Float) = 2
  14. [Enum(Off,0,On,1)]
  15. _Coverage ("Alpha To Coverage", Float) = 0
  16. [Space(5)]
  17. [IntRange] _StencilRef ("Stencil Reference", Range (0, 255)) = 0
  18. [IntRange] _ReadMask (" Read Mask", Range (0, 255)) = 255
  19. [Enum(UnityEngine.Rendering.CompareFunction)]
  20. _StencilCompare ("Stencil Comparison", Int) = 6
  21. [Header(Outline)]
  22. [Space(8)]
  23. _OutlineColor ("Color", Color) = (1,1,1,1)
  24. _Border ("Width", Float) = 3
  25. [Space(5)]
  26. [Toggle(_APPLYFOG)]
  27. _ApplyFog ("Enable Fog", Float) = 0.0
  28. [Header(Surface Inputs)]
  29. [Space(8)]
  30. [MainColor]
  31. _BaseColor ("Color", Color) = (1,1,1,1)
  32. [MainTexture]
  33. _BaseMap ("Albedo (RGB) Alpha (A)", 2D) = "white" {}
  34. _Cutoff ("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
  35. // Lightmapper and outline selection shader need _MainTex, _Color and _Cutoff
  36. [HideInInspector] _MainTex ("Albedo", 2D) = "white" {}
  37. [HideInInspector] _Color ("Color", Color) = (1,1,1,1)
  38. }
  39. SubShader
  40. {
  41. Tags
  42. {
  43. "RenderPipeline" = "UniversalPipeline"
  44. "RenderType" = "Opaque"
  45. "Queue" = "Transparent+59" // +59 smalltest to get drawn on top of transparents
  46. }
  47. LOD 100
  48. Pass
  49. {
  50. Name "StandardUnlit"
  51. Tags{"LightMode" = "UniversalForward"}
  52. Stencil {
  53. Ref [_StencilRef]
  54. ReadMask [_ReadMask]
  55. Comp [_StencilCompare]
  56. Pass Keep
  57. }
  58. ZWrite On
  59. ZTest [_ZTest]
  60. Cull [_Cull]
  61. AlphaToMask [_Coverage]
  62. HLSLPROGRAM
  63. // Required to compile gles 2.0 with standard SRP library
  64. #pragma prefer_hlslcc gles
  65. #pragma exclude_renderers d3d11_9x
  66. // Shader target needs to be 3.0 due to tex2Dlod in the vertex shader and VFACE
  67. #pragma target 2.0
  68. // -------------------------------------
  69. // Material Keywords
  70. #define _ALPHATEST_ON
  71. #pragma shader_feature_local_fragment _APPLYFOG
  72. // -------------------------------------
  73. // Unity defined keywords
  74. #pragma multi_compile_fog
  75. //--------------------------------------
  76. // GPU Instancing
  77. #pragma multi_compile_instancing
  78. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  79. // Include base inputs and all other needed "base" includes
  80. #include "Includes/Lux URP Fast Outlines AlphaTested Inputs.hlsl"
  81. #pragma vertex LitPassVertex
  82. #pragma fragment LitPassFragment
  83. //--------------------------------------
  84. // Vertex shader
  85. VertexOutputSimple LitPassVertex(VertexInputSimple input)
  86. {
  87. VertexOutputSimple output = (VertexOutputSimple)0;
  88. UNITY_SETUP_INSTANCE_ID(input);
  89. UNITY_TRANSFER_INSTANCE_ID(input, output);
  90. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  91. VertexPositionInputs vertexInput;
  92. vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  93. #if defined(_APPLYFOG)
  94. output.fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  95. #endif
  96. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  97. output.positionCS = vertexInput.positionCS;
  98. return output;
  99. }
  100. //--------------------------------------
  101. // Fragment shader and functions
  102. inline void InitializeSurfaceData(
  103. float2 uv,
  104. out SurfaceDescriptionSimple outSurfaceData)
  105. {
  106. half innerAlpha = SampleAlbedoAlpha(uv.xy, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a;
  107. // Outline
  108. float2 offset = float2(1,1);
  109. float2 shift = fwidth(uv) * _Border * 0.5f;
  110. float2 sampleCoord = uv + shufflefast(offset, shift);
  111. half shuffleAlpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, sampleCoord).a;
  112. offset = float2(-1,1);
  113. sampleCoord = uv + shufflefast(offset, shift);
  114. shuffleAlpha += SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, sampleCoord).a;
  115. offset = float2(1,-1);
  116. sampleCoord = uv + shufflefast(offset, shift);
  117. shuffleAlpha += SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, sampleCoord).a;
  118. offset = float2(-1,-1);
  119. sampleCoord = uv + shufflefast(offset, shift);
  120. shuffleAlpha += SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, sampleCoord).a;
  121. // Mask inner parts - which is not really needed when using the stencil buffer. Let's do it anyway, just in case.
  122. shuffleAlpha = lerp(shuffleAlpha, 0, step(_Cutoff, innerAlpha) );
  123. // Apply clip
  124. outSurfaceData.alpha = Alpha(shuffleAlpha, 1, _Cutoff);
  125. }
  126. void InitializeInputData(VertexOutputSimple input, out InputData inputData)
  127. {
  128. inputData = (InputData)0;
  129. #if defined(_APPLYFOG)
  130. inputData.fogCoord = input.fogFactor;
  131. #endif
  132. }
  133. half4 LitPassFragment(VertexOutputSimple input) : SV_Target
  134. {
  135. UNITY_SETUP_INSTANCE_ID(input);
  136. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  137. // Get the surface description
  138. SurfaceDescriptionSimple surfaceData;
  139. InitializeSurfaceData(input.uv, surfaceData);
  140. // Prepare surface data (like bring normal into world space and get missing inputs like gi). Super simple here.
  141. InputData inputData;
  142. InitializeInputData(input, inputData);
  143. // Apply color – as we do not have any lighting.
  144. half4 color = half4(_OutlineColor.rgb, surfaceData.alpha);
  145. // Add fog
  146. #if defined(_APPLYFOG)
  147. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  148. #endif
  149. return color;
  150. }
  151. ENDHLSL
  152. }
  153. // End Passes -----------------------------------------------------
  154. }
  155. FallBack "Hidden/InternalErrorShader"
  156. }