particles_copy.glsl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #ifdef USERDATA_COUNT
  15. vec4 userdata[USERDATA_COUNT];
  16. #endif
  17. };
  18. layout(set = 0, binding = 1, std430) restrict readonly buffer Particles {
  19. ParticleData data[];
  20. }
  21. particles;
  22. layout(set = 0, binding = 2, std430) restrict writeonly buffer Transforms {
  23. vec4 data[];
  24. }
  25. instances;
  26. #ifdef USE_SORT_BUFFER
  27. layout(set = 1, binding = 0, std430) restrict buffer SortBuffer {
  28. vec2 data[];
  29. }
  30. sort_buffer;
  31. #endif // USE_SORT_BUFFER
  32. layout(set = 2, binding = 0, std430) restrict readonly buffer TrailBindPoses {
  33. mat4 data[];
  34. }
  35. trail_bind_poses;
  36. layout(push_constant, std430) uniform Params {
  37. vec3 sort_direction;
  38. uint total_particles;
  39. uint trail_size;
  40. uint trail_total;
  41. float frame_delta;
  42. float frame_remainder;
  43. vec3 align_up;
  44. uint align_mode;
  45. bool order_by_lifetime;
  46. uint lifetime_split;
  47. bool lifetime_reverse;
  48. bool copy_mode_2d;
  49. mat4 inv_emission_transform;
  50. }
  51. params;
  52. #define TRANSFORM_ALIGN_DISABLED 0
  53. #define TRANSFORM_ALIGN_Z_BILLBOARD 1
  54. #define TRANSFORM_ALIGN_Y_TO_VELOCITY 2
  55. #define TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY 3
  56. void main() {
  57. #ifdef MODE_FILL_SORT_BUFFER
  58. uint particle = gl_GlobalInvocationID.x;
  59. if (particle >= params.total_particles) {
  60. return; //discard
  61. }
  62. uint src_particle = particle;
  63. if (params.trail_size > 1) {
  64. src_particle = src_particle * params.trail_size + params.trail_size / 2; //use trail center for sorting
  65. }
  66. sort_buffer.data[particle].x = dot(params.sort_direction, particles.data[src_particle].xform[3].xyz);
  67. sort_buffer.data[particle].y = float(particle);
  68. #endif
  69. #ifdef MODE_FILL_INSTANCES
  70. uint particle = gl_GlobalInvocationID.x;
  71. if (particle >= params.total_particles) {
  72. return; //discard
  73. }
  74. #ifdef USE_SORT_BUFFER
  75. if (params.trail_size > 1) {
  76. particle = uint(sort_buffer.data[particle / params.trail_size].y) + (particle % params.trail_size);
  77. } else {
  78. particle = uint(sort_buffer.data[particle].y); //use index from sort buffer
  79. }
  80. #else
  81. if (params.order_by_lifetime) {
  82. if (params.trail_size > 1) {
  83. uint limit = (params.total_particles / params.trail_size) - params.lifetime_split;
  84. uint base_index = particle / params.trail_size;
  85. uint base_offset = particle % params.trail_size;
  86. if (params.lifetime_reverse) {
  87. base_index = (params.total_particles / params.trail_size) - base_index - 1;
  88. }
  89. if (base_index < limit) {
  90. base_index = params.lifetime_split + base_index;
  91. } else {
  92. base_index -= limit;
  93. }
  94. particle = base_index * params.trail_size + base_offset;
  95. } else {
  96. uint limit = params.total_particles - params.lifetime_split;
  97. if (params.lifetime_reverse) {
  98. particle = params.total_particles - particle - 1;
  99. }
  100. if (particle < limit) {
  101. particle = params.lifetime_split + particle;
  102. } else {
  103. particle -= limit;
  104. }
  105. }
  106. }
  107. #endif // USE_SORT_BUFFER
  108. mat4 txform;
  109. if (bool(particles.data[particle].flags & PARTICLE_FLAG_ACTIVE) || bool(particles.data[particle].flags & PARTICLE_FLAG_TRAILED)) {
  110. txform = particles.data[particle].xform;
  111. if (params.trail_size > 1) {
  112. // Since the steps don't fit precisely in the history frames, must do a tiny bit of
  113. // interpolation to get them close to their intended location.
  114. uint part_ofs = particle % params.trail_size;
  115. float natural_ofs = fract((float(part_ofs) / float(params.trail_size)) * float(params.trail_total)) * params.frame_delta;
  116. txform[3].xyz -= particles.data[particle].velocity * natural_ofs;
  117. }
  118. switch (params.align_mode) {
  119. case TRANSFORM_ALIGN_DISABLED: {
  120. } break; //nothing
  121. case TRANSFORM_ALIGN_Z_BILLBOARD: {
  122. mat3 local = mat3(normalize(cross(params.align_up, params.sort_direction)), params.align_up, params.sort_direction);
  123. local = local * mat3(txform);
  124. txform[0].xyz = local[0];
  125. txform[1].xyz = local[1];
  126. txform[2].xyz = local[2];
  127. } break;
  128. case TRANSFORM_ALIGN_Y_TO_VELOCITY: {
  129. vec3 v = particles.data[particle].velocity;
  130. float s = (length(txform[0]) + length(txform[1]) + length(txform[2])) / 3.0;
  131. if (length(v) > 0.0) {
  132. txform[1].xyz = normalize(v);
  133. } else {
  134. txform[1].xyz = normalize(txform[1].xyz);
  135. }
  136. txform[0].xyz = normalize(cross(txform[1].xyz, txform[2].xyz));
  137. txform[2].xyz = vec3(0.0, 0.0, 1.0) * s;
  138. txform[0].xyz *= s;
  139. txform[1].xyz *= s;
  140. } break;
  141. case TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY: {
  142. vec3 v = particles.data[particle].velocity;
  143. vec3 sv = v - params.sort_direction * dot(params.sort_direction, v); //screen velocity
  144. float s = (length(txform[0]) + length(txform[1]) + length(txform[2])) / 3.0;
  145. if (length(sv) == 0) {
  146. sv = params.align_up;
  147. }
  148. sv = normalize(sv);
  149. txform[0].xyz = normalize(cross(sv, params.sort_direction)) * s;
  150. txform[1].xyz = sv * s;
  151. txform[2].xyz = params.sort_direction * s;
  152. } break;
  153. }
  154. txform[3].xyz += particles.data[particle].velocity * params.frame_remainder;
  155. if (params.trail_size > 1) {
  156. uint part_ofs = particle % params.trail_size;
  157. txform = txform * trail_bind_poses.data[part_ofs];
  158. }
  159. if (params.copy_mode_2d) {
  160. // In global mode, bring 2D particles to local coordinates
  161. // as they will be drawn with the node position as origin.
  162. txform = params.inv_emission_transform * txform;
  163. }
  164. txform = transpose(txform);
  165. } else {
  166. txform = mat4(vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0)); //zero scale, becomes invisible
  167. }
  168. if (params.copy_mode_2d) {
  169. uint write_offset = gl_GlobalInvocationID.x * (2 + 1 + 1); //xform + color + custom
  170. instances.data[write_offset + 0] = txform[0];
  171. instances.data[write_offset + 1] = txform[1];
  172. instances.data[write_offset + 2] = particles.data[particle].color;
  173. instances.data[write_offset + 3] = particles.data[particle].custom;
  174. } else {
  175. uint write_offset = gl_GlobalInvocationID.x * (3 + 1 + 1); //xform + color + custom
  176. instances.data[write_offset + 0] = txform[0];
  177. instances.data[write_offset + 1] = txform[1];
  178. instances.data[write_offset + 2] = txform[2];
  179. instances.data[write_offset + 3] = particles.data[particle].color;
  180. instances.data[write_offset + 4] = particles.data[particle].custom;
  181. }
  182. #endif
  183. }