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

JIRA: STUDIO-7494
Change-Id: I717999e8b7ab1d7d350299b412a3a270c6ba7a9e
(cherry picked from commit 62b1d00d1fd6675fd067b76778d6a577dfae0c24)
This commit is contained in:
lane.wei 2024-09-02 19:51:02 +08:00 committed by Noisyfox
parent f702ad9fd2
commit 6e063bdc8d
7 changed files with 157 additions and 6 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

@ -79,11 +79,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;
@ -137,7 +150,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

@ -12,9 +12,9 @@
namespace Slic3r {
struct GCodeProcessorResult;
enum class BuildVolume_Type : unsigned char {
enum class BuildVolume_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.
@ -24,6 +24,7 @@ enum class BuildVolume_Type : unsigned char {
// Some non convex shape.
Custom
};
// For collision detection of objects and G-code (extrusion paths) against the build volume.
class BuildVolume
{
@ -31,12 +32,26 @@ public:
struct BuildExtruderVolume {
bool same_with_bed{false};
Type type{Type::Invalid};
BuildVolume_Type type{BuildVolume_Type::Invalid};
BoundingBox bbox;
BoundingBoxf3 bboxf;
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
@ -46,6 +61,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
BuildVolume_Type type() const { return m_type; }
@ -118,8 +134,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

@ -690,15 +690,32 @@ void Bed3D::render_model(const Transform3d& view_matrix, const Transform3d& proj
}
if (!m_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);
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);
}
m_model.render();
shader->stop_using();
}

View file

@ -50,6 +50,7 @@ std::pair<bool, std::string> GLShadersManager::init()
valid &= append_shader("thumbnail", { prefix + "thumbnail.vs", prefix + "thumbnail.fs"});
// used to render printbed
valid &= append_shader("printbed", { prefix + "printbed.vs", prefix + "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", { prefix + "gouraud_light_instanced.vs", prefix + "gouraud_light_instanced.fs" });

View file

@ -41,7 +41,7 @@ class Button;
namespace Slic3r {
class BuildVolume;
enum class BuildVolume_Type : unsigned char;
enum class BuildVolume_Type : char;
class Model;
class ModelObject;
class ModelInstance;