particles_copy.glsl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* clang-format off */
  2. #[modes]
  3. mode_default =
  4. #[specializations]
  5. MODE_3D = false
  6. #[vertex]
  7. #include "stdlib_inc.glsl"
  8. // ParticleData
  9. layout(location = 0) in highp vec4 color;
  10. layout(location = 1) in highp vec4 velocity_flags;
  11. layout(location = 2) in highp vec4 custom;
  12. layout(location = 3) in highp vec4 xform_1;
  13. layout(location = 4) in highp vec4 xform_2;
  14. #ifdef MODE_3D
  15. layout(location = 5) in highp vec4 xform_3;
  16. #endif
  17. /* clang-format on */
  18. out highp vec4 out_xform_1; //tfb:
  19. out highp vec4 out_xform_2; //tfb:
  20. #ifdef MODE_3D
  21. out highp vec4 out_xform_3; //tfb:MODE_3D
  22. #endif
  23. flat out highp uvec4 instance_color_custom_data; //tfb:
  24. uniform lowp vec3 sort_direction;
  25. uniform highp float frame_remainder;
  26. uniform highp vec3 align_up;
  27. uniform highp uint align_mode;
  28. uniform highp mat4 inv_emission_transform;
  29. #define TRANSFORM_ALIGN_DISABLED uint(0)
  30. #define TRANSFORM_ALIGN_Z_BILLBOARD uint(1)
  31. #define TRANSFORM_ALIGN_Y_TO_VELOCITY uint(2)
  32. #define TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY uint(3)
  33. #define PARTICLE_FLAG_ACTIVE uint(1)
  34. #define FLT_MAX float(3.402823466e+38)
  35. void main() {
  36. // Set scale to zero and translate to -INF so particle will be invisible
  37. // even for materials that ignore rotation/scale (i.e. billboards).
  38. mat4 txform = mat4(vec4(0.0), vec4(0.0), vec4(0.0), vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, 0.0));
  39. if (bool(floatBitsToUint(velocity_flags.w) & PARTICLE_FLAG_ACTIVE)) {
  40. #ifdef MODE_3D
  41. txform = transpose(mat4(xform_1, xform_2, xform_3, vec4(0.0, 0.0, 0.0, 1.0)));
  42. #else
  43. txform = transpose(mat4(xform_1, xform_2, vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0)));
  44. #endif
  45. switch (align_mode) {
  46. case TRANSFORM_ALIGN_DISABLED: {
  47. } break; //nothing
  48. case TRANSFORM_ALIGN_Z_BILLBOARD: {
  49. mat3 local = mat3(normalize(cross(align_up, sort_direction)), align_up, sort_direction);
  50. local = local * mat3(txform);
  51. txform[0].xyz = local[0];
  52. txform[1].xyz = local[1];
  53. txform[2].xyz = local[2];
  54. } break;
  55. case TRANSFORM_ALIGN_Y_TO_VELOCITY: {
  56. vec3 v = velocity_flags.xyz;
  57. float s = (length(txform[0]) + length(txform[1]) + length(txform[2])) / 3.0;
  58. if (length(v) > 0.0) {
  59. txform[1].xyz = normalize(v);
  60. } else {
  61. txform[1].xyz = normalize(txform[1].xyz);
  62. }
  63. txform[0].xyz = normalize(cross(txform[1].xyz, txform[2].xyz));
  64. txform[2].xyz = vec3(0.0, 0.0, 1.0) * s;
  65. txform[0].xyz *= s;
  66. txform[1].xyz *= s;
  67. } break;
  68. case TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY: {
  69. vec3 sv = velocity_flags.xyz - sort_direction * dot(sort_direction, velocity_flags.xyz); //screen velocity
  70. float s = (length(txform[0]) + length(txform[1]) + length(txform[2])) / 3.0;
  71. if (length(sv) == 0.0) {
  72. sv = align_up;
  73. }
  74. sv = normalize(sv);
  75. txform[0].xyz = normalize(cross(sv, sort_direction)) * s;
  76. txform[1].xyz = sv * s;
  77. txform[2].xyz = sort_direction * s;
  78. } break;
  79. }
  80. txform[3].xyz += velocity_flags.xyz * frame_remainder;
  81. #ifndef MODE_3D
  82. // In global mode, bring 2D particles to local coordinates
  83. // as they will be drawn with the node position as origin.
  84. txform = inv_emission_transform * txform;
  85. #endif
  86. }
  87. txform = transpose(txform);
  88. instance_color_custom_data = uvec4(packHalf2x16(color.xy), packHalf2x16(color.zw), packHalf2x16(custom.xy), packHalf2x16(custom.zw));
  89. out_xform_1 = txform[0];
  90. out_xform_2 = txform[1];
  91. #ifdef MODE_3D
  92. out_xform_3 = txform[2];
  93. #endif
  94. }
  95. /* clang-format off */
  96. #[fragment]
  97. void main() {
  98. }
  99. /* clang-format on */