particles_copy.glsl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #[compute]
  2. #version 450
  3. #VERSION_DEFINES
  4. layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
  5. #define PARTICLE_FLAG_ACTIVE uint(1)
  6. #define PARTICLE_FLAG_STARTED uint(2)
  7. #define PARTICLE_FLAG_TRAILED uint(4)
  8. struct ParticleData {
  9. mat4 xform;
  10. vec3 velocity;
  11. uint flags;
  12. vec4 color;
  13. vec4 custom;
  14. };
  15. layout(set = 0, binding = 1, std430) restrict readonly buffer Particles {
  16. ParticleData data[];
  17. }
  18. particles;
  19. layout(set = 0, binding = 2, std430) restrict writeonly buffer Transforms {
  20. vec4 data[];
  21. }
  22. instances;
  23. #ifdef USE_SORT_BUFFER
  24. layout(set = 1, binding = 0, std430) restrict buffer SortBuffer {
  25. vec2 data[];
  26. }
  27. sort_buffer;
  28. #endif // USE_SORT_BUFFER
  29. layout(set = 2, binding = 0, std430) restrict readonly buffer TrailBindPoses {
  30. mat4 data[];
  31. }
  32. trail_bind_poses;
  33. layout(push_constant, binding = 0, std430) uniform Params {
  34. vec3 sort_direction;
  35. uint total_particles;
  36. uint trail_size;
  37. uint trail_total;
  38. float frame_delta;
  39. float frame_remainder;
  40. vec3 align_up;
  41. uint align_mode;
  42. }
  43. params;
  44. #define TRANSFORM_ALIGN_DISABLED 0
  45. #define TRANSFORM_ALIGN_Z_BILLBOARD 1
  46. #define TRANSFORM_ALIGN_Y_TO_VELOCITY 2
  47. #define TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY 3
  48. void main() {
  49. #ifdef MODE_FILL_SORT_BUFFER
  50. uint particle = gl_GlobalInvocationID.x;
  51. if (particle >= params.total_particles) {
  52. return; //discard
  53. }
  54. uint src_particle = particle;
  55. if (params.trail_size > 1) {
  56. src_particle = src_particle * params.trail_size + params.trail_size / 2; //use trail center for sorting
  57. }
  58. sort_buffer.data[particle].x = dot(params.sort_direction, particles.data[src_particle].xform[3].xyz);
  59. sort_buffer.data[particle].y = float(particle);
  60. #endif
  61. #ifdef MODE_FILL_INSTANCES
  62. uint particle = gl_GlobalInvocationID.x;
  63. uint write_offset = gl_GlobalInvocationID.x * (3 + 1 + 1); //xform + color + custom
  64. if (particle >= params.total_particles) {
  65. return; //discard
  66. }
  67. #ifdef USE_SORT_BUFFER
  68. if (params.trail_size > 1) {
  69. particle = uint(sort_buffer.data[particle / params.trail_size].y) + (particle % params.trail_size);
  70. } else {
  71. particle = uint(sort_buffer.data[particle].y); //use index from sort buffer
  72. }
  73. #endif
  74. mat4 txform;
  75. if (bool(particles.data[particle].flags & PARTICLE_FLAG_ACTIVE) || bool(particles.data[particle].flags & PARTICLE_FLAG_TRAILED)) {
  76. txform = particles.data[particle].xform;
  77. if (params.trail_size > 1) {
  78. // since the steps dont fit precisely in the history frames, must do a tiny bit of
  79. // interpolation to get them close to their intended location.
  80. uint part_ofs = particle % params.trail_size;
  81. float natural_ofs = fract((float(part_ofs) / float(params.trail_size)) * float(params.trail_total)) * params.frame_delta;
  82. txform[3].xyz -= particles.data[particle].velocity * natural_ofs;
  83. }
  84. switch (params.align_mode) {
  85. case TRANSFORM_ALIGN_DISABLED: {
  86. } break; //nothing
  87. case TRANSFORM_ALIGN_Z_BILLBOARD: {
  88. mat3 local = mat3(normalize(cross(params.align_up, params.sort_direction)), params.align_up, params.sort_direction);
  89. local = local * mat3(txform);
  90. txform[0].xyz = local[0];
  91. txform[1].xyz = local[1];
  92. txform[2].xyz = local[2];
  93. } break;
  94. case TRANSFORM_ALIGN_Y_TO_VELOCITY: {
  95. vec3 v = particles.data[particle].velocity;
  96. float s = (length(txform[0]) + length(txform[1]) + length(txform[2])) / 3.0;
  97. if (length(v) > 0.0) {
  98. txform[1].xyz = normalize(v);
  99. } else {
  100. txform[1].xyz = normalize(txform[1].xyz);
  101. }
  102. txform[0].xyz = normalize(cross(txform[1].xyz, txform[2].xyz));
  103. txform[2].xyz = vec3(0.0, 0.0, 1.0) * s;
  104. txform[0].xyz *= s;
  105. txform[1].xyz *= s;
  106. } break;
  107. case TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY: {
  108. vec3 v = particles.data[particle].velocity;
  109. vec3 sv = v - params.sort_direction * dot(params.sort_direction, v); //screen velocity
  110. float s = (length(txform[0]) + length(txform[1]) + length(txform[2])) / 3.0;
  111. if (length(sv) == 0) {
  112. sv = params.align_up;
  113. }
  114. sv = normalize(sv);
  115. txform[0].xyz = normalize(cross(sv, params.sort_direction)) * s;
  116. txform[1].xyz = sv * s;
  117. txform[2].xyz = params.sort_direction * s;
  118. } break;
  119. }
  120. txform[3].xyz += particles.data[particle].velocity * params.frame_remainder;
  121. if (params.trail_size > 1) {
  122. uint part_ofs = particle % params.trail_size;
  123. txform = txform * trail_bind_poses.data[part_ofs];
  124. }
  125. txform = transpose(txform);
  126. } else {
  127. txform = mat4(vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0)); //zero scale, becomes invisible
  128. }
  129. instances.data[write_offset + 0] = txform[0];
  130. instances.data[write_offset + 1] = txform[1];
  131. instances.data[write_offset + 2] = txform[2];
  132. instances.data[write_offset + 3] = particles.data[particle].color;
  133. instances.data[write_offset + 4] = particles.data[particle].custom;
  134. #endif
  135. }