gouraud.fs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 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. varying vec3 clipping_planes_dots;
  33. varying float color_clip_plane_dot;
  34. // x = diffuse, y = specular;
  35. varying vec2 intensity;
  36. varying vec4 world_pos;
  37. varying float world_normal_z;
  38. varying vec3 eye_normal;
  39. void main()
  40. {
  41. if (any(lessThan(clipping_planes_dots, ZERO)))
  42. discard;
  43. vec4 color;
  44. if (use_color_clip_plane) {
  45. color.rgb = (color_clip_plane_dot < 0.0) ? uniform_color_clip_plane_1.rgb : uniform_color_clip_plane_2.rgb;
  46. color.a = uniform_color.a;
  47. }
  48. else
  49. color = uniform_color;
  50. if (slope.actived && world_normal_z < slope.normal_z - EPSILON) {
  51. color.rgb = vec3(0.7, 0.7, 1.0);
  52. color.a = 1.0;
  53. }
  54. // if the fragment is outside the print volume -> use darker color
  55. vec3 pv_check_min = ZERO;
  56. vec3 pv_check_max = ZERO;
  57. if (print_volume.type == 0) {
  58. // rectangle
  59. pv_check_min = world_pos.xyz - vec3(print_volume.xy_data.x, print_volume.xy_data.y, print_volume.z_data.x);
  60. pv_check_max = world_pos.xyz - vec3(print_volume.xy_data.z, print_volume.xy_data.w, print_volume.z_data.y);
  61. }
  62. else if (print_volume.type == 1) {
  63. // circle
  64. float delta_radius = print_volume.xy_data.z - distance(world_pos.xy, print_volume.xy_data.xy);
  65. pv_check_min = vec3(delta_radius, 0.0, world_pos.z - print_volume.z_data.x);
  66. pv_check_max = vec3(0.0, 0.0, world_pos.z - print_volume.z_data.y);
  67. }
  68. color.rgb = (any(lessThan(pv_check_min, ZERO)) || any(greaterThan(pv_check_max, ZERO))) ? mix(color.rgb, ZERO, 0.3333) : color.rgb;
  69. #ifdef ENABLE_ENVIRONMENT_MAP
  70. if (use_environment_tex)
  71. 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);
  72. else
  73. #endif
  74. gl_FragColor = vec4(vec3(intensity.y) + color.rgb * intensity.x, color.a);
  75. }