Lux URP Foliage Inputs.hlsl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #ifndef INPUT_LUXLWRP_BASE_INCLUDED
  2. #define INPUT_LUXLWRP_BASE_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  4. // defines a bunch of helper functions (like lerpwhiteto)
  5. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  6. // defines SurfaceData, textures and the functions Alpha, SampleAlbedoAlpha, SampleNormal, SampleEmission
  7. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  8. // defines e.g. "DECLARE_LIGHTMAP_OR_SH"
  9. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  10. #include "../Includes/Lux URP Translucent Lighting.hlsl"
  11. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  12. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
  13. // Material Inputs
  14. CBUFFER_START(UnityPerMaterial)
  15. float4 _BaseMap_ST;
  16. half _Cutoff;
  17. half _Smoothness;
  18. half4 _SpecColor;
  19. half4 _WindMultiplier;
  20. float _SampleSize;
  21. //#ifdef _NORMALMAP
  22. half _GlossMapScale;
  23. half _BumpScale;
  24. //#endif
  25. float2 _DistanceFade;
  26. half _TranslucencyPower;
  27. half _TranslucencyStrength;
  28. half _ShadowStrength;
  29. half _MaskByShadowStrength;
  30. half _Distortion;
  31. #if defined(DEBUG)
  32. half _DebugColor;
  33. half _Brightness;
  34. #endif
  35. CBUFFER_END
  36. // Additional textures
  37. TEXTURE2D(_BumpSpecMap); SAMPLER(sampler_BumpSpecMap); float4 _BumpSpecMap_TexelSize;
  38. TEXTURE2D(_LuxLWRPWindRT); SAMPLER(sampler_LuxLWRPWindRT);
  39. // Global Inputs
  40. float4 _LuxLWRPWindDirSize;
  41. float4 _LuxLWRPWindStrengthMultipliers;
  42. float4 _LuxLWRPSinTime;
  43. float2 _LuxLWRPGust;
  44. // Structs
  45. struct VertexInput
  46. {
  47. float3 positionOS : POSITION;
  48. float3 normalOS : NORMAL;
  49. float4 tangentOS : TANGENT;
  50. float2 texcoord : TEXCOORD0;
  51. float2 lightmapUV : TEXCOORD1;
  52. half4 color : COLOR;
  53. UNITY_VERTEX_INPUT_INSTANCE_ID
  54. };
  55. struct VertexOutput
  56. {
  57. float4 positionCS : SV_POSITION;
  58. float2 uv : TEXCOORD0;
  59. half fade : TEXCOORD9;
  60. #if !defined(UNITY_PASS_SHADOWCASTER) && !defined(DEPTHONLYPASS)
  61. float3 normalWS : TEXCOORD3;
  62. #endif
  63. #if !defined(UNITY_PASS_SHADOWCASTER) && !defined(DEPTHONLYPASS)
  64. DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);
  65. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  66. float3 positionWS : TEXCOORD2;
  67. #endif
  68. float3 viewDirWS : TEXCOORD4;
  69. #ifdef _NORMALMAP
  70. float4 tangentWS : TEXCOORD5;
  71. #endif
  72. half4 fogFactorAndVertexLight : TEXCOORD6;
  73. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  74. float4 shadowCoord : TEXCOORD7;
  75. #endif
  76. #endif
  77. #if defined(DEBUG)
  78. half4 color : COLOR;
  79. #endif
  80. UNITY_VERTEX_INPUT_INSTANCE_ID
  81. UNITY_VERTEX_OUTPUT_STEREO
  82. };
  83. struct SurfaceDescription
  84. {
  85. float3 albedo;
  86. float alpha;
  87. float3 normalTS;
  88. float3 emission;
  89. float metallic;
  90. float3 specular;
  91. float smoothness;
  92. float occlusion;
  93. float translucency;
  94. };
  95. half4 SmoothCurve( half4 x ) {
  96. return x * x *( 3.0h - 2.0h * x );
  97. }
  98. half4 TriangleWave( half4 x ) {
  99. return abs( frac( x + 0.5h ) * 2.0h - 1.0h );
  100. }
  101. half4 SmoothTriangleWave( half4 x ) {
  102. return SmoothCurve( TriangleWave( x ) );
  103. }
  104. half2 SmoothCurve( half2 x ) {
  105. return x * x *( 3.0h - 2.0h * x );
  106. }
  107. half2 TriangleWave( half2 x ) {
  108. return abs( frac( x + 0.5h ) * 2.0h - 1.0h );
  109. }
  110. half2 SmoothTriangleWave( half2 x ) {
  111. return SmoothCurve( TriangleWave( x ) );
  112. }
  113. #define foliageMainWindStrengthFromZone _LuxLWRPWindStrengthMultipliers.y
  114. #define primaryBending _WindMultiplier.x
  115. #define secondaryBending _WindMultiplier.y
  116. #define edgeFlutter _WindMultiplier.z
  117. void animateVertex(half4 animParams, half3 normalOS, inout float3 positionOS) {
  118. float origLength = length(positionOS.xyz);
  119. float3 windDir = mul(UNITY_MATRIX_I_M, float4(_LuxLWRPWindDirSize.xyz, 0)).xyz;
  120. half fDetailAmp = 0.1h;
  121. half fBranchAmp = 0.3h;
  122. #if !defined(_WIND_MATH)
  123. float2 samplePos = TransformObjectToWorld(positionOS.xyz * _SampleSize).xz * _LuxLWRPWindDirSize.ww;
  124. half fVtxPhase = dot( normalize(positionOS.xyz), ((animParams.g + animParams.r) * 0.5).xxx );
  125. float4 wind = SAMPLE_TEXTURE2D_LOD(_LuxLWRPWindRT, sampler_LuxLWRPWindRT, samplePos.xy, _WindMultiplier.w);
  126. // Factor in bending params from Material
  127. animParams.abg *= _WindMultiplier.xyz;
  128. // Make math match
  129. animParams.ab *= 2;
  130. // Primary bending
  131. positionOS.xz += animParams.a * windDir.xz * foliageMainWindStrengthFromZone * smoothstep(-1.5h, 1.0h, wind.r * (wind.g * 1.0h - 0.243h));
  132. // Second texture sample taking phase into account
  133. wind = SAMPLE_TEXTURE2D_LOD(_LuxLWRPWindRT, sampler_LuxLWRPWindRT, samplePos.xy - animParams.rr * 0.5, _WindMultiplier.w);
  134. // Edge Flutter
  135. float3 bend = normalOS.xyz * (animParams.g * fDetailAmp * lerp(_LuxLWRPSinTime.y, _LuxLWRPSinTime.z, wind.r));
  136. bend.y = animParams.b * 0.3h;
  137. // Edge Flutter and Secondary Bending
  138. positionOS.xyz += ( bend + ( animParams.b * windDir * wind.r * (wind.g * 2.0h - 0.243h) ) ) * foliageMainWindStrengthFromZone;
  139. #else
  140. float3 objectWorldPos = UNITY_MATRIX_M._m03_m13_m23;
  141. // Animate incoming wind
  142. float3 absObjectWorldPos = abs(objectWorldPos.xyz * 0.125h);
  143. float sinuswave = _SinTime.z;
  144. half2 vOscillations = SmoothTriangleWave( half2(absObjectWorldPos.x + sinuswave, absObjectWorldPos.z + sinuswave * 0.7h) );
  145. // x used for main wind bending / y used for tumbling
  146. half2 fOsc = (vOscillations.xy * vOscillations.xy);
  147. fOsc = 0.75h + (fOsc + 3.33h) * 0.33h;
  148. half fObjPhase = dot(objectWorldPos, 1);
  149. half fBranchPhase = fObjPhase + animParams.r;
  150. half fVtxPhase = dot(positionOS.xyz, animParams.g + fBranchPhase);
  151. // Factor in bending params from Material
  152. animParams.abg *= _WindMultiplier.xyz;
  153. // x is used for edges; y is used for branches
  154. float2 vWavesIn = _Time.yy + half2(fVtxPhase, fBranchPhase); // changed to float (android issues)
  155. // 1.975, 0.793, 0.375, 0.193 are good frequencies
  156. half4 vWaves = frac( vWavesIn.xxyy * float4(1.975f, 0.793f, 0.375f, 0.193f) ) * 2.0f - 1.0f; // changed to float (android issues)
  157. vWaves = SmoothTriangleWave( vWaves );
  158. half2 vWavesSum = vWaves.xz + vWaves.yw;
  159. // Primary bending / animated by * fOsc.x
  160. positionOS.xz += animParams.a * windDir.xz * foliageMainWindStrengthFromZone * fOsc.x;
  161. float3 bend = normalOS.xyz * (animParams.g * fDetailAmp);
  162. bend.y = animParams.b * fBranchAmp;
  163. positionOS.xyz += ( (vWavesSum.xyx * bend) + (animParams.b * windDir * fOsc.y * vWavesSum.y) ) * foliageMainWindStrengthFromZone;
  164. #endif
  165. positionOS.xyz = normalize(positionOS.xyz) * origLength;
  166. }
  167. #endif