gouraud.fs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #version 100
  2. precision highp float;
  3. const vec3 ZERO = vec3(0.0, 0.0, 0.0);
  4. const float EPSILON = 0.0001;
  5. struct PrintVolumeDetection
  6. {
  7. // 0 = rectangle, 1 = circle, 2 = custom, 3 = invalid
  8. int type;
  9. // type = 0 (rectangle):
  10. // x = min.x, y = min.y, z = max.x, w = max.y
  11. // type = 1 (circle):
  12. // x = center.x, y = center.y, z = radius
  13. vec4 xy_data;
  14. // x = min z, y = max z
  15. vec2 z_data;
  16. };
  17. struct SlopeDetection
  18. {
  19. bool actived;
  20. float normal_z;
  21. mat3 volume_world_normal_matrix;
  22. };
  23. uniform vec4 uniform_color;
  24. uniform bool use_color_clip_plane;
  25. uniform vec4 uniform_color_clip_plane_1;
  26. uniform vec4 uniform_color_clip_plane_2;
  27. uniform SlopeDetection slope;
  28. #ifdef ENABLE_ENVIRONMENT_MAP
  29. uniform sampler2D environment_tex;
  30. uniform bool use_environment_tex;
  31. #endif // ENABLE_ENVIRONMENT_MAP
  32. uniform PrintVolumeDetection print_volume;
  33. varying vec3 clipping_planes_dots;
  34. varying float color_clip_plane_dot;
  35. // x = diffuse, y = specular;
  36. varying vec2 intensity;
  37. varying vec4 world_pos;
  38. varying float world_normal_z;
  39. varying vec3 eye_normal;
  40. void main()
  41. {
  42. if (any(lessThan(clipping_planes_dots, ZERO)))
  43. discard;
  44. vec4 color;
  45. if (use_color_clip_plane) {
  46. color.rgb = (color_clip_plane_dot < 0.0) ? uniform_color_clip_plane_1.rgb : uniform_color_clip_plane_2.rgb;
  47. color.a = uniform_color.a;
  48. }
  49. else
  50. color = uniform_color;
  51. if (slope.actived && world_normal_z < slope.normal_z - EPSILON) {
  52. color.rgb = vec3(0.7, 0.7, 1.0);
  53. color.a = 1.0;
  54. }
  55. // if the fragment is outside the print volume -> use darker color
  56. vec3 pv_check_min = ZERO;
  57. vec3 pv_check_max = ZERO;
  58. if (print_volume.type == 0) {
  59. // rectangle
  60. pv_check_min = world_pos.xyz - vec3(print_volume.xy_data.x, print_volume.xy_data.y, print_volume.z_data.x);
  61. pv_check_max = world_pos.xyz - vec3(print_volume.xy_data.z, print_volume.xy_data.w, print_volume.z_data.y);
  62. }
  63. else if (print_volume.type == 1) {
  64. // circle
  65. float delta_radius = print_volume.xy_data.z - distance(world_pos.xy, print_volume.xy_data.xy);
  66. pv_check_min = vec3(delta_radius, 0.0, world_pos.z - print_volume.z_data.x);
  67. pv_check_max = vec3(0.0, 0.0, world_pos.z - print_volume.z_data.y);
  68. }
  69. color.rgb = (any(lessThan(pv_check_min, ZERO)) || any(greaterThan(pv_check_max, ZERO))) ? mix(color.rgb, ZERO, 0.3333) : color.rgb;
  70. #ifdef ENABLE_ENVIRONMENT_MAP
  71. if (use_environment_tex)
  72. gl_FragColor = vec4(0.45 * texture(environment_tex, normalize(eye_normal).xy * 0.5 + 0.5).xyz + 0.8 * color.rgb * intensity.x, color.a);
  73. else
  74. #endif
  75. gl_FragColor = vec4(vec3(intensity.y) + color.rgb * intensity.x, color.a);
  76. }