Flow.cginc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. float2 CoordFromPixel(float2 pixel, float2 size)
  2. {
  3. #ifndef UNITY_HALF_TEXEL_OFFSET
  4. pixel += 0.5f;
  5. #endif
  6. return pixel / size;
  7. }
  8. float2 SnapCoordFromPixel(float2 pixel, float2 size)
  9. {
  10. return CoordFromPixel(floor(pixel), size);
  11. }
  12. float2 SnapCoord(float2 coord, float2 size)
  13. {
  14. return SnapCoordFromPixel(coord * size, size);
  15. }
  16. // Fluid
  17. struct Fluid
  18. {
  19. float Depth;
  20. float4 RGBA;
  21. float4 ESMV;
  22. float4 F123;
  23. };
  24. struct FluidData
  25. {
  26. float c : SV_Target0;
  27. float4 d : SV_Target1;
  28. float4 e : SV_Target2;
  29. float4 f : SV_Target3;
  30. };
  31. Fluid DecodeFluid(float4 c, float4 d, float4 e, float4 f)
  32. {
  33. Fluid o;
  34. o.Depth = c.x;
  35. o.RGBA = d;
  36. o.ESMV = e;
  37. o.F123 = f;
  38. return o;
  39. }
  40. FluidData EncodeFluid(Fluid f)
  41. {
  42. FluidData o;
  43. o.c = f.Depth;
  44. o.d = f.RGBA;
  45. o.e = f.ESMV;
  46. o.f = f.F123;
  47. return o;
  48. }
  49. void ContributeFluid(inout Fluid fluid, Fluid inflowFluid)
  50. {
  51. float finalDepth = fluid.Depth + inflowFluid.Depth;
  52. float scaleDepth = max(finalDepth, 0.00001f);
  53. fluid.RGBA = (fluid.RGBA * fluid.Depth + inflowFluid.RGBA * inflowFluid.Depth) / scaleDepth;
  54. fluid.ESMV = (fluid.ESMV * fluid.Depth + inflowFluid.ESMV * inflowFluid.Depth) / scaleDepth;
  55. fluid.F123 = (fluid.F123 * fluid.Depth + inflowFluid.F123 * inflowFluid.Depth) / scaleDepth;
  56. fluid.Depth = finalDepth;
  57. }
  58. void ContributeFluid(inout Fluid fluid, Fluid inflowFluid, float totalFluid)
  59. {
  60. fluid.RGBA = (fluid.RGBA * fluid.Depth + inflowFluid.RGBA) / totalFluid;
  61. fluid.ESMV = (fluid.ESMV * fluid.Depth + inflowFluid.ESMV) / totalFluid;
  62. fluid.F123 = (fluid.F123 * fluid.Depth + inflowFluid.F123) / totalFluid;
  63. }
  64. // Column
  65. struct Column
  66. {
  67. float GroundHeight;
  68. float WetHeight;
  69. float4 Outflow;
  70. };
  71. Column DecodeColumn(float4 packedA, float4 packedB)
  72. {
  73. Column o;
  74. o.GroundHeight = packedA.x;
  75. o.WetHeight = packedA.y;
  76. o.Outflow = packedB;
  77. return o;
  78. }
  79. float4 EncodeColumnA(Column unpacked)
  80. {
  81. return float4(unpacked.GroundHeight, unpacked.WetHeight, 0, 0);
  82. }
  83. // Particles
  84. struct Particle
  85. {
  86. float3 Velocity;
  87. float3 Position;
  88. float Age;
  89. float Life;
  90. };
  91. Particle DecodeParticle(float4 packedA, float4 packedB)
  92. {
  93. Particle o;
  94. o.Velocity = packedA.xyz;
  95. o.Age = packedA.w;
  96. o.Position = packedB.xyz;
  97. o.Life = packedB.w;
  98. return o;
  99. }
  100. float4 EncodeParticleA(Particle unpacked)
  101. {
  102. return float4(unpacked.Velocity, unpacked.Age);
  103. }
  104. float4 EncodeParticleB(Particle unpacked)
  105. {
  106. return float4(unpacked.Position, unpacked.Life);
  107. }