Skip to content

Commit ca874dd

Browse files
committed
fix: stl export normalization 2
1 parent f74c65d commit ca874dd

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/lib/3d/mesh.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BoxGeometry, CylinderGeometry, Vector3 } from 'three';
22
import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
33
import { Font, FontLoader } from 'three/addons/loaders/FontLoader.js';
4+
import { mergeVertices } from 'three/addons/utils/BufferGeometryUtils.js';
45
import { ADDITION, Brush, Evaluator, SUBTRACTION } from 'three-bvh-csg';
56

67
import { MathMinMax } from '$lib/Math';
@@ -506,25 +507,32 @@ const generateMesh = async (
506507
await reportProgress(); // Circle added to positive mesh
507508
}
508509

510+
// Fix non-manifold edges: snap near-duplicate CSG seam vertices to identical values.
511+
// mergeVertices: non-indexed → indexed (deduplicates vertices within epsilon)
512+
// toNonIndexed: indexed → non-indexed (expands back, but shared edge vertices are now exact)
513+
const gMain = mergeVertices(mesh.geometry).toNonIndexed();
514+
const gHollow = mergeVertices(meshHollow.geometry).toNonIndexed();
515+
const gPositive = mergeVertices(meshPositive.geometry).toNonIndexed();
516+
509517
return {
510518
main: {
511-
vertexArray: new Float32Array(mesh.geometry.attributes['position'].array),
519+
vertexArray: new Float32Array(gMain.attributes['position'].array),
512520
dimensions: {
513521
width: adjustedPanelWidth + 2 * EDGE_THICKNESS,
514522
height: adjustedPanelHeight + 2 * EDGE_THICKNESS,
515523
depth: needHeight + BOTTOM_THICKNESS
516524
}
517525
},
518526
hollow: {
519-
vertexArray: new Float32Array(meshHollow.geometry.attributes['position'].array),
527+
vertexArray: new Float32Array(gHollow.attributes['position'].array),
520528
dimensions: {
521529
width: adjustedPanelWidth + 2 * EDGE_THICKNESS,
522530
height: adjustedPanelHeight + 2 * EDGE_THICKNESS,
523531
depth: hollowHeight + BOTTOM_THICKNESS
524532
}
525533
},
526534
positive: {
527-
vertexArray: new Float32Array(meshPositive.geometry.attributes['position'].array),
535+
vertexArray: new Float32Array(gPositive.attributes['position'].array),
528536
dimensions: {
529537
width: panel.width,
530538
height: panel.height,

0 commit comments

Comments
 (0)