layers3d.shader 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. [shaders]
  2. vertex41core =
  3. #version 410
  4. uniform highp mat4 u_modelMatrix;
  5. uniform highp mat4 u_viewMatrix;
  6. uniform highp mat4 u_projectionMatrix;
  7. uniform lowp float u_active_extruder;
  8. uniform lowp float u_max_feedrate;
  9. uniform lowp float u_min_feedrate;
  10. uniform lowp float u_max_thickness;
  11. uniform lowp float u_min_thickness;
  12. uniform lowp float u_max_line_width;
  13. uniform lowp float u_min_line_width;
  14. uniform lowp int u_layer_view_type;
  15. uniform lowp mat4 u_extruder_opacity; // currently only for max 16 extruders, others always visible
  16. uniform highp mat4 u_normalMatrix;
  17. in highp vec4 a_vertex;
  18. in lowp vec4 a_color;
  19. in lowp vec4 a_material_color;
  20. in highp vec4 a_normal;
  21. in highp vec2 a_line_dim; // line width and thickness
  22. in highp float a_extruder;
  23. in highp float a_prev_line_type;
  24. in highp float a_line_type;
  25. in highp float a_feedrate;
  26. in highp float a_thickness;
  27. out lowp vec4 v_color;
  28. out highp vec3 v_vertex;
  29. out highp vec3 v_normal;
  30. out lowp vec2 v_line_dim;
  31. out highp int v_extruder;
  32. out highp mat4 v_extruder_opacity;
  33. out float v_prev_line_type;
  34. out float v_line_type;
  35. out lowp vec4 f_color;
  36. out highp vec3 f_vertex;
  37. out highp vec3 f_normal;
  38. vec4 feedrateGradientColor(float abs_value, float min_value, float max_value)
  39. {
  40. float value = (abs_value - min_value)/(max_value - min_value);
  41. float red = value;
  42. float green = 1-abs(1-4*value);
  43. if (value > 0.375)
  44. {
  45. green = 0.5;
  46. }
  47. float blue = max(1-4*value, 0);
  48. return vec4(red, green, blue, 1.0);
  49. }
  50. vec4 layerThicknessGradientColor(float abs_value, float min_value, float max_value)
  51. {
  52. float value = (abs_value - min_value)/(max_value - min_value);
  53. float red = min(max(4*value-2, 0), 1);
  54. float green = min(1.5*value, 0.75);
  55. if (value > 0.75)
  56. {
  57. green = value;
  58. }
  59. float blue = 0.75-abs(0.25-value);
  60. return vec4(red, green, blue, 1.0);
  61. }
  62. vec4 lineWidthGradientColor(float abs_value, float min_value, float max_value)
  63. {
  64. float value = (abs_value - min_value) / (max_value - min_value);
  65. float red = value;
  66. float green = 1 - abs(1 - 4 * value);
  67. if(value > 0.375)
  68. {
  69. green = 0.5;
  70. }
  71. float blue = max(1 - 4 * value, 0);
  72. return vec4(red, green, blue, 1.0);
  73. }
  74. void main()
  75. {
  76. vec4 v1_vertex = a_vertex;
  77. v1_vertex.y -= a_line_dim.y / 2; // half layer down
  78. vec4 world_space_vert = u_modelMatrix * v1_vertex;
  79. gl_Position = world_space_vert;
  80. // shade the color depending on the extruder index stored in the alpha component of the color
  81. switch (u_layer_view_type) {
  82. case 0: // "Material color"
  83. v_color = a_material_color;
  84. break;
  85. case 1: // "Line type"
  86. v_color = a_color;
  87. break;
  88. case 2: // "Speed", or technically 'Feedrate'
  89. v_color = feedrateGradientColor(a_feedrate, u_min_feedrate, u_max_feedrate);
  90. break;
  91. case 3: // "Layer thickness"
  92. v_color = layerThicknessGradientColor(a_line_dim.y, u_min_thickness, u_max_thickness);
  93. break;
  94. case 4: // "Line width"
  95. v_color = lineWidthGradientColor(a_line_dim.x, u_min_line_width, u_max_line_width);
  96. break;
  97. }
  98. v_vertex = world_space_vert.xyz;
  99. v_normal = (u_normalMatrix * normalize(a_normal)).xyz;
  100. v_line_dim = a_line_dim;
  101. v_extruder = int(a_extruder);
  102. v_prev_line_type = a_prev_line_type;
  103. v_line_type = a_line_type;
  104. v_extruder_opacity = u_extruder_opacity;
  105. // for testing without geometry shader
  106. f_color = v_color;
  107. f_vertex = v_vertex;
  108. f_normal = v_normal;
  109. }
  110. geometry41core =
  111. #version 410
  112. uniform highp mat4 u_modelMatrix;
  113. uniform highp mat4 u_viewMatrix;
  114. uniform highp mat4 u_projectionMatrix;
  115. uniform lowp vec4 u_starts_color;
  116. uniform int u_show_travel_moves;
  117. uniform int u_show_helpers;
  118. uniform int u_show_skin;
  119. uniform int u_show_infill;
  120. uniform int u_show_starts;
  121. uniform int u_transparent_pass;
  122. layout(lines) in;
  123. layout(triangle_strip, max_vertices = 40) out;
  124. in vec4 v_color[];
  125. in vec3 v_vertex[];
  126. in vec3 v_normal[];
  127. in vec2 v_line_dim[];
  128. in int v_extruder[];
  129. in mat4 v_extruder_opacity[];
  130. in float v_prev_line_type[];
  131. in float v_line_type[];
  132. out vec4 f_color;
  133. out vec3 f_normal;
  134. out vec3 f_vertex;
  135. // Set the set of variables and EmitVertex
  136. void myEmitVertex(vec3 vertex, vec4 color, vec3 normal, vec4 pos) {
  137. f_vertex = vertex;
  138. f_color = color;
  139. f_normal = normal;
  140. gl_Position = pos;
  141. EmitVertex();
  142. }
  143. void main()
  144. {
  145. highp mat4 viewProjectionMatrix = u_projectionMatrix * u_viewMatrix;
  146. vec4 g_vertex_delta;
  147. vec3 g_vertex_normal_horz; // horizontal and vertical in respect to layers
  148. vec4 g_vertex_offset_horz; // vec4 to match gl_in[x].gl_Position
  149. vec3 g_vertex_normal_vert;
  150. vec4 g_vertex_offset_vert;
  151. vec3 g_vertex_normal_horz_head;
  152. vec4 g_vertex_offset_horz_head;
  153. float size_x;
  154. float size_y;
  155. if ((v_extruder_opacity[0][int(mod(v_extruder[0], 4))][v_extruder[0] / 4] == 0.0) && (v_line_type[0] != 8) && (v_line_type[0] != 9)) {
  156. return;
  157. }
  158. // See LayerPolygon; 8 is MoveCombingType, 9 is RetractionType
  159. if ((u_show_travel_moves == 0) && ((v_line_type[0] == 8) || (v_line_type[0] == 9))) {
  160. return;
  161. }
  162. if ((u_show_helpers == 0) && ((v_line_type[0] == 4) || (v_line_type[0] == 5) || (v_line_type[0] == 7) || (v_line_type[0] == 10) || v_line_type[0] == 11)) {
  163. return;
  164. }
  165. if ((u_show_skin == 0) && ((v_line_type[0] == 1) || (v_line_type[0] == 2) || (v_line_type[0] == 3))) {
  166. return;
  167. }
  168. if ((u_show_infill == 0) && (v_line_type[0] == 6)) {
  169. return;
  170. }
  171. if ((u_transparent_pass == 0) && (v_color[0][3] < 1.0))
  172. {
  173. return; //Skip transparent bits if it's not the transparent pass.
  174. }
  175. if ((u_transparent_pass == 1) && (v_color[0][3] >= 1.0))
  176. {
  177. return; //Skip opaque bits if it is the transparent pass.
  178. }
  179. if ((v_line_type[0] == 8) || (v_line_type[0] == 9)) {
  180. // fixed size for movements
  181. size_x = 0.05;
  182. } else {
  183. size_x = v_line_dim[1].x / 2 + 0.01; // radius, and make it nicely overlapping
  184. }
  185. size_y = v_line_dim[1].y / 2 + 0.01;
  186. g_vertex_delta = gl_in[1].gl_Position - gl_in[0].gl_Position; //Actual movement exhibited by the line.
  187. g_vertex_normal_horz_head = normalize(vec3(-g_vertex_delta.x, -g_vertex_delta.y, -g_vertex_delta.z)); //Lengthwise normal vector pointing backwards.
  188. g_vertex_offset_horz_head = vec4(g_vertex_normal_horz_head * size_x, 0.0); //Lengthwise offset vector pointing backwards.
  189. g_vertex_normal_horz = normalize(vec3(g_vertex_delta.z, g_vertex_delta.y, -g_vertex_delta.x)); //Normal vector pointing right.
  190. g_vertex_offset_horz = vec4(g_vertex_normal_horz * size_x, 0.0); //Offset vector pointing right.
  191. g_vertex_normal_vert = vec3(0.0, 1.0, 0.0); //Upwards normal vector.
  192. g_vertex_offset_vert = vec4(g_vertex_normal_vert * size_y, 0.0); //Upwards offset vector. Goes up by half the layer thickness.
  193. if ((v_line_type[0] == 8) || (v_line_type[0] == 9)) { //Travel or retraction moves.
  194. vec4 va_head = viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz_head + g_vertex_offset_vert);
  195. vec4 va_up = viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz + g_vertex_offset_vert);
  196. vec4 va_down = viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_horz + g_vertex_offset_vert);
  197. vec4 vb_head = viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz_head + g_vertex_offset_vert);
  198. vec4 vb_down = viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz + g_vertex_offset_vert);
  199. vec4 vb_up = viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz + g_vertex_offset_vert);
  200. // Travels: flat plane with pointy ends
  201. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_up);
  202. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_head);
  203. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_down);
  204. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_up);
  205. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_down);
  206. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_up);
  207. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_head);
  208. //And reverse so that the line is also visible from the back side.
  209. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_up);
  210. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_down);
  211. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_up);
  212. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_down);
  213. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_head);
  214. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_up);
  215. EndPrimitive();
  216. } else {
  217. vec4 va_m_horz = viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_horz); //Line start, left vertex.
  218. vec4 vb_m_horz = viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz); //Line end, left vertex.
  219. vec4 va_p_vert = viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_vert); //Line start, top vertex.
  220. vec4 vb_p_vert = viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_vert); //Line end, top vertex.
  221. vec4 va_p_horz = viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz); //Line start, right vertex.
  222. vec4 vb_p_horz = viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz); //Line end, right vertex.
  223. vec4 va_m_vert = viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_vert); //Line start, bottom vertex.
  224. vec4 vb_m_vert = viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_vert); //Line end, bottom vertex.
  225. vec4 va_head = viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz_head); //Line start, tip.
  226. vec4 vb_head = viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz_head); //Line end, tip.
  227. // All normal lines are rendered as 3d tubes.
  228. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, va_m_horz);
  229. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, vb_m_horz);
  230. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_p_vert);
  231. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_p_vert);
  232. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, va_p_horz);
  233. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, vb_p_horz);
  234. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_vert, va_m_vert);
  235. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_vert, vb_m_vert);
  236. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, va_m_horz);
  237. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, vb_m_horz);
  238. EndPrimitive();
  239. // left side
  240. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, va_m_horz);
  241. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_p_vert);
  242. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz_head, va_head);
  243. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, va_p_horz);
  244. EndPrimitive();
  245. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, va_p_horz);
  246. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_vert, va_m_vert);
  247. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz_head, va_head);
  248. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, va_m_horz);
  249. EndPrimitive();
  250. // right side
  251. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, vb_p_horz);
  252. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_p_vert);
  253. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz_head, vb_head);
  254. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, vb_m_horz);
  255. EndPrimitive();
  256. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, vb_m_horz);
  257. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_vert, vb_m_vert);
  258. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz_head, vb_head);
  259. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, vb_p_horz);
  260. EndPrimitive();
  261. }
  262. if ((u_show_starts == 1) && (v_prev_line_type[0] != 1) && (v_line_type[0] == 1)) {
  263. float w = v_line_dim[0].x / 2;
  264. float h = v_line_dim[0].y / 2;
  265. myEmitVertex(v_vertex[0] + vec3( w, h, w), u_starts_color, normalize(vec3( 1.0, 1.0, 1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4( w, h, w, 0.0))); // Front-top-left
  266. myEmitVertex(v_vertex[0] + vec3(-w, h, w), u_starts_color, normalize(vec3(-1.0, 1.0, 1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4(-w, h, w, 0.0))); // Front-top-right
  267. myEmitVertex(v_vertex[0] + vec3( w, -h, w), u_starts_color, normalize(vec3( 1.0, -1.0, 1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4( w, -h, w, 0.0))); // Front-bottom-left
  268. myEmitVertex(v_vertex[0] + vec3(-w, -h, w), u_starts_color, normalize(vec3(-1.0, -1.0, 1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4(-w, -h, w, 0.0))); // Front-bottom-right
  269. myEmitVertex(v_vertex[0] + vec3(-w, -h, -w), u_starts_color, normalize(vec3(-1.0, -1.0, -1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4(-w, -h, -w, 0.0))); // Back-bottom-right
  270. myEmitVertex(v_vertex[0] + vec3(-w, h, w), u_starts_color, normalize(vec3(-1.0, 1.0, 1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4(-w, h, w, 0.0))); // Front-top-right
  271. myEmitVertex(v_vertex[0] + vec3(-w, h, -w), u_starts_color, normalize(vec3(-1.0, 1.0, -1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4(-w, h, -w, 0.0))); // Back-top-right
  272. myEmitVertex(v_vertex[0] + vec3( w, h, w), u_starts_color, normalize(vec3( 1.0, 1.0, 1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4( w, h, w, 0.0))); // Front-top-left
  273. myEmitVertex(v_vertex[0] + vec3( w, h, -w), u_starts_color, normalize(vec3( 1.0, 1.0, -1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4( w, h, -w, 0.0))); // Back-top-left
  274. myEmitVertex(v_vertex[0] + vec3( w, -h, w), u_starts_color, normalize(vec3( 1.0, -1.0, 1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4( w, -h, w, 0.0))); // Front-bottom-left
  275. myEmitVertex(v_vertex[0] + vec3( w, -h, -w), u_starts_color, normalize(vec3( 1.0, -1.0, -1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4( w, -h, -w, 0.0))); // Back-bottom-left
  276. myEmitVertex(v_vertex[0] + vec3(-w, -h, -w), u_starts_color, normalize(vec3(-1.0, -1.0, -1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4(-w, -h, -w, 0.0))); // Back-bottom-right
  277. myEmitVertex(v_vertex[0] + vec3( w, h, -w), u_starts_color, normalize(vec3( 1.0, 1.0, -1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4( w, h, -w, 0.0))); // Back-top-left
  278. myEmitVertex(v_vertex[0] + vec3(-w, h, -w), u_starts_color, normalize(vec3(-1.0, 1.0, -1.0)), viewProjectionMatrix * (gl_in[0].gl_Position + vec4(-w, h, -w, 0.0))); // Back-top-right
  279. EndPrimitive();
  280. }
  281. }
  282. fragment41core =
  283. #version 410
  284. in lowp vec4 f_color;
  285. in lowp vec3 f_normal;
  286. in lowp vec3 f_vertex;
  287. out vec4 frag_color;
  288. uniform mediump vec4 u_ambientColor;
  289. uniform mediump vec4 u_minimumAlbedo;
  290. uniform highp vec3 u_lightPosition;
  291. void main()
  292. {
  293. mediump vec4 finalColor = vec4(0.0);
  294. finalColor.a = f_color.a;
  295. finalColor.rgb += f_color.rgb * 0.2 + u_minimumAlbedo.rgb;
  296. highp vec3 normal = normalize(f_normal);
  297. highp vec3 light_dir = normalize(u_lightPosition - f_vertex);
  298. // Diffuse Component
  299. highp float NdotL = clamp(dot(normal, light_dir), 0.0, 1.0);
  300. finalColor.rgb += (NdotL * f_color).rgb;
  301. //Premultiply the alpha component into the colour. Combine this with additive blending.
  302. //See https://apoorvaj.io/alpha-compositing-opengl-blending-and-premultiplied-alpha/ for a layout of theory.
  303. finalColor *= f_color.a;
  304. frag_color = finalColor;
  305. }
  306. [defaults]
  307. u_active_extruder = 0.0
  308. u_layer_view_type = 0
  309. u_extruder_opacity = [[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]
  310. u_specularColor = [0.4, 0.4, 0.4, 1.0]
  311. u_ambientColor = [0.3, 0.3, 0.3, 0.0]
  312. u_diffuseColor = [1.0, 0.79, 0.14, 1.0]
  313. u_minimumAlbedo = [0.1, 0.1, 0.1, 1.0]
  314. u_shininess = 20.0
  315. u_starts_color = [1.0, 1.0, 1.0, 1.0]
  316. u_show_travel_moves = 0
  317. u_show_helpers = 1
  318. u_show_skin = 1
  319. u_show_infill = 1
  320. u_show_starts = 1
  321. u_min_feedrate = 0
  322. u_max_feedrate = 1
  323. u_min_thickness = 0
  324. u_max_thickness = 1
  325. [bindings]
  326. u_modelMatrix = model_matrix
  327. u_viewMatrix = view_matrix
  328. u_projectionMatrix = projection_matrix
  329. u_normalMatrix = normal_matrix
  330. u_lightPosition = light_0_position
  331. [attributes]
  332. a_vertex = vertex
  333. a_color = color
  334. a_normal = normal
  335. a_line_dim = line_dim
  336. a_extruder = extruder
  337. a_material_color = material_color
  338. a_prev_line_type = prev_line_type
  339. a_line_type = line_type
  340. a_feedrate = feedrate
  341. a_thickness = thickness