FlowFieldPS.usf 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2023 ZSttt, Inc. All Rights Reserved.
  2. #include "/Engine/Public/Platform.ush"
  3. #include "/Engine/Private/Common.ush"
  4. Texture2D FlowFieldMap;
  5. SamplerState FlowFieldMapSampler;
  6. Texture2D ColorMap;
  7. SamplerState ColorMapSampler;
  8. float4 SpeedRange;
  9. void DrawParticles
  10. (
  11. in float2 Particle : TEXCOORD0,
  12. out float4 OutColor : SV_Target0
  13. )
  14. {
  15. float2 pos = Particle.xy;
  16. //UE Transform
  17. pos.y = 1.0 - pos.y;
  18. float4 value = Texture2DSample(FlowFieldMap, FlowFieldMapSampler, pos);
  19. if (value.a == 0.0)
  20. {
  21. OutColor = float4(0,0,0,0);
  22. }
  23. else
  24. {
  25. float2 velocity = lerp(SpeedRange.xy, SpeedRange.ba, value.xy);
  26. float speed = length(velocity) / length(SpeedRange.zw);
  27. OutColor = Texture2DSample(ColorMap, ColorMapSampler, float2(speed, 0.5));
  28. }
  29. }
  30. Texture2D<float4> Texture;
  31. float FadeOpacity;
  32. float FadeOutOpacity;
  33. void DrawScreen
  34. (
  35. in FScreenVertexOutput Input,
  36. out float4 OutColor : SV_Target0
  37. )
  38. {
  39. float4 Color = Texture.Load(uint3(Input.Position.xy, 0));
  40. if (Color.a < FadeOutOpacity)
  41. OutColor = float4(0,0,0,0);
  42. else
  43. {
  44. OutColor = Color;
  45. OutColor.a = floor(255.0 * Color.a * FadeOpacity) / 255.0;
  46. }
  47. //OutColor = floor(255.0 * Color * FadeOpacity) / 255.0;
  48. }
  49. Texture2D<float4> Particles;
  50. uint2 FlowFieldMapResolution;
  51. float RandomSeed;
  52. uint2 ParticlesResolution;
  53. float SpeedFactor;
  54. float RandomRate;
  55. float DropRate;
  56. float Spherical;
  57. static const float3 rand_constants = float3(12.9898, 78.233, 4375.85453);
  58. float rand(float2 uv)
  59. {
  60. float t = dot(rand_constants.xy, uv);
  61. return frac(sin(t) * (rand_constants.z + t));
  62. }
  63. float4 GetFlowFieldValue(float2 UV)
  64. {
  65. //UE Transform
  66. float2 pos = float2(UV.x, 1.0 - UV.y);
  67. return Texture2DSample(FlowFieldMap, FlowFieldMapSampler, pos);
  68. }
  69. float2 lookup(float2 uv)
  70. {
  71. float2 px = 1.0 / (float) FlowFieldMapResolution;
  72. float2 vc = (floor(uv * (float) FlowFieldMapResolution)) * px;
  73. float2 f = frac(uv * (float) FlowFieldMapResolution);
  74. float2 tl = GetFlowFieldValue(vc).xy;
  75. float2 tr = GetFlowFieldValue(vc + float2(px.x, 0)).xy;
  76. float2 bl = GetFlowFieldValue(vc + float2(0, px.y)).xy;
  77. float2 br = GetFlowFieldValue(vc + px).xy;
  78. return lerp(lerp(tl, tr, f.x), lerp(bl, br, f.x), f.y);
  79. }
  80. void UpdateParticles
  81. (
  82. in FScreenVertexOutput Input,
  83. out float4 OutColor : SV_Target0
  84. )
  85. {
  86. float4 particle = Particles.Load(uint3(Input.Position.xy, 0));
  87. float2 pos = float2(particle.r / 255.0 + particle.b, particle.g / 255.0 + particle.a);
  88. float4 color = GetFlowFieldValue(pos);
  89. if (color.a == 0.0)
  90. {
  91. pos = frac(1.0 + pos + (RandomSeed * pos));
  92. float2 seed = (pos + Input.UV) * RandomSeed;
  93. pos = float2(rand(seed + 1.3), rand(seed + 2.1));
  94. }
  95. else
  96. {
  97. float2 velocity = lerp(SpeedRange.xy, SpeedRange.zw, lookup(pos));
  98. float speed = length(velocity) / length(SpeedRange.ba);
  99. float2 offset = 0.f;
  100. if (Spherical > 0.f)
  101. {
  102. float distortion = cos(radians(pos.y * 180.0 - 90.0));
  103. offset = float2(velocity.x / distortion, velocity.y) * 0.0001 * SpeedFactor;
  104. }
  105. else
  106. {
  107. offset = float2(velocity.x, velocity.y) * 0.0001 * SpeedFactor;
  108. }
  109. pos = frac(1.0 + pos + offset);
  110. float2 seed = (pos + Input.UV) * RandomSeed;
  111. float drop_rate = RandomRate + speed * DropRate;
  112. float drop = step(1.0 - drop_rate, rand(seed));
  113. float2 random_pos = float2(rand(seed + 1.3), rand(seed + 2.1));
  114. pos = lerp(pos, random_pos, drop);
  115. }
  116. OutColor = float4(frac(pos * 255.0), floor(pos * 255.0) / 255.0);
  117. }