'gouraud' shader modified to allow to draw an object with two different colors in Cut Gizmo

This commit is contained in:
enricoturri1966 2023-02-09 15:03:39 +01:00 committed by YuSanka
parent b40473be51
commit 224ee922c5
10 changed files with 89 additions and 24 deletions

View File

@ -24,6 +24,9 @@ struct SlopeDetection
}; };
uniform vec4 uniform_color; uniform vec4 uniform_color;
uniform bool use_color_clip_plane;
uniform vec4 uniform_color_clip_plane_1;
uniform vec4 uniform_color_clip_plane_2;
uniform SlopeDetection slope; uniform SlopeDetection slope;
#ifdef ENABLE_ENVIRONMENT_MAP #ifdef ENABLE_ENVIRONMENT_MAP
@ -34,6 +37,7 @@ uniform SlopeDetection slope;
uniform PrintVolumeDetection print_volume; uniform PrintVolumeDetection print_volume;
varying vec3 clipping_planes_dots; varying vec3 clipping_planes_dots;
varying float color_clip_plane_dot;
// x = diffuse, y = specular; // x = diffuse, y = specular;
varying vec2 intensity; varying vec2 intensity;
@ -46,12 +50,16 @@ void main()
{ {
if (any(lessThan(clipping_planes_dots, ZERO))) if (any(lessThan(clipping_planes_dots, ZERO)))
discard; discard;
vec3 color = uniform_color.rgb;
float alpha = uniform_color.a; vec4 color;
if (use_color_clip_plane)
color = (color_clip_plane_dot < 0.0) ? uniform_color_clip_plane_1 : uniform_color_clip_plane_2;
else
color = uniform_color;
if (slope.actived && world_normal_z < slope.normal_z - EPSILON) { if (slope.actived && world_normal_z < slope.normal_z - EPSILON) {
color = vec3(0.7, 0.7, 1.0); color.rgb = vec3(0.7, 0.7, 1.0);
alpha = 1.0; color.a = 1.0;
} }
// if the fragment is outside the print volume -> use darker color // if the fragment is outside the print volume -> use darker color
@ -68,12 +76,12 @@ void main()
pv_check_min = vec3(delta_radius, 0.0, world_pos.z - print_volume.z_data.x); pv_check_min = vec3(delta_radius, 0.0, world_pos.z - print_volume.z_data.x);
pv_check_max = vec3(0.0, 0.0, world_pos.z - print_volume.z_data.y); pv_check_max = vec3(0.0, 0.0, world_pos.z - print_volume.z_data.y);
} }
color = (any(lessThan(pv_check_min, ZERO)) || any(greaterThan(pv_check_max, ZERO))) ? mix(color, ZERO, 0.3333) : color; color.rgb = (any(lessThan(pv_check_min, ZERO)) || any(greaterThan(pv_check_max, ZERO))) ? mix(color.rgb, ZERO, 0.3333) : color.rgb;
#ifdef ENABLE_ENVIRONMENT_MAP #ifdef ENABLE_ENVIRONMENT_MAP
if (use_environment_tex) if (use_environment_tex)
gl_FragColor = vec4(0.45 * texture2D(environment_tex, normalize(eye_normal).xy * 0.5 + 0.5).xyz + 0.8 * color * intensity.x, alpha); 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);
else else
#endif #endif
gl_FragColor = vec4(vec3(intensity.y) + color * intensity.x, alpha); gl_FragColor = vec4(vec3(intensity.y) + color.rgb * intensity.x, color.a);
} }

View File

@ -35,6 +35,8 @@ uniform SlopeDetection slope;
uniform vec2 z_range; uniform vec2 z_range;
// Clipping plane - general orientation. Used by the SLA gizmo. // Clipping plane - general orientation. Used by the SLA gizmo.
uniform vec4 clipping_plane; uniform vec4 clipping_plane;
// Color clip plane - general orientation. Used by the cut gizmo.
uniform vec4 color_clip_plane;
attribute vec3 v_position; attribute vec3 v_position;
attribute vec3 v_normal; attribute vec3 v_normal;
@ -43,6 +45,7 @@ attribute vec3 v_normal;
varying vec2 intensity; varying vec2 intensity;
varying vec3 clipping_planes_dots; varying vec3 clipping_planes_dots;
varying float color_clip_plane_dot;
varying vec4 world_pos; varying vec4 world_pos;
varying float world_normal_z; varying float world_normal_z;
@ -74,4 +77,5 @@ void main()
gl_Position = projection_matrix * position; gl_Position = projection_matrix * position;
// Fill in the scalars for fragment shader clipping. Fragments with any of these components lower than zero are discarded. // Fill in the scalars for fragment shader clipping. Fragments with any of these components lower than zero are discarded.
clipping_planes_dots = vec3(dot(world_pos, clipping_plane), world_pos.z - z_range.x, z_range.y - world_pos.z); clipping_planes_dots = vec3(dot(world_pos, clipping_plane), world_pos.z - z_range.x, z_range.y - world_pos.z);
color_clip_plane_dot = dot(world_pos, color_clip_plane);
} }

View File

@ -24,6 +24,9 @@ struct SlopeDetection
}; };
uniform vec4 uniform_color; uniform vec4 uniform_color;
uniform bool use_color_clip_plane;
uniform vec4 uniform_color_clip_plane_1;
uniform vec4 uniform_color_clip_plane_2;
uniform SlopeDetection slope; uniform SlopeDetection slope;
#ifdef ENABLE_ENVIRONMENT_MAP #ifdef ENABLE_ENVIRONMENT_MAP
@ -34,6 +37,7 @@ uniform SlopeDetection slope;
uniform PrintVolumeDetection print_volume; uniform PrintVolumeDetection print_volume;
in vec3 clipping_planes_dots; in vec3 clipping_planes_dots;
in float color_clip_plane_dot;
// x = diffuse, y = specular; // x = diffuse, y = specular;
in vec2 intensity; in vec2 intensity;
@ -48,12 +52,16 @@ void main()
{ {
if (any(lessThan(clipping_planes_dots, ZERO))) if (any(lessThan(clipping_planes_dots, ZERO)))
discard; discard;
vec3 color = uniform_color.rgb;
float alpha = uniform_color.a; vec4 color;
if (use_color_clip_plane)
color = (color_clip_plane_dot < 0.0) ? uniform_color_clip_plane_1 : uniform_color_clip_plane_2;
else
color = uniform_color;
if (slope.actived && world_normal_z < slope.normal_z - EPSILON) { if (slope.actived && world_normal_z < slope.normal_z - EPSILON) {
color = vec3(0.7, 0.7, 1.0); color.rgb = vec3(0.7, 0.7, 1.0);
alpha = 1.0; color.a = 1.0;
} }
// if the fragment is outside the print volume -> use darker color // if the fragment is outside the print volume -> use darker color
@ -70,12 +78,12 @@ void main()
pv_check_min = vec3(delta_radius, 0.0, world_pos.z - print_volume.z_data.x); pv_check_min = vec3(delta_radius, 0.0, world_pos.z - print_volume.z_data.x);
pv_check_max = vec3(0.0, 0.0, world_pos.z - print_volume.z_data.y); pv_check_max = vec3(0.0, 0.0, world_pos.z - print_volume.z_data.y);
} }
color = (any(lessThan(pv_check_min, ZERO)) || any(greaterThan(pv_check_max, ZERO))) ? mix(color, ZERO, 0.3333) : color; color.rgb = (any(lessThan(pv_check_min, ZERO)) || any(greaterThan(pv_check_max, ZERO))) ? mix(color.rgb, ZERO, 0.3333) : color.rgb;
#ifdef ENABLE_ENVIRONMENT_MAP #ifdef ENABLE_ENVIRONMENT_MAP
if (use_environment_tex) if (use_environment_tex)
out_color = vec4(0.45 * texture(environment_tex, normalize(eye_normal).xy * 0.5 + 0.5).xyz + 0.8 * color * intensity.x, alpha); 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);
else else
#endif #endif
out_color = vec4(vec3(intensity.y) + color * intensity.x, alpha); out_color = vec4(vec3(intensity.y) + color.rgb * intensity.x, color.a);
} }

View File

@ -35,6 +35,8 @@ uniform SlopeDetection slope;
uniform vec2 z_range; uniform vec2 z_range;
// Clipping plane - general orientation. Used by the SLA gizmo. // Clipping plane - general orientation. Used by the SLA gizmo.
uniform vec4 clipping_plane; uniform vec4 clipping_plane;
// Color clip plane - general orientation. Used by the cut gizmo.
uniform vec4 color_clip_plane;
in vec3 v_position; in vec3 v_position;
in vec3 v_normal; in vec3 v_normal;
@ -43,6 +45,7 @@ in vec3 v_normal;
out vec2 intensity; out vec2 intensity;
out vec3 clipping_planes_dots; out vec3 clipping_planes_dots;
out float color_clip_plane_dot;
out vec4 world_pos; out vec4 world_pos;
out float world_normal_z; out float world_normal_z;
@ -74,4 +77,5 @@ void main()
gl_Position = projection_matrix * position; gl_Position = projection_matrix * position;
// Fill in the scalars for fragment shader clipping. Fragments with any of these components lower than zero are discarded. // Fill in the scalars for fragment shader clipping. Fragments with any of these components lower than zero are discarded.
clipping_planes_dots = vec3(dot(world_pos, clipping_plane), world_pos.z - z_range.x, z_range.y - world_pos.z); clipping_planes_dots = vec3(dot(world_pos, clipping_plane), world_pos.z - z_range.x, z_range.y - world_pos.z);
color_clip_plane_dot = dot(world_pos, color_clip_plane);
} }

View File

@ -26,6 +26,9 @@ struct SlopeDetection
}; };
uniform vec4 uniform_color; uniform vec4 uniform_color;
uniform bool use_color_clip_plane;
uniform vec4 uniform_color_clip_plane_1;
uniform vec4 uniform_color_clip_plane_2;
uniform SlopeDetection slope; uniform SlopeDetection slope;
#ifdef ENABLE_ENVIRONMENT_MAP #ifdef ENABLE_ENVIRONMENT_MAP
@ -36,6 +39,7 @@ uniform SlopeDetection slope;
uniform PrintVolumeDetection print_volume; uniform PrintVolumeDetection print_volume;
varying vec3 clipping_planes_dots; varying vec3 clipping_planes_dots;
varying float color_clip_plane_dot;
// x = diffuse, y = specular; // x = diffuse, y = specular;
varying vec2 intensity; varying vec2 intensity;
@ -48,12 +52,16 @@ void main()
{ {
if (any(lessThan(clipping_planes_dots, ZERO))) if (any(lessThan(clipping_planes_dots, ZERO)))
discard; discard;
vec3 color = uniform_color.rgb;
float alpha = uniform_color.a; vec4 color;
if (use_color_clip_plane)
color = (color_clip_plane_dot < 0.0) ? uniform_color_clip_plane_1 : uniform_color_clip_plane_2;
else
color = uniform_color;
if (slope.actived && world_normal_z < slope.normal_z - EPSILON) { if (slope.actived && world_normal_z < slope.normal_z - EPSILON) {
color = vec3(0.7, 0.7, 1.0); color.rgb = vec3(0.7, 0.7, 1.0);
alpha = 1.0; color.a = 1.0;
} }
// if the fragment is outside the print volume -> use darker color // if the fragment is outside the print volume -> use darker color
@ -70,12 +78,12 @@ void main()
pv_check_min = vec3(delta_radius, 0.0, world_pos.z - print_volume.z_data.x); pv_check_min = vec3(delta_radius, 0.0, world_pos.z - print_volume.z_data.x);
pv_check_max = vec3(0.0, 0.0, world_pos.z - print_volume.z_data.y); pv_check_max = vec3(0.0, 0.0, world_pos.z - print_volume.z_data.y);
} }
color = (any(lessThan(pv_check_min, ZERO)) || any(greaterThan(pv_check_max, ZERO))) ? mix(color, ZERO, 0.3333) : color; color.rgb = (any(lessThan(pv_check_min, ZERO)) || any(greaterThan(pv_check_max, ZERO))) ? mix(color.rgb, ZERO, 0.3333) : color.rgb;
#ifdef ENABLE_ENVIRONMENT_MAP #ifdef ENABLE_ENVIRONMENT_MAP
if (use_environment_tex) if (use_environment_tex)
gl_FragColor = vec4(0.45 * texture2D(environment_tex, normalize(eye_normal).xy * 0.5 + 0.5).xyz + 0.8 * color * intensity.x, alpha); 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);
else else
#endif #endif
gl_FragColor = vec4(vec3(intensity.y) + color * intensity.x, alpha); gl_FragColor = vec4(vec3(intensity.y) + color.rgb * intensity.x, color.a);
} }

View File

@ -35,6 +35,8 @@ uniform SlopeDetection slope;
uniform vec2 z_range; uniform vec2 z_range;
// Clipping plane - general orientation. Used by the SLA gizmo. // Clipping plane - general orientation. Used by the SLA gizmo.
uniform vec4 clipping_plane; uniform vec4 clipping_plane;
// Color clip plane - general orientation. Used by the cut gizmo.
uniform vec4 color_clip_plane;
attribute vec3 v_position; attribute vec3 v_position;
attribute vec3 v_normal; attribute vec3 v_normal;
@ -43,6 +45,7 @@ attribute vec3 v_normal;
varying vec2 intensity; varying vec2 intensity;
varying vec3 clipping_planes_dots; varying vec3 clipping_planes_dots;
varying float color_clip_plane_dot;
varying vec4 world_pos; varying vec4 world_pos;
varying float world_normal_z; varying float world_normal_z;
@ -74,4 +77,5 @@ void main()
gl_Position = projection_matrix * position; gl_Position = projection_matrix * position;
// Fill in the scalars for fragment shader clipping. Fragments with any of these components lower than zero are discarded. // Fill in the scalars for fragment shader clipping. Fragments with any of these components lower than zero are discarded.
clipping_planes_dots = vec3(dot(world_pos, clipping_plane), world_pos.z - z_range.x, z_range.y - world_pos.z); clipping_planes_dots = vec3(dot(world_pos, clipping_plane), world_pos.z - z_range.x, z_range.y - world_pos.z);
color_clip_plane_dot = dot(world_pos, color_clip_plane);
} }

View File

@ -814,6 +814,10 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disab
shader->set_uniform("z_range", m_z_range); shader->set_uniform("z_range", m_z_range);
shader->set_uniform("clipping_plane", m_clipping_plane); shader->set_uniform("clipping_plane", m_clipping_plane);
shader->set_uniform("use_color_clip_plane", m_use_color_clip_plane);
shader->set_uniform("color_clip_plane", m_color_clip_plane);
shader->set_uniform("uniform_color_clip_plane_1", m_color_clip_plane_colors[0]);
shader->set_uniform("uniform_color_clip_plane_2", m_color_clip_plane_colors[1]);
shader->set_uniform("print_volume.type", static_cast<int>(m_print_volume.type)); shader->set_uniform("print_volume.type", static_cast<int>(m_print_volume.type));
shader->set_uniform("print_volume.xy_data", m_print_volume.data); shader->set_uniform("print_volume.xy_data", m_print_volume.data);
shader->set_uniform("print_volume.z_data", m_print_volume.zs); shader->set_uniform("print_volume.z_data", m_print_volume.zs);

View File

@ -387,6 +387,12 @@ private:
// plane coeffs for clipping in shaders // plane coeffs for clipping in shaders
std::array<double, 4> m_clipping_plane; std::array<double, 4> m_clipping_plane;
// plane coeffs for render volumes with different colors in shaders
// used by cut gizmo
std::array<double, 4> m_color_clip_plane;
bool m_use_color_clip_plane{ false };
std::array<ColorRGBA, 2> m_color_clip_plane_colors{ ColorRGBA::RED(), ColorRGBA::BLUE() };
struct Slope struct Slope
{ {
// toggle for slope rendering // toggle for slope rendering
@ -445,6 +451,10 @@ public:
const std::array<float, 2>& get_z_range() const { return m_z_range; } const std::array<float, 2>& get_z_range() const { return m_z_range; }
const std::array<double, 4>& get_clipping_plane() const { return m_clipping_plane; } const std::array<double, 4>& get_clipping_plane() const { return m_clipping_plane; }
void set_use_color_clip_plane(bool use) { m_use_color_clip_plane = use; }
void set_color_clip_plane(const std::array<double, 4>& coeffs) { m_color_clip_plane = coeffs; }
void set_color_clip_plane_colors(const std::array<ColorRGBA, 2>& colors) { m_color_clip_plane_colors = colors; }
bool is_slope_active() const { return m_slope.active; } bool is_slope_active() const { return m_slope.active; }
void set_slope_active(bool active) { m_slope.active = active; } void set_slope_active(bool active) { m_slope.active = active; }

View File

@ -712,6 +712,10 @@ public:
bool get_use_clipping_planes() const { return m_use_clipping_planes; } bool get_use_clipping_planes() const { return m_use_clipping_planes; }
const std::array<ClippingPlane, 2> &get_clipping_planes() const { return m_clipping_planes; }; const std::array<ClippingPlane, 2> &get_clipping_planes() const { return m_clipping_planes; };
void set_use_color_clip_plane(bool use) { m_volumes.set_use_color_clip_plane(use); }
void set_color_clip_plane(const std::array<double, 4>& coeffs) { m_volumes.set_color_clip_plane(coeffs); }
void set_color_clip_plane_colors(const std::array<ColorRGBA, 2>& colors) { m_volumes.set_color_clip_plane_colors(colors); }
void refresh_camera_scene_box(); void refresh_camera_scene_box();
BoundingBoxf3 volumes_bounding_box() const; BoundingBoxf3 volumes_bounding_box() const;

View File

@ -413,7 +413,7 @@ void GLGizmoCut3D::update_clipper()
Vec3d normal = m_cut_normal = end - beg; Vec3d normal = m_cut_normal = end - beg;
m_cut_normal.normalize(); m_cut_normal.normalize();
if (!is_looking_forward()) { if (!is_looking_forward() && m_connectors_editing) {
end = beg = m_plane_center; end = beg = m_plane_center;
beg[Z] = box.center().z() + m_radius; beg[Z] = box.center().z() + m_radius;
end[Z] = box.center().z() - m_radius; end[Z] = box.center().z() - m_radius;
@ -438,6 +438,13 @@ void GLGizmoCut3D::update_clipper()
put_connectors_on_cut_plane(normal, offset); put_connectors_on_cut_plane(normal, offset);
std::array<double, 4> data = m_c->object_clipper()->get_clipping_plane(true)->get_data();
// we need to invert the normal for the shader to work properly
for (int i = 0; i < 3; ++i) {
data[i] = -data[i];
}
m_parent.set_color_clip_plane(data);
if (m_raycasters.empty()) if (m_raycasters.empty())
on_register_raycasters_for_picking(); on_register_raycasters_for_picking();
else else
@ -901,6 +908,9 @@ std::string GLGizmoCut3D::on_get_name() const
void GLGizmoCut3D::on_set_state() void GLGizmoCut3D::on_set_state()
{ {
if (m_state == On) { if (m_state == On) {
m_parent.set_use_color_clip_plane(true);
m_parent.set_color_clip_plane_colors({ ABOVE_GRABBER_COLOR , BELOW_GRABBER_COLOR });
update_bb(); update_bb();
m_connectors_editing = !m_selected.empty(); m_connectors_editing = !m_selected.empty();
@ -916,6 +926,7 @@ void GLGizmoCut3D::on_set_state()
oc->release(); oc->release();
} }
m_selected.clear(); m_selected.clear();
m_parent.set_use_color_clip_plane(false);
} }
force_update_clipper_on_render = m_state == On; force_update_clipper_on_render = m_state == On;
} }