xray_composite.shader 6.0 KB

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