47 lines
1.8 KiB
JavaScript
47 lines
1.8 KiB
JavaScript
import * as THREE from 'three';
|
|
import { SUBTRACTION, Brush, Evaluator } from 'three-bvh-csg';
|
|
|
|
class CSGHexNutGeometry extends THREE.BufferGeometry {
|
|
constructor(radius = 1, height = 0.5, holeRadius = 0.6) {
|
|
super();
|
|
|
|
this.parameters = {
|
|
radius: radius,
|
|
height: height,
|
|
holeRadius: holeRadius
|
|
};
|
|
|
|
this.buildGeometry();
|
|
}
|
|
|
|
buildGeometry() {
|
|
const radius = this.parameters.radius;
|
|
const height = this.parameters.height;
|
|
const holeRadius = this.parameters.holeRadius;
|
|
|
|
// 创建螺丝帽主体 (六棱柱)
|
|
const hexagonGeometry = new THREE.CylinderGeometry(radius, radius, height, 6, 1, false);
|
|
const hexagonMesh = new THREE.Mesh(hexagonGeometry); // 注意这里没有材质
|
|
const hexagonBrush = new Brush(hexagonMesh.geometry); // 使用 Brush 构造函数
|
|
|
|
// 创建内部的孔洞 (圆柱体)
|
|
const holeHeight = height + 0.1;
|
|
const holeGeometry = new THREE.CylinderGeometry(holeRadius, holeRadius, holeHeight, 32);
|
|
const holeMesh = new THREE.Mesh(holeGeometry); // 注意这里没有材质
|
|
const holeBrush = new Brush(holeMesh.geometry); // 使用 Brush 构造函数
|
|
|
|
// 执行布尔运算 (差集)
|
|
const evaluator = new Evaluator();
|
|
const resultBrush = evaluator.evaluate(hexagonBrush, holeBrush, SUBTRACTION);
|
|
|
|
// 将布尔运算结果转换为 BufferGeometry
|
|
const finalGeometry = resultBrush.geometry;
|
|
|
|
// 将结果的属性复制到当前的 BufferGeometry 实例
|
|
this.setAttribute('position', finalGeometry.getAttribute('position'));
|
|
this.setIndex(finalGeometry.getIndex());
|
|
this.computeVertexNormals(); // 计算法线
|
|
}
|
|
}
|
|
|
|
export { CSGHexNutGeometry }; |