layers3d.shader 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 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 gradientColor(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-2*value);
  39. float blue = 1-value;
  40. return vec4(red, green, blue, 1.0);
  41. }
  42. void main()
  43. {
  44. vec4 v1_vertex = a_vertex;
  45. v1_vertex.y -= a_line_dim.y / 2; // half layer down
  46. vec4 world_space_vert = u_modelMatrix * v1_vertex;
  47. gl_Position = world_space_vert;
  48. // shade the color depending on the extruder index stored in the alpha component of the color
  49. switch (u_layer_view_type) {
  50. case 0: // "Material color"
  51. v_color = a_material_color;
  52. break;
  53. case 1: // "Line type"
  54. v_color = a_color;
  55. break;
  56. case 2: // "Feedrate"
  57. v_color = gradientColor(a_feedrate, u_min_feedrate, u_max_feedrate);
  58. break;
  59. case 3: // "Layer thickness"
  60. v_color = gradientColor(a_line_dim.y, u_min_thickness, u_max_thickness);
  61. break;
  62. }
  63. v_vertex = world_space_vert.xyz;
  64. v_normal = (u_normalMatrix * normalize(a_normal)).xyz;
  65. v_line_dim = a_line_dim;
  66. v_extruder = int(a_extruder);
  67. v_line_type = a_line_type;
  68. v_extruder_opacity = u_extruder_opacity;
  69. // for testing without geometry shader
  70. f_color = v_color;
  71. f_vertex = v_vertex;
  72. f_normal = v_normal;
  73. }
  74. geometry41core =
  75. #version 410
  76. uniform highp mat4 u_viewProjectionMatrix;
  77. uniform int u_show_travel_moves;
  78. uniform int u_show_helpers;
  79. uniform int u_show_skin;
  80. uniform int u_show_infill;
  81. layout(lines) in;
  82. layout(triangle_strip, max_vertices = 26) out;
  83. in vec4 v_color[];
  84. in vec3 v_vertex[];
  85. in vec3 v_normal[];
  86. in vec2 v_line_dim[];
  87. in int v_extruder[];
  88. in vec4 v_extruder_opacity[];
  89. in float v_line_type[];
  90. out vec4 f_color;
  91. out vec3 f_normal;
  92. out vec3 f_vertex;
  93. // Set the set of variables and EmitVertex
  94. void myEmitVertex(vec3 vertex, vec4 color, vec3 normal, vec4 pos) {
  95. f_vertex = vertex;
  96. f_color = color;
  97. f_normal = normal;
  98. gl_Position = pos;
  99. EmitVertex();
  100. }
  101. void main()
  102. {
  103. vec4 g_vertex_delta;
  104. vec3 g_vertex_normal_horz; // horizontal and vertical in respect to layers
  105. vec4 g_vertex_offset_horz; // vec4 to match gl_in[x].gl_Position
  106. vec3 g_vertex_normal_vert;
  107. vec4 g_vertex_offset_vert;
  108. vec3 g_vertex_normal_horz_head;
  109. vec4 g_vertex_offset_horz_head;
  110. float size_x;
  111. float size_y;
  112. if ((v_extruder_opacity[0][v_extruder[0]] == 0.0) && (v_line_type[0] != 8) && (v_line_type[0] != 9)) {
  113. return;
  114. }
  115. // See LayerPolygon; 8 is MoveCombingType, 9 is RetractionType
  116. if ((u_show_travel_moves == 0) && ((v_line_type[0] == 8) || (v_line_type[0] == 9))) {
  117. return;
  118. }
  119. 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))) {
  120. return;
  121. }
  122. if ((u_show_skin == 0) && ((v_line_type[0] == 1) || (v_line_type[0] == 2) || (v_line_type[0] == 3))) {
  123. return;
  124. }
  125. if ((u_show_infill == 0) && (v_line_type[0] == 6)) {
  126. return;
  127. }
  128. if ((v_line_type[0] == 8) || (v_line_type[0] == 9)) {
  129. // fixed size for movements
  130. size_x = 0.05;
  131. } else {
  132. size_x = v_line_dim[1].x / 2 + 0.01; // radius, and make it nicely overlapping
  133. }
  134. size_y = v_line_dim[1].y / 2 + 0.01;
  135. g_vertex_delta = gl_in[1].gl_Position - gl_in[0].gl_Position;
  136. g_vertex_normal_horz_head = normalize(vec3(-g_vertex_delta.x, -g_vertex_delta.y, -g_vertex_delta.z));
  137. g_vertex_offset_horz_head = vec4(g_vertex_normal_horz_head * size_x, 0.0);
  138. g_vertex_normal_horz = normalize(vec3(g_vertex_delta.z, g_vertex_delta.y, -g_vertex_delta.x));
  139. g_vertex_offset_horz = vec4(g_vertex_normal_horz * size_x, 0.0); //size * g_vertex_normal_horz;
  140. g_vertex_normal_vert = vec3(0.0, 1.0, 0.0);
  141. g_vertex_offset_vert = vec4(g_vertex_normal_vert * size_y, 0.0);
  142. if ((v_line_type[0] == 8) || (v_line_type[0] == 9)) {
  143. // Travels: flat plane with pointy ends
  144. 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));
  145. 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));
  146. 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));
  147. 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));
  148. 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));
  149. 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));
  150. 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));
  151. EndPrimitive();
  152. } else {
  153. // All normal lines are rendered as 3d tubes.
  154. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz));
  155. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz));
  156. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_vert));
  157. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_vert));
  158. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_horz));
  159. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz));
  160. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_vert));
  161. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_vert));
  162. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz));
  163. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz));
  164. EndPrimitive();
  165. // left side
  166. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz));
  167. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_vert));
  168. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz_head, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz_head));
  169. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_horz));
  170. EndPrimitive();
  171. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_horz));
  172. myEmitVertex(v_vertex[0], v_color[0], -g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[0].gl_Position - g_vertex_offset_vert));
  173. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz_head, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz_head));
  174. myEmitVertex(v_vertex[0], v_color[0], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[0].gl_Position + g_vertex_offset_horz));
  175. EndPrimitive();
  176. // right side
  177. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz));
  178. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_vert));
  179. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz_head, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz_head));
  180. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz));
  181. EndPrimitive();
  182. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz));
  183. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_vert, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_vert));
  184. myEmitVertex(v_vertex[1], v_color[1], -g_vertex_normal_horz_head, u_viewProjectionMatrix * (gl_in[1].gl_Position - g_vertex_offset_horz_head));
  185. myEmitVertex(v_vertex[1], v_color[1], g_vertex_normal_horz, u_viewProjectionMatrix * (gl_in[1].gl_Position + g_vertex_offset_horz));
  186. EndPrimitive();
  187. }
  188. }
  189. fragment41core =
  190. #version 410
  191. in lowp vec4 f_color;
  192. in lowp vec3 f_normal;
  193. in lowp vec3 f_vertex;
  194. out vec4 frag_color;
  195. uniform mediump vec4 u_ambientColor;
  196. uniform highp vec3 u_lightPosition;
  197. void main()
  198. {
  199. mediump vec4 finalColor = vec4(0.0);
  200. float alpha = f_color.a;
  201. finalColor.rgb += f_color.rgb * 0.3;
  202. highp vec3 normal = normalize(f_normal);
  203. highp vec3 light_dir = normalize(u_lightPosition - f_vertex);
  204. // Diffuse Component
  205. highp float NdotL = clamp(dot(normal, light_dir), 0.0, 1.0);
  206. finalColor += (NdotL * f_color);
  207. finalColor.a = alpha; // Do not change alpha in any way
  208. frag_color = finalColor;
  209. }
  210. [defaults]
  211. u_active_extruder = 0.0
  212. u_layer_view_type = 0
  213. u_extruder_opacity = [1.0, 1.0, 1.0, 1.0]
  214. u_specularColor = [0.4, 0.4, 0.4, 1.0]
  215. u_ambientColor = [0.3, 0.3, 0.3, 0.0]
  216. u_diffuseColor = [1.0, 0.79, 0.14, 1.0]
  217. u_shininess = 20.0
  218. u_show_travel_moves = 0
  219. u_show_helpers = 1
  220. u_show_skin = 1
  221. u_show_infill = 1
  222. u_min_feedrate = 0
  223. u_max_feedrate = 1
  224. u_min_thickness = 0
  225. u_max_thickness = 1
  226. [bindings]
  227. u_modelViewProjectionMatrix = model_view_projection_matrix
  228. u_modelMatrix = model_matrix
  229. u_viewProjectionMatrix = view_projection_matrix
  230. u_normalMatrix = normal_matrix
  231. u_lightPosition = light_0_position
  232. [attributes]
  233. a_vertex = vertex
  234. a_color = color
  235. a_normal = normal
  236. a_line_dim = line_dim
  237. a_extruder = extruder
  238. a_material_color = material_color
  239. a_line_type = line_type
  240. a_feedrate = feedrate
  241. a_thickness = thickness