gouraud.fs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #version 140
  2. const vec3 ZERO = vec3(0.0, 0.0, 0.0);
  3. const float EPSILON = 0.0001;
  4. struct PrintVolumeDetection
  5. {
  6. // 0 = rectangle, 1 = circle, 2 = custom, 3 = invalid
  7. int type;
  8. // type = 0 (rectangle):
  9. // x = min.x, y = min.y, z = max.x, w = max.y
  10. // type = 1 (circle):
  11. // x = center.x, y = center.y, z = radius
  12. vec4 xy_data;
  13. // x = min z, y = max z
  14. vec2 z_data;
  15. };
  16. struct SlopeDetection
  17. {
  18. bool actived;
  19. float normal_z;
  20. mat3 volume_world_normal_matrix;
  21. };
  22. uniform vec4 uniform_color;
  23. uniform bool use_color_clip_plane;
  24. uniform vec4 uniform_color_clip_plane_1;
  25. uniform vec4 uniform_color_clip_plane_2;
  26. uniform SlopeDetection slope;
  27. #ifdef ENABLE_ENVIRONMENT_MAP
  28. uniform sampler2D environment_tex;
  29. uniform bool use_environment_tex;
  30. #endif // ENABLE_ENVIRONMENT_MAP
  31. uniform PrintVolumeDetection print_volume;
  32. in vec3 clipping_planes_dots;
  33. in float color_clip_plane_dot;
  34. // x = diffuse, y = specular;
  35. in vec2 intensity;
  36. in vec4 world_pos;
  37. in float world_normal_z;
  38. in vec3 eye_normal;
  39. out vec4 out_color;
  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. out_color = 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. out_color = vec4(vec3(intensity.y) + color.rgb * intensity.x, color.a);
  76. }