No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SpeedTree9Wind.hlsl 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. // Unity built-in shader source. Copyright (c) 2024 Unity Technologies. MIT license (see license.txt)
  2. #ifndef SPEEDTREE_WIND_9_INCLUDED
  3. #define SPEEDTREE_WIND_9_INCLUDED
  4. #define SPEEDTREE_VERSION_9
  5. #include "SpeedTreeCommon.hlsl"
  6. //
  7. // DATA DEFINITIONS
  8. //
  9. struct WindBranchState // 8 floats | 32B
  10. {
  11. float3 m_vNoisePosTurbulence;
  12. float m_fIndependence;
  13. float m_fBend;
  14. float m_fOscillation;
  15. float m_fTurbulence;
  16. float m_fFlexibility;
  17. };
  18. struct WindRippleState // 8 floats | 32B
  19. {
  20. float3 m_vNoisePosTurbulence;
  21. float m_fIndependence;
  22. float m_fPlanar;
  23. float m_fDirectional;
  24. float m_fFlexibility;
  25. float m_fShimmer;
  26. };
  27. struct CBufferSpeedTree9 // 44 floats | 176B
  28. {
  29. float3 m_vWindDirection;
  30. float m_fWindStrength;
  31. float3 m_vTreeExtents;
  32. float m_fSharedHeightStart;
  33. float m_fBranch1StretchLimit;
  34. float m_fBranch2StretchLimit;
  35. float m_fWindIndependence;
  36. float m_fImportScaling;
  37. WindBranchState m_sShared;
  38. WindBranchState m_sBranch1;
  39. WindBranchState m_sBranch2;
  40. WindRippleState m_sRipple;
  41. };
  42. CBUFFER_START(SpeedTreeWind)
  43. float4 _ST_WindVector;
  44. float4 _ST_TreeExtents_SharedHeightStart;
  45. float4 _ST_BranchStretchLimits;
  46. float4 _ST_Shared_NoisePosTurbulence_Independence;
  47. float4 _ST_Shared_Bend_Oscillation_Turbulence_Flexibility;
  48. float4 _ST_Branch1_NoisePosTurbulence_Independence;
  49. float4 _ST_Branch1_Bend_Oscillation_Turbulence_Flexibility;
  50. float4 _ST_Branch2_NoisePosTurbulence_Independence;
  51. float4 _ST_Branch2_Bend_Oscillation_Turbulence_Flexibility;
  52. float4 _ST_Ripple_NoisePosTurbulence_Independence;
  53. float4 _ST_Ripple_Planar_Directional_Flexibility_Shimmer;
  54. CBUFFER_END
  55. CBUFFER_START(SpeedTreeWindHistory)
  56. float4 _ST_HistoryWindVector;
  57. float4 _ST_HistoryTreeExtents_SharedHeightStart;
  58. float4 _ST_HistoryBranchStretchLimits;
  59. float4 _ST_HistoryShared_NoisePosTurbulence_Independence;
  60. float4 _ST_HistoryShared_Bend_Oscillation_Turbulence_Flexibility;
  61. float4 _ST_HistoryBranch1_NoisePosTurbulence_Independence;
  62. float4 _ST_HistoryBranch1_Bend_Oscillation_Turbulence_Flexibility;
  63. float4 _ST_HistoryBranch2_NoisePosTurbulence_Independence;
  64. float4 _ST_HistoryBranch2_Bend_Oscillation_Turbulence_Flexibility;
  65. float4 _ST_HistoryRipple_NoisePosTurbulence_Independence;
  66. float4 _ST_HistoryRipple_Planar_Directional_Flexibility_Shimmer;
  67. CBUFFER_END
  68. #ifdef UNITY_DOTS_INSTANCING_ENABLED
  69. #define DOTS_ST_WindVector DOTS_ST_WindParam0
  70. #define DOTS_ST_TreeExtents_SharedHeightStart DOTS_ST_WindParam1
  71. #define DOTS_ST_BranchStretchLimits DOTS_ST_WindParam2
  72. #define DOTS_ST_Shared_NoisePosTurbulence_Independence DOTS_ST_WindParam3
  73. #define DOTS_ST_Shared_Bend_Oscillation_Turbulence_Flexibility DOTS_ST_WindParam4
  74. #define DOTS_ST_Branch1_NoisePosTurbulence_Independence DOTS_ST_WindParam5
  75. #define DOTS_ST_Branch1_Bend_Oscillation_Turbulence_Flexibility DOTS_ST_WindParam6
  76. #define DOTS_ST_Branch2_NoisePosTurbulence_Independence DOTS_ST_WindParam7
  77. #define DOTS_ST_Branch2_Bend_Oscillation_Turbulence_Flexibility DOTS_ST_WindParam8
  78. #define DOTS_ST_Ripple_NoisePosTurbulence_Independence DOTS_ST_WindParam9
  79. #define DOTS_ST_Ripple_Planar_Directional_Flexibility_Shimmer DOTS_ST_WindParam10
  80. #define DOTS_ST_HistoryWindVector DOTS_ST_WindHistoryParam0
  81. #define DOTS_ST_HistoryTreeExtents_SharedHeightStart DOTS_ST_WindHistoryParam1
  82. #define DOTS_ST_HistoryBranchStretchLimits DOTS_ST_WindHistoryParam2
  83. #define DOTS_ST_HistoryShared_NoisePosTurbulence_Independence DOTS_ST_WindHistoryParam3
  84. #define DOTS_ST_HistoryShared_Bend_Oscillation_Turbulence_Flexibility DOTS_ST_WindHistoryParam4
  85. #define DOTS_ST_HistoryBranch1_NoisePosTurbulence_Independence DOTS_ST_WindHistoryParam5
  86. #define DOTS_ST_HistoryBranch1_Bend_Oscillation_Turbulence_Flexibility DOTS_ST_WindHistoryParam6
  87. #define DOTS_ST_HistoryBranch2_NoisePosTurbulence_Independence DOTS_ST_WindHistoryParam7
  88. #define DOTS_ST_HistoryBranch2_Bend_Oscillation_Turbulence_Flexibility DOTS_ST_WindHistoryParam8
  89. #define DOTS_ST_HistoryRipple_NoisePosTurbulence_Independence DOTS_ST_WindHistoryParam9
  90. #define DOTS_ST_HistoryRipple_Planar_Directional_Flexibility_Shimmer DOTS_ST_WindHistoryParam10
  91. UNITY_DOTS_INSTANCING_START(UserPropertyMetadata)
  92. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_WindVector)
  93. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_TreeExtents_SharedHeightStart)
  94. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_BranchStretchLimits)
  95. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_Shared_NoisePosTurbulence_Independence)
  96. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_Shared_Bend_Oscillation_Turbulence_Flexibility)
  97. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_Branch1_NoisePosTurbulence_Independence)
  98. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_Branch1_Bend_Oscillation_Turbulence_Flexibility)
  99. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_Branch2_NoisePosTurbulence_Independence)
  100. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_Branch2_Bend_Oscillation_Turbulence_Flexibility)
  101. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_Ripple_NoisePosTurbulence_Independence)
  102. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_Ripple_Planar_Directional_Flexibility_Shimmer)
  103. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryWindVector)
  104. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryTreeExtents_SharedHeightStart)
  105. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryBranchStretchLimits)
  106. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryShared_NoisePosTurbulence_Independence)
  107. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryShared_Bend_Oscillation_Turbulence_Flexibility)
  108. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryBranch1_NoisePosTurbulence_Independence)
  109. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryBranch1_Bend_Oscillation_Turbulence_Flexibility)
  110. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryBranch2_NoisePosTurbulence_Independence)
  111. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryBranch2_Bend_Oscillation_Turbulence_Flexibility)
  112. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryRipple_NoisePosTurbulence_Independence)
  113. UNITY_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryRipple_Planar_Directional_Flexibility_Shimmer)
  114. UNITY_DOTS_INSTANCING_END(UserPropertyMetadata)
  115. #define _ST_WindVector UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_WindVector)
  116. #define _ST_TreeExtents_SharedHeightStart UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_TreeExtents_SharedHeightStart)
  117. #define _ST_BranchStretchLimits UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_BranchStretchLimits)
  118. #define _ST_Shared_NoisePosTurbulence_Independence UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_Shared_NoisePosTurbulence_Independence)
  119. #define _ST_Shared_Bend_Oscillation_Turbulence_Flexibility UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_Shared_Bend_Oscillation_Turbulence_Flexibility)
  120. #define _ST_Branch1_NoisePosTurbulence_Independence UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_Branch1_NoisePosTurbulence_Independence)
  121. #define _ST_Branch1_Bend_Oscillation_Turbulence_Flexibility UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_Branch1_Bend_Oscillation_Turbulence_Flexibility)
  122. #define _ST_Branch2_NoisePosTurbulence_Independence UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_Branch2_NoisePosTurbulence_Independence)
  123. #define _ST_Branch2_Bend_Oscillation_Turbulence_Flexibility UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_Branch2_Bend_Oscillation_Turbulence_Flexibility)
  124. #define _ST_Ripple_NoisePosTurbulence_Independence UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_Ripple_NoisePosTurbulence_Independence)
  125. #define _ST_Ripple_Planar_Directional_Flexibility_Shimmer UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_Ripple_Planar_Directional_Flexibility_Shimmer)
  126. #define _ST_HistoryWindVector UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryWindVector)
  127. #define _ST_HistoryTreeExtents_SharedHeightStart UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryTreeExtents_SharedHeightStart)
  128. #define _ST_HistoryBranchStretchLimits UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryBranchStretchLimits)
  129. #define _ST_HistoryShared_NoisePosTurbulence_Independence UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryShared_NoisePosTurbulence_Independence)
  130. #define _ST_HistoryShared_Bend_Oscillation_Turbulence_Flexibility UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryShared_Bend_Oscillation_Turbulence_Flexibility)
  131. #define _ST_HistoryBranch1_NoisePosTurbulence_Independence UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryBranch1_NoisePosTurbulence_Independence)
  132. #define _ST_HistoryBranch1_Bend_Oscillation_Turbulence_Flexibility UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryBranch1_Bend_Oscillation_Turbulence_Flexibility)
  133. #define _ST_HistoryBranch2_NoisePosTurbulence_Independence UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryBranch2_NoisePosTurbulence_Independence)
  134. #define _ST_HistoryBranch2_Bend_Oscillation_Turbulence_Flexibility UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryBranch2_Bend_Oscillation_Turbulence_Flexibility)
  135. #define _ST_HistoryRipple_NoisePosTurbulence_Independence UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryRipple_NoisePosTurbulence_Independence)
  136. #define _ST_HistoryRipple_Planar_Directional_Flexibility_Shimmer UNITY_ACCESS_DOTS_INSTANCED_PROP(float4, DOTS_ST_HistoryRipple_Planar_Directional_Flexibility_Shimmer)
  137. #endif
  138. CBufferSpeedTree9 ReadCBuffer(bool bHistory /*must be known compile-time*/)
  139. {
  140. CBufferSpeedTree9 cb;
  141. cb.m_vWindDirection = bHistory ? _ST_HistoryWindVector.xyz : _ST_WindVector.xyz;
  142. cb.m_fWindStrength = bHistory ? _ST_HistoryWindVector.w : _ST_WindVector.w;
  143. cb.m_vTreeExtents = bHistory ? _ST_HistoryTreeExtents_SharedHeightStart.xyz : _ST_TreeExtents_SharedHeightStart.xyz;
  144. cb.m_fSharedHeightStart = bHistory ? _ST_HistoryTreeExtents_SharedHeightStart.w : _ST_TreeExtents_SharedHeightStart.w;
  145. cb.m_fBranch1StretchLimit = bHistory ? _ST_HistoryBranchStretchLimits.x : _ST_BranchStretchLimits.x;
  146. cb.m_fBranch2StretchLimit = bHistory ? _ST_HistoryBranchStretchLimits.y : _ST_BranchStretchLimits.y;
  147. cb.m_fWindIndependence = bHistory ? _ST_HistoryBranchStretchLimits.z : _ST_BranchStretchLimits.z;
  148. cb.m_fImportScaling = bHistory ? _ST_HistoryBranchStretchLimits.w : _ST_BranchStretchLimits.w;
  149. // Shared Wind State
  150. cb.m_sShared.m_vNoisePosTurbulence = bHistory ? _ST_HistoryShared_NoisePosTurbulence_Independence.xyz : _ST_Shared_NoisePosTurbulence_Independence.xyz;
  151. cb.m_sShared.m_fIndependence = bHistory ? _ST_HistoryShared_NoisePosTurbulence_Independence.w : _ST_Shared_NoisePosTurbulence_Independence.w;
  152. cb.m_sShared.m_fBend = bHistory ? _ST_HistoryShared_Bend_Oscillation_Turbulence_Flexibility.x : _ST_Shared_Bend_Oscillation_Turbulence_Flexibility.x;
  153. cb.m_sShared.m_fOscillation = bHistory ? _ST_HistoryShared_Bend_Oscillation_Turbulence_Flexibility.y : _ST_Shared_Bend_Oscillation_Turbulence_Flexibility.y;
  154. cb.m_sShared.m_fTurbulence = bHistory ? _ST_HistoryShared_Bend_Oscillation_Turbulence_Flexibility.z : _ST_Shared_Bend_Oscillation_Turbulence_Flexibility.z;
  155. cb.m_sShared.m_fFlexibility = bHistory ? _ST_HistoryShared_Bend_Oscillation_Turbulence_Flexibility.w : _ST_Shared_Bend_Oscillation_Turbulence_Flexibility.w;
  156. // Branch1 Wind State
  157. cb.m_sBranch1.m_vNoisePosTurbulence = bHistory ? _ST_HistoryBranch1_NoisePosTurbulence_Independence.xyz : _ST_Branch1_NoisePosTurbulence_Independence.xyz;
  158. cb.m_sBranch1.m_fIndependence = bHistory ? _ST_HistoryBranch1_NoisePosTurbulence_Independence.w : _ST_Branch1_NoisePosTurbulence_Independence.w;
  159. cb.m_sBranch1.m_fBend = bHistory ? _ST_HistoryBranch1_Bend_Oscillation_Turbulence_Flexibility.x : _ST_Branch1_Bend_Oscillation_Turbulence_Flexibility.x;
  160. cb.m_sBranch1.m_fOscillation = bHistory ? _ST_HistoryBranch1_Bend_Oscillation_Turbulence_Flexibility.y : _ST_Branch1_Bend_Oscillation_Turbulence_Flexibility.y;
  161. cb.m_sBranch1.m_fTurbulence = bHistory ? _ST_HistoryBranch1_Bend_Oscillation_Turbulence_Flexibility.z : _ST_Branch1_Bend_Oscillation_Turbulence_Flexibility.z;
  162. cb.m_sBranch1.m_fFlexibility = bHistory ? _ST_HistoryBranch1_Bend_Oscillation_Turbulence_Flexibility.w : _ST_Branch1_Bend_Oscillation_Turbulence_Flexibility.w;
  163. // Branch2 Wind State
  164. cb.m_sBranch2.m_vNoisePosTurbulence = bHistory ? _ST_HistoryBranch2_NoisePosTurbulence_Independence.xyz : _ST_Branch2_NoisePosTurbulence_Independence.xyz;
  165. cb.m_sBranch2.m_fIndependence = bHistory ? _ST_HistoryBranch2_NoisePosTurbulence_Independence.w : _ST_Branch2_NoisePosTurbulence_Independence.w;
  166. cb.m_sBranch2.m_fBend = bHistory ? _ST_HistoryBranch2_Bend_Oscillation_Turbulence_Flexibility.x : _ST_Branch2_Bend_Oscillation_Turbulence_Flexibility.x;
  167. cb.m_sBranch2.m_fOscillation = bHistory ? _ST_HistoryBranch2_Bend_Oscillation_Turbulence_Flexibility.y : _ST_Branch2_Bend_Oscillation_Turbulence_Flexibility.y;
  168. cb.m_sBranch2.m_fTurbulence = bHistory ? _ST_HistoryBranch2_Bend_Oscillation_Turbulence_Flexibility.z : _ST_Branch2_Bend_Oscillation_Turbulence_Flexibility.z;
  169. cb.m_sBranch2.m_fFlexibility = bHistory ? _ST_HistoryBranch2_Bend_Oscillation_Turbulence_Flexibility.w : _ST_Branch2_Bend_Oscillation_Turbulence_Flexibility.w;
  170. // Ripple Wind State
  171. cb.m_sRipple.m_vNoisePosTurbulence = bHistory ? _ST_HistoryRipple_NoisePosTurbulence_Independence.xyz : _ST_Ripple_NoisePosTurbulence_Independence.xyz;
  172. cb.m_sRipple.m_fIndependence = bHistory ? _ST_HistoryRipple_NoisePosTurbulence_Independence.w : _ST_Ripple_NoisePosTurbulence_Independence.w;
  173. cb.m_sRipple.m_fPlanar = bHistory ? _ST_HistoryRipple_Planar_Directional_Flexibility_Shimmer.x : _ST_Ripple_Planar_Directional_Flexibility_Shimmer.x;
  174. cb.m_sRipple.m_fDirectional = bHistory ? _ST_HistoryRipple_Planar_Directional_Flexibility_Shimmer.y : _ST_Ripple_Planar_Directional_Flexibility_Shimmer.y;
  175. cb.m_sRipple.m_fFlexibility = bHistory ? _ST_HistoryRipple_Planar_Directional_Flexibility_Shimmer.z : _ST_Ripple_Planar_Directional_Flexibility_Shimmer.z;
  176. cb.m_sRipple.m_fShimmer = bHistory ? _ST_HistoryRipple_Planar_Directional_Flexibility_Shimmer.w : _ST_Ripple_Planar_Directional_Flexibility_Shimmer.w;
  177. cb.m_vWindDirection = TransformWindVectorFromWorldToLocalSpace(cb.m_vWindDirection);
  178. return cb;
  179. }
  180. //
  181. // UTILS
  182. //
  183. float NoiseHash(float n) { return frac(sin(n) * 1e4); }
  184. float NoiseHash(float2 p){ return frac(1e4 * sin(17.0f * p.x + p.y * 0.1f) * (0.1f + abs(sin(p.y * 13.0f + p.x)))); }
  185. float QNoise(float2 x)
  186. {
  187. float2 i = floor(x);
  188. float2 f = frac(x);
  189. // four corners in 2D of a tile
  190. float a = NoiseHash(i);
  191. float b = NoiseHash(i + float2(1.0, 0.0));
  192. float c = NoiseHash(i + float2(0.0, 1.0));
  193. float d = NoiseHash(i + float2(1.0, 1.0));
  194. // same code, with the clamps in smoothstep and common subexpressions optimized away.
  195. float2 u = f * f * (float2(3.0, 3.0) - float2(2.0, 2.0) * f);
  196. return lerp(a, b, u.x) + (c - a) * u.y * (1.0f - u.x) + (d - b) * u.x * u.y;
  197. }
  198. float4 RuntimeSdkNoise2DFlat(float3 vNoisePos3d)
  199. {
  200. float2 vNoisePos = vNoisePos3d.xz;
  201. #ifdef USE_ST_NOISE_TEXTURE // test this toggle during shader perf tuning
  202. return texture2D(g_samNoiseKernel, vNoisePos.xy) - float4(0.5f, 0.5f, 0.5f, 0.5f);
  203. #else
  204. // fallback, slower noise lookup method
  205. const float c_fFrequecyScale = 20.0f;
  206. const float c_fAmplitudeScale = 1.0f;
  207. const float c_fAmplitueShift = 0.0f;
  208. float fNoiseX = (QNoise(vNoisePos * c_fFrequecyScale) + c_fAmplitueShift) * c_fAmplitudeScale;
  209. float fNoiseY = (QNoise(vNoisePos.yx * 0.5f * c_fFrequecyScale) + c_fAmplitueShift) * c_fAmplitudeScale;
  210. return float4(fNoiseX, fNoiseY, fNoiseX+fNoiseY, 0.0f) - 0.5f.xxxx;
  211. #endif
  212. }
  213. float WindUtil_Square(float fValue) { return fValue * fValue; }
  214. float2 WindUtil_Square(float2 fValue) { return fValue * fValue; }
  215. float3 WindUtil_Square(float3 fValue) { return fValue * fValue; }
  216. float4 WindUtil_Square(float4 fValue) { return fValue * fValue; }
  217. float3 WindUtil_UnpackNormalizedFloat(float fValue)
  218. {
  219. float3 vReturn = frac(float3(fValue * 0.01f, fValue, fValue * 100.0f));
  220. vReturn -= 0.5f;
  221. vReturn *= 2.0f;
  222. vReturn = normalize(vReturn);
  223. return vReturn;
  224. }
  225. //
  226. // SPEEDTREE WIND 9 FUNCTIONS
  227. //
  228. // returns position offset (caller must apply to the vertex position)
  229. float3 RippleWindMotion(
  230. float3 vUp,
  231. float3 vWindDirection,
  232. float3 vVertexPositionIn,
  233. float3 vGlobalNoisePosition,
  234. float fRippleWeight,
  235. float3 vRippleNoisePosTurbulence,
  236. float fRippleIndependence,
  237. float fRippleFlexibility,
  238. float fRippleDirectional,
  239. float fRipplePlanar,
  240. float fTreeHeight,
  241. float fImportScaling
  242. )
  243. {
  244. float fImportScalingInv = (1.0f / fImportScaling);
  245. float3 vNoisePosition = vGlobalNoisePosition
  246. + vRippleNoisePosTurbulence
  247. + (vVertexPositionIn * fImportScalingInv) * fRippleIndependence
  248. + vWindDirection * fRippleFlexibility * fRippleWeight;
  249. float2 vNoise = RuntimeSdkNoise2DFlat(vNoisePosition);
  250. vNoise.r += 0.25f;
  251. float3 vMotion = vWindDirection * vNoise.r * fRippleDirectional
  252. + vUp * (vNoise.g * fRipplePlanar)
  253. ;
  254. vMotion *= fRippleWeight;
  255. return vMotion;
  256. }
  257. float3 RippleWindMotion_cb(
  258. float3 vUpVector,
  259. float3 vVertexPositionIn,
  260. float3 vGlobalNoisePosition,
  261. float fRippleWeight,
  262. in CBufferSpeedTree9 cb
  263. )
  264. {
  265. return RippleWindMotion(
  266. vUpVector,
  267. cb.m_vWindDirection,
  268. vVertexPositionIn,
  269. vGlobalNoisePosition,
  270. fRippleWeight,
  271. cb.m_sRipple.m_vNoisePosTurbulence,
  272. cb.m_sRipple.m_fIndependence,
  273. cb.m_sRipple.m_fFlexibility,
  274. cb.m_sRipple.m_fDirectional,
  275. cb.m_sRipple.m_fPlanar,
  276. cb.m_vTreeExtents.y, // y-up = height
  277. cb.m_fImportScaling
  278. );
  279. }
  280. // returns updated position
  281. float3 BranchWindPosition(
  282. float3 vUp,
  283. float3 vWindDirection,
  284. float3 vVertexPositionIn,
  285. float3 vGlobalNoisePosition,
  286. float fPackedBranchDir,
  287. float fPackedBranchNoiseOffset,
  288. float fBranchWeight,
  289. float fBranchStretchLimit,
  290. float3 vBranchNoisePosTurbulence,
  291. float fBranchIndependence,
  292. float fBranchTurbulence,
  293. float fBranchOscillation,
  294. float fBranchBend,
  295. float fBranchFlexibility,
  296. float fTreeHeight,
  297. float fImportScaling
  298. )
  299. {
  300. float fImportScalingInv = (1.0f / fImportScaling);
  301. float fLength = fBranchWeight * fBranchStretchLimit;
  302. if (fBranchWeight * fBranchStretchLimit <= 0.0f)
  303. {
  304. return vVertexPositionIn;
  305. }
  306. float3 vBranchDir = WindUtil_UnpackNormalizedFloat(fPackedBranchDir);
  307. float3 vBranchNoiseOffset = WindUtil_UnpackNormalizedFloat(fPackedBranchNoiseOffset);
  308. // SpeedTree Modeler packs Z up, rotate around X for -90deg
  309. vBranchDir = float3(vBranchDir.x, -vBranchDir.z, vBranchDir.y);
  310. vBranchNoiseOffset = float3(vBranchNoiseOffset.x, -vBranchNoiseOffset.z, vBranchNoiseOffset.y);
  311. float3 vAnchor = vVertexPositionIn - vBranchDir * fLength;
  312. vVertexPositionIn -= vAnchor;
  313. float fBranchDotWindSq = WindUtil_Square(dot(vBranchDir, vWindDirection));
  314. float3 vWind = normalize(vWindDirection + vUp * fBranchDotWindSq);
  315. // Undo modifications to fBranchIndependence:
  316. // (1) Modeler divides fBranchIndependence by fTreeHeight before export
  317. // (2) Importer scales fTreeHeight by fImportScaling during import
  318. fBranchIndependence *= (fTreeHeight * fImportScalingInv);
  319. float3 vNoisePosition = vGlobalNoisePosition
  320. + vBranchNoisePosTurbulence
  321. + vBranchNoiseOffset * fBranchIndependence
  322. + vWind * (fBranchFlexibility * fBranchWeight);
  323. float4 vNoise = RuntimeSdkNoise2DFlat(vNoisePosition);
  324. vNoise.r *= 0.65; // tune down the 'flexy' branches
  325. vNoise.g *= 0.50; // tune down the 'flexy' branches
  326. float3 vOscillationTurbulent = vUp * fBranchTurbulence;
  327. float3 vMotion = (vWind * vNoise.r + vOscillationTurbulent * vNoise.g) * fBranchOscillation;
  328. vMotion += vWind * (fBranchBend * (1.0f - vNoise.b));
  329. vMotion *= fBranchWeight;
  330. return normalize(vVertexPositionIn + vMotion) * fLength + vAnchor;
  331. }
  332. float3 BranchWindPosition_cb(
  333. float3 vUp,
  334. float3 vGlobalNoisePosition,
  335. float3 vVertexPositionIn,
  336. float fBranchWeight,
  337. float fPackedBranchDir,
  338. float fPackedBranchNoiseOffset,
  339. in CBufferSpeedTree9 cb,
  340. const int iBranch // 1 or 2, compile time constants
  341. )
  342. {
  343. if (iBranch == 1)
  344. {
  345. return BranchWindPosition(
  346. vUp,
  347. cb.m_vWindDirection,
  348. vVertexPositionIn,
  349. vGlobalNoisePosition,
  350. fPackedBranchDir,
  351. fPackedBranchNoiseOffset,
  352. fBranchWeight,
  353. cb.m_fBranch1StretchLimit,
  354. cb.m_sBranch1.m_vNoisePosTurbulence,
  355. cb.m_sBranch1.m_fIndependence,
  356. cb.m_sBranch1.m_fTurbulence,
  357. cb.m_sBranch1.m_fOscillation,
  358. cb.m_sBranch1.m_fBend,
  359. cb.m_sBranch1.m_fFlexibility,
  360. cb.m_vTreeExtents.y, // y-up = height
  361. cb.m_fImportScaling
  362. );
  363. }
  364. return BranchWindPosition(
  365. vUp,
  366. cb.m_vWindDirection,
  367. vVertexPositionIn,
  368. vGlobalNoisePosition,
  369. fPackedBranchDir,
  370. fPackedBranchNoiseOffset,
  371. fBranchWeight,
  372. cb.m_fBranch2StretchLimit,
  373. cb.m_sBranch2.m_vNoisePosTurbulence,
  374. cb.m_sBranch2.m_fIndependence,
  375. cb.m_sBranch2.m_fTurbulence,
  376. cb.m_sBranch2.m_fOscillation,
  377. cb.m_sBranch2.m_fBend,
  378. cb.m_sBranch2.m_fFlexibility,
  379. cb.m_vTreeExtents.y, // y-up = height
  380. cb.m_fImportScaling
  381. );
  382. }
  383. // returns updated position
  384. float3 SharedWindPosition(
  385. float3 vUp,
  386. float3 vWindDirection,
  387. float3 vVertexPositionIn,
  388. float3 vGlobalNoisePosition,
  389. float fTreeHeight,
  390. float fSharedHeightStart,
  391. float3 vSharedNoisePosTurbulence,
  392. float fSharedTurbulence,
  393. float fSharedOscillation,
  394. float fSharedBend,
  395. float fSharedFlexibility,
  396. float fImportScaling
  397. )
  398. {
  399. float fImportScalingInv = (1.0f / fImportScaling);
  400. float fLengthSq = dot(vVertexPositionIn, vVertexPositionIn);
  401. if (fLengthSq == 0.0f)
  402. {
  403. return vVertexPositionIn;
  404. }
  405. float fLength = sqrt(fLengthSq);
  406. float fHeight = vVertexPositionIn.y; // y-up
  407. float fMaxHeight = fTreeHeight;
  408. float fWeight = WindUtil_Square(max(fHeight - (fMaxHeight * fSharedHeightStart), 0.0f) / fMaxHeight);
  409. float3 vNoisePosition = vGlobalNoisePosition
  410. + vSharedNoisePosTurbulence;
  411. + vWindDirection * (fSharedFlexibility * fWeight);
  412. float4 vNoise = RuntimeSdkNoise2DFlat(vNoisePosition);
  413. float3 vOscillationTurbulent = cross(vWindDirection, vUp) * fSharedTurbulence;
  414. float3 vMotion = (vWindDirection * vNoise.r + vOscillationTurbulent * vNoise.g) * fSharedOscillation
  415. + vWindDirection * (fSharedBend * (1.0f - vNoise.b));
  416. ;
  417. vMotion *= fWeight;
  418. return normalize(vVertexPositionIn + vMotion) * fLength;
  419. }
  420. float3 SharedWindPosition_cb(
  421. float3 vUp,
  422. float3 vVertexPositionIn,
  423. float3 vGlobalNoisePosition,
  424. in CBufferSpeedTree9 cb
  425. )
  426. {
  427. return SharedWindPosition(
  428. vUp,
  429. cb.m_vWindDirection,
  430. vVertexPositionIn,
  431. vGlobalNoisePosition,
  432. cb.m_vTreeExtents.y, // y-up = height
  433. cb.m_fSharedHeightStart,
  434. cb.m_sShared.m_vNoisePosTurbulence,
  435. cb.m_sShared.m_fTurbulence,
  436. cb.m_sShared.m_fOscillation,
  437. cb.m_sShared.m_fBend,
  438. cb.m_sShared.m_fFlexibility,
  439. cb.m_fImportScaling
  440. );
  441. }
  442. //====================================================================================================
  443. //
  444. // SPEEDTREE WIND 9 ENTRY
  445. //
  446. float3 SpeedTree9Wind(
  447. float3 vPos,
  448. float3 vNormal,
  449. float4 vTexcoord ,
  450. float4 vTexcoord1,
  451. float4 vTexcoord2,
  452. float4 vTexcoord3,
  453. bool bHistory
  454. )
  455. {
  456. CBufferSpeedTree9 cb = ReadCBuffer(bHistory);
  457. const float fWindDirectionLengthSq = dot(cb.m_vWindDirection, cb.m_vWindDirection);
  458. if (fWindDirectionLengthSq == 0.0f) // check if we have valid wind vector
  459. {
  460. return vPos;
  461. }
  462. float3 vUp = float3(0.0, 1.0, 0.0);
  463. float3 vWindyPosition = vPos;
  464. // get world space sposition from the model matrix
  465. float3 vWorldPosition = float3(UNITY_MATRIX_M[0].w, UNITY_MATRIX_M[1].w, UNITY_MATRIX_M[2].w);
  466. #if SHADEROPTIONS_CAMERA_RELATIVE_RENDERING // HDRP has camera translation encoded in the matrix
  467. vWorldPosition += _WorldSpaceCameraPos;
  468. #endif
  469. // global noise applied to animation instances to break off synchronized
  470. // movement among multiple instances under wind effect.
  471. float3 vGlobalNoisePosition = vWorldPosition * cb.m_fWindIndependence;
  472. #if defined(_WIND_RIPPLE)
  473. {
  474. float fRippleWeight = vTexcoord1.w;
  475. float3 vMotion = RippleWindMotion_cb(
  476. vUp,
  477. vWindyPosition,
  478. vGlobalNoisePosition,
  479. fRippleWeight,
  480. cb
  481. );
  482. vWindyPosition += vMotion;
  483. #if defined(_WIND_SHIMMER)
  484. {
  485. vNormal = normalize(vNormal - (vMotion * cb.m_sRipple.m_fShimmer));
  486. }
  487. #endif
  488. }
  489. #endif
  490. #if defined(_WIND_BRANCH2)
  491. {
  492. const int BRANCH2 = 2;
  493. float fBranch2Weight = vTexcoord2.z;
  494. float fPackedBranch2Dir = vTexcoord2.y;
  495. float fPackedBranch2NoiseOffset = vTexcoord2.x;
  496. vWindyPosition = BranchWindPosition_cb(
  497. vUp,
  498. vGlobalNoisePosition,
  499. vWindyPosition,
  500. fBranch2Weight,
  501. fPackedBranch2Dir,
  502. fPackedBranch2NoiseOffset,
  503. cb,
  504. BRANCH2
  505. );
  506. }
  507. #endif
  508. #if defined(_WIND_BRANCH1)
  509. {
  510. const int BRANCH1 = 1;
  511. float fBranch1Weight = vTexcoord1.z;
  512. float fPackedBranch1Dir = vTexcoord.w;
  513. float fPackedBranch1NoiseOffset = vTexcoord.z;
  514. vWindyPosition = BranchWindPosition_cb(
  515. vUp,
  516. vGlobalNoisePosition,
  517. vWindyPosition,
  518. fBranch1Weight,
  519. fPackedBranch1Dir,
  520. fPackedBranch1NoiseOffset,
  521. cb,
  522. BRANCH1
  523. );
  524. }
  525. #endif
  526. #if defined(_WIND_SHARED)
  527. {
  528. vWindyPosition = SharedWindPosition_cb(
  529. vUp,
  530. vWindyPosition,
  531. vGlobalNoisePosition,
  532. cb
  533. );
  534. }
  535. #endif
  536. return vWindyPosition;
  537. }
  538. // This version is used by ShaderGraph
  539. void SpeedTree9Wind_float(
  540. // in
  541. float3 vPos,
  542. float3 vNormal,
  543. float4 vTexcoord0,
  544. float4 vTexcoord1,
  545. float4 vTexcoord2,
  546. float4 vTexcoord3,
  547. bool bHistory,
  548. // out
  549. out float3 outPos
  550. )
  551. {
  552. outPos = SpeedTree9Wind(
  553. vPos,
  554. vNormal,
  555. vTexcoord0,
  556. vTexcoord1,
  557. vTexcoord2,
  558. vTexcoord3,
  559. bHistory
  560. );
  561. }
  562. #endif // SPEEDTREE_WIND_9_INCLUDED