layers3d.shader 11 KB

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