CTI URP Billboard.shader 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. Shader "CTI/URP Billboard"
  2. {
  3. Properties
  4. {
  5. [Header(Surface Inputs)]
  6. [Space(5)]
  7. _HueVariation ("Color Variation", Color) = (0.9,0.5,0.0,0.1)
  8. [NoScaleOffset] _BaseMap ("Albedo (RGB) Smoothness (A)", 2D) = "white" {}
  9. _Cutoff ("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
  10. _AlphaLeak ("Alpha Leak Suppression", Range(0.5,1.0)) = 0.6
  11. _Smoothness ("Smoothness", Range(0.0, 1.0)) = 1.0
  12. _SpecColor ("Specular", Color) = (0.2, 0.2, 0.2)
  13. _OcclusionStrength ("Occlusion Strength", Range(0,1)) = 1
  14. [Space(5)]
  15. [Toggle(_NORMALMAP)]
  16. _ApplyNormal ("Enable Normal Map", Float) = 1.0
  17. [NoScaleOffset]
  18. _BumpSpecMap (" Normal (AG) Translucency(R) Smoothness(B)", 2D) = "white" {}
  19. _BumpScale (" Normal Scale", Float) = 1.0
  20. [Header(Transmission)]
  21. [Space(5)]
  22. [CTI_LWRPTransDrawer]
  23. _Translucency ("Strength (X) Power (Y)", Vector) = (1, 8, 0, 0)
  24. [Header(Wind)]
  25. [Space(3)]
  26. _WindStrength ("Wind Strength", Float) = 1.0
  27. [Header(Ambient)]
  28. [Space(5)]
  29. _AmbientReflection ("Ambient Reflection", Range(0, 1)) = 1
  30. [Header(Legacy)]
  31. [Space(5)]
  32. _BillboardScale ("Billboard Scale", Float) = 2
  33. }
  34. SubShader
  35. {
  36. Tags
  37. {
  38. "RenderPipeline" = "LightweightPipeline"
  39. "RenderType" = "Opaque"
  40. "Queue"="AlphaTest"
  41. "IgnoreProjector" = "True"
  42. }
  43. // Base -----------------------------------------------------
  44. Pass
  45. {
  46. Name "ForwardLit"
  47. Tags {"LightMode" = "LightweightForward"}
  48. ZWrite On
  49. // LWRP billboardNormal Orientation is flipped?
  50. Cull Off
  51. //AlphaToMask On
  52. HLSLPROGRAM
  53. // Required to compile gles 2.0 with standard SRP library
  54. #pragma prefer_hlslcc gles
  55. #pragma exclude_renderers d3d11_9x
  56. #pragma target 2.0
  57. // -------------------------------------
  58. // Material Keywords
  59. #define _ALPHATEST_ON 1
  60. #pragma shader_feature _NORMALMAP
  61. #define CTIBILLBOARD
  62. #define _SPECULAR_SETUP
  63. // -------------------------------------
  64. // Lightweight Pipeline keywords
  65. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  66. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  67. #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
  68. #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
  69. #pragma multi_compile _ _SHADOWS_SOFT
  70. #pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE
  71. // -------------------------------------
  72. // Unity defined keywords
  73. //#pragma multi_compile _ DIRLIGHTMAP_COMBINED
  74. //#pragma multi_compile _ LIGHTMAP_ON
  75. #pragma multi_compile_fog
  76. #pragma multi_compile _ LOD_FADE_CROSSFADE
  77. //--------------------------------------
  78. // GPU Instancing
  79. #pragma multi_compile_instancing
  80. #include "Includes/CTI URP Inputs.hlsl"
  81. #include "Includes/CTI URP Billboard.hlsl"
  82. #include "Includes/CTI URP Lighting.hlsl"
  83. #pragma vertex LitPassVertex
  84. #pragma fragment LitPassFragment
  85. /// --------
  86. void InitializeInputData(CTIVertexBBOutput input, half3 normalTS, out InputData inputData)
  87. {
  88. inputData = (InputData)0;
  89. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  90. inputData.positionWS = input.positionWS;
  91. #endif
  92. #ifdef _NORMALMAP
  93. half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  94. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
  95. #else
  96. half3 viewDirWS = input.viewDirWS;
  97. inputData.normalWS = input.normalWS;
  98. #endif
  99. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  100. viewDirWS = SafeNormalize(viewDirWS);
  101. inputData.viewDirectionWS = viewDirWS;
  102. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  103. inputData.shadowCoord = input.shadowCoord;
  104. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  105. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  106. #else
  107. inputData.shadowCoord = float4(0, 0, 0, 0);
  108. #endif
  109. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  110. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  111. inputData.bakedGI = SAMPLE_GI(input.texcoord1, input.vertexSH, inputData.normalWS);
  112. }
  113. CTIVertexBBOutput LitPassVertex(CTIVertexBBInput input)
  114. {
  115. CTIVertexBBOutput output = (CTIVertexBBOutput)0;
  116. UNITY_SETUP_INSTANCE_ID(input);
  117. UNITY_TRANSFER_INSTANCE_ID(input, output);
  118. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  119. CTIBillboardVert(input, 0);
  120. // Set color variation
  121. output.colorVariation = input.color.r;
  122. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  123. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  124. half3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  125. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  126. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  127. output.uv.xy = input.texcoord.xy;
  128. #ifdef _NORMALMAP
  129. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  130. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  131. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  132. #else
  133. output.normalWS = NormalizeNormalPerVertex(normalInput.normalWS);
  134. output.viewDirWS = viewDirWS;
  135. #endif
  136. //OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  137. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  138. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  139. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  140. output.positionWS = vertexInput.positionWS;
  141. #endif
  142. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  143. output.shadowCoord = GetShadowCoord(vertexInput);
  144. #endif
  145. output.positionCS = vertexInput.positionCS;
  146. return output;
  147. }
  148. half4 LitPassFragment(CTIVertexBBOutput IN) : SV_Target
  149. {
  150. UNITY_SETUP_INSTANCE_ID(IN);
  151. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(IN);
  152. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  153. LODDitheringTransition(IN.positionCS.xyz, unity_LODFade.x);
  154. #endif
  155. SurfaceDescriptionLeaves surfaceData;
  156. // Get the surface description / defined in "Includes/CTI LWRP Inputs.hlsl"
  157. InitializeStandardLitSurfaceData(IN.colorVariation, IN.uv.xy, surfaceData);
  158. // Add ambient occlusion from alpha
  159. surfaceData.occlusion = (surfaceData.occlusion <= _AlphaLeak) ? 1 : surfaceData.occlusion; // Eliminate alpha leaking into ao
  160. surfaceData.occlusion = lerp(1, surfaceData.occlusion * 2 - 1, _OcclusionStrength);
  161. InputData inputData;
  162. InitializeInputData(IN, surfaceData.normalTS, inputData);
  163. // Apply lighting
  164. //half4 color = LightweightFragmentPBR(inputData, surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.occlusion, surfaceData.emission, surfaceData.alpha);
  165. half4 color = CTILightweightFragmentPBR(
  166. inputData,
  167. surfaceData.albedo,
  168. surfaceData.metallic,
  169. surfaceData.specular,
  170. surfaceData.smoothness,
  171. surfaceData.occlusion,
  172. surfaceData.emission,
  173. surfaceData.alpha,
  174. _Translucency * half3(surfaceData.translucency, 1, 1),
  175. _AmbientReflection);
  176. // Add fog
  177. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  178. return color;
  179. }
  180. ENDHLSL
  181. }
  182. // Shadows -----------------------------------------------------
  183. Pass
  184. {
  185. Name "ShadowCaster"
  186. Tags{"LightMode" = "ShadowCaster"}
  187. ZWrite On
  188. ZTest LEqual
  189. Cull Off
  190. HLSLPROGRAM
  191. // Required to compile gles 2.0 with standard srp library
  192. #pragma prefer_hlslcc gles
  193. #pragma exclude_renderers d3d11_9x
  194. #pragma target 2.0
  195. // -------------------------------------
  196. // Material Keywords
  197. #define _ALPHATEST_ON 1
  198. #define CTIBILLBOARD
  199. //--------------------------------------
  200. // GPU Instancing
  201. #pragma multi_compile_instancing
  202. #pragma multi_compile _ LOD_FADE_CROSSFADE
  203. #pragma vertex ShadowPassVertex
  204. #pragma fragment ShadowPassFragment
  205. #include "Includes/CTI URP Inputs.hlsl"
  206. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  207. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  208. #include "Includes/CTI URP Billboard.hlsl"
  209. float3 _LightDirection;
  210. CTIVertexBBOutput ShadowPassVertex(CTIVertexBBInput input)
  211. {
  212. CTIVertexBBOutput output = (CTIVertexBBOutput)0;
  213. UNITY_SETUP_INSTANCE_ID(input);
  214. UNITY_TRANSFER_INSTANCE_ID(input, output);
  215. CTIBillboardVert(input, _LightDirection);
  216. output.uv = input.texcoord.xy;
  217. float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
  218. float3 normalWS = TransformObjectToWorldDir(input.normalOS);
  219. output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
  220. #if UNITY_REVERSED_Z
  221. output.positionCS.z = min(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  222. #else
  223. output.positionCS.z = max(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  224. #endif
  225. return output;
  226. }
  227. half4 ShadowPassFragment(CTIVertexOutput IN) : SV_TARGET
  228. {
  229. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  230. LODDitheringTransition(IN.positionCS.xyz, unity_LODFade.x);
  231. #endif
  232. half alpha = SampleAlbedoAlpha(IN.uv.xy, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a;
  233. clip(alpha - _Cutoff);
  234. return 1;
  235. }
  236. ENDHLSL
  237. }
  238. // Depth -----------------------------------------------------
  239. Pass
  240. {
  241. Name "DepthOnly"
  242. Tags {"LightMode" = "DepthOnly"}
  243. ZWrite On
  244. ColorMask 0
  245. Cull Off
  246. HLSLPROGRAM
  247. // Required to compile gles 2.0 with standard srp library
  248. #pragma prefer_hlslcc gles
  249. #pragma exclude_renderers d3d11_9x
  250. #pragma target 2.0
  251. #pragma vertex DepthOnlyVertex
  252. #pragma fragment DepthOnlyFragment
  253. // -------------------------------------
  254. // Material Keywords
  255. #define _ALPHATEST_ON 1
  256. #define CTIBILLBOARD
  257. //--------------------------------------
  258. // GPU Instancing
  259. #pragma multi_compile_instancing
  260. #pragma multi_compile _ LOD_FADE_CROSSFADE
  261. #define DEPTHONLYPASS
  262. #include "Includes/CTI URP Inputs.hlsl"
  263. #include "Includes/CTI URP Billboard.hlsl"
  264. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  265. CTIVertexBBOutput DepthOnlyVertex(CTIVertexBBInput input)
  266. {
  267. CTIVertexBBOutput output = (CTIVertexBBOutput)0;
  268. UNITY_SETUP_INSTANCE_ID(input);
  269. UNITY_TRANSFER_INSTANCE_ID(input, output);
  270. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  271. CTIBillboardVert(input, 0);
  272. VertexPositionInputs vertexPosition = GetVertexPositionInputs(input.positionOS.xyz);
  273. output.uv.xy = input.texcoord.xy;
  274. output.positionCS = vertexPosition.positionCS;
  275. return output;
  276. }
  277. half4 DepthOnlyFragment(CTIVertexBBOutput IN) : SV_TARGET
  278. {
  279. UNITY_SETUP_INSTANCE_ID(IN);
  280. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(IN);
  281. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES) // enable dithering LOD transition if user select CrossFade transition in LOD group
  282. LODDitheringTransition(IN.positionCS.xyz, unity_LODFade.x);
  283. #endif
  284. half alpha = SampleAlbedoAlpha(IN.uv.xy, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a;
  285. clip(alpha - _Cutoff);
  286. return 1;
  287. }
  288. ENDHLSL
  289. }
  290. // Meta -----------------------------------------------------
  291. }
  292. FallBack "Hidden/InternalErrorShader"
  293. }