gouraud.fs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #version 110
  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 SlopeDetection slope;
  24. uniform bool offset_depth_buffer;
  25. #ifdef ENABLE_ENVIRONMENT_MAP
  26. uniform sampler2D environment_tex;
  27. uniform bool use_environment_tex;
  28. #endif // ENABLE_ENVIRONMENT_MAP
  29. varying vec3 clipping_planes_dots;
  30. // x = diffuse, y = specular;
  31. varying vec2 intensity;
  32. uniform PrintVolumeDetection print_volume;
  33. varying vec4 model_pos;
  34. varying vec4 world_pos;
  35. varying float world_normal_z;
  36. varying vec3 eye_normal;
  37. void main()
  38. {
  39. if (any(lessThan(clipping_planes_dots, ZERO)))
  40. discard;
  41. vec3 color = uniform_color.rgb;
  42. float alpha = uniform_color.a;
  43. if (slope.actived && world_normal_z < slope.normal_z - EPSILON) {
  44. color = vec3(0.7, 0.7, 1.0);
  45. alpha = 1.0;
  46. }
  47. // if the fragment is outside the print volume -> use darker color
  48. vec3 pv_check_min = ZERO;
  49. vec3 pv_check_max = ZERO;
  50. if (print_volume.type == 0) {
  51. // rectangle
  52. pv_check_min = world_pos.xyz - vec3(print_volume.xy_data.x, print_volume.xy_data.y, print_volume.z_data.x);
  53. pv_check_max = world_pos.xyz - vec3(print_volume.xy_data.z, print_volume.xy_data.w, print_volume.z_data.y);
  54. }
  55. else if (print_volume.type == 1) {
  56. // circle
  57. float delta_radius = print_volume.xy_data.z - distance(world_pos.xy, print_volume.xy_data.xy);
  58. pv_check_min = vec3(delta_radius, 0.0, world_pos.z - print_volume.z_data.x);
  59. pv_check_max = vec3(0.0, 0.0, world_pos.z - print_volume.z_data.y);
  60. }
  61. color = (any(lessThan(pv_check_min, ZERO)) || any(greaterThan(pv_check_max, ZERO))) ? mix(color, ZERO, 0.3333) : color;
  62. #ifdef ENABLE_ENVIRONMENT_MAP
  63. if (use_environment_tex)
  64. gl_FragColor = vec4(0.45 * texture2D(environment_tex, normalize(eye_normal).xy * 0.5 + 0.5).xyz + 0.8 * color * intensity.x, alpha);
  65. else
  66. #endif
  67. gl_FragColor = vec4(vec3(intensity.y) + color * intensity.x, alpha);
  68. // In the support painting gizmo and the seam painting gizmo are painted triangles rendered over the already
  69. // rendered object. To resolved z-fighting between previously rendered object and painted triangles, values
  70. // inside the depth buffer are offset by small epsilon for painted triangles inside those gizmos.
  71. gl_FragDepth = gl_FragCoord.z - (offset_depth_buffer ? EPSILON : 0.0);
  72. }