debug_view.glslf 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #version GLSL_VERSION
  2. /*==============================================================================
  3. VARS
  4. ==============================================================================*/
  5. #var DEBUG_SPHERE 0
  6. #var WIREFRAME_QUALITY 0
  7. #var ALPHA 0
  8. #var DEBUG_SPHERE_DYNAMIC 0
  9. /*==============================================================================
  10. INCLUDES
  11. ==============================================================================*/
  12. #include <precision_statement.glslf>
  13. #include <std.glsl>
  14. #include <color_util.glslf>
  15. /*==============================================================================
  16. UNIFORMS
  17. ==============================================================================*/
  18. #if !DEBUG_SPHERE
  19. uniform int u_debug_view_mode;
  20. uniform float u_cluster_id;
  21. uniform float u_batch_debug_id_color;
  22. uniform float u_batch_debug_main_render_time;
  23. uniform float u_debug_colors_seed;
  24. uniform float u_debug_render_time_threshold;
  25. uniform vec3 u_wireframe_edge_color;
  26. #endif
  27. /*==============================================================================
  28. SHADER INTERFACE
  29. ==============================================================================*/
  30. GLSL_IN vec3 v_barycentric;
  31. //------------------------------------------------------------------------------
  32. GLSL_OUT vec4 GLSL_OUT_FRAG_COLOR;
  33. /*==============================================================================
  34. CONST
  35. ==============================================================================*/
  36. const float WIREFRAME_WIDTH = 1.0;
  37. #if DEBUG_SPHERE
  38. # if DEBUG_SPHERE_DYNAMIC
  39. const vec3 DEBUG_SPHERE_COLOR = vec3(1.0, 0.05, 0.05);
  40. # else
  41. const vec3 DEBUG_SPHERE_COLOR = vec3(0.05, 0.05, 1.0);
  42. # endif
  43. #else
  44. const vec3 FRONT_COLOR = vec3(0.4, 0.4, 1.0);
  45. const vec3 BACK_COLOR = vec3(1.0, 0.4, 0.4);
  46. const vec3 FACE_COLOR_OPAQUE = vec3(1.0, 1.0, 1.0);
  47. // just eyes-friendly pretty colors
  48. const vec3 GREEN = vec3(0.0, 133.0/255.0, 66.0/255.0);
  49. const vec3 RED = vec3(160.0/255.0, 38.0/255.0, 33.0/255.0);
  50. #endif
  51. #if !DEBUG_SPHERE
  52. float get_wireframe_edge_factor() {
  53. // 0.0 means edge, 1.0 means polygon's center (approximately)
  54. float factor = 1.0;
  55. # if WIREFRAME_QUALITY == 0
  56. vec3 dist = sign(v_barycentric - vec3(0.02 * WIREFRAME_WIDTH));
  57. if (dist.x < 0.0 || dist.y < 0.0 || dist.z < 0.0)
  58. factor = 0.0;
  59. # elif WIREFRAME_QUALITY == 1
  60. # if GLSL1
  61. #extension GL_OES_standard_derivatives: enable
  62. # endif
  63. vec3 derivatives = fwidth(v_barycentric);
  64. vec3 smoothed_bc = smoothstep(vec3(0.0), derivatives * WIREFRAME_WIDTH, v_barycentric);
  65. factor = min(min(smoothed_bc.x, smoothed_bc.y), smoothed_bc.z);
  66. factor = clamp(factor, 0.0, 1.0);
  67. # endif
  68. return factor;
  69. }
  70. #endif
  71. /*==============================================================================
  72. MAIN
  73. ==============================================================================*/
  74. void main() {
  75. vec3 color = vec3(0.0);
  76. float alpha = 0.0;
  77. // batches with a sphere geometry
  78. #if DEBUG_SPHERE
  79. vec3 dist = sign(v_barycentric - vec3(0.02 * WIREFRAME_WIDTH));
  80. if (dist.x < 0.0 || dist.y < 0.0 || dist.z < 0.0) {
  81. color = DEBUG_SPHERE_COLOR;
  82. alpha = 1.0;
  83. } else
  84. discard;
  85. // batches with the duplicated source geometry
  86. #else
  87. if (u_debug_view_mode == DV_OPAQUE_WIREFRAME) {
  88. float factor = get_wireframe_edge_factor();
  89. color = vec3(mix(u_wireframe_edge_color, FACE_COLOR_OPAQUE, factor));
  90. alpha = 1.0;
  91. } else if (u_debug_view_mode == DV_TRANSPARENT_WIREFRAME) {
  92. float factor = get_wireframe_edge_factor();
  93. color = u_wireframe_edge_color;
  94. alpha = mix(1.0, 0.0, factor);
  95. } else if (u_debug_view_mode == DV_FRONT_BACK_VIEW) {
  96. float factor = get_wireframe_edge_factor();
  97. if (gl_FrontFacing)
  98. color = mix(u_wireframe_edge_color, FRONT_COLOR, factor);
  99. else
  100. color = mix(u_wireframe_edge_color, BACK_COLOR, factor);
  101. alpha = 1.0;
  102. } else if (u_debug_view_mode == DV_CLUSTERS_VIEW) {
  103. // NOTE: add 2 because u_cluster_id can be -1 and we don't want zeros
  104. float val = u_cluster_id + u_debug_colors_seed + 2.0;
  105. // random color
  106. color = vec3(fract(val * 19.73), fract(val * 6.34), fract(val * 1.56));
  107. alpha = 1.0;
  108. } else if (u_debug_view_mode == DV_BATCHES_VIEW) {
  109. float val = u_batch_debug_id_color + u_debug_colors_seed;
  110. // random color
  111. color = vec3(fract(val * 19.73), fract(val * 6.34), fract(val * 1.56));
  112. alpha = 1.0;
  113. } else if (u_debug_view_mode == DV_RENDER_TIME) {
  114. float render_time;
  115. if (u_debug_render_time_threshold > 0.0)
  116. render_time = clamp(u_batch_debug_main_render_time, 0.0,
  117. u_debug_render_time_threshold) / u_debug_render_time_threshold;
  118. else
  119. render_time = 1.0;
  120. float r_coeff = clamp(2.0 * render_time, 0.0, 1.0);
  121. float g_coeff = clamp(2.0 * render_time - 1.0, 0.0, 1.0);
  122. float b_coeff = render_time;
  123. color = mix(GREEN, RED, vec3(r_coeff, g_coeff, b_coeff));
  124. alpha = 1.0;
  125. }
  126. #endif
  127. lin_to_srgb(color);
  128. #if ALPHA
  129. premultiply_alpha(color, alpha);
  130. #endif
  131. GLSL_OUT_FRAG_COLOR = vec4(color, alpha);
  132. }