layers3d.shader 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. layout(lines) in;
  122. layout(triangle_strip, max_vertices = 40) out;
  123. in vec4 v_color[];
  124. in vec3 v_vertex[];
  125. in vec3 v_normal[];
  126. in vec2 v_line_dim[];
  127. in int v_extruder[];
  128. in mat4 v_extruder_opacity[];
  129. in float v_prev_line_type[];
  130. in float v_line_type[];
  131. out vec4 f_color;
  132. out vec3 f_normal;
  133. out vec3 f_vertex;
  134. // Set the set of variables and EmitVertex
  135. void myEmitVertex(vec3 vertex, vec4 color, vec3 normal, vec4 pos) {
  136. f_vertex = vertex;
  137. f_color = color;
  138. f_normal = normal;
  139. gl_Position = pos;
  140. EmitVertex();
  141. }
  142. void main()
  143. {
  144. highp mat4 viewProjectionMatrix = u_projectionMatrix * u_viewMatrix;
  145. vec4 g_vertex_delta;
  146. vec3 g_vertex_normal_horz; // horizontal and vertical in respect to layers
  147. vec4 g_vertex_offset_horz; // vec4 to match gl_in[x].gl_Position
  148. vec3 g_vertex_normal_vert;
  149. vec4 g_vertex_offset_vert;
  150. vec3 g_vertex_normal_horz_head;
  151. vec4 g_vertex_offset_horz_head;
  152. float size_x;
  153. float size_y;
  154. 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)) {
  155. return;
  156. }
  157. // See LayerPolygon; 8 is MoveCombingType, 9 is RetractionType
  158. if ((u_show_travel_moves == 0) && ((v_line_type[0] == 8) || (v_line_type[0] == 9))) {
  159. return;
  160. }
  161. 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)) {
  162. return;
  163. }
  164. if ((u_show_skin == 0) && ((v_line_type[0] == 1) || (v_line_type[0] == 2) || (v_line_type[0] == 3))) {
  165. return;
  166. }
  167. if ((u_show_infill == 0) && (v_line_type[0] == 6)) {
  168. return;
  169. }
  170. if ((v_line_type[0] == 8) || (v_line_type[0] == 9)) {
  171. // fixed size for movements
  172. size_x = 0.05;
  173. } else {
  174. size_x = v_line_dim[1].x / 2 + 0.01; // radius, and make it nicely overlapping
  175. }
  176. size_y = v_line_dim[1].y / 2 + 0.01;
  177. g_vertex_delta = gl_in[1].gl_Position - gl_in[0].gl_Position; //Actual movement exhibited by the line.
  178. g_vertex_normal_horz_head = normalize(vec3(-g_vertex_delta.x, -g_vertex_delta.y, -g_vertex_delta.z)); //Lengthwise normal vector pointing backwards.
  179. g_vertex_offset_horz_head = vec4(g_vertex_normal_horz_head * size_x, 0.0); //Lengthwise offset vector pointing backwards.
  180. g_vertex_normal_horz = normalize(vec3(g_vertex_delta.z, g_vertex_delta.y, -g_vertex_delta.x)); //Normal vector pointing right.
  181. g_vertex_offset_horz = vec4(g_vertex_normal_horz * size_x, 0.0); //Offset vector pointing right.
  182. g_vertex_normal_vert = vec3(0.0, 1.0, 0.0); //Upwards normal vector.
  183. g_vertex_offset_vert = vec4(g_vertex_normal_vert * size_y, 0.0); //Upwards offset vector. Goes up by half the layer thickness.
  184. if ((v_line_type[0] == 8) || (v_line_type[0] == 9)) { //Travel or retraction moves.
  185. vec4 va_head = viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz_head + g_vertex_offset_vert);
  186. vec4 va_up = viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz + g_vertex_offset_vert);
  187. vec4 va_down = viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_horz + g_vertex_offset_vert);
  188. vec4 vb_head = viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz_head + g_vertex_offset_vert);
  189. vec4 vb_down = viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz + g_vertex_offset_vert);
  190. vec4 vb_up = viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz + g_vertex_offset_vert);
  191. // Travels: flat plane with pointy ends
  192. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_up);
  193. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_head);
  194. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_down);
  195. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_up);
  196. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_down);
  197. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_up);
  198. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_head);
  199. //And reverse so that the line is also visible from the back side.
  200. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_up);
  201. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_down);
  202. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_up);
  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_head);
  205. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_up);
  206. EndPrimitive();
  207. } else {
  208. vec4 va_m_horz = viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_horz); //Line start, left vertex.
  209. vec4 vb_m_horz = viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz); //Line end, left vertex.
  210. vec4 va_p_vert = viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_vert); //Line start, top vertex.
  211. vec4 vb_p_vert = viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_vert); //Line end, top vertex.
  212. vec4 va_p_horz = viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz); //Line start, right vertex.
  213. vec4 vb_p_horz = viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz); //Line end, right vertex.
  214. vec4 va_m_vert = viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_vert); //Line start, bottom vertex.
  215. vec4 vb_m_vert = viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_vert); //Line end, bottom vertex.
  216. vec4 va_head = viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz_head); //Line start, tip.
  217. vec4 vb_head = viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz_head); //Line end, tip.
  218. // All normal lines are rendered as 3d tubes.
  219. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, va_m_horz);
  220. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, vb_m_horz);
  221. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_p_vert);
  222. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_p_vert);
  223. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, va_p_horz);
  224. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, vb_p_horz);
  225. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_vert, va_m_vert);
  226. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_vert, vb_m_vert);
  227. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, va_m_horz);
  228. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, vb_m_horz);
  229. EndPrimitive();
  230. // left side
  231. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, va_m_horz);
  232. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, va_p_vert);
  233. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz_head, va_head);
  234. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, va_p_horz);
  235. EndPrimitive();
  236. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, va_p_horz);
  237. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_vert, va_m_vert);
  238. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz_head, va_head);
  239. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, va_m_horz);
  240. EndPrimitive();
  241. // right side
  242. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, vb_p_horz);
  243. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, vb_p_vert);
  244. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz_head, vb_head);
  245. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, vb_m_horz);
  246. EndPrimitive();
  247. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, vb_m_horz);
  248. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_vert, vb_m_vert);
  249. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz_head, vb_head);
  250. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, vb_p_horz);
  251. EndPrimitive();
  252. }
  253. if ((u_show_starts == 1) && (v_prev_line_type[0] != 1) && (v_line_type[0] == 1)) {
  254. float w = v_line_dim[0].x / 2;
  255. float h = v_line_dim[0].y / 2;
  256. 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
  257. 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
  258. 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
  259. 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
  260. 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
  261. 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
  262. 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
  263. 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
  264. 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
  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-bottom-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))); // Back-bottom-left
  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))); // Back-bottom-right
  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))); // Back-top-left
  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-top-right
  270. EndPrimitive();
  271. }
  272. }
  273. fragment41core =
  274. #version 410
  275. in lowp vec4 f_color;
  276. in lowp vec3 f_normal;
  277. in lowp vec3 f_vertex;
  278. out vec4 frag_color;
  279. uniform mediump vec4 u_ambientColor;
  280. uniform mediump vec4 u_minimumAlbedo;
  281. uniform highp vec3 u_lightPosition;
  282. void main()
  283. {
  284. mediump vec4 finalColor = vec4(0.0);
  285. float alpha = f_color.a;
  286. finalColor.rgb += f_color.rgb * 0.2 + u_minimumAlbedo.rgb;
  287. highp vec3 normal = normalize(f_normal);
  288. highp vec3 light_dir = normalize(u_lightPosition - f_vertex);
  289. // Diffuse Component
  290. highp float NdotL = clamp(dot(normal, light_dir), 0.0, 1.0);
  291. finalColor += (NdotL * f_color);
  292. finalColor.a = alpha; // Do not change alpha in any way
  293. frag_color = finalColor;
  294. }
  295. [defaults]
  296. u_active_extruder = 0.0
  297. u_layer_view_type = 0
  298. 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]]
  299. u_specularColor = [0.4, 0.4, 0.4, 1.0]
  300. u_ambientColor = [0.3, 0.3, 0.3, 0.0]
  301. u_diffuseColor = [1.0, 0.79, 0.14, 1.0]
  302. u_minimumAlbedo = [0.1, 0.1, 0.1, 1.0]
  303. u_shininess = 20.0
  304. u_starts_color = [1.0, 1.0, 1.0, 1.0]
  305. u_show_travel_moves = 0
  306. u_show_helpers = 1
  307. u_show_skin = 1
  308. u_show_infill = 1
  309. u_show_starts = 1
  310. u_min_feedrate = 0
  311. u_max_feedrate = 1
  312. u_min_thickness = 0
  313. u_max_thickness = 1
  314. [bindings]
  315. u_modelMatrix = model_matrix
  316. u_viewMatrix = view_matrix
  317. u_projectionMatrix = projection_matrix
  318. u_normalMatrix = normal_matrix
  319. u_lightPosition = light_0_position
  320. [attributes]
  321. a_vertex = vertex
  322. a_color = color
  323. a_normal = normal
  324. a_line_dim = line_dim
  325. a_extruder = extruder
  326. a_material_color = material_color
  327. a_prev_line_type = prev_line_type
  328. a_line_type = line_type
  329. a_feedrate = feedrate
  330. a_thickness = thickness