particles_copy.glsl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. bool order_by_lifetime;
  43. uint lifetime_split;
  44. bool lifetime_reverse;
  45. uint pad;
  46. }
  47. params;
  48. #define TRANSFORM_ALIGN_DISABLED 0
  49. #define TRANSFORM_ALIGN_Z_BILLBOARD 1
  50. #define TRANSFORM_ALIGN_Y_TO_VELOCITY 2
  51. #define TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY 3
  52. void main() {
  53. #ifdef MODE_FILL_SORT_BUFFER
  54. uint particle = gl_GlobalInvocationID.x;
  55. if (particle >= params.total_particles) {
  56. return; //discard
  57. }
  58. uint src_particle = particle;
  59. if (params.trail_size > 1) {
  60. src_particle = src_particle * params.trail_size + params.trail_size / 2; //use trail center for sorting
  61. }
  62. sort_buffer.data[particle].x = dot(params.sort_direction, particles.data[src_particle].xform[3].xyz);
  63. sort_buffer.data[particle].y = float(particle);
  64. #endif
  65. #ifdef MODE_FILL_INSTANCES
  66. uint particle = gl_GlobalInvocationID.x;
  67. if (particle >= params.total_particles) {
  68. return; //discard
  69. }
  70. #ifdef USE_SORT_BUFFER
  71. if (params.trail_size > 1) {
  72. particle = uint(sort_buffer.data[particle / params.trail_size].y) + (particle % params.trail_size);
  73. } else {
  74. particle = uint(sort_buffer.data[particle].y); //use index from sort buffer
  75. }
  76. #else
  77. if (params.order_by_lifetime) {
  78. if (params.trail_size > 1) {
  79. uint limit = (params.total_particles / params.trail_size) - params.lifetime_split;
  80. uint base_index = particle / params.trail_size;
  81. uint base_offset = particle % params.trail_size;
  82. if (params.lifetime_reverse) {
  83. base_index = (params.total_particles / params.trail_size) - base_index - 1;
  84. }
  85. if (base_index < limit) {
  86. base_index = params.lifetime_split + base_index;
  87. } else {
  88. base_index -= limit;
  89. }
  90. particle = base_index * params.trail_size + base_offset;
  91. } else {
  92. uint limit = params.total_particles - params.lifetime_split;
  93. if (params.lifetime_reverse) {
  94. particle = params.total_particles - particle - 1;
  95. }
  96. if (particle < limit) {
  97. particle = params.lifetime_split + particle;
  98. } else {
  99. particle -= limit;
  100. }
  101. }
  102. }
  103. #endif // USE_SORT_BUFFER
  104. mat4 txform;
  105. if (bool(particles.data[particle].flags & PARTICLE_FLAG_ACTIVE) || bool(particles.data[particle].flags & PARTICLE_FLAG_TRAILED)) {
  106. txform = particles.data[particle].xform;
  107. if (params.trail_size > 1) {
  108. // Since the steps don't fit precisely in the history frames, must do a tiny bit of
  109. // interpolation to get them close to their intended location.
  110. uint part_ofs = particle % params.trail_size;
  111. float natural_ofs = fract((float(part_ofs) / float(params.trail_size)) * float(params.trail_total)) * params.frame_delta;
  112. txform[3].xyz -= particles.data[particle].velocity * natural_ofs;
  113. }
  114. switch (params.align_mode) {
  115. case TRANSFORM_ALIGN_DISABLED: {
  116. } break; //nothing
  117. case TRANSFORM_ALIGN_Z_BILLBOARD: {
  118. mat3 local = mat3(normalize(cross(params.align_up, params.sort_direction)), params.align_up, params.sort_direction);
  119. local = local * mat3(txform);
  120. txform[0].xyz = local[0];
  121. txform[1].xyz = local[1];
  122. txform[2].xyz = local[2];
  123. } break;
  124. case TRANSFORM_ALIGN_Y_TO_VELOCITY: {
  125. vec3 v = particles.data[particle].velocity;
  126. float s = (length(txform[0]) + length(txform[1]) + length(txform[2])) / 3.0;
  127. if (length(v) > 0.0) {
  128. txform[1].xyz = normalize(v);
  129. } else {
  130. txform[1].xyz = normalize(txform[1].xyz);
  131. }
  132. txform[0].xyz = normalize(cross(txform[1].xyz, txform[2].xyz));
  133. txform[2].xyz = vec3(0.0, 0.0, 1.0) * s;
  134. txform[0].xyz *= s;
  135. txform[1].xyz *= s;
  136. } break;
  137. case TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY: {
  138. vec3 v = particles.data[particle].velocity;
  139. vec3 sv = v - params.sort_direction * dot(params.sort_direction, v); //screen velocity
  140. float s = (length(txform[0]) + length(txform[1]) + length(txform[2])) / 3.0;
  141. if (length(sv) == 0) {
  142. sv = params.align_up;
  143. }
  144. sv = normalize(sv);
  145. txform[0].xyz = normalize(cross(sv, params.sort_direction)) * s;
  146. txform[1].xyz = sv * s;
  147. txform[2].xyz = params.sort_direction * s;
  148. } break;
  149. }
  150. txform[3].xyz += particles.data[particle].velocity * params.frame_remainder;
  151. if (params.trail_size > 1) {
  152. uint part_ofs = particle % params.trail_size;
  153. txform = txform * trail_bind_poses.data[part_ofs];
  154. }
  155. txform = transpose(txform);
  156. } else {
  157. txform = mat4(vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0)); //zero scale, becomes invisible
  158. }
  159. #ifdef MODE_2D
  160. uint write_offset = gl_GlobalInvocationID.x * (2 + 1 + 1); //xform + color + custom
  161. instances.data[write_offset + 0] = txform[0];
  162. instances.data[write_offset + 1] = txform[1];
  163. instances.data[write_offset + 2] = particles.data[particle].color;
  164. instances.data[write_offset + 3] = particles.data[particle].custom;
  165. #else
  166. uint write_offset = gl_GlobalInvocationID.x * (3 + 1 + 1); //xform + color + custom
  167. instances.data[write_offset + 0] = txform[0];
  168. instances.data[write_offset + 1] = txform[1];
  169. instances.data[write_offset + 2] = txform[2];
  170. instances.data[write_offset + 3] = particles.data[particle].color;
  171. instances.data[write_offset + 4] = particles.data[particle].custom;
  172. #endif //MODE_2D
  173. #endif
  174. }