particles_copy.glsl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. if (align_mode == TRANSFORM_ALIGN_DISABLED) {
  46. // nothing
  47. } else if (align_mode == TRANSFORM_ALIGN_Z_BILLBOARD) {
  48. mat3 local = mat3(normalize(cross(align_up, sort_direction)), align_up, sort_direction);
  49. local = local * mat3(txform);
  50. txform[0].xyz = local[0];
  51. txform[1].xyz = local[1];
  52. txform[2].xyz = local[2];
  53. } else if (align_mode == TRANSFORM_ALIGN_Y_TO_VELOCITY) {
  54. vec3 v = velocity_flags.xyz;
  55. float s = (length(txform[0]) + length(txform[1]) + length(txform[2])) / 3.0;
  56. if (length(v) > 0.0) {
  57. txform[1].xyz = normalize(v);
  58. } else {
  59. txform[1].xyz = normalize(txform[1].xyz);
  60. }
  61. txform[0].xyz = normalize(cross(txform[1].xyz, txform[2].xyz));
  62. txform[2].xyz = vec3(0.0, 0.0, 1.0) * s;
  63. txform[0].xyz *= s;
  64. txform[1].xyz *= s;
  65. } else if (align_mode == TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY) {
  66. vec3 sv = velocity_flags.xyz - sort_direction * dot(sort_direction, velocity_flags.xyz); //screen velocity
  67. if (length(sv) == 0.0) {
  68. sv = align_up;
  69. }
  70. sv = normalize(sv);
  71. txform[0].xyz = normalize(cross(sv, sort_direction)) * length(txform[0]);
  72. txform[1].xyz = sv * length(txform[1]);
  73. txform[2].xyz = sort_direction * length(txform[2]);
  74. }
  75. txform[3].xyz += velocity_flags.xyz * frame_remainder;
  76. #ifndef MODE_3D
  77. // In global mode, bring 2D particles to local coordinates
  78. // as they will be drawn with the node position as origin.
  79. txform = inv_emission_transform * txform;
  80. #endif
  81. }
  82. txform = transpose(txform);
  83. instance_color_custom_data.x = packHalf2x16(color.xy);
  84. instance_color_custom_data.y = packHalf2x16(color.zw);
  85. instance_color_custom_data.z = packHalf2x16(custom.xy);
  86. instance_color_custom_data.w = packHalf2x16(custom.zw);
  87. out_xform_1 = txform[0];
  88. out_xform_2 = txform[1];
  89. #ifdef MODE_3D
  90. out_xform_3 = txform[2];
  91. #endif
  92. }
  93. /* clang-format off */
  94. #[fragment]
  95. void main() {
  96. }
  97. /* clang-format on */