editor.shader 4.1 KB

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