refactor: move centering to 3mf gen
This commit is contained in:
parent
c24c3b920d
commit
b099d5695d
|
|
@ -51,14 +51,6 @@ export default function MdTokenViewer(props: TokenViewerProps) {
|
||||||
|
|
||||||
// 3MF 文件可能返回一个 Group,包含多个 Mesh
|
// 3MF 文件可能返回一个 Group,包含多个 Mesh
|
||||||
group = object instanceof THREE.Group ? object : new THREE.Group().add(object);
|
group = object instanceof THREE.Group ? object : new THREE.Group().add(object);
|
||||||
|
|
||||||
// 计算边界框并居中
|
|
||||||
const box = new THREE.Box3().setFromObject(group);
|
|
||||||
const center = box.getCenter(new THREE.Vector3());
|
|
||||||
group.position.x = -center.x;
|
|
||||||
group.position.y = -center.y;
|
|
||||||
group.position.z = -center.z;
|
|
||||||
|
|
||||||
group.rotateX(Math.PI);
|
group.rotateX(Math.PI);
|
||||||
|
|
||||||
// 为每个 mesh 启用原始颜色
|
// 为每个 mesh 启用原始颜色
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import * as THREE from "three";
|
import * as THREE from "three";
|
||||||
import { exportTo3MF } from "three-3mf-exporter";
|
import { exportTo3MF } from "three-3mf-exporter";
|
||||||
import type { TraceResult } from "./image-tracer";
|
import type { TraceResult } from "./image-tracer";
|
||||||
|
import {Vector3} from "three";
|
||||||
|
|
||||||
export interface ExtrusionSettings {
|
export interface ExtrusionSettings {
|
||||||
size: number; // 模型整体尺寸 (mm)
|
size: number; // 模型整体尺寸 (mm)
|
||||||
|
|
@ -53,6 +54,7 @@ export async function generate3MF(
|
||||||
const meshes: LayerMesh[] = [];
|
const meshes: LayerMesh[] = [];
|
||||||
let layerIndex = 0;
|
let layerIndex = 0;
|
||||||
|
|
||||||
|
let boundingBox = new THREE.Box3();
|
||||||
for (const layerSetting of settings.layers) {
|
for (const layerSetting of settings.layers) {
|
||||||
const layer = traceResult.layers.find((l) => l.id === layerSetting.id);
|
const layer = traceResult.layers.find((l) => l.id === layerSetting.id);
|
||||||
if (!layer) continue;
|
if (!layer) continue;
|
||||||
|
|
@ -61,9 +63,11 @@ export async function generate3MF(
|
||||||
const extrudeSettings: THREE.ExtrudeGeometryOptions = {
|
const extrudeSettings: THREE.ExtrudeGeometryOptions = {
|
||||||
depth: layerSetting.thickness,
|
depth: layerSetting.thickness,
|
||||||
curveSegments: 36,
|
curveSegments: 36,
|
||||||
bevelEnabled: false,
|
bevelEnabled: true,
|
||||||
|
bevelSize: 0.4
|
||||||
};
|
};
|
||||||
const geometry: THREE.ExtrudeGeometry = new THREE.ExtrudeGeometry(layer.paths, extrudeSettings);
|
const geometry: THREE.ExtrudeGeometry = new THREE.ExtrudeGeometry(layer.paths, extrudeSettings);
|
||||||
|
geometry.computeBoundingBox();
|
||||||
|
|
||||||
// 为该图层生成颜色
|
// 为该图层生成颜色
|
||||||
const color = generateLayerColor(layerIndex);
|
const color = generateLayerColor(layerIndex);
|
||||||
|
|
@ -74,6 +78,7 @@ export async function generate3MF(
|
||||||
});
|
});
|
||||||
|
|
||||||
const mesh = new THREE.Mesh(geometry, material);
|
const mesh = new THREE.Mesh(geometry, material);
|
||||||
|
boundingBox.expandByObject(mesh);
|
||||||
|
|
||||||
// 设置图层高度(堆叠)
|
// 设置图层高度(堆叠)
|
||||||
mesh.position.y = currentHeight;
|
mesh.position.y = currentHeight;
|
||||||
|
|
@ -90,6 +95,11 @@ export async function generate3MF(
|
||||||
layerIndex++;
|
layerIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const center = boundingBox.getCenter(new Vector3());
|
||||||
|
for(const mesh of meshes){
|
||||||
|
mesh.mesh.position.sub(center);
|
||||||
|
}
|
||||||
|
|
||||||
if (meshes.length === 0) {
|
if (meshes.length === 0) {
|
||||||
throw new Error("没有可生成的图层");
|
throw new Error("没有可生成的图层");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue