Visualizer: keep models centered on the build plate across load, rotation, and printer changes#185
Conversation
Co-authored-by: jgphilpott <4128208+jgphilpott@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Aligns the visualizer’s initial model placement with Polyslice’s slice-time centering/Z-floor behavior so the mesh preview and the sliced G-code occupy the same build-plate coordinates.
Changes:
- Add build-plate dimension lookup (via saved printer +
window.Polyslice.Printer, with preset/default fallbacks). - Add
positionMeshOnBuildPlate(object)to translate loaded models to Z=0 and center them in XY. - Call
positionMeshOnBuildPlate()insidedisplayMeshbefore adding the model to the scene.
You can also share your feedback on Copilot code review. Take the survey.
Co-authored-by: jgphilpott <4128208+jgphilpott@users.noreply.github.com>
|
Okay this looks good @copilot but I now see a few more updates I would like to make as well:
|
…nter change Co-authored-by: jgphilpott <4128208+jgphilpott@users.noreply.github.com>
Done in ddf175d. Both updates are in place: Gizmo drag-end repositioning — removed Axes and grid reflect printer build volume — added |
When a model was loaded in the visualizer it appeared at its raw world-space origin, while sliced G-code was always centred on the build plate — making it look like the model "teleported" after slicing. Additionally, applying rotation or switching printers after load could re-introduce the same mismatch.
Changes
examples/visualizer/modules/loaders.jsgetBuildPlateDimensions()— reads the saved printer name fromlocalStorage, querieswindow.Polyslice.Printerfor exact dimensions when available, falls back to a hardcoded table for all bundled printer presets, then defaults to 220 × 220 × 250 mm (Ender3). Now also returnsheightfor Z-axis scaling. Exported so other modules can read the current printer dimensions.positionMeshOnBuildPlate(object)— exported function that translates the loaded object so its bottom sits at Z = 0 and its XY centre aligns with(buildPlateWidth / 2, buildPlateLength / 2).displayMeshbeforescene.add(object), matching the offset logic the slicer applies internally (centerOffsetX/Y, Z-floor raise), so the visual model and the G-code visualisation occupy the same position.examples/visualizer/modules/scene.jsupdateBuildVolume(width, length, height)— new exported function that updates the X, Y, and Z axis endpoint positions in-place and rebuilds the grid lines for the new build plate dimensions. The in-place mutation preserves all existing axis-visibility toggle event-listener references so the checkboxes continue to work without re-registration.createAxesandcreateGridHelpernow accept explicit dimension parameters (defaulting to the Ender3 220 mm values).examples/visualizer/visualizer.jsapplyMeshRotation— callspositionMeshOnBuildPlate(mesh)after each slider-driven axis change so the mesh stays centred on the build plate after rotation.syncTransformToSliders— no longer repositions the mesh on every drag frame; repositioning is deferred to thedragging-changedhandler (see below).initTransformControls— thedragging-changedlistener now callspositionMeshOnBuildPlateonce when the drag ends (event.value === false), so the rotation gizmo stays centred on the mesh throughout the drag and the build-plate snap only happens at release.repositionMesh()— new helper that calls bothpositionMeshOnBuildPlate(meshObject)andupdateBuildVolume(width, length, height), passed as arepositionCallbackto bothcreateSlicingGUIcall sites.examples/visualizer/modules/ui.jscreateSlicingGUIgains an optionalrepositionCallbackparameter, called (a) once after all three saved rotation values are applied at GUI creation time and (b) inside theprinteronChangehandler so switching printers re-centres the mesh and redraws the axes/grid for the new build volume.💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.