halo_color.glslf 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #ifndef HALO_COLOR_GLSLF
  2. #define HALO_COLOR_GLSLF
  3. // #import u_diffuse_color;
  4. // #import u_halo_rings_color;
  5. // #import u_halo_lines_color;
  6. // #import u_halo_hardness;
  7. // #import u_halo_size;
  8. // #import u_sun_intensity;
  9. // #import u_halo_stars_blend;
  10. // #import u_halo_stars_height;
  11. // #import u_cam_water_depth;
  12. // #import v_position_world;
  13. // #import v_texcoord;
  14. // #import v_vertex_random;
  15. /*==============================================================================
  16. VARS
  17. ==============================================================================*/
  18. #var NUM_RINGS 0
  19. #var NUM_LINES 0
  20. #var NUM_STARS 0
  21. #var SKY_STARS 0
  22. #var WAVES_HEIGHT 1.0
  23. #var WATER_EFFECTS 0
  24. #var DISABLE_FOG 0
  25. /*============================================================================*/
  26. #if NUM_RINGS > NUM_LINES
  27. const int rand_arr_length = NUM_RINGS;
  28. #else
  29. const int rand_arr_length = NUM_LINES;
  30. #endif
  31. float mod001(float x) {
  32. return x - floor(x * (1.0 / 0.01)) * 0.01;
  33. }
  34. #if NUM_RINGS
  35. void generate_rings(inout float ringf, in float randoms[rand_arr_length],
  36. in float radist) {
  37. for (int a = 0; a < NUM_RINGS; a++) {
  38. // random number for every halo and every line (-40..0)
  39. float rand_ring = 40.0 * randoms[a];
  40. // ring size randomization
  41. float size_rand = 300.0 * (mod001(rand_ring) - 0.005);
  42. // ring blurrines randomization
  43. float blur_rand = rand_ring;
  44. // ring visibility factor
  45. float fac = abs(blur_rand * (u_halo_size * size_rand - radist));
  46. ringf += 1.0 - min(fac, 1.0); // if (fac < 1.0) ringf += (1.0 - fac);
  47. }
  48. }
  49. #endif
  50. #if NUM_LINES
  51. void generate_lines(inout float linef, in float randoms[rand_arr_length],
  52. in float dist) {
  53. for (int a = 0; a < NUM_LINES; a++) {
  54. // random number for every halo and every line (-1..0)
  55. float rand_line = randoms[a];
  56. // line x coordinate randomization
  57. float x_random = rand_line;
  58. // line y coordinate randomization
  59. float y_random = 1000.0 * (mod001(rand_line) - 0.005);
  60. // line visibility factor
  61. float fac = 20.0 * abs(x_random * v_texcoord.x + y_random * v_texcoord.y);
  62. linef += 1.0 - min(fac, 1.0); // if (fac < 1.0) linef += (1.0 - fac);
  63. }
  64. linef *= dist;
  65. }
  66. #endif
  67. void generate_stars(inout float dist, in vec2 texcoord) {
  68. float ster, angle;
  69. // rotation
  70. angle = atan(texcoord.y, texcoord.x);
  71. angle *= (1.0 + 0.25 * float(NUM_STARS));
  72. float co = cos(angle);
  73. float si = sin(angle);
  74. angle = (co * texcoord.x + si * texcoord.y)
  75. * (co * texcoord.y - si * texcoord.x);
  76. ster = abs(angle);
  77. if (ster < 1.0) {
  78. ster = ( 0.01 * u_halo_size) / (ster);
  79. // correct alpha with a star factor value
  80. dist *= sqrt(min(ster, 1.0));
  81. }
  82. }
  83. vec4 halo_color(void) {
  84. float dist = (v_texcoord.x * v_texcoord.x + v_texcoord.y * v_texcoord.y);
  85. float radist = sqrt(dist);
  86. dist = max(1.0 - dist, 0.0);
  87. // apply halo hardness
  88. dist = pow(dist, u_halo_hardness);
  89. float alpha = u_diffuse_color.a;
  90. #if NUM_RINGS || NUM_LINES
  91. // generate array of random numbers long enough for both lines and rings
  92. float randoms[rand_arr_length];
  93. for (int i = 0; i < rand_arr_length; i++) {
  94. randoms[i] = fract(v_vertex_random / float(i + 1)) - 1.0;
  95. }
  96. #endif
  97. #if NUM_RINGS
  98. float ringf = 0.0;
  99. generate_rings(ringf, randoms, radist);
  100. #endif
  101. #if NUM_LINES
  102. float linef = 0.0;
  103. generate_lines(linef, randoms, dist);
  104. #endif
  105. #if NUM_STARS
  106. generate_stars(dist, v_texcoord);
  107. #endif
  108. dist *= alpha;
  109. vec3 color = u_diffuse_color.rgb;
  110. #if NUM_RINGS
  111. // apply rings modification
  112. ringf *= dist;
  113. color += u_halo_rings_color * ringf;
  114. dist += ringf;
  115. #endif
  116. #if NUM_LINES
  117. // apply lines modification
  118. linef *= alpha;
  119. color += u_halo_lines_color * linef;
  120. dist += linef;
  121. #endif
  122. #if SKY_STARS
  123. // stars are visible only when sun has low brightness
  124. dist *= max(1.0 - 2.0 * u_sun_intensity.x, 0.0);
  125. # if WATER_EFFECTS && !DISABLE_FOG
  126. // stars has lower alpha near the horizont
  127. float height_factor = u_halo_stars_blend
  128. * (v_position_world.y - u_halo_stars_height);
  129. dist *= clamp(height_factor, 0.0, 1.0);
  130. // hide stars when underwater
  131. dist *= min(u_cam_water_depth + 2.0 * WAVES_HEIGHT, 1.0);
  132. # endif
  133. #endif
  134. vec4 vec_out = vec4(color, dist);
  135. return vec_out;
  136. }
  137. #endif