xray_composite.shader 6.3 KB

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