diff --git a/docs/HLSD/gcode-preview-dragging.md b/docs/HLSD/gcode-preview-dragging.md index 10a1a8debd..2997e934a5 100644 --- a/docs/HLSD/gcode-preview-dragging.md +++ b/docs/HLSD/gcode-preview-dragging.md @@ -98,23 +98,30 @@ the legend shows them. `libvgcode::keep_in_reduced_set()` holds the rule for bot cut out. The solid stands in for the toolpaths, so it wears their colours. In the views that colour by -filament (Summary, Filament, Tool) each body keeps the filament colour its shell was loaded with. -In every other view `GCodeViewer::update_solid_model_colors()` samples the toolpaths through +filament (Summary, Filament, Tool) each body keeps the filament colour its shell was loaded with. In +every other view `GCodeViewer::update_solid_model_colors()` samples the toolpaths through `Viewer::get_vertex_color()`: the body takes the outer wall's colour over the layers the solid -stands for, its faces that point up, within about ten degrees, the top surfaces', and those that -point down the bridges' or, failing those, the bottom surfaces', so that a broad floor or ledge -keeps the colour it shows at rest rather than the walls'. -`GLVolumeCollection::set_flat_face_colors()` hands those two to the `gouraud` shader per body. Each -layer cap takes the infill's colour in the layer it is cut under, sparse infill before solid, and -the prime tower its own role's. One vertex stands for the role, chosen by +stands for, its faces that point up the top surfaces', and those that point down the bridges' or, +failing those, the bottom surfaces', so that a broad floor or ledge keeps the colour it shows at +rest rather than the walls'. A sloped face counts as flat where the strip each layer leaves +uncovered by the next, the layer height over the slope's tangent, is wider than the walls, since +narrower strips print as walls alone. The cut-off is worked out per object when its shell loads, +from the configuration and the model rather than the sliced layers, which the background slicing may +be rebuilding: the thinnest layer the object's layer height, variable layer height profile and +height ranges allow, and the widest walls of its regions, all of their loops. Under a variable layer +height a slope that prints as walls on some of its layers therefore keeps the walls' colour, and a +configuration the slicer would reject falls back to about ten degrees. +`GLVolumeCollection::set_flat_faces()` hands the two colours and the cut-off to the `gouraud` shader +per body. Each layer cap takes the infill's colour in the layer it is cut under, sparse infill +before solid, and the prime tower its own role's. One vertex stands for the role, chosen by `Viewer::find_extrusion_vertex()` among up to eight of the interval's layers that have the role, spread evenly over them, so that a role printed on a few layers only, as top surfaces and bridges are, is found where it was printed; the layer table records the roles of each layer as it is built. On a plate of several objects another object's layers with the role can take every pick; where none of them has the role inside the body's footprint, the interval's other layers with the role are walked until one has. In a view that grades the toolpaths by a value, it is the length-weighted -median of that value over the role's extrusions on the sampled layers, or on that one layer when -the walk found it, so that neither the acceleration ramp a wall starts on nor one layer with the fan +median of that value over the role's extrusions on the sampled layers, or on that one layer when the +walk found it, so that neither the acceleration ramp a wall starts on nor one layer with the fan turned up sets the colour of the whole body; in any other view the first extrusion found. The colours are resolved when a drag starts and when the view type or the bands change, not per frame. diff --git a/resources/shaders/110/gouraud.fs b/resources/shaders/110/gouraud.fs index bcee7181af..36ec112b45 100644 --- a/resources/shaders/110/gouraud.fs +++ b/resources/shaders/110/gouraud.fs @@ -35,12 +35,12 @@ uniform vec4 uniform_color_clip_plane_1; uniform vec4 uniform_color_clip_plane_2; uniform SlopeDetection slope; // The G-code preview's solid model stands in for the toolpaths, so a face that points up or -// down, within FLAT_FACE_NORMAL_Z (about 10 degrees), wears the colour of the top or bottom surface -// printed there rather than the walls'. +// down, past flat_face_normal_z, wears the colour of the top or bottom surface printed there rather +// than the walls'. uniform bool use_flat_face_colors; uniform vec4 up_face_color; uniform vec4 down_face_color; -const float FLAT_FACE_NORMAL_Z = 0.985; +uniform float flat_face_normal_z; @@ -123,9 +123,9 @@ void main() color = uniform_color; if (use_flat_face_colors) { - if (world_normal_z > FLAT_FACE_NORMAL_Z) + if (world_normal_z > flat_face_normal_z) color.rgb = up_face_color.rgb; - else if (world_normal_z < -FLAT_FACE_NORMAL_Z) + else if (world_normal_z < -flat_face_normal_z) color.rgb = down_face_color.rgb; } diff --git a/resources/shaders/140/gouraud.fs b/resources/shaders/140/gouraud.fs index 3b1ac38971..c0f3992c39 100644 --- a/resources/shaders/140/gouraud.fs +++ b/resources/shaders/140/gouraud.fs @@ -35,12 +35,12 @@ uniform vec4 uniform_color_clip_plane_1; uniform vec4 uniform_color_clip_plane_2; uniform SlopeDetection slope; // The G-code preview's solid model stands in for the toolpaths, so a face that points up or -// down, within FLAT_FACE_NORMAL_Z (about 10 degrees), wears the colour of the top or bottom surface -// printed there rather than the walls'. +// down, past flat_face_normal_z, wears the colour of the top or bottom surface printed there rather +// than the walls'. uniform bool use_flat_face_colors; uniform vec4 up_face_color; uniform vec4 down_face_color; -const float FLAT_FACE_NORMAL_Z = 0.985; +uniform float flat_face_normal_z; // ORCA: a part of a translucent filament, drawn as its print would show in premultiplied color, see // GLVolumeCollection::render(). It catches a highlight only with translucent_gloss, in realistic view. @@ -136,9 +136,9 @@ void main() color = uniform_color; if (use_flat_face_colors) { - if (world_normal_z > FLAT_FACE_NORMAL_Z) + if (world_normal_z > flat_face_normal_z) color.rgb = up_face_color.rgb; - else if (world_normal_z < -FLAT_FACE_NORMAL_Z) + else if (world_normal_z < -flat_face_normal_z) color.rgb = down_face_color.rgb; } diff --git a/src/slic3r/GUI/3DScene.cpp b/src/slic3r/GUI/3DScene.cpp index 2538a15844..8ecaceb32e 100644 --- a/src/slic3r/GUI/3DScene.cpp +++ b/src/slic3r/GUI/3DScene.cpp @@ -1131,12 +1131,13 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, 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]); const unsigned int volume_id = volume.second.first; - const bool flat_face_colors = volume_id < m_flat_face_colors.size() && - (m_flat_face_colors[volume_id][0].has_value() || m_flat_face_colors[volume_id][1].has_value()); + const FlatFaces* flat_faces = volume_id < m_flat_faces.size() ? &m_flat_faces[volume_id] : nullptr; + const bool flat_face_colors = flat_faces != nullptr && (flat_faces->colors[0].has_value() || flat_faces->colors[1].has_value()); shader->set_uniform("use_flat_face_colors", flat_face_colors); if (flat_face_colors) { - shader->set_uniform("up_face_color", m_flat_face_colors[volume_id][0].value_or(volume.first->render_color)); - shader->set_uniform("down_face_color", m_flat_face_colors[volume_id][1].value_or(volume.first->render_color)); + shader->set_uniform("up_face_color", flat_faces->colors[0].value_or(volume.first->render_color)); + shader->set_uniform("down_face_color", flat_faces->colors[1].value_or(volume.first->render_color)); + shader->set_uniform("flat_face_normal_z", flat_faces->normal_z); } //BOOST_LOG_TRIVIAL(info) << boost::format("set uniform_color to {%1%, %2%, %3%, %4%}, with_outline=%5%, selected %6%") // %volume.first->render_color[0]%volume.first->render_color[1]%volume.first->render_color[2]%volume.first->render_color[3] diff --git a/src/slic3r/GUI/3DScene.hpp b/src/slic3r/GUI/3DScene.hpp index b0e4f0c811..30857764c3 100644 --- a/src/slic3r/GUI/3DScene.hpp +++ b/src/slic3r/GUI/3DScene.hpp @@ -432,6 +432,14 @@ public: std::array zs; }; + // The G-code preview's solid model: the colours of a volume's faces that point up and down, and + // the world normal z past which a face counts as one + struct FlatFaces + { + std::array, 2> colors; + float normal_z{ 0.985f }; + }; + private: PrintVolume m_print_volume; PrintVolume m_render_volume; @@ -448,9 +456,8 @@ private: bool m_use_color_clip_plane{ false }; std::array m_color_clip_plane_colors{ ColorRGBA::RED(), ColorRGBA::BLUE() }; - // per volume, by index: the colours of its faces that point up and down, used by the G-code - // preview's solid model; a volume past the end, or an unset colour, keeps its own - std::vector, 2>> m_flat_face_colors; + // per volume, by index; a volume past the end, or an unset colour, keeps its own + std::vector m_flat_faces; struct Slope { @@ -545,7 +552,7 @@ public: m_color_clip_plane[3] = offset; } void set_color_clip_plane_colors(const std::array& colors) { m_color_clip_plane_colors = colors; } - void set_flat_face_colors(std::vector, 2>> colors) { m_flat_face_colors = std::move(colors); } + void set_flat_faces(std::vector flat_faces) { m_flat_faces = std::move(flat_faces); } bool is_slope_GlobalActive() const { return m_slope.isGlobalActive; } bool is_slope_active() const { return m_slope.active; } diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index 1e778d2ea6..73546f9bcd 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -74,6 +74,8 @@ #include #include #include "libslic3r/PrintConfig.hpp" +#include "libslic3r/Flow.hpp" +#include "libslic3r/Exception.hpp" #include "slic3r/GUI/Event.hpp" #include #include @@ -2021,6 +2023,7 @@ void GCodeViewer::set_shell_transparency(float alpha) { m_shells.volumes.set_tra void GCodeViewer::reset_shell() { m_shells.volumes.clear(); + m_shells.flat_face_normal_z.clear(); m_shells.print_id = -1; m_shells.with_wipe_tower = false; m_shell_bounding_box = BoundingBoxf3(); @@ -3432,6 +3435,46 @@ void GCodeViewer::export_toolpaths_to_obj(const char* filename) const exporter.export_to(filename); } +// The world normal z past which a face of the object counts as flat in the solid model. The strip a +// layer leaves uncovered by the next is the layer height over the slope's tangent wide, and it +// prints as a top or bottom surface only where it is wider than the walls that follow the outline. +// The thinnest layer and the widest walls are taken, so that under a variable layer height a slope +// that prints as walls on some of its layers keeps the walls' colour. Read from the configuration +// and the model, which the background slicing does not change, and loaded whatever the drag mode, +// so a configuration the slicer would reject falls back to the default rather than throwing. +static float flat_face_normal_z(const PrintObject& object) +{ + const float fallback = GLVolumeCollection::FlatFaces().normal_z; + double layer_height = object.config().layer_height.value; + const ModelObject& model_object = *object.model_object(); + // the profile holds pairs of z and layer height + const std::vector profile = model_object.layer_height_profile.get(); + for (size_t i = 1; i < profile.size(); i += 2) + if (profile[i] > 0.) + layer_height = std::min(layer_height, profile[i]); + for (const auto& [range, config] : model_object.layer_config_ranges) + if (const ConfigOption* height = config.option("layer_height"); height != nullptr && height->getFloat() > 0.) + layer_height = std::min(layer_height, height->getFloat()); + if (layer_height <= 0.) + return fallback; + double walls = 0.; + try { + for (const PrintRegion& region : object.all_regions()) { + // only_one_wall_top is left out: it drops the inner walls only where the top surface is + // wider than min_width_top_surface, which a slope's strip seldom is, and never over a bridge + const int loops = std::max(1, region.config().wall_loops.value); + const double outer = region.flow(object, frExternalPerimeter, layer_height).width(); + const double inner = region.flow(object, frPerimeter, layer_height).spacing(); + walls = std::max(walls, outer + (loops - 1) * inner); + } + } catch (const InvalidArgument&) { + return fallback; + } + if (walls <= 0.) + return fallback; + return static_cast(walls / std::hypot(walls, layer_height)); +} + void GCodeViewer::load_shells(const Print& print, bool initialized, bool force_previewing) { BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(": initialized=%1%, force_previewing=%2%")%initialized %force_previewing; @@ -3487,6 +3530,7 @@ void GCodeViewer::load_shells(const Print& print, bool initialized, bool force_p size_t current_volumes_count = m_shells.volumes.volumes.size(); m_shells.volumes.load_object(model_obj, object_idx, instance_ids, "object", initialized, false); + m_shells.flat_face_normal_z.resize(m_shells.volumes.volumes.size(), flat_face_normal_z(*obj)); // adjust shells' z if raft is present const SlicingParameters& slicing_parameters = obj->slicing_parameters(); @@ -3528,6 +3572,9 @@ void GCodeViewer::load_shells(const Print& print, bool initialized, bool force_p while (true) { GLVolumePtrs::iterator it = std::find_if(m_shells.volumes.volumes.begin(), m_shells.volumes.volumes.end(), [](GLVolume* volume) { return volume->is_modifier; }); if (it != m_shells.volumes.volumes.end()) { + // the cut-offs are kept by volume index, so each goes with its volume + if (const size_t index = std::distance(m_shells.volumes.volumes.begin(), it); index < m_shells.flat_face_normal_z.size()) + m_shells.flat_face_normal_z.erase(m_shells.flat_face_normal_z.begin() + index); delete (*it); m_shells.volumes.volumes.erase(it); } @@ -3932,7 +3979,7 @@ void GCodeViewer::render_solid_model(int canvas_width, int canvas_height) own_colors.reserve(m_shells.volumes.volumes.size()); // the tower draws itself in its own colours and leaves the volume's at the default; one list per volume std::vector> own_tower_colors(m_shells.volumes.volumes.size()); - std::vector, 2>> flat_face_colors(m_shells.volumes.volumes.size()); + std::vector flat_faces(m_shells.volumes.volumes.size()); for (size_t k = 0; k < m_shells.volumes.volumes.size(); ++k) { GLVolume* volume = m_shells.volumes.volumes[k]; own_colors.push_back(volume->color); @@ -3946,10 +3993,12 @@ void GCodeViewer::render_solid_model(int canvas_width, int canvas_height) volume->set_render_color(); for (size_t f = 0; f < 2; ++f) { if (const std::optional& face = colors.bodies[k].faces[f]; face.has_value()) - flat_face_colors[k][f] = shade(*face); + flat_faces[k].colors[f] = shade(*face); } + if (k < m_shells.flat_face_normal_z.size()) + flat_faces[k].normal_z = m_shells.flat_face_normal_z[k]; } - m_shells.volumes.set_flat_face_colors(std::move(flat_face_colors)); + m_shells.volumes.set_flat_faces(std::move(flat_faces)); m_shells.volumes.set_z_range(z_bottom, z_top); // the section view cuts the solid as it cuts the toolpaths m_shells.volumes.set_clipping_plane({ m_clipping_plane[0], m_clipping_plane[1], m_clipping_plane[2], m_clipping_plane[3] }); @@ -4034,7 +4083,7 @@ void GCodeViewer::render_solid_model(int canvas_width, int canvas_height) } m_shells.volumes.set_z_range(-FLT_MAX, FLT_MAX); - m_shells.volumes.set_flat_face_colors({}); + m_shells.volumes.set_flat_faces({}); for (size_t k = 0; k < m_shells.volumes.volumes.size(); ++k) { GLVolume* volume = m_shells.volumes.volumes[k]; if (auto* tower = dynamic_cast(volume); tower != nullptr) diff --git a/src/slic3r/GUI/GCodeViewer.hpp b/src/slic3r/GUI/GCodeViewer.hpp index ffc72ad10f..e129005878 100644 --- a/src/slic3r/GUI/GCodeViewer.hpp +++ b/src/slic3r/GUI/GCodeViewer.hpp @@ -204,6 +204,9 @@ public: bool previewing{false}; // the prime tower was loaded with the objects, for the solid model bool with_wipe_tower{false}; + // per object volume, by index: the world normal z past which the solid model's faces count + // as flat; the prime tower's, added after the objects, are past the end + std::vector flat_face_normal_z; }; //BBS ConflictResultOpt m_conflict_result;