particles_copy.glsl 6.6 KB

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