layers3d_shadow.shader 11 KB

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