layers3d.shader 11 KB

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