particles.glslv 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef PARTICLES_GLSLV
  2. #define PARTICLES_GLSLV
  3. // #import u_p_time u_p_cyclic u_p_length
  4. // #import u_p_nfactor u_p_gravity u_p_mass u_p_wind_fac u_p_max_lifetime
  5. // #import u_p_fade_in u_p_fade_out u_p_color_ramp u_color_ramp_tex
  6. // #import u_model_tsr u_wind
  7. // #import a_position a_normal a_p_vels a_p_data
  8. /*==============================================================================
  9. VARS
  10. ==============================================================================*/
  11. #var EPSILON 0.000001
  12. #var COLOR_RAMP_LENGTH 0
  13. #var WORLD_SPACE 0
  14. #var USE_COLOR_RAMP 0
  15. /*============================================================================*/
  16. #include <math.glslv>
  17. struct part_params {
  18. float size;
  19. vec3 position;
  20. float alpha;
  21. vec3 color;
  22. float angle;
  23. vec3 velocity;
  24. vec3 ang_velocity;
  25. float age;
  26. };
  27. #if COLOR_RAMP_LENGTH > 0
  28. void process_color_ramp(inout vec3 color, float where, vec4 left, vec4 right) {
  29. float gap_size = right.x - left.x;
  30. float mix_factor = (where - left.x) / gap_size;
  31. color = mix(color, right.yzw, clamp(mix_factor, 0.0, 1.0));
  32. }
  33. #endif
  34. #if COLOR_RAMP_LENGTH > 0
  35. vec3 color_from_ramp(float t, float lifetime, vec4 ramp[COLOR_RAMP_LENGTH]) {
  36. float where = t/lifetime;
  37. vec3 color = ramp[0].yzw;
  38. // avoid loops becaus of performance issues on some mobiles
  39. # if COLOR_RAMP_LENGTH > 1
  40. process_color_ramp(color, where, ramp[0], ramp[1]);
  41. # endif
  42. # if COLOR_RAMP_LENGTH > 2
  43. process_color_ramp(color, where, ramp[1], ramp[2]);
  44. # endif
  45. # if COLOR_RAMP_LENGTH > 3
  46. process_color_ramp(color, where, ramp[2], ramp[3]);
  47. # endif
  48. return color;
  49. }
  50. #endif
  51. /*
  52. * Calculate alpha according to fade-in and fade-out intervals
  53. */
  54. float fade_alpha(float t, float lifetime, float fade_in, float fade_out) {
  55. float fin = max(0.01, min(lifetime, fade_in));
  56. float fout = max(0.01, min(lifetime, fade_out));
  57. float fout_start = lifetime - fout;
  58. float alpha = clamp(t/fin, 0.0, 1.0) -
  59. step(fout_start, t) * (t - fout_start) / fout;
  60. return alpha;
  61. }
  62. part_params calc_part_params(void) {
  63. part_params sp;
  64. float t;
  65. float lifetime = a_p_data[0];
  66. float delay = a_p_data[1];
  67. if (u_p_cyclic == 1) {
  68. t = mod(u_p_time, u_p_length) - delay;
  69. if (t < 0.0)
  70. t += u_p_length;
  71. }
  72. //} else {
  73. if (u_p_cyclic != 1) {
  74. t = u_p_time - delay;
  75. }
  76. if (t < 0.0 || t >= lifetime) {
  77. sp.size = 0.0001;
  78. sp.position = vec3(99999.0, 0.0, 0.0);
  79. }
  80. //} else {
  81. if (!(t < 0.0 || t >= lifetime)) {
  82. /* position */
  83. vec3 norm_tbn = qrot(a_tbn_quat, vec3(0.0, 1.0, 0.0));
  84. #if WORLD_SPACE
  85. vec3 pos = a_position;
  86. vec3 norm = norm_tbn;
  87. #else
  88. vec3 pos = tsr9_transform(u_model_tsr, a_position);
  89. vec3 norm = tsr9_transform_dir(u_model_tsr, norm_tbn);
  90. #endif
  91. /* cinematics */
  92. vec3 vel = u_p_nfactor * norm;
  93. vel += a_p_vels.xyz;
  94. vel.z -= u_p_gravity * t / 2.0;
  95. float mass = max(u_p_mass, EPSILON);
  96. vel += (u_p_wind_fac * u_wind / mass) * t /2.0;
  97. sp.age = t;
  98. sp.velocity = vel;
  99. sp.ang_velocity = norm_tbn * a_p_vels.w;
  100. sp.position = pos + vel * t;
  101. sp.angle = a_p_vels.w * t;
  102. #if USE_COLOR_RAMP
  103. sp.size = GLSL_TEXTURE(u_color_ramp_tex, vec2(t / u_p_max_lifetime, 0.5)).g;
  104. #else
  105. sp.size = 1.0;
  106. #endif
  107. #if COLOR_RAMP_LENGTH > 0
  108. sp.color = color_from_ramp(t, u_p_max_lifetime, u_p_color_ramp);
  109. #else
  110. sp.color = vec3(1.0);
  111. #endif
  112. sp.alpha = fade_alpha(t, a_p_data[0], u_p_fade_in, u_p_fade_out);
  113. }
  114. return sp;
  115. }
  116. #endif