@chestnutlabs/gcode-model-renderer

中文文档
0.21.1发布于 2026/11/04MITTypeScriptES Module构建来源已验证近 30 天下载 2144

Three.js presentation renderer for source models (STL/3MF) — clean thumbnails/cards, distinct from toolpath inspection (DD-018).

原始使用文档

@chestnutlabs/gcode-model-renderer

Three.js presentation renderer for source models — draws an .stl or .3mf mesh as a clean, studio-lit thumbnail. It answers "what object is this?", and is deliberately separate from the toolpath renderer, which answers "how does this print/cut run?" (design: DD-018).

Peer dependency: three (supported range ^0.178.0) — install it alongside this package.

Two presentation thumbnails: a neutral gray STL part (materials: unavailable) beside a red/amber/green three-object 3MF (materials: known)

Use it for file-browser thumbnails, library cards, and "what's in this file" previews — the picture a slicer shows of a part, not the toolpath. For inspecting the actual moves (layers, travel, seams, color modes, live progress), use @chestnutlabs/gcode-renderer-three instead.

  • STL and 3MF — a bare STL is a single object with no declared material; a 3MF brings its own multi-object structure, per-object transforms, and per-object / per-triangle material colors, including files that use the 3MF Production Extension (p:path external parts).
  • Production multicolor — real Bambu Studio / OrcaSlicer files paint per-region color with a proprietary paint_color facet attribute and keep the palette in project_settings.config, not in standard 3MF materials. The renderer decodes that facet-paint format (clean-room, see RR-005) and reads the filament_colour palette itself, so a designer's multicolor model renders in its true colors without slicing.
  • Capability-honest color — when the source declares colors — standard 3MF basematerials, or paint_color + a filament palette — the render uses them and reports materials: 'known' ('approximated' when a handful of multi-color facets are flattened). When it declares none — a plain STL, or a paint format with no palette present — it draws a neutral default and reports materials: 'unavailable'. It never invents a source color.
  • Fixed presentation pose — framed at a 3/4 angle on the shared render "stage" from @chestnutlabs/gcode-renderer-three, under a neutral studio light rig, on a transparent (default) or solid background you can composite onto a card.
  • Headless still — renderModelStill mirrors the toolpath side's renderStill: hand it bytes, get back a canvas plus a stable cacheKey and the materials confidence for that render. Runs in any Chromium-class WebGL2 context (an OffscreenCanvas in a Worker, or headless Chromium).
  • Interactive viewer — createModelViewer is the live analogue of the still: orbit, zoom, and pan the same STL / 3MF (including production multicolor) in a browser <canvas>, with camera presets, a serializable camera state, and an event stream for readiness and errors.
  • Three-free public types — ModelScene / ModelObject / MeshGeometry are plain typed arrays, so the package's surface never leaks three; the renderer builds three meshes internally.
import { renderModelStill } from '@chestnutlabs/gcode-model-renderer';

// In a Worker with an OffscreenCanvas, or headless Chromium:
const { canvas, objectCount, materials, cacheKey } = await renderModelStill(
  { kind: '3mf', bytes },                        // or { kind: 'stl', bytes }
  { canvas: new OffscreenCanvas(512, 512), background: 'transparent' }
);
// materials === 'known'  → the render used colors the file declared
// materials === 'unavailable' → the file carried none; a neutral default was used (never faked)
const blob = await canvas.convertToBlob();

Already hold a corrected or richer filament palette (e.g. re-rendering a sliced file)? Pass filamentPalette (hex per 0-based slot) to renderModelStill / parse3mf to override the one read from project_settings.config. Optional — the renderer reads the file's own palette without it.

Interactive viewer

For a live surface a user can orbit — a "View in 3D" for a source model, as opposed to a static thumbnail — createModelViewer drives the same scene under the shared camera and orbit controls the toolpath renderer uses:

import { createModelViewer } from '@chestnutlabs/gcode-model-renderer';

const viewer = createModelViewer(canvas);           // a real <canvas> in the page
viewer.onEvent((e) => {
  if (e.type === 'ready') {
    // e.info.materials === 'known' | 'approximated' → showing the file's true colors
    // e.info.materials === 'unavailable'            → neutral render; don't claim "true colors"
    console.log(e.info.objectCount, e.info.materials, e.info.bounds);
  }
  if (e.type === 'renderer-unsupported') {
    // No WebGL — fall back to a renderModelStill image or a static thumbnail
  }
});

await viewer.setSource({ kind: '3mf', bytes });     // or { kind: 'stl', bytes }; parse → build → frame
viewer.setView('front');                            // 'iso' | 'top' | 'front' | 'back' | 'left' | 'right' | 'bottom'
// ...on unmount:
viewer.dispose();

Drag to orbit, scroll to zoom, right-drag to pan. getCameraState() / setCameraState() persist and restore a pose (the same serializable CameraState as the toolpath renderer), resize(w, h) matches a ResizeObserver, and setInteractionQuality('auto') trades detail for smoothness while orbiting. New source formats become viewable by registering a ModelLoader for a new kind, with no change to the viewer's API (design: DD-021).

The first frame and frame() (a "Reset view" button) fit the model's bounding box to the canvas: a wide, flat part fills a wide canvas and still fits a narrow 375 px panel, centered with a small margin. Until the user orbits, zooms, or pans, each resize(w, h) re-fits the model, so a sidebar collapse or a window resize never leaves it cropped. After the user moves the camera, resize keeps their view, and frame() hands framing back to the viewer. Pass autoFrameOnResize: false to keep the first framing across resizes. The camera-changed event fires only when a user gesture settles.

Prefer to build your own scene? parseStl / parse3mf return a three-free ModelScene, and both the still and the viewer accept a pre-built ModelScene directly.

Framework-neutral Prepare controller

createModelPreviewController wraps createModelViewer in the controller shape the framework adapters consume — it is the Prepare-side analogue of the toolpath core controller. On top of the raw viewer it adds a canvas-bindCanvas lifecycle (rebind survives a remount; the last source and any queued control ops replay on bind), a single reactive state snapshot (getState() / onStateChange()), and an op queue so calls made before the canvas exists are not lost. It backs the /model subpath of the Vue, React, Svelte, and Web Component packages; use it directly to build your own adapter.

import { createModelPreviewController } from '@chestnutlabs/gcode-model-renderer';

const controller = createModelPreviewController();
const off = controller.onStateChange((s) => {
  // s: ModelPreviewState — loading, ready, rendererSupported, objectCount, materials,
  //    instancedCount, decimationApplied, bounds, plates, hasPlates, cameraState, progress, error
});
controller.bindCanvas(canvas);                       // a real <canvas> in the page
await controller.controls.setSource({ kind: '3mf', bytes });
// ...on teardown:
off();
controller.dispose();

The two contract types are ModelPreviewControls (setSource / setView / getCameraState / setCameraState / setBackground / setInteractionQuality / setRenderScope / frame / resize / capture) and the ModelPreviewState snapshot above; materials in that snapshot is the same honest tier the still and viewer report ('known' / 'approximated' / 'unavailable', never invented).

A portable behavioral suite ships at the /testing subpath: runModelBehavioralSuite(name, api, harness) runs one shared set of assertions against any adapter, so every framework's <ModelViewer> is verified against the same contract as the core controller.

import { runModelBehavioralSuite } from '@chestnutlabs/gcode-model-renderer/testing';

See docs/manual/adapters.md "Two viewers: Preview and Prepare" for the cross-adapter tour of the four framework wrappers.

Determinism (stills): same input + same environment ⇒ identical output. Cross-GPU/driver pixel identity is not promised — cache by the returned cacheKey.

Part of Chestnut Labs G-code Preview · MIT

版本与发布记录

@chestnutlabs/gcode-model-renderer 共发布 18 个版本,首次发布于 2026/08/25;过去 12 个月发布了 18 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.21.1latest2026/11/045 天前
0.21.02026/10/2911 天前
0.20.12026/09/032 个月前
0.20.02026/08/312 个月前
0.19.02026/08/282 个月前
0.18.02026/08/282 个月前

关于 @chestnutlabs/gcode-model-renderer 的常见问题

@chestnutlabs/gcode-model-renderer 是什么?

Three.js presentation renderer for source models (STL/3MF) — clean thumbnails/cards, distinct from toolpath inspection (DD-018).

如何安装 @chestnutlabs/gcode-model-renderer?

在项目目录运行 npm install @chestnutlabs/gcode-model-renderer,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:three(^0.178.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@chestnutlabs/gcode-model-renderer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@chestnutlabs/gcode-model-renderer 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@chestnutlabs/gcode-model-renderer 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@chestnutlabs/gcode-model-renderer 可以商用吗?

@chestnutlabs/gcode-model-renderer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chestnutlabs/gcode-model-renderer 的下载量有多少?

根据 npm 官方下载统计,@chestnutlabs/gcode-model-renderer 在 2026/10/10 – 2026/11/08 这 30 天内被下载 2,144 次,日均约 71 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chestnutlabs/gcode-model-renderer 还在维护吗?

最新版本 0.21.1 发布于 2026/11/04(5 天前);至今共发布 18 个版本,其中过去 12 个月发布了 18 个;首次发布于 2026/08/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。