refactor: move centering to 3mf gen
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import * as THREE from "three";
|
||||
import { exportTo3MF } from "three-3mf-exporter";
|
||||
import type { TraceResult } from "./image-tracer";
|
||||
import {Vector3} from "three";
|
||||
|
||||
export interface ExtrusionSettings {
|
||||
size: number; // 模型整体尺寸 (mm)
|
||||
@@ -53,6 +54,7 @@ export async function generate3MF(
|
||||
const meshes: LayerMesh[] = [];
|
||||
let layerIndex = 0;
|
||||
|
||||
let boundingBox = new THREE.Box3();
|
||||
for (const layerSetting of settings.layers) {
|
||||
const layer = traceResult.layers.find((l) => l.id === layerSetting.id);
|
||||
if (!layer) continue;
|
||||
@@ -61,9 +63,11 @@ export async function generate3MF(
|
||||
const extrudeSettings: THREE.ExtrudeGeometryOptions = {
|
||||
depth: layerSetting.thickness,
|
||||
curveSegments: 36,
|
||||
bevelEnabled: false,
|
||||
bevelEnabled: true,
|
||||
bevelSize: 0.4
|
||||
};
|
||||
const geometry: THREE.ExtrudeGeometry = new THREE.ExtrudeGeometry(layer.paths, extrudeSettings);
|
||||
geometry.computeBoundingBox();
|
||||
|
||||
// 为该图层生成颜色
|
||||
const color = generateLayerColor(layerIndex);
|
||||
@@ -74,6 +78,7 @@ export async function generate3MF(
|
||||
});
|
||||
|
||||
const mesh = new THREE.Mesh(geometry, material);
|
||||
boundingBox.expandByObject(mesh);
|
||||
|
||||
// 设置图层高度(堆叠)
|
||||
mesh.position.y = currentHeight;
|
||||
@@ -90,6 +95,11 @@ export async function generate3MF(
|
||||
layerIndex++;
|
||||
}
|
||||
|
||||
const center = boundingBox.getCenter(new Vector3());
|
||||
for(const mesh of meshes){
|
||||
mesh.mesh.position.sub(center);
|
||||
}
|
||||
|
||||
if (meshes.length === 0) {
|
||||
throw new Error("没有可生成的图层");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user