Lux URP Foliage.shader 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. // Shader uses custom editor to set double sided GI
  2. // Needs _Culling to be set properly
  3. Shader "Lux URP/Vegetation/Foliage"
  4. {
  5. Properties
  6. {
  7. [HeaderHelpLuxURP_URL(iwibq8un2c3h)]
  8. [Header(Surface Options)]
  9. [Space(8)]
  10. [Enum(UnityEngine.Rendering.CullMode)]
  11. _Cull ("Culling", Float) = 0
  12. [Toggle(_ALPHATEST_ON)]
  13. _AlphaClip ("Alpha Clipping", Float) = 1.0
  14. _Cutoff (" Threshold", Range(0.0, 1.0)) = 0.5
  15. [Enum(Off,0,On,1)]_Coverage ("Alpha To Coverage", Float) = 0
  16. [ToggleOff(_RECEIVE_SHADOWS_OFF)]
  17. _ReceiveShadows ("Receive Shadows", Float) = 1.0
  18. [Header(Surface Inputs)]
  19. [Space(8)]
  20. [NoScaleOffset][MainTexture]
  21. _BaseMap ("Albedo (RGB) Alpha (A)", 2D) = "white" {}
  22. [HideInInspector][MainColor]
  23. _BaseColor ("Color", Color) = (1,1,1,1)
  24. [Space(5)]
  25. _Smoothness ("Smoothness", Range(0.0, 1.0)) = 0.5
  26. _SpecColor ("Specular", Color) = (0.2, 0.2, 0.2)
  27. [Space(5)]
  28. [Toggle(_NORMALMAP)]
  29. _ApplyNormal ("Enable Normal Smoothness Trans Map", Float) = 0.0
  30. [NoScaleOffset] _BumpSpecMap
  31. (" Normal (AG) Smoothness (B) Trans (R)", 2D) = "white" {}
  32. _BumpScale (" Normal Scale", Range(0.0, 8.0)) = 1.0
  33. _GlossMapScale (" Smoothness Scale", Range(0.0, 1.0)) = 1.0
  34. [Header(Transmission)]
  35. [Space(8)]
  36. _TranslucencyPower ("Power", Range(0.0, 10.0)) = 7.0
  37. _TranslucencyStrength ("Strength", Range(0.0, 1.0)) = 1.0
  38. _ShadowStrength ("Shadow Strength", Range(0.0, 1.0)) = 0.7
  39. _MaskByShadowStrength ("Mask by incoming Shadow Strength", Range(0.0, 1.0)) = 0.0
  40. _Distortion ("Distortion", Range(0.0, 0.1)) = 0.01
  41. [Header(Wind)]
  42. [Space(8)]
  43. [KeywordEnum(Texture, Math)]
  44. _Wind ("Wind Input", Float) = 0
  45. [LuxURPWindFoliageDrawer]
  46. _WindMultiplier ("Wind Strength (X) Secondary Strength (Y) Edge Flutter (Z) Lod Level (W)", Vector) = (1, 2, 1, 0)
  47. _SampleSize ("Sample Size", Range(0.0, 1.0)) = 0.5
  48. [Header(Distance Fading)]
  49. [Space(8)]
  50. [LuxURPDistanceFadeDrawer]
  51. _DistanceFade ("Distance Fade Params", Vector) = (2500, 0.001, 0, 0)
  52. [Header(Advanced)]
  53. [Space(8)]
  54. [ToggleOff]
  55. _SpecularHighlights ("Enable Specular Highlights", Float) = 1.0
  56. [ToggleOff]
  57. _EnvironmentReflections ("Environment Reflections", Float) = 1.0
  58. // Lightmapper and outline selection shader need _MainTex, _Color and _Cutoff
  59. [HideInInspector] _MainTex ("Albedo", 2D) = "white" {}
  60. [HideInInspector] _Color ("Color", Color) = (1,1,1,1)
  61. }
  62. SubShader
  63. {
  64. Tags
  65. {
  66. "RenderPipeline" = "UniversalPipeline"
  67. "RenderType" = "Opaque" // "RenderType" = "TransparentCutout" // Gets adjusted by inspector
  68. "IgnoreProjector" = "True"
  69. "Queue"="Geometry" // "Queue"="AlphaTest" // Gets adjusted by inspector
  70. "ShaderModel"="4.5"
  71. }
  72. LOD 300
  73. Pass
  74. {
  75. Name "ForwardLit"
  76. Tags{"LightMode" = "UniversalForward"}
  77. ZWrite On
  78. Cull[_Cull]
  79. AlphaToMask [_Coverage]
  80. HLSLPROGRAM
  81. // Required to compile gles 2.0 with standard srp library
  82. #pragma prefer_hlslcc gles
  83. #pragma exclude_renderers d3d11_9x
  84. #pragma target 2.0
  85. // -------------------------------------
  86. // Material Keywords
  87. #define _SPECULAR_SETUP 1
  88. #pragma shader_feature_local _NORMALMAP
  89. #pragma shader_feature_local_fragment _ALPHATEST_ON
  90. #pragma shader_feature_local_fragment _SPECULARHIGHLIGHTS_OFF
  91. #pragma shader_feature_local_fragment _ENVIRONMENTREFLECTIONS_OFF
  92. #pragma shader_feature_local _RECEIVE_SHADOWS_OFF
  93. #pragma shader_feature_local _WIND_MATH
  94. // -------------------------------------
  95. // Universal Pipeline keywords
  96. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  97. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  98. #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
  99. #pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
  100. #pragma multi_compile_fragment _ _SHADOWS_SOFT
  101. #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
  102. #pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
  103. #pragma multi_compile _ SHADOWS_SHADOWMASK
  104. // -------------------------------------
  105. // Unity defined keywords
  106. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  107. #pragma multi_compile _ LIGHTMAP_ON
  108. #pragma multi_compile_fog
  109. //--------------------------------------
  110. // GPU Instancing
  111. #pragma multi_compile_instancing
  112. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  113. // Include base inputs and all other needed "base" includes
  114. #include "Includes/Lux URP Foliage Inputs.hlsl"
  115. #pragma vertex LitPassVertex
  116. #pragma fragment LitPassFragment
  117. //--------------------------------------
  118. // Vertex shader
  119. VertexOutput LitPassVertex(VertexInput input)
  120. {
  121. VertexOutput output = (VertexOutput)0;
  122. UNITY_SETUP_INSTANCE_ID(input);
  123. UNITY_TRANSFER_INSTANCE_ID(input, output);
  124. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  125. // Set distance fade value
  126. float3 worldInstancePos = UNITY_MATRIX_M._m03_m13_m23;
  127. float3 diff = (_WorldSpaceCameraPos - worldInstancePos);
  128. float dist = dot(diff, diff);
  129. output.fade = saturate( (_DistanceFade.x - dist) * _DistanceFade.y );
  130. // Shrink mesh if alpha testing is disabled
  131. #if !defined(_ALPHATEST_ON)
  132. input.positionOS.xyz *= output.fade;
  133. #endif
  134. // Wind in ObjectSpace -------------------------------
  135. animateVertex(input.color, input.normalOS.xyz, input.positionOS.xyz);
  136. // End Wind -------------------------------
  137. VertexPositionInputs vertexInput; // = GetVertexPositionInputs(input.positionOS.xyz);
  138. vertexInput.positionWS = TransformObjectToWorld(input.positionOS.xyz);
  139. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  140. // We have to recalculate ClipPos! / see: GetVertexPositionInputs in Core.hlsl
  141. vertexInput.positionVS = TransformWorldToView(vertexInput.positionWS);
  142. vertexInput.positionCS = TransformWorldToHClip(vertexInput.positionWS);
  143. float4 ndc = vertexInput.positionCS * 0.5f;
  144. vertexInput.positionNDC.xy = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
  145. vertexInput.positionNDC.zw = vertexInput.positionCS.zw;
  146. float3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  147. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  148. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  149. output.uv.xy = input.texcoord;
  150. // already normalized from normal transform to WS
  151. output.normalWS = normalInput.normalWS;
  152. output.viewDirWS = viewDirWS;
  153. #ifdef _NORMALMAP
  154. float sign = input.tangentOS.w * GetOddNegativeScale();
  155. output.tangentWS = float4(normalInput.tangentWS, sign);
  156. #endif
  157. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  158. #if defined(_NORMALMAP)
  159. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  160. #else
  161. // TODO: When no normal map is applied we have to lookup SH fully per pixel
  162. #if !defined(LIGHTMAP_ON)
  163. output.vertexSH = 0;
  164. #endif
  165. #endif
  166. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  167. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  168. output.positionWS = vertexInput.positionWS;
  169. #endif
  170. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  171. output.shadowCoord = GetShadowCoord(vertexInput);
  172. #endif
  173. output.positionCS = vertexInput.positionCS;
  174. return output;
  175. }
  176. //--------------------------------------
  177. // Fragment shader and functions
  178. inline void InitializeFoliageLitSurfaceData(float2 uv, half fade, out SurfaceDescription outSurfaceData)
  179. {
  180. half4 albedoAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
  181. // Add fade
  182. albedoAlpha.a *= fade;
  183. // Early out
  184. outSurfaceData.alpha = Alpha(albedoAlpha.a, 1, _Cutoff);
  185. outSurfaceData.albedo = albedoAlpha.rgb;
  186. outSurfaceData.metallic = 0;
  187. outSurfaceData.specular = _SpecColor.rgb;
  188. // Normal Map
  189. #if defined (_NORMALMAP)
  190. float4 sampleNormal = SAMPLE_TEXTURE2D(_BumpSpecMap, sampler_BumpSpecMap, uv);
  191. float3 tangentNormal;
  192. tangentNormal.xy = sampleNormal.ag * 2 - 1;
  193. tangentNormal.z = sqrt(1.0 - dot(tangentNormal.xy, tangentNormal.xy));
  194. // From Packing.hlsl:
  195. // must scale after reconstruction of normal.z which also
  196. // mirrors UnpackNormalRGB(). This does imply normal is not returned
  197. // as a unit length vector but doesn't need it since it will get normalized after TBN transformation.
  198. tangentNormal.xy *= _BumpScale;
  199. outSurfaceData.normalTS = tangentNormal;
  200. outSurfaceData.smoothness = sampleNormal.b * _GlossMapScale;
  201. outSurfaceData.translucency = sampleNormal.r;
  202. #else
  203. outSurfaceData.normalTS = float3(0, 0, 1);
  204. outSurfaceData.smoothness = _Smoothness;
  205. outSurfaceData.translucency = 1;
  206. #endif
  207. outSurfaceData.occlusion = 1;
  208. outSurfaceData.emission = 0;
  209. }
  210. void InitializeInputData(VertexOutput input, half3 normalTS, half facing, out InputData inputData)
  211. {
  212. inputData = (InputData)0;
  213. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  214. inputData.positionWS = input.positionWS;
  215. #endif
  216. half3 viewDirWS = SafeNormalize(input.viewDirWS);
  217. #ifdef _NORMALMAP
  218. normalTS.z *= facing;
  219. float sgn = input.tangentWS.w; // should be either +1 or -1
  220. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  221. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS.xyz));
  222. #else
  223. inputData.normalWS = input.normalWS * facing;
  224. #endif
  225. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  226. inputData.viewDirectionWS = viewDirWS;
  227. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  228. inputData.shadowCoord = input.shadowCoord;
  229. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  230. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  231. #else
  232. inputData.shadowCoord = float4(0, 0, 0, 0);
  233. #endif
  234. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  235. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  236. //
  237. #if defined(_NORMALMAP)
  238. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
  239. #endif
  240. // TODO: Using VFACE and vertex normals – so we should sample SH fully per pixel
  241. #if !defined(_NORMALMAP) && !defined(LIGHTMAP_ON)
  242. inputData.bakedGI = SampleSH(inputData.normalWS);
  243. #endif
  244. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  245. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  246. }
  247. half4 LitPassFragment(VertexOutput input, half facing : VFACE) : SV_Target
  248. {
  249. UNITY_SETUP_INSTANCE_ID(input);
  250. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  251. // Get the surface description
  252. SurfaceDescription surfaceData;
  253. InitializeFoliageLitSurfaceData(input.uv.xy, input.fade, surfaceData);
  254. // Prepare surface data (like bring normal into world space (incl. VFACE)) and get missing inputs like gi
  255. InputData inputData;
  256. InitializeInputData(input, surfaceData.normalTS, facing, inputData);
  257. // Apply lighting
  258. half4 color = LuxURPTranslucentFragmentPBR(
  259. inputData,
  260. surfaceData.albedo,
  261. surfaceData.metallic,
  262. surfaceData.specular,
  263. surfaceData.smoothness,
  264. surfaceData.occlusion,
  265. surfaceData.emission,
  266. surfaceData.alpha,
  267. half4(_TranslucencyStrength * surfaceData.translucency, _TranslucencyPower, _ShadowStrength, _Distortion),
  268. 1, //_AmbientReflection
  269. _MaskByShadowStrength
  270. );
  271. // Add fog
  272. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  273. return color;
  274. }
  275. ENDHLSL
  276. }
  277. // Shadows -----------------------------------------------------
  278. Pass
  279. {
  280. Name "ShadowCaster"
  281. Tags{"LightMode" = "ShadowCaster"}
  282. ZWrite On
  283. ZTest LEqual
  284. ColorMask 0
  285. Cull[_Cull]
  286. HLSLPROGRAM
  287. // Required to compile gles 2.0 with standard srp library
  288. #pragma prefer_hlslcc gles
  289. #pragma exclude_renderers d3d11_9x
  290. #pragma target 2.0
  291. // -------------------------------------
  292. // Material Keywords
  293. #pragma shader_feature_local_fragment _ALPHATEST_ON
  294. #pragma shader_feature_local _WIND_MATH
  295. //--------------------------------------
  296. // GPU Instancing
  297. #pragma multi_compile_instancing
  298. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  299. #pragma vertex ShadowPassVertex
  300. #pragma fragment ShadowPassFragment
  301. // Include base inputs and all other needed "base" includes
  302. #include "Includes/Lux URP Foliage Inputs.hlsl"
  303. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  304. // Shadow caster specific input
  305. float3 _LightDirection;
  306. VertexOutput ShadowPassVertex(VertexInput input)
  307. {
  308. VertexOutput output;
  309. UNITY_SETUP_INSTANCE_ID(input);
  310. UNITY_TRANSFER_INSTANCE_ID(input, output);
  311. // Set distance fade value
  312. float3 worldInstancePos = UNITY_MATRIX_M._m03_m13_m23;
  313. float3 diff = (_WorldSpaceCameraPos - worldInstancePos);
  314. float dist = dot(diff, diff);
  315. output.fade = saturate( (_DistanceFade.x - dist) * _DistanceFade.y );
  316. // Shrink mesh if alpha testing is disabled
  317. #if !defined(_ALPHATEST_ON)
  318. input.positionOS.xyz *= output.fade;
  319. #endif
  320. output.uv = input.texcoord;
  321. // Wind in Object Space -------------------------------
  322. animateVertex(input.color, input.normalOS.xyz, input.positionOS.xyz);
  323. // End Wind -------------------------------
  324. float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
  325. float3 normalWS = TransformObjectToWorldDir(input.normalOS);
  326. output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
  327. #if UNITY_REVERSED_Z
  328. output.positionCS.z = min(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  329. #else
  330. output.positionCS.z = max(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  331. #endif
  332. return output;
  333. }
  334. half4 ShadowPassFragment(VertexOutput input) : SV_TARGET
  335. {
  336. #if defined(_ALPHATEST_ON)
  337. half alpha = SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a;
  338. // Works in scene view but not in GameView?
  339. alpha *= input.fade;
  340. clip(alpha - _Cutoff);
  341. #endif
  342. return 0;
  343. }
  344. ENDHLSL
  345. }
  346. // Depth -----------------------------------------------------
  347. Pass
  348. {
  349. Tags{"LightMode" = "DepthOnly"}
  350. ZWrite On
  351. ColorMask 0
  352. Cull[_Cull]
  353. HLSLPROGRAM
  354. // Required to compile gles 2.0 with standard srp library
  355. #pragma prefer_hlslcc gles
  356. #pragma exclude_renderers d3d11_9x
  357. #pragma target 2.0
  358. #pragma vertex DepthOnlyVertex
  359. #pragma fragment DepthOnlyFragment
  360. // -------------------------------------
  361. // Material Keywords
  362. #pragma shader_feature_local_fragment _ALPHATEST_ON
  363. #pragma shader_feature_local _WIND_MATH
  364. //--------------------------------------
  365. // GPU Instancing
  366. #pragma multi_compile_instancing
  367. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  368. #define DEPTHONLYPASS
  369. #include "Includes/Lux URP Foliage Inputs.hlsl"
  370. VertexOutput DepthOnlyVertex(VertexInput input)
  371. {
  372. VertexOutput output = (VertexOutput)0;
  373. UNITY_SETUP_INSTANCE_ID(input);
  374. UNITY_TRANSFER_INSTANCE_ID(input, output);
  375. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  376. // Set distance fade value
  377. float3 worldInstancePos = UNITY_MATRIX_M._m03_m13_m23;
  378. float3 diff = (_WorldSpaceCameraPos - worldInstancePos);
  379. float dist = dot(diff, diff);
  380. output.fade = saturate( (_DistanceFade.x - dist) * _DistanceFade.y );
  381. // Shrink mesh if alpha testing is disabled
  382. #if !defined(_ALPHATEST_ON)
  383. input.positionOS.xyz *= output.fade;
  384. #endif
  385. // Wind in Object Space -------------------------------
  386. animateVertex(input.color, input.normalOS.xyz, input.positionOS.xyz);
  387. // End Wind -------------------------------
  388. VertexPositionInputs vertexInput;
  389. vertexInput.positionWS = TransformObjectToWorld(input.positionOS.xyz);
  390. // We have to recalculate ClipPos! / see: GetVertexPositionInputs in Core.hlsl
  391. vertexInput.positionVS = TransformWorldToView(vertexInput.positionWS);
  392. vertexInput.positionCS = TransformWorldToHClip(vertexInput.positionWS);
  393. float4 ndc = vertexInput.positionCS * 0.5f;
  394. vertexInput.positionNDC.xy = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
  395. vertexInput.positionNDC.zw = vertexInput.positionCS.zw;
  396. // End Wind -------------------------------
  397. output.uv.xy = input.texcoord;
  398. output.positionCS = vertexInput.positionCS;
  399. return output;
  400. }
  401. half4 DepthOnlyFragment(VertexOutput input) : SV_TARGET
  402. {
  403. UNITY_SETUP_INSTANCE_ID(input);
  404. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  405. #if defined(_ALPHATEST_ON)
  406. half alpha = SampleAlbedoAlpha(input.uv.xy, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a;
  407. alpha *= input.fade;
  408. clip(alpha - _Cutoff);
  409. #endif
  410. return 0;
  411. }
  412. ENDHLSL
  413. }
  414. // Depth Normal -----------------------------------------------------
  415. Pass
  416. {
  417. Name "DepthNormals"
  418. Tags{"LightMode" = "DepthNormals"}
  419. ZWrite On
  420. Cull[_Cull]
  421. HLSLPROGRAM
  422. // Required to compile gles 2.0 with standard srp library
  423. #pragma prefer_hlslcc gles
  424. #pragma exclude_renderers d3d11_9x
  425. #pragma target 2.0
  426. #pragma vertex DepthNormalsVertex
  427. #pragma fragment DepthNormalsFragment
  428. // -------------------------------------
  429. // Material Keywords
  430. #pragma shader_feature_local_fragment _ALPHATEST_ON
  431. #pragma shader_feature_local _WIND_MATH
  432. //--------------------------------------
  433. // GPU Instancing
  434. #pragma multi_compile_instancing
  435. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  436. #define DEPTNORMALPASS
  437. #include "Includes/Lux URP Foliage Inputs.hlsl"
  438. VertexOutput DepthNormalsVertex(VertexInput input)
  439. {
  440. VertexOutput output = (VertexOutput)0;
  441. UNITY_SETUP_INSTANCE_ID(input);
  442. UNITY_TRANSFER_INSTANCE_ID(input, output);
  443. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  444. // Set distance fade value
  445. float3 worldInstancePos = UNITY_MATRIX_M._m03_m13_m23;
  446. float3 diff = (_WorldSpaceCameraPos - worldInstancePos);
  447. float dist = dot(diff, diff);
  448. output.fade = saturate( (_DistanceFade.x - dist) * _DistanceFade.y );
  449. // Shrink mesh if alpha testing is disabled
  450. #if !defined(_ALPHATEST_ON)
  451. input.positionOS.xyz *= output.fade;
  452. #endif
  453. // Wind in Object Space -------------------------------
  454. animateVertex(input.color, input.normalOS.xyz, input.positionOS.xyz);
  455. // End Wind -------------------------------
  456. VertexPositionInputs vertexInput;
  457. vertexInput.positionWS = TransformObjectToWorld(input.positionOS.xyz);
  458. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, float4(1,1,1,1)); //input.tangentOS);
  459. // We have to recalculate ClipPos! / see: GetVertexPositionInputs in Core.hlsl
  460. vertexInput.positionVS = TransformWorldToView(vertexInput.positionWS);
  461. vertexInput.positionCS = TransformWorldToHClip(vertexInput.positionWS);
  462. float4 ndc = vertexInput.positionCS * 0.5f;
  463. vertexInput.positionNDC.xy = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
  464. vertexInput.positionNDC.zw = vertexInput.positionCS.zw;
  465. // End Wind -------------------------------
  466. output.uv.xy = input.texcoord;
  467. output.positionCS = vertexInput.positionCS;
  468. output.normalWS = normalInput.normalWS;
  469. return output;
  470. }
  471. half4 DepthNormalsFragment(VertexOutput input) : SV_TARGET
  472. {
  473. UNITY_SETUP_INSTANCE_ID(input);
  474. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  475. #if defined(_ALPHATEST_ON)
  476. half alpha = SampleAlbedoAlpha(input.uv.xy, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a;
  477. alpha *= input.fade;
  478. clip(alpha - _Cutoff);
  479. #endif
  480. float3 normal = input.normalWS;
  481. normal = TransformWorldToViewDir(normal, true);
  482. normal.z = abs(normal.z);
  483. return float4(PackNormalOctRectEncode(normal), 0.0, 0.0);
  484. }
  485. ENDHLSL
  486. }
  487. // Meta -----------------------------------------------------
  488. Pass
  489. {
  490. Tags{"LightMode" = "Meta"}
  491. Cull Off
  492. HLSLPROGRAM
  493. // Required to compile gles 2.0 with standard srp library
  494. #pragma prefer_hlslcc gles
  495. #pragma exclude_renderers d3d11_9x
  496. #pragma vertex UniversalVertexMeta
  497. #pragma fragment UniversalFragmentMeta
  498. #define _SPECULAR_SETUP
  499. #pragma shader_feature_local_fragment _ALPHATEST_ON
  500. // First include all our custom stuff
  501. #include "Includes/Lux URP Foliage Inputs.hlsl"
  502. //--------------------------------------
  503. // Fragment shader and functions
  504. inline void InitializeStandardLitSurfaceData(float2 uv, out SurfaceData outSurfaceData)
  505. {
  506. half4 albedoAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
  507. outSurfaceData.alpha = Alpha(albedoAlpha.a, 1, _Cutoff);
  508. outSurfaceData.albedo = albedoAlpha.rgb;
  509. outSurfaceData.metallic = 0;
  510. outSurfaceData.specular = _SpecColor.rgb;
  511. outSurfaceData.smoothness = _Smoothness;
  512. outSurfaceData.normalTS = half3(0,0,1); //SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  513. outSurfaceData.occlusion = 1;
  514. outSurfaceData.emission = 0;
  515. outSurfaceData.clearCoatMask = 0;
  516. outSurfaceData.clearCoatSmoothness = 0;
  517. }
  518. // Finally include the meta pass related stuff
  519. #include "Packages/com.unity.render-pipelines.universal/Shaders/LitMetaPass.hlsl"
  520. ENDHLSL
  521. }
  522. // End Passes -----------------------------------------------------
  523. }
  524. FallBack "Hidden/InternalErrorShader"
  525. CustomEditor "LuxURPCustomSingleSidedShaderGUI"
  526. }