layers3d.shader 13 KB

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