Advertisement
Guest User

Untitled

a guest
Jul 18th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #pragma kernel ResetKernel
  2. #pragma kernel StepKernel
  3.  
  4. static int nthreads = 1;
  5.  
  6. RWTexture2D<float4> readC;
  7. RWTexture2D<float4> readD;
  8.  
  9.  
  10. RWTexture2D<float4> writeC;
  11. RWTexture2D<float4> writeD;
  12.  
  13. int resolution;
  14.  
  15. float4 S(RWTexture2D<float4> target, uint2 id)
  16. {
  17. return target[(id.xy + resolution)%resolution];
  18. }
  19.  
  20. [numthreads(nthreads, nthreads, 1)]
  21. void ResetKernel(uint3 id : SV_DispatchThreadID)
  22. {
  23.  
  24. float4 values = float4(0, 0, 0, 0);
  25.  
  26. if (distance(id.xy, float2(resolution / 2.0, resolution / 2.0)) < 10)
  27. {
  28. values.xy = float2(0,.25);
  29. }
  30.  
  31. readC[id.xy] = values;
  32. readD[id.xy] = float4(0, 0, 0, 1);
  33.  
  34. writeC[id.xy] = values;
  35. writeD[id.xy] = float4(0, 0, 0, 1);
  36. }
  37.  
  38. void Fluid(RWTexture2D<float4> target, uint2 id, float2 offset, float4 values, inout float2 velocity, inout float pressure, inout float divergence, inout float neighbors)
  39. {
  40. float2 v = target[id.xy+offset].xy;
  41. float4 s = S(target, id.xy + offset.xy - v);
  42.  
  43. offset = normalize(offset);
  44.  
  45. velocity += offset * (s.w - values.w);
  46.  
  47. pressure += s.w;
  48.  
  49. divergence += dot(offset.xy, s.xy);
  50.  
  51. ++neighbors;
  52. }
  53.  
  54. void StepVelocity(uint3 id, RWTexture2D<float4> write, RWTexture2D<float4> read, bool addJet)
  55. {
  56. float4 values = S(read, id.xy);
  57. values = S(read, id.xy - values.xy);
  58.  
  59. float2 velocity = float2(0,0);
  60. float pressure = 0;
  61. float neighbors = 0;
  62. float divergence = 0;
  63.  
  64. Fluid(read, id.xy, float2(0, 1), values, velocity, pressure, divergence, neighbors);
  65.  
  66. Fluid(read, id.xy, float2(0, -1), values, velocity, pressure, divergence, neighbors);
  67.  
  68. Fluid(read, id.xy, float2(1, 0), values, velocity, pressure, divergence, neighbors);
  69. Fluid(read, id.xy, float2(-1, 0), values, velocity, pressure, divergence, neighbors);
  70.  
  71. velocity = velocity / neighbors;
  72. divergence = divergence / neighbors;
  73. pressure = pressure/neighbors;
  74.  
  75. values.w = pressure - divergence;
  76. values.xy -= velocity;
  77.  
  78. if (addJet && distance(id.xy, float2(resolution / 2.0, resolution / 2.0)) < 10)
  79. values = float4(0,.25,0,values.w);
  80.  
  81. write[id.xy] = values;
  82.  
  83. }
  84.  
  85. void StepFluid(uint3 id)
  86. {
  87. float2 p = id.xy;
  88.  
  89. for (int i = 0; i < 4; i++)
  90. p -= readC[p].xy;
  91.  
  92.  
  93. float4 values = S(readD, p);
  94.  
  95. if (distance(id.xy, float2(resolution / 2.0, resolution / 2.0)) < 10)
  96. values = float4(0, 0, 1, 1);
  97.  
  98. writeD[id.xy] = S(writeC, id.xy);
  99.  
  100. }
  101.  
  102. [numthreads(nthreads, nthreads, 1)]
  103. void StepKernel(uint3 id : SV_DispatchThreadID)
  104. {
  105. StepVelocity(id, writeC, readC, true);
  106.  
  107. StepFluid(id);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement