xray_composite.shader 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. [shaders]
  2. vertex =
  3. uniform highp mat4 u_modelViewProjectionMatrix;
  4. attribute highp vec4 a_vertex;
  5. attribute highp vec2 a_uvs;
  6. varying highp vec2 v_uvs;
  7. void main()
  8. {
  9. gl_Position = u_modelViewProjectionMatrix * a_vertex;
  10. v_uvs = a_uvs;
  11. }
  12. fragment =
  13. #ifdef GL_ES
  14. #ifdef GL_FRAGMENT_PRECISION_HIGH
  15. precision highp float;
  16. #else
  17. precision mediump float;
  18. #endif // GL_FRAGMENT_PRECISION_HIGH
  19. #endif // GL_ES
  20. uniform sampler2D u_layer0; //Default pass.
  21. uniform sampler2D u_layer1; //Selection pass.
  22. uniform sampler2D u_layer2; //X-ray pass.
  23. uniform vec2 u_offset[9];
  24. uniform float u_outline_strength;
  25. uniform vec4 u_outline_color;
  26. uniform vec4 u_background_color;
  27. uniform float u_xray_error_strength;
  28. const vec3 x_axis = vec3(1.0, 0.0, 0.0);
  29. const vec3 y_axis = vec3(0.0, 1.0, 0.0);
  30. const vec3 z_axis = vec3(0.0, 0.0, 1.0);
  31. varying vec2 v_uvs;
  32. float kernel[9];
  33. vec3 shiftHue(vec3 color, float hue)
  34. {
  35. // Make sure colors are distinct when grey-scale is used too:
  36. if ((max(max(color.r, color.g), color.b) - min(min(color.r, color.g), color.b)) < 0.1)
  37. {
  38. color = vec3(1.0, 0.0, 0.0);
  39. }
  40. // The actual hue shift:
  41. const vec3 k = vec3(0.57735, 0.57735, 0.57735);
  42. float cosAngle = cos(hue);
  43. return vec3(color * cosAngle + cross(k, color) * sin(hue) + k * dot(k, color) * (1.0 - cosAngle));
  44. }
  45. void main()
  46. {
  47. kernel[0] = 0.0; kernel[1] = 1.0; kernel[2] = 0.0;
  48. kernel[3] = 1.0; kernel[4] = -4.0; kernel[5] = 1.0;
  49. kernel[6] = 0.0; kernel[7] = 1.0; kernel[8] = 0.0;
  50. vec4 result = u_background_color;
  51. vec4 layer0 = texture2D(u_layer0, v_uvs);
  52. float hue_shift = (layer0.a - 0.333) * 6.2831853;
  53. if (layer0.a > 0.5)
  54. {
  55. layer0.a = 1.0;
  56. }
  57. result = mix(result, layer0, layer0.a);
  58. float intersection_count = texture2D(u_layer2, v_uvs).r * 51.0; // (1 / .02) + 1 (+1 magically fixes issues with high intersection count models)
  59. float rest = mod(intersection_count + .01, 2.0);
  60. if (rest > 1.0 && rest < 1.5 && intersection_count < 49.0)
  61. {
  62. result = vec4(shiftHue(layer0.rgb, hue_shift), result.a);
  63. }
  64. vec4 sum = vec4(0.0);
  65. for (int i = 0; i < 9; i++)
  66. {
  67. vec4 color = vec4(texture2D(u_layer1, v_uvs.xy + u_offset[i]).a);
  68. sum += color * (kernel[i] / u_outline_strength);
  69. }
  70. vec4 layer1 = texture2D(u_layer1, v_uvs);
  71. if((layer1.rgb == x_axis || layer1.rgb == y_axis || layer1.rgb == z_axis))
  72. {
  73. gl_FragColor = result;
  74. }
  75. else
  76. {
  77. gl_FragColor = mix(result, u_outline_color, abs(sum.a));
  78. }
  79. gl_FragColor.a = gl_FragColor.a > 0.5 ? 1.0 : 0.0;
  80. }
  81. vertex41core =
  82. #version 410
  83. uniform highp mat4 u_modelViewProjectionMatrix;
  84. in highp vec4 a_vertex;
  85. in highp vec2 a_uvs;
  86. out highp vec2 v_uvs;
  87. void main()
  88. {
  89. gl_Position = u_modelViewProjectionMatrix * a_vertex;
  90. v_uvs = a_uvs;
  91. }
  92. fragment41core =
  93. #version 410
  94. uniform sampler2D u_layer0; //Default pass.
  95. uniform sampler2D u_layer1; //Selection pass.
  96. uniform sampler2D u_layer2; //X-ray pass.
  97. uniform vec2 u_offset[9];
  98. uniform float u_outline_strength;
  99. uniform vec4 u_outline_color;
  100. uniform vec4 u_background_color;
  101. uniform float u_xray_error_strength;
  102. const vec3 x_axis = vec3(1.0, 0.0, 0.0);
  103. const vec3 y_axis = vec3(0.0, 1.0, 0.0);
  104. const vec3 z_axis = vec3(0.0, 0.0, 1.0);
  105. in vec2 v_uvs;
  106. out vec4 frag_color;
  107. float kernel[9];
  108. vec3 shiftHue(vec3 color, float hue)
  109. {
  110. // Make sure colors are distinct when grey-scale is used too:
  111. if ((max(max(color.r, color.g), color.b) - min(min(color.r, color.g), color.b)) < 0.1)
  112. {
  113. color = vec3(1.0, 0.0, 0.0);
  114. }
  115. // The actual hue shift:
  116. const vec3 k = vec3(0.57735, 0.57735, 0.57735);
  117. float cosAngle = cos(hue);
  118. return vec3(color * cosAngle + cross(k, color) * sin(hue) + k * dot(k, color) * (1.0 - cosAngle));
  119. }
  120. void main()
  121. {
  122. kernel[0] = 0.0; kernel[1] = 1.0; kernel[2] = 0.0;
  123. kernel[3] = 1.0; kernel[4] = -4.0; kernel[5] = 1.0;
  124. kernel[6] = 0.0; kernel[7] = 1.0; kernel[8] = 0.0;
  125. vec4 result = u_background_color;
  126. vec4 layer0 = texture(u_layer0, v_uvs);
  127. float hue_shift = (layer0.a - 0.333) * 6.2831853;
  128. if (layer0.a > 0.5)
  129. {
  130. layer0.a = 1.0;
  131. }
  132. result = mix(result, layer0, layer0.a);
  133. float intersection_count = texture(u_layer2, v_uvs).r * 51; // (1 / .02) + 1 (+1 magically fixes issues with high intersection count models)
  134. float rest = mod(intersection_count + .01, 2.0);
  135. if (rest > 1.0 && rest < 1.5 && intersection_count < 49)
  136. {
  137. result = vec4(shiftHue(layer0.rgb, hue_shift), result.a);
  138. }
  139. vec4 sum = vec4(0.0);
  140. for (int i = 0; i < 9; i++)
  141. {
  142. vec4 color = vec4(texture(u_layer1, v_uvs.xy + u_offset[i]).a);
  143. sum += color * (kernel[i] / u_outline_strength);
  144. }
  145. vec4 layer1 = texture(u_layer1, v_uvs);
  146. if((layer1.rgb == x_axis || layer1.rgb == y_axis || layer1.rgb == z_axis))
  147. {
  148. frag_color = result;
  149. }
  150. else
  151. {
  152. frag_color = mix(result, u_outline_color, abs(sum.a));
  153. }
  154. frag_color.a = frag_color.a > 0.5 ? 1.0 : 0.0;
  155. }
  156. [defaults]
  157. u_layer0 = 0
  158. u_layer1 = 1
  159. u_layer2 = 2
  160. u_background_color = [0.965, 0.965, 0.965, 1.0]
  161. u_outline_strength = 1.0
  162. u_outline_color = [0.05, 0.66, 0.89, 1.0]
  163. [bindings]
  164. [attributes]
  165. a_vertex = vertex
  166. a_uvs = uv