editor.shader 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. include = [
  2. "core/shaders/common.shader"
  3. "core/shaders/default.shader"
  4. ]
  5. render_states = {
  6. selection = {
  7. inherit = "default"
  8. states = {
  9. alpha_write_enable = false
  10. }
  11. }
  12. }
  13. bgfx_shaders = {
  14. selection = {
  15. includes = [ "common" ]
  16. varying = """
  17. vec3 a_position : POSITION;
  18. """
  19. vs_input_output = """
  20. $input a_position
  21. """
  22. vs_code = """
  23. void main()
  24. {
  25. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  26. }
  27. """
  28. fs_input_output = """
  29. """
  30. fs_code = """
  31. uniform vec4 u_unit_id;
  32. void main()
  33. {
  34. gl_FragColor.r = u_unit_id.x;
  35. }
  36. """
  37. }
  38. outline = {
  39. includes = [ "common" ]
  40. varying = """
  41. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  42. vec3 a_position : POSITION;
  43. vec2 a_texcoord0 : TEXCOORD0;
  44. """
  45. vs_input_output = """
  46. $input a_position, a_texcoord0
  47. $output v_texcoord0
  48. """
  49. vs_code = """
  50. void main()
  51. {
  52. gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0) );
  53. v_texcoord0 = a_texcoord0;
  54. }
  55. """
  56. fs_input_output = """
  57. $input v_texcoord0
  58. """
  59. fs_code = """
  60. #if !BX_PLATFORM_EMSCRIPTEN
  61. USAMPLER2D(s_selection_map, 0);
  62. SAMPLER2D(s_selection_depth_map, 1);
  63. SAMPLER2D(s_depth_map, 2);
  64. uniform vec4 u_outline_color;
  65. void main()
  66. {
  67. vec2 tex_size = vec2(textureSize(s_selection_map, 0)) - vec2(1, 1);
  68. uint id[8];
  69. id[0] = texelFetch(s_selection_map, ivec2(v_texcoord0 * tex_size + vec2(-1, -1)), 0).r;
  70. id[1] = texelFetch(s_selection_map, ivec2(v_texcoord0 * tex_size + vec2( 0, -1)), 0).r;
  71. id[2] = texelFetch(s_selection_map, ivec2(v_texcoord0 * tex_size + vec2( 1, -1)), 0).r;
  72. id[3] = texelFetch(s_selection_map, ivec2(v_texcoord0 * tex_size + vec2( 1, 0)), 0).r;
  73. id[4] = texelFetch(s_selection_map, ivec2(v_texcoord0 * tex_size + vec2( 1, 1)), 0).r;
  74. id[5] = texelFetch(s_selection_map, ivec2(v_texcoord0 * tex_size + vec2( 0, 1)), 0).r;
  75. id[6] = texelFetch(s_selection_map, ivec2(v_texcoord0 * tex_size + vec2(-1, 1)), 0).r;
  76. id[7] = texelFetch(s_selection_map, ivec2(v_texcoord0 * tex_size + vec2(-1, 0)), 0).r;
  77. uint ref_id = texelFetch(s_selection_map, ivec2(v_texcoord0 * tex_size), 0).r;
  78. float alpha = 0.0;
  79. for (int ii = 0; ii < 8; ++ii)
  80. {
  81. if (ref_id != id[ii])
  82. alpha += 1.0/8.0;
  83. }
  84. if (alpha == 0.0)
  85. {
  86. gl_FragColor = vec4(0, 0, 0, 0);
  87. return;
  88. }
  89. alpha = max(0.6, alpha);
  90. // Scan the depth around the center and choose the value closest
  91. // to the viewer. This is to avoid getting depth = 1.0.
  92. float depth = 1.0;
  93. depth = min(depth, texelFetch(s_selection_depth_map, ivec2(v_texcoord0 * tex_size + vec2(-1, -1)), 0).r);
  94. depth = min(depth, texelFetch(s_selection_depth_map, ivec2(v_texcoord0 * tex_size + vec2( 0, -1)), 0).r);
  95. depth = min(depth, texelFetch(s_selection_depth_map, ivec2(v_texcoord0 * tex_size + vec2( 1, -1)), 0).r);
  96. depth = min(depth, texelFetch(s_selection_depth_map, ivec2(v_texcoord0 * tex_size + vec2( 1, 0)), 0).r);
  97. depth = min(depth, texelFetch(s_selection_depth_map, ivec2(v_texcoord0 * tex_size + vec2( 1, 1)), 0).r);
  98. depth = min(depth, texelFetch(s_selection_depth_map, ivec2(v_texcoord0 * tex_size + vec2( 0, 1)), 0).r);
  99. depth = min(depth, texelFetch(s_selection_depth_map, ivec2(v_texcoord0 * tex_size + vec2(-1, 1)), 0).r);
  100. depth = min(depth, texelFetch(s_selection_depth_map, ivec2(v_texcoord0 * tex_size + vec2(-1, 0)), 0).r);
  101. depth = min(depth, texelFetch(s_selection_depth_map, ivec2(v_texcoord0 * tex_size + vec2( 0, 0)), 0).r);
  102. // Dim alpha if selected object is behind another object.
  103. if (depth > texelFetch(s_depth_map, ivec2(v_texcoord0 * tex_size), 0).r)
  104. alpha *= 0.35;
  105. gl_FragColor = vec4(u_outline_color.xyz, alpha);
  106. }
  107. #else
  108. void main()
  109. {
  110. gl_FragColor = vec4_splat(0.0);
  111. }
  112. #endif
  113. """
  114. }
  115. }
  116. shaders = {
  117. selection = {
  118. bgfx_shader = "selection"
  119. render_state = "selection"
  120. }
  121. outline = {
  122. bgfx_shader = "outline"
  123. render_state = "blit"
  124. }
  125. }
  126. static_compile = [
  127. { shader = "selection" defines = [] }
  128. { shader = "outline" defines = [] }
  129. ]