ENH: 3dbed: support rendering extruder area with different color

JIRA: STUDIO-7494
Change-Id: I717999e8b7ab1d7d350299b412a3a270c6ba7a9e
This commit is contained in:
lane.wei 2024-09-02 19:51:02 +08:00
parent 2d791aea17
commit 62b1d00d1f
8 changed files with 166 additions and 8 deletions

View File

@ -0,0 +1,43 @@
#version 110
const vec3 ZERO = vec3(0.0, 0.0, 0.0);
const vec3 WHITE = vec3(1.0, 1.0, 1.0);
struct PrintVolumeDetection
{
// 0 = rectangle, 1 = circle, 2 = custom, 3 = invalid
int type;
// type = 0 (rectangle):
// x = min.x, y = min.y, z = max.x, w = max.y
// type = 1 (circle):
// x = center.x, y = center.y, z = radius
vec4 xy_data;
// x = min z, y = max z
vec2 z_data;
};
uniform vec4 uniform_color;
uniform float emission_factor;
uniform PrintVolumeDetection print_volume;
// x = diffuse, y = specular;
varying vec2 intensity;
varying vec4 world_pos;
void main()
{
vec3 color = uniform_color.rgb;
float alpha = uniform_color.a;
// if the fragment is outside the print volume -> use darker color
vec3 pv_check_min = ZERO;
vec3 pv_check_max = ZERO;
if (print_volume.type == 0) {// rectangle
pv_check_min = world_pos.xyz - vec3(print_volume.xy_data.x, print_volume.xy_data.y, print_volume.z_data.x);
pv_check_max = world_pos.xyz - vec3(print_volume.xy_data.z, print_volume.xy_data.w, print_volume.z_data.y);
}
else if (print_volume.type == 1) {// circle
float delta_radius = print_volume.xy_data.z - distance(world_pos.xy, print_volume.xy_data.xy);
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);
}
color = (any(lessThan(pv_check_min, ZERO)) || any(greaterThan(pv_check_max, ZERO))) ? mix(color, WHITE, 0.3333) : color;
//gl_FragColor = vec4(vec3(intensity.y) + color * intensity.x, alpha);
gl_FragColor = vec4(vec3(intensity.y) + color * (intensity.x + emission_factor), alpha);
}

View File

@ -0,0 +1,47 @@
#version 110
#define INTENSITY_CORRECTION 0.6
// normalized values for (-0.6/1.31, 0.6/1.31, 1./1.31)
const vec3 LIGHT_TOP_DIR = vec3(-0.4574957, 0.4574957, 0.7624929);
#define LIGHT_TOP_DIFFUSE (0.8 * INTENSITY_CORRECTION)
#define LIGHT_TOP_SPECULAR (0.125 * INTENSITY_CORRECTION)
#define LIGHT_TOP_SHININESS 20.0
// normalized values for (1./1.43, 0.2/1.43, 1./1.43)
const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);
#define LIGHT_FRONT_DIFFUSE (0.3 * INTENSITY_CORRECTION)
#define INTENSITY_AMBIENT 0.3
uniform mat4 view_model_matrix;
uniform mat4 projection_matrix;
uniform mat3 view_normal_matrix;
uniform mat4 volume_world_matrix;
attribute vec3 v_position;
attribute vec3 v_normal;
// x = tainted, y = specular;
varying vec2 intensity;
varying vec4 world_pos;
void main()
{
// First transform the normal into camera space and normalize the result.
vec3 normal = normalize(view_normal_matrix * v_normal);
// Compute the cos of the angle between the normal and lights direction. The light is directional so the direction is constant for every vertex.
// Since these two are normalized the cosine is the dot product. We also need to clamp the result to the [0,1] range.
float NdotL = max(dot(normal, LIGHT_TOP_DIR), 0.0);
intensity.x = INTENSITY_AMBIENT + NdotL * LIGHT_TOP_DIFFUSE;
world_pos = volume_world_matrix * vec4(v_position, 1.0);
vec4 position = view_model_matrix * vec4(v_position, 1.0);
intensity.y = LIGHT_TOP_SPECULAR * pow(max(dot(-normalize(position.xyz), reflect(-LIGHT_TOP_DIR, normal)), 0.0), LIGHT_TOP_SHININESS);
// Perform the same lighting calculation for the 2nd light source (no specular applied).
NdotL = max(dot(normal, LIGHT_FRONT_DIR), 0.0);
intensity.x += NdotL * LIGHT_FRONT_DIFFUSE;
gl_Position = projection_matrix * position;
}

View File

@ -78,11 +78,24 @@ BuildVolume::BuildVolume(const std::vector<Vec2d> &printable_area, const double
if (m_extruder_shapes.size() > 0)
{
m_shared_volume.data[0] = m_bboxf.min.x();
m_shared_volume.data[1] = m_bboxf.min.y();
m_shared_volume.data[2] = m_bboxf.max.x();
m_shared_volume.data[3] = m_bboxf.max.y();
for (unsigned int index = 0; index < m_extruder_shapes.size(); index++)
{
std::vector<Vec2d>& extruder_shape = m_extruder_shapes[index];
BuildExtruderVolume extruder_volume;
if (extruder_shape.empty())
{
//should not happen
BOOST_LOG_TRIVIAL(warning) << boost::format("Found invalid extruder_printable_area of index %1%")%index;
assert(false);
m_extruder_shapes.clear();
return;
}
if (extruder_shape == printable_area) {
extruder_volume.same_with_bed = true;
extruder_volume.type = m_type;
@ -136,7 +149,20 @@ BuildVolume::BuildVolume(const std::vector<Vec2d> &printable_area, const double
}
m_extruder_volumes.push_back(std::move(extruder_volume));
}
if (m_shared_volume.data[0] < extruder_volume.bboxf.min.x())
m_shared_volume.data[0] = extruder_volume.bboxf.min.x();
if (m_shared_volume.data[1] < extruder_volume.bboxf.min.y())
m_shared_volume.data[1] = extruder_volume.bboxf.min.y();
if (m_shared_volume.data[2] > extruder_volume.bboxf.max.x())
m_shared_volume.data[2] = extruder_volume.bboxf.max.x();
if (m_shared_volume.data[3] > extruder_volume.bboxf.max.y())
m_shared_volume.data[3] = extruder_volume.bboxf.max.y();
}
m_shared_volume.type = static_cast<int>(m_type);
m_shared_volume.zs[0] = 0.f;
m_shared_volume.zs[1] = printable_height;
}
BOOST_LOG_TRIVIAL(debug) << "BuildVolume printable_area clasified as: " << this->type_name();

View File

@ -17,10 +17,10 @@ struct GCodeProcessorResult;
class BuildVolume
{
public:
enum class Type : unsigned char
enum class Type : char
{
// Not set yet or undefined.
Invalid,
Invalid = -1,
// Rectangular print bed. Most common, cheap to work with.
Rectangle,
// Circular print bed. Common on detals, cheap to work with.
@ -39,6 +39,20 @@ public:
Geometry::Circled circle;
};
struct BuildSharedVolume
{
// see: Bed3D::EShapeType
int type{ 0 };
// data contains:
// Rectangle:
// [0] = min.x, [1] = min.y, [2] = max.x, [3] = max.y
// Circle:
// [0] = center.x, [1] = center.y, [3] = radius
std::array<float, 4> data;
// [0] = min z, [1] = max z
std::array<float, 2> zs;
};
// Initialized to empty, all zeros, Invalid.
BuildVolume() {}
// Initialize from PrintConfig::printable_area and PrintConfig::printable_height
@ -48,6 +62,7 @@ public:
const std::vector<Vec2d>& printable_area() const { return m_bed_shape; }
double printable_height() const { return m_max_print_height; }
const std::vector<std::vector<Vec2d>>& extruder_areas() const { return m_extruder_shapes; }
const BuildSharedVolume& get_shared_volume() const { return m_shared_volume; }
// Derived data
Type type() const { return m_type; }
@ -117,8 +132,9 @@ private:
// Source definition of the print bed geometry (PrintConfig::printable_area)
std::vector<Vec2d> m_bed_shape;
//BBS: extruder shapes
std::vector<std::vector<Vec2d>> m_extruder_shapes;
std::vector<std::vector<Vec2d>> m_extruder_shapes; //original data from config
std::vector<BuildExtruderVolume> m_extruder_volumes;
BuildSharedVolume m_shared_volume; //used for rendering
// Source definition of the print volume height (PrintConfig::printable_height)
double m_max_print_height { 0.f };

View File

@ -685,14 +685,33 @@ void Bed3D::render_model() const
}
if (!model->get_filename().empty()) {
GLShaderProgram* shader = wxGetApp().get_shader("gouraud_light");
const Camera & camera = wxGetApp().plater()->get_camera();
const Transform3d &view_matrix = camera.get_view_matrix();
const Transform3d &projection_matrix = camera.get_projection_matrix();
GLShaderProgram* shader = wxGetApp().get_shader("hotbed");
if (shader != nullptr) {
shader->start_using();
shader->set_uniform("emission_factor", 0.0f);
glsafe(::glPushMatrix());
glsafe(::glTranslated(m_model_offset.x(), m_model_offset.y(), m_model_offset.z()));
model->render();
glsafe(::glPopMatrix());
const Transform3d model_matrix = Geometry::assemble_transform(m_model_offset);
shader->set_uniform("volume_world_matrix", model_matrix);
shader->set_uniform("view_model_matrix", view_matrix * model_matrix);
shader->set_uniform("projection_matrix", projection_matrix);
const Matrix3d view_normal_matrix = view_matrix.matrix().block(0, 0, 3, 3) * model_matrix.matrix().block(0, 0, 3, 3).inverse().transpose();
shader->set_uniform("view_normal_matrix", view_normal_matrix);
if (m_build_volume.get_extruder_area_count() > 0) {
const BuildVolume::BuildSharedVolume& shared_volume = m_build_volume.get_shared_volume();
std::array<float, 4> xy_data = shared_volume.data;
shader->set_uniform("print_volume.type", shared_volume.type);
shader->set_uniform("print_volume.xy_data", xy_data);
std::array<float, 2> zs = shared_volume.zs;
zs[0] = -1;
shader->set_uniform("print_volume.z_data", zs);
}
else {
//use -1 ad a invalid type
shader->set_uniform("print_volume.type", -1);
}
model->render_geometry();
shader->stop_using();
}
}

View File

@ -344,6 +344,11 @@ bool GLShaderProgram::set_uniform(const char* name, const Matrix3f& value) const
return false;
}
bool GLShaderProgram::set_uniform(const char *name, const Matrix3d &value) const
{
return set_uniform(name, (Matrix3f) value.cast<float>());
}
bool GLShaderProgram::set_uniform(const char* name, const Vec3f& value) const
{
int id = get_uniform_location(name);

View File

@ -58,6 +58,7 @@ public:
bool set_uniform(const char* name, const Transform3f& value) const;
bool set_uniform(const char* name, const Transform3d& value) const;
bool set_uniform(const char* name, const Matrix3f& value) const;
bool set_uniform(const char *name, const Matrix3d &value) const;
bool set_uniform(const char* name, const Vec3f& value) const;
bool set_uniform(const char* name, const Vec3d& value) const;

View File

@ -43,6 +43,7 @@ std::pair<bool, std::string> GLShadersManager::init()
valid &= append_shader("flat_instance", {"110/flat_instance.vs", "110/flat.fs"});
// used to render printbed
valid &= append_shader("printbed", {"110/printbed.vs", "110/printbed.fs"});
valid &= append_shader("hotbed", {"110/hotbed.vs", "110/hotbed.fs"});
// used to render options in gcode preview
if (GUI::wxGetApp().is_gl_version_greater_or_equal_to(3, 3))
valid &= append_shader("gouraud_light_instanced", { "gouraud_light_instanced.vs", "gouraud_light_instanced.fs" });