layers3d.shader 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. [shaders]
  2. vertex41core =
  3. #version 410
  4. uniform highp mat4 u_modelViewProjectionMatrix;
  5. uniform highp mat4 u_modelMatrix;
  6. uniform highp mat4 u_viewProjectionMatrix;
  7. uniform lowp float u_active_extruder;
  8. uniform lowp int u_layer_view_type;
  9. uniform lowp vec4 u_extruder_opacity; // currently only for max 4 extruders, others always visible
  10. uniform highp mat4 u_normalMatrix;
  11. in highp vec4 a_vertex;
  12. in lowp vec4 a_color;
  13. in lowp vec4 a_material_color;
  14. in highp vec4 a_normal;
  15. in highp vec2 a_line_dim; // line width and thickness
  16. in highp float a_extruder;
  17. in highp float a_line_type;
  18. out lowp vec4 v_color;
  19. out highp vec3 v_vertex;
  20. out highp vec3 v_normal;
  21. out lowp vec2 v_line_dim;
  22. out highp int v_extruder;
  23. out highp vec4 v_extruder_opacity;
  24. out float v_line_type;
  25. out lowp vec4 f_color;
  26. out highp vec3 f_vertex;
  27. out highp vec3 f_normal;
  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 = int(a_extruder);
  47. v_line_type = a_line_type;
  48. v_extruder_opacity = u_extruder_opacity;
  49. // for testing without geometry shader
  50. f_color = v_color;
  51. f_vertex = v_vertex;
  52. f_normal = v_normal;
  53. }
  54. geometry41core =
  55. #version 410
  56. uniform highp mat4 u_viewProjectionMatrix;
  57. uniform int u_show_travel_moves;
  58. uniform int u_show_helpers;
  59. uniform int u_show_skin;
  60. uniform int u_show_infill;
  61. layout(lines) in;
  62. layout(triangle_strip, max_vertices = 26) out;
  63. in vec4 v_color[];
  64. in vec3 v_vertex[];
  65. in vec3 v_normal[];
  66. in vec2 v_line_dim[];
  67. in int v_extruder[];
  68. in vec4 v_extruder_opacity[];
  69. in float v_line_type[];
  70. out vec4 f_color;
  71. out vec3 f_normal;
  72. out vec3 f_vertex;
  73. // Set the set of variables and EmitVertex
  74. void myEmitVertex(vec3 vertex, vec4 color, vec3 normal, vec4 pos) {
  75. f_vertex = vertex;
  76. f_color = color;
  77. f_normal = normal;
  78. gl_Position = pos;
  79. EmitVertex();
  80. }
  81. void main()
  82. {
  83. vec4 g_vertex_delta;
  84. vec3 g_vertex_normal_horz; // horizontal and vertical in respect to layers
  85. vec4 g_vertex_offset_horz; // vec4 to match gl_in[x].gl_Position
  86. vec3 g_vertex_normal_vert;
  87. vec4 g_vertex_offset_vert;
  88. vec3 g_vertex_normal_horz_head;
  89. vec4 g_vertex_offset_horz_head;
  90. float size_x;
  91. float size_y;
  92. if ((v_extruder_opacity[0][v_extruder[0]] == 0.0) && (v_line_type[0] != 8) && (v_line_type[0] != 9)) {
  93. return;
  94. }
  95. // See LayerPolygon; 8 is MoveCombingType, 9 is RetractionType
  96. if ((u_show_travel_moves == 0) && ((v_line_type[0] == 8) || (v_line_type[0] == 9))) {
  97. return;
  98. }
  99. 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))) {
  100. return;
  101. }
  102. if ((u_show_skin == 0) && ((v_line_type[0] == 1) || (v_line_type[0] == 2) || (v_line_type[0] == 3))) {
  103. return;
  104. }
  105. if ((u_show_infill == 0) && (v_line_type[0] == 6)) {
  106. return;
  107. }
  108. if ((v_line_type[0] == 8) || (v_line_type[0] == 9)) {
  109. // fixed size for movements
  110. size_x = 0.05;
  111. } else {
  112. size_x = v_line_dim[0].x / 2 + 0.01; // radius, and make it nicely overlapping
  113. }
  114. size_y = v_line_dim[0].y / 2 + 0.01;
  115. g_vertex_delta = gl_in[1].gl_Position - gl_in[0].gl_Position;
  116. g_vertex_normal_horz_head = normalize(vec3(-g_vertex_delta.x, -g_vertex_delta.y, -g_vertex_delta.z));
  117. g_vertex_offset_horz_head = vec4(g_vertex_normal_horz_head * size_x, 0.0);
  118. g_vertex_normal_horz = normalize(vec3(g_vertex_delta.z, g_vertex_delta.y, -g_vertex_delta.x));
  119. g_vertex_offset_horz = vec4(g_vertex_normal_horz * size_x, 0.0); //size * g_vertex_normal_horz;
  120. g_vertex_normal_vert = vec3(0.0, 1.0, 0.0);
  121. g_vertex_offset_vert = vec4(g_vertex_normal_vert * size_y, 0.0);
  122. if ((v_line_type[0] == 8) || (v_line_type[0] == 9)) {
  123. // Travels: flat plane with pointy ends
  124. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz + g_vertex_offset_vert));
  125. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz_head + g_vertex_offset_vert));
  126. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_horz + g_vertex_offset_vert));
  127. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz + g_vertex_offset_vert));
  128. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz + g_vertex_offset_vert));
  129. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz + g_vertex_offset_vert));
  130. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz_head + g_vertex_offset_vert));
  131. EndPrimitive();
  132. } else {
  133. // All normal lines are rendered as 3d tubes.
  134. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz));
  135. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz));
  136. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_vert));
  137. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_vert));
  138. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_horz));
  139. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz));
  140. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_vert));
  141. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_vert));
  142. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz));
  143. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz));
  144. EndPrimitive();
  145. // left side
  146. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz));
  147. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_vert));
  148. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz_head, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz_head));
  149. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_horz));
  150. EndPrimitive();
  151. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_horz));
  152. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_vert));
  153. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz_head, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz_head));
  154. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz));
  155. EndPrimitive();
  156. // right side
  157. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz));
  158. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_vert));
  159. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz_head, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz_head));
  160. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz));
  161. EndPrimitive();
  162. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz));
  163. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_vert));
  164. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz_head, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz_head));
  165. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz));
  166. EndPrimitive();
  167. }
  168. }
  169. fragment41core =
  170. #version 410
  171. in lowp vec4 f_color;
  172. in lowp vec3 f_normal;
  173. in lowp vec3 f_vertex;
  174. out vec4 frag_color;
  175. uniform mediump vec4 u_ambientColor;
  176. uniform highp vec3 u_lightPosition;
  177. void main()
  178. {
  179. mediump vec4 finalColor = vec4(0.0);
  180. float alpha = f_color.a;
  181. finalColor.rgb += f_color.rgb * 0.3;
  182. highp vec3 normal = normalize(f_normal);
  183. highp vec3 light_dir = normalize(u_lightPosition - f_vertex);
  184. // Diffuse Component
  185. highp float NdotL = clamp(dot(normal, light_dir), 0.0, 1.0);
  186. finalColor += (NdotL * f_color);
  187. finalColor.a = alpha; // Do not change alpha in any way
  188. frag_color = finalColor;
  189. }
  190. [defaults]
  191. u_active_extruder = 0.0
  192. u_layer_view_type = 0
  193. u_extruder_opacity = [1.0, 1.0, 1.0, 1.0]
  194. u_specularColor = [0.4, 0.4, 0.4, 1.0]
  195. u_ambientColor = [0.3, 0.3, 0.3, 0.0]
  196. u_diffuseColor = [1.0, 0.79, 0.14, 1.0]
  197. u_shininess = 20.0
  198. u_show_travel_moves = 0
  199. u_show_helpers = 1
  200. u_show_skin = 1
  201. u_show_infill = 1
  202. [bindings]
  203. u_modelViewProjectionMatrix = model_view_projection_matrix
  204. u_modelMatrix = model_matrix
  205. u_viewProjectionMatrix = view_projection_matrix
  206. u_normalMatrix = normal_matrix
  207. u_lightPosition = light_0_position
  208. [attributes]
  209. a_vertex = vertex
  210. a_color = color
  211. a_normal = normal
  212. a_line_dim = line_dim
  213. a_extruder = extruder
  214. a_material_color = material_color
  215. a_line_type = line_type