Skip to content

Visualizer: keep models centered on the build plate across load, rotation, and printer changes#185

Merged
jgphilpott merged 4 commits intomainfrom
copilot/update-visualizer-center-models
Mar 13, 2026
Merged

Visualizer: keep models centered on the build plate across load, rotation, and printer changes#185
jgphilpott merged 4 commits intomainfrom
copilot/update-visualizer-center-models

Conversation

Copy link
Contributor

Copilot AI commented Mar 13, 2026

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.js

  • getBuildPlateDimensions() — reads the saved printer name from localStorage, queries window.Polyslice.Printer for 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 returns height for 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).
  • Called inside displayMesh before scene.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.js

  • updateBuildVolume(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.
  • createAxes and createGridHelper now accept explicit dimension parameters (defaulting to the Ender3 220 mm values).

examples/visualizer/visualizer.js

  • applyMeshRotation — calls positionMeshOnBuildPlate(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 the dragging-changed handler (see below).
  • initTransformControls — the dragging-changed listener now calls positionMeshOnBuildPlate once 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 both positionMeshOnBuildPlate(meshObject) and updateBuildVolume(width, length, height), passed as a repositionCallback to both createSlicingGUI call sites.

examples/visualizer/modules/ui.js

  • createSlicingGUI gains an optional repositionCallback parameter, called (a) once after all three saved rotation values are applied at GUI creation time and (b) inside the printer onChange handler so switching printers re-centres the mesh and redraws the axes/grid for the new build volume.
// Positioning mirrors what the slicer does at slice time:
//   centerOffsetX = (buildPlateWidth / 2) - meshCenterX  →  net offset = 0
//   centerOffsetY = (buildPlateLength / 2) - meshCenterY →  net offset = 0
positionMeshOnBuildPlate(object);  // called on load, after rotation (drag-end), and on printer change
updateBuildVolume(width, length, height);  // called on printer change to redraw axes and grid

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: jgphilpott <4128208+jgphilpott@users.noreply.github.com>
Copilot AI changed the title [WIP] Update visualizer to center and lay flat loaded models Visualizer: auto-center and lay models flat on the build plate at load time Mar 13, 2026
Copilot AI requested a review from jgphilpott March 13, 2026 06:38
@jgphilpott jgphilpott marked this pull request as ready for review March 13, 2026 06:52
Copilot AI review requested due to automatic review settings March 13, 2026 06:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() inside displayMesh before 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>
Copilot AI changed the title Visualizer: auto-center and lay models flat on the build plate at load time Visualizer: keep models centered on the build plate across load, rotation, and printer changes Mar 13, 2026
@jgphilpott
Copy link
Owner

Okay this looks good @copilot but I now see a few more updates I would like to make as well:

  • The rotation gizmo should always remain centered on the mesh/model and only call positionMeshOnBuildPlate when the rotation is finished (mouse up event), not on every little movement while dragging the gizmo.
  • Also, do you think we could redraw/update the axes and grid lines when the selected printer changes to reflect the build volume of the newly selected printer?

…nter change

Co-authored-by: jgphilpott <4128208+jgphilpott@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 13, 2026

Okay this looks good @copilot but I now see a few more updates I would like to make as well:

  • The rotation gizmo should always remain centered on the mesh/model and only call `po...

Done in ddf175d. Both updates are in place:

Gizmo drag-end repositioning — removed positionMeshOnBuildPlate from syncTransformToSliders (which fired on every objectChange frame during a drag). It now runs once in the dragging-changed handler when event.value becomes false (drag released). The gizmo stays centered on the mesh throughout the rotation and the build-plate re-centering only happens once at the end.

Axes and grid reflect printer build volume — added updateBuildVolume(width, length, height) to scene.js. It updates the X/Y/Z axis endpoint positions in-place and rebuilds the grid lines for the new dimensions, so existing axis-visibility toggle event listeners continue to work without re-registration. repositionMesh() (called on printer change) now also calls updateBuildVolume, so switching printers immediately redraws the grid/axes to match the new build plate size. Height values were added to the printer fallback table and to the window.Polyslice.Printer path.

@jgphilpott jgphilpott merged commit 0cff9d5 into main Mar 13, 2026
1 check passed
@jgphilpott jgphilpott deleted the copilot/update-visualizer-center-models branch March 13, 2026 07:35
Copilot AI mentioned this pull request Mar 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants