Lux URP Grass.shader 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. // Shader uses custom editor to set double sided GI
  2. // Needs _Culling to be set properly
  3. Shader "Lux URP/Vegetation/Grass"
  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. [Toggle(_NORMALMAP)]
  26. _EnableNormal ("Enable Normal Map", Float) = 0
  27. [NoScaleOffset] _BumpMap (" Normal Map", 2D) = "bump" {}
  28. _BumpScale (" Normal Scale", Float) = 1.0
  29. [Space(5)]
  30. _Smoothness ("Smoothness", Range(0.0, 1.0)) = 0.5
  31. _SpecColor ("Specular", Color) = (0.2, 0.2, 0.2)
  32. _Occlusion ("Occlusion", Range(0.0, 1.0)) = 1.0
  33. [Header(Wind)]
  34. [Space(5)]
  35. [KeywordEnum(Blue, Alpha)]
  36. _BendingMode ("Main Bending", Float) = 0
  37. [Space(5)]
  38. [LuxURPWindGrassDrawer]
  39. _WindMultiplier ("Wind Strength (X) Normal Strength (Y) Sample Size (Z) Lod Level (W)", Vector) = (1, 2, 1, 0)
  40. [Header(Distance Fading)]
  41. [Space(8)]
  42. [LuxURPDistanceFadeDrawer]
  43. _DistanceFade ("Distance Fade Params", Vector) = (900, 0.005, 0, 0)
  44. [Header(Advanced)]
  45. [Space(8)]
  46. [Toggle(_BLINNPHONG)]
  47. _BlinnPhong ("Enable Blinn Phong Lighting", Float) = 0.0
  48. [Space(5)]
  49. [ToggleOff]
  50. _SpecularHighlights ("Enable Specular Highlights", Float) = 1.0
  51. [ToggleOff]
  52. _EnvironmentReflections ("Environment Reflections", Float) = 1.0
  53. // Needed by the inspector
  54. [HideInInspector] _Culling ("Culling", Float) = 0.0
  55. // Lightmapper and outline selection shader need _MainTex, _Color and _Cutoff
  56. [HideInInspector] _MainTex ("Albedo", 2D) = "white" {}
  57. [HideInInspector] _Color ("Color", Color) = (1,1,1,1)
  58. }
  59. SubShader
  60. {
  61. Tags
  62. {
  63. "RenderPipeline" = "UniversalPipeline"
  64. "RenderType" = "Opaque" //"RenderType" = "TransparentCutout"
  65. "IgnoreProjector" = "True"
  66. "Queue"="Geometry"
  67. "ShaderModel"="4.5"
  68. }
  69. LOD 100
  70. Pass
  71. {
  72. Name "ForwardLit"
  73. Tags{"LightMode" = "UniversalForward"}
  74. ZWrite On
  75. Cull [_Cull]
  76. AlphaToMask [_Coverage]
  77. HLSLPROGRAM
  78. // Required to compile gles 2.0 with standard srp library
  79. #pragma prefer_hlslcc gles
  80. #pragma exclude_renderers d3d11_9x
  81. #pragma target 2.0
  82. // -------------------------------------
  83. // Material Keywords
  84. #define _SPECULAR_SETUP 1
  85. #pragma shader_feature_local _NORMALMAP
  86. #pragma shader_feature_local _ALPHATEST_ON // not per fragment!
  87. #pragma shader_feature_local_fragment _SPECULARHIGHLIGHTS_OFF
  88. #pragma shader_feature_local_fragment _ENVIRONMENTREFLECTIONS_OFF
  89. #pragma shader_feature_local _RECEIVE_SHADOWS_OFF
  90. #pragma shader_feature_local_fragment _BLINNPHONG
  91. #pragma shader_feature_local _BENDINGMODE_ALPHA
  92. // Needed to make BlinnPhong work
  93. #define _SPECULAR_COLOR
  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 Grass 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. // VSPro like vertex colors
  126. #if defined (_BENDINGMODE_ALPHA)
  127. #define bendAmount input.color.a
  128. #else
  129. #define bendAmount input.color.b
  130. #endif
  131. #define phase input.color.gg
  132. #define vocclusion input.color.r
  133. #if !defined(_ALPHATEST_ON)
  134. float3 worldInstancePos = UNITY_MATRIX_M._m03_m13_m23;
  135. float3 diff = (_WorldSpaceCameraPos - worldInstancePos);
  136. float dist = dot(diff, diff);
  137. output.fadeOcclusion.x = saturate( (_DistanceFade.x - dist) * _DistanceFade.y );
  138. // Shrinken mesh if alpha testing is disabled
  139. input.positionOS.xyz *= output.fadeOcclusion.x;
  140. #endif
  141. // Wind in WorldSpace -------------------------------
  142. VertexPositionInputs vertexInput; // = GetVertexPositionInputs(input.positionOS.xyz);
  143. vertexInput.positionWS = TransformObjectToWorld(input.positionOS.xyz);
  144. // Do the texture lookup as soon as possible
  145. half4 wind = SAMPLE_TEXTURE2D_LOD(_LuxLWRPWindRT, sampler_LuxLWRPWindRT, vertexInput.positionWS.xz * _LuxLWRPWindDirSize.w + phase * _WindMultiplier.z, _WindMultiplier.w);
  146. // Do as much as possible unrelated to the final position afterwards to hide latency
  147. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  148. half windStrength = bendAmount * _LuxLWRPWindStrengthMultipliers.x * _WindMultiplier.x;
  149. half3 windDir = _LuxLWRPWindDirSize.xyz;
  150. half2 normalWindDir = windDir.xz * _WindMultiplier.y;
  151. // Set distance fade value
  152. #if defined(_ALPHATEST_ON)
  153. float3 worldInstancePos = UNITY_MATRIX_M._m03_m13_m23;
  154. float3 diff = (_WorldSpaceCameraPos - worldInstancePos);
  155. float dist = dot(diff, diff);
  156. output.fadeOcclusion.x = saturate( (_DistanceFade.x - dist) * _DistanceFade.y );
  157. #endif
  158. // Do other stuff here
  159. output.uv.xy = input.texcoord;
  160. output.fadeOcclusion.y = lerp(1.0h, vocclusion, _Occlusion);
  161. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  162. // From now on we rely on the texture sample being available
  163. wind.r = wind.r * (wind.g * 2.0h - 0.243h /* not a "real" normal as we want to keep the base direction */ );
  164. windStrength *= wind.r;
  165. vertexInput.positionWS.xz += windDir.xz * windStrength;
  166. // Do something to the normal as well
  167. normalInput.normalWS.xz += normalWindDir * windStrength;
  168. #ifdef _NORMALMAP
  169. normalInput.normalWS = NormalizeNormalPerVertex(normalInput.normalWS);
  170. #endif
  171. // We have to recalculate ClipPos! / see: GetVertexPositionInputs in Core.hlsl
  172. vertexInput.positionVS = TransformWorldToView(vertexInput.positionWS);
  173. vertexInput.positionCS = TransformWorldToHClip(vertexInput.positionWS);
  174. float4 ndc = vertexInput.positionCS * 0.5f;
  175. vertexInput.positionNDC.xy = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
  176. vertexInput.positionNDC.zw = vertexInput.positionCS.zw;
  177. // End Wind -------------------------------
  178. float3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  179. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  180. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  181. output.normalWS = normalInput.normalWS;
  182. output.viewDirWS = viewDirWS;
  183. #ifdef _NORMALMAP
  184. float sign = input.tangentOS.w * GetOddNegativeScale();
  185. output.tangentWS = float4(normalInput.tangentWS.xyz, sign);
  186. #endif
  187. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  188. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  189. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  190. output.positionWS = vertexInput.positionWS;
  191. #endif
  192. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  193. output.shadowCoord = GetShadowCoord(vertexInput);
  194. #endif
  195. output.positionCS = vertexInput.positionCS;
  196. return output;
  197. }
  198. //--------------------------------------
  199. // Fragment shader and functions
  200. //inline void InitializeGrassLitSurfaceData(float2 uv, half2 fadeOcclusion, out SurfaceDescription outSurfaceData)
  201. inline void InitializeGrassLitSurfaceData(float2 uv, half2 fadeOcclusion, out SurfaceData outSurfaceData)
  202. {
  203. half4 albedoAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
  204. // Add fade
  205. albedoAlpha.a *= fadeOcclusion.x;
  206. // Early out
  207. outSurfaceData.alpha = Alpha(albedoAlpha.a, 1, _Cutoff);
  208. outSurfaceData.albedo = albedoAlpha.rgb;
  209. outSurfaceData.metallic = 0;
  210. outSurfaceData.specular = _SpecColor.rgb;
  211. // Normal Map
  212. #if defined (_NORMALMAP)
  213. //outSurfaceData.normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  214. half4 sampleNormal = SAMPLE_TEXTURE2D(_BumpMap, sampler_BumpMap, uv);
  215. outSurfaceData.normalTS = UnpackNormalScale(sampleNormal, _BumpScale);
  216. #else
  217. outSurfaceData.normalTS = float3(0, 0, 1);
  218. #endif
  219. outSurfaceData.smoothness = _Smoothness;
  220. outSurfaceData.occlusion = fadeOcclusion.y;
  221. outSurfaceData.emission = 0;
  222. outSurfaceData.clearCoatMask = 0;
  223. outSurfaceData.clearCoatSmoothness = 0;
  224. }
  225. void InitializeInputData(VertexOutput input, half3 normalTS, out InputData inputData)
  226. {
  227. inputData = (InputData)0;
  228. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  229. inputData.positionWS = input.positionWS;
  230. #endif
  231. half3 viewDirWS = SafeNormalize(input.viewDirWS);
  232. #ifdef _NORMALMAP
  233. float sgn = input.tangentWS.w; // should be either +1 or -1
  234. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  235. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS.xyz));
  236. #else
  237. inputData.normalWS = input.normalWS;
  238. #endif
  239. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  240. viewDirWS = SafeNormalize(viewDirWS);
  241. inputData.viewDirectionWS = viewDirWS;
  242. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  243. inputData.shadowCoord = input.shadowCoord;
  244. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  245. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  246. #else
  247. inputData.shadowCoord = float4(0, 0, 0, 0);
  248. #endif
  249. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  250. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  251. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
  252. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  253. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  254. }
  255. half4 LitPassFragment(VertexOutput input) : SV_Target
  256. {
  257. UNITY_SETUP_INSTANCE_ID(input);
  258. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  259. // Get the surface description
  260. //SurfaceDescription surfaceData;
  261. SurfaceData surfaceData;
  262. InitializeGrassLitSurfaceData(input.uv.xy, input.fadeOcclusion, surfaceData);
  263. // Prepare surface data (like bring normal into world space) and get missing inputs like gi
  264. InputData inputData;
  265. InitializeInputData(input, surfaceData.normalTS, inputData);
  266. // Apply lighting
  267. #if defined(_BLINNPHONG)
  268. surfaceData.smoothness = max(0.01, surfaceData.smoothness);
  269. //half4 color = LightweightFragmentBlinnPhong(inputData, surfaceData.albedo, half4(surfaceData.specular, surfaceData.smoothness), surfaceData.smoothness, surfaceData.emission, surfaceData.alpha);
  270. //#else
  271. //half4 color = LightweightFragmentPBR(inputData, surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.occlusion, surfaceData.emission, surfaceData.alpha);
  272. #endif
  273. half4 color = UniversalFragmentPBR(inputData, surfaceData);
  274. // Add fog
  275. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  276. return color;
  277. }
  278. ENDHLSL
  279. }
  280. // Shadows -----------------------------------------------------
  281. Pass
  282. {
  283. Name "ShadowCaster"
  284. Tags{"LightMode" = "ShadowCaster"}
  285. ZWrite On
  286. ZTest LEqual
  287. ColorMask 0
  288. Cull[_Cull]
  289. HLSLPROGRAM
  290. // Required to compile gles 2.0 with standard srp library
  291. #pragma prefer_hlslcc gles
  292. #pragma exclude_renderers d3d11_9x
  293. #pragma target 2.0
  294. // -------------------------------------
  295. // Material Keywords
  296. #pragma shader_feature_local _ALPHATEST_ON // Not per fragment!
  297. #pragma shader_feature_local _BENDINGMODE_ALPHA
  298. //--------------------------------------
  299. // GPU Instancing
  300. #pragma multi_compile_instancing
  301. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  302. #pragma vertex ShadowPassVertex
  303. #pragma fragment ShadowPassFragment
  304. // Include base inputs and all other needed "base" includes
  305. #include "Includes/Lux URP Grass Inputs.hlsl"
  306. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  307. // Shadow caster specific input
  308. float3 _LightDirection;
  309. VertexOutput ShadowPassVertex(VertexInput input)
  310. {
  311. VertexOutput output = (VertexOutput)0;
  312. UNITY_SETUP_INSTANCE_ID(input);
  313. UNITY_TRANSFER_INSTANCE_ID(input, output);
  314. // VSPro like vertex colors
  315. #if defined (_BENDINGMODE_ALPHA)
  316. #define bendAmount input.color.a
  317. #else
  318. #define bendAmount input.color.b
  319. #endif
  320. #define phase input.color.gg
  321. #define vocclusion input.color.r
  322. // Shrink mesh if alpha testing is disabled
  323. #if !defined(_ALPHATEST_ON)
  324. float3 worldInstancePos = UNITY_MATRIX_M._m03_m13_m23;
  325. float3 diff = (_WorldSpaceCameraPos - worldInstancePos);
  326. float dist = dot(diff, diff);
  327. half fade = saturate( (_DistanceFade.x - dist) * _DistanceFade.y );
  328. // Shrink mesh
  329. input.positionOS.xyz *= fade;
  330. #endif
  331. float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
  332. // Wind in WorldSpace -------------------------------
  333. // Do the texture lookup as soon as possible
  334. half4 wind = SAMPLE_TEXTURE2D_LOD(_LuxLWRPWindRT, sampler_LuxLWRPWindRT, positionWS.xz * _LuxLWRPWindDirSize.w + phase * _WindMultiplier.z, _WindMultiplier.w);
  335. // Do as much as possible unrelated to the final position afterwards to hide latency
  336. // Calculate world space normal
  337. half3 normalWS = TransformObjectToWorldNormal(input.normalOS);
  338. // Set distance fade value
  339. #if defined(_ALPHATEST_ON)
  340. float3 worldInstancePos = UNITY_MATRIX_M._m03_m13_m23;
  341. float3 diff = (_WorldSpaceCameraPos - worldInstancePos);
  342. float dist = dot(diff, diff);
  343. output.fadeOcclusion.x = saturate( (_DistanceFade.x - dist) * _DistanceFade.y );
  344. #endif
  345. // Do other stuff here
  346. #if defined(_ALPHATEST_ON)
  347. output.uv = input.texcoord;
  348. #endif
  349. half3 windDir = _LuxLWRPWindDirSize.xyz;
  350. half windStrength = bendAmount * _LuxLWRPWindStrengthMultipliers.x * _WindMultiplier.x;
  351. // From now on we rely on the texture sample being available
  352. wind.r = wind.r * (wind.g * 2.0h - 0.243h /* not a "real" normal as we want to keep the base direction */ );
  353. windStrength *= wind.r;
  354. positionWS.xz += windDir.xz * windStrength;
  355. // We have to recalculate ClipPos! / see: GetVertexPositionInputs in Core.hlsl
  356. // End Wind -------------------------------
  357. output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
  358. #if UNITY_REVERSED_Z
  359. output.positionCS.z = min(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  360. #else
  361. output.positionCS.z = max(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  362. #endif
  363. return output;
  364. }
  365. half4 ShadowPassFragment(VertexOutput input) : SV_TARGET
  366. {
  367. UNITY_SETUP_INSTANCE_ID(input);
  368. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  369. #if defined(_ALPHATEST_ON)
  370. Alpha(SampleAlbedoAlpha(input.uv.xy, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a * input.fadeOcclusion.x, /*_BaseColor*/ half4(1,1,1,1), _Cutoff);
  371. #endif
  372. return 0;
  373. }
  374. ENDHLSL
  375. }
  376. // Depth -----------------------------------------------------
  377. Pass
  378. {
  379. Tags{"LightMode" = "DepthOnly"}
  380. ZWrite On
  381. ColorMask 0
  382. Cull [_Cull]
  383. HLSLPROGRAM
  384. // Required to compile gles 2.0 with standard srp library
  385. #pragma prefer_hlslcc gles
  386. #pragma exclude_renderers d3d11_9x
  387. #pragma target 2.0
  388. #pragma vertex DepthOnlyVertex
  389. #pragma fragment DepthOnlyFragment
  390. // -------------------------------------
  391. // Material Keywords
  392. #pragma shader_feature_local _ALPHATEST_ON // not per fragment!
  393. #pragma shader_feature_local _BENDINGMODE_ALPHA
  394. //--------------------------------------
  395. // GPU Instancing
  396. #pragma multi_compile_instancing
  397. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  398. #define DEPTHONLYPASS
  399. #include "Includes/Lux URP Grass Inputs.hlsl"
  400. VertexOutput DepthOnlyVertex(VertexInput input)
  401. {
  402. VertexOutput output = (VertexOutput)0;
  403. UNITY_SETUP_INSTANCE_ID(input);
  404. UNITY_TRANSFER_INSTANCE_ID(input, output);
  405. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  406. // VSPro like vertex colors
  407. #if defined (_BENDINGMODE_ALPHA)
  408. #define bendAmount input.color.a
  409. #else
  410. #define bendAmount input.color.b
  411. #endif
  412. #define phase input.color.gg
  413. #define vocclusion input.color.r
  414. // Shrink mesh if alpha testing is disabled
  415. #if !defined(_ALPHATEST_ON)
  416. float3 worldInstancePos = UNITY_MATRIX_M._m03_m13_m23;
  417. float3 diff = (_WorldSpaceCameraPos - worldInstancePos);
  418. float dist = dot(diff, diff);
  419. half fade = saturate( (_DistanceFade.x - dist) * _DistanceFade.y );
  420. // Shrink mesh
  421. input.positionOS.xyz *= fade;
  422. #endif
  423. // Wind in WorldSpace -------------------------------
  424. VertexPositionInputs vertexInput; // = GetVertexPositionInputs(input.positionOS.xyz);
  425. vertexInput.positionWS = TransformObjectToWorld(input.positionOS.xyz);
  426. // Do the texture lookup as soon as possible
  427. half4 wind = SAMPLE_TEXTURE2D_LOD(_LuxLWRPWindRT, sampler_LuxLWRPWindRT, vertexInput.positionWS.xz * _LuxLWRPWindDirSize.w + phase * _WindMultiplier.z, _WindMultiplier.w);
  428. // Do as much as possible unrelated to the final position afterwards to hide latency
  429. // Calculate fade
  430. #if defined(_ALPHATEST_ON)
  431. float3 worldInstancePos = UNITY_MATRIX_M._m03_m13_m23;
  432. float3 diff = (_WorldSpaceCameraPos - worldInstancePos);
  433. float dist = dot(diff, diff);
  434. output.fadeOcclusion.x = saturate( (_DistanceFade.x - dist) * _DistanceFade.y );
  435. #endif
  436. half windStrength = bendAmount * _LuxLWRPWindStrengthMultipliers.x * _WindMultiplier.x;
  437. half3 windDir = _LuxLWRPWindDirSize.xyz;
  438. #if defined(_ALPHATEST_ON)
  439. output.uv.xy = input.texcoord;
  440. #endif
  441. // From now on we rely on the texture sample being available
  442. wind.r = wind.r * (wind.g * 2.0h - 0.243h /* not a "real" normal as we want to keep the base direction */ );
  443. windStrength *= wind.r;
  444. vertexInput.positionWS.xz += windDir.xz * windStrength;
  445. // End Wind -------------------------------
  446. // We have to recalculate ClipPos!
  447. output.positionCS = TransformWorldToHClip(vertexInput.positionWS);
  448. return output;
  449. }
  450. half4 DepthOnlyFragment(VertexOutput input) : SV_TARGET
  451. {
  452. UNITY_SETUP_INSTANCE_ID(input);
  453. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  454. #if defined(_ALPHATEST_ON)
  455. Alpha(SampleAlbedoAlpha(input.uv.xy, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a * input.fadeOcclusion.x, /*_BaseColor*/ half4(1,1,1,1), _Cutoff);
  456. #endif
  457. return 0;
  458. }
  459. ENDHLSL
  460. }
  461. // DepthNormals -----------------------------------------------------
  462. Pass
  463. {
  464. Name "DepthNormals"
  465. Tags{"LightMode" = "DepthNormals"}
  466. ZWrite On
  467. Cull [_Cull]
  468. HLSLPROGRAM
  469. // Required to compile gles 2.0 with standard srp library
  470. #pragma prefer_hlslcc gles
  471. #pragma exclude_renderers d3d11_9x
  472. #pragma target 2.0
  473. #pragma vertex DepthNormalsVertex
  474. #pragma fragment DepthNormalsFragment
  475. // -------------------------------------
  476. // Material Keywords
  477. #pragma shader_feature_local _ALPHATEST_ON // not per fragment!
  478. #pragma shader_feature_local _BENDINGMODE_ALPHA
  479. //--------------------------------------
  480. // GPU Instancing
  481. #pragma multi_compile_instancing
  482. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  483. #define DEPTHNORMALPASS
  484. #include "Includes/Lux URP Grass Inputs.hlsl"
  485. VertexOutput DepthNormalsVertex(VertexInput input)
  486. {
  487. VertexOutput output = (VertexOutput)0;
  488. UNITY_SETUP_INSTANCE_ID(input);
  489. UNITY_TRANSFER_INSTANCE_ID(input, output);
  490. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  491. // VSPro like vertex colors
  492. #if defined (_BENDINGMODE_ALPHA)
  493. #define bendAmount input.color.a
  494. #else
  495. #define bendAmount input.color.b
  496. #endif
  497. #define phase input.color.gg
  498. #define vocclusion input.color.r
  499. // Shrink mesh if alpha testing is disabled
  500. #if !defined(_ALPHATEST_ON)
  501. float3 worldInstancePos = UNITY_MATRIX_M._m03_m13_m23;
  502. float3 diff = (_WorldSpaceCameraPos - worldInstancePos);
  503. float dist = dot(diff, diff);
  504. half fade = saturate( (_DistanceFade.x - dist) * _DistanceFade.y );
  505. // Shrink mesh
  506. input.positionOS.xyz *= fade;
  507. #endif
  508. // Wind in WorldSpace -------------------------------
  509. VertexPositionInputs vertexInput; // = GetVertexPositionInputs(input.positionOS.xyz);
  510. vertexInput.positionWS = TransformObjectToWorld(input.positionOS.xyz);
  511. // Do the texture lookup as soon as possible
  512. half4 wind = SAMPLE_TEXTURE2D_LOD(_LuxLWRPWindRT, sampler_LuxLWRPWindRT, vertexInput.positionWS.xz * _LuxLWRPWindDirSize.w + phase * _WindMultiplier.z, _WindMultiplier.w);
  513. // Do as much as possible unrelated to the final position afterwards to hide latency
  514. // Calculate fade
  515. #if defined(_ALPHATEST_ON)
  516. float3 worldInstancePos = UNITY_MATRIX_M._m03_m13_m23;
  517. float3 diff = (_WorldSpaceCameraPos - worldInstancePos);
  518. float dist = dot(diff, diff);
  519. output.fadeOcclusion.x = saturate( (_DistanceFade.x - dist) * _DistanceFade.y );
  520. #endif
  521. half windStrength = bendAmount * _LuxLWRPWindStrengthMultipliers.x * _WindMultiplier.x;
  522. half3 windDir = _LuxLWRPWindDirSize.xyz;
  523. half2 normalWindDir = windDir.xz * _WindMultiplier.y;
  524. #if defined(_ALPHATEST_ON)
  525. output.uv.xy = input.texcoord;
  526. #endif
  527. // From now on we rely on the texture sample being available
  528. wind.r = wind.r * (wind.g * 2.0h - 0.243h /* not a "real" normal as we want to keep the base direction */ );
  529. windStrength *= wind.r;
  530. vertexInput.positionWS.xz += windDir.xz * windStrength;
  531. // Do something to the normal as well
  532. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, float4(1,1,1,1)); //input.tangentOS);
  533. normalInput.normalWS.xz += normalWindDir * windStrength;
  534. #ifdef _NORMALMAP
  535. normalInput.normalWS = NormalizeNormalPerVertex(normalInput.normalWS);
  536. #endif
  537. output.normalWS = normalInput.normalWS;
  538. // End Wind -------------------------------
  539. // We have to recalculate ClipPos!
  540. output.positionCS = TransformWorldToHClip(vertexInput.positionWS);
  541. return output;
  542. }
  543. half4 DepthNormalsFragment(VertexOutput input) : SV_TARGET
  544. {
  545. UNITY_SETUP_INSTANCE_ID(input);
  546. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  547. #if defined(_ALPHATEST_ON)
  548. Alpha(SampleAlbedoAlpha(input.uv.xy, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a * input.fadeOcclusion.x, /*_BaseColor*/ half4(1,1,1,1), _Cutoff);
  549. #endif
  550. float3 normal = input.normalWS;
  551. normal = TransformWorldToViewDir(normal, true);
  552. // Make the normal face the camera!
  553. normal.z = abs(normal.z);
  554. return float4(PackNormalOctRectEncode(normal), 0.0, 0.0);
  555. }
  556. ENDHLSL
  557. }
  558. // Meta -----------------------------------------------------
  559. Pass
  560. {
  561. Tags{"LightMode" = "Meta"}
  562. Cull Off
  563. HLSLPROGRAM
  564. #pragma exclude_renderers gles gles3 glcore
  565. #pragma target 4.5
  566. #pragma vertex UniversalVertexMeta
  567. #pragma fragment UniversalFragmentMeta
  568. #define _SPECULAR_SETUP 1
  569. #pragma shader_feature_local _ALPHATEST_ON
  570. // 1
  571. // First include all our custom stuff
  572. #include "Includes/Lux URP Grass Inputs.hlsl"
  573. //--------------------------------------
  574. // Fragment shader and functions - usually defined in LitInput.hlsl
  575. inline void InitializeStandardLitSurfaceData(float2 uv, out SurfaceData outSurfaceData)
  576. {
  577. half4 albedoAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
  578. outSurfaceData.alpha = Alpha(albedoAlpha.a, half4(1.0h, 1.0h, 1.0h, 1.0h), _Cutoff);
  579. outSurfaceData.albedo = albedoAlpha.rgb;
  580. outSurfaceData.metallic = 1.0h; // crazy?
  581. outSurfaceData.specular = _SpecColor.rgb;
  582. outSurfaceData.smoothness = _Smoothness;
  583. outSurfaceData.normalTS = half3(0,0,1);
  584. outSurfaceData.occlusion = 1;
  585. outSurfaceData.emission = 0.5h;
  586. outSurfaceData.clearCoatMask = 0;
  587. outSurfaceData.clearCoatSmoothness = 0;
  588. }
  589. // Finally include the meta pass related stuff
  590. #include "Packages/com.unity.render-pipelines.universal/Shaders/LitMetaPass.hlsl"
  591. ENDHLSL
  592. }
  593. // End Passes -----------------------------------------------------
  594. }
  595. FallBack "Hidden/InternalErrorShader"
  596. CustomEditor "LuxURPCustomSingleSidedShaderGUI"
  597. }