Files
farm/objects/育苗盘.js
2025-05-06 22:59:15 +08:00

59 lines
2.2 KiB
JavaScript

import * as THREE from 'three';
import { SUBTRACTION, ADDITION, Brush, Evaluator } from 'three-bvh-csg';
export class 方形育苗盘 extends THREE.BufferGeometry {
constructor(width = 0.6, depth = 0.4, height = 0.01, holeDiameter = 0.02, holeRadius = .1, holeRows = 20, holeCols = 12, rowSpacing = .01, colSpacing = .01) {
super();
this.parameters = {
width: width,
depth: depth,
height: height,
holeDiameter: holeDiameter,
holeRadius: holeRadius,
holeRows: holeRows,
holeCols: holeCols,
rowSpacing: rowSpacing,
colSpacing: colSpacing
};
this.buildGeometry();
}
buildGeometry() {
const { width, depth, height, holeDiameter, holeRows, holeCols, rowSpacing, colSpacing } = this.parameters;
// 创建长方体主体
const baseBrush = new Brush(new THREE.BoxGeometry(width, height, depth));
const holes = [];
for (let i = 0; i < holeRows; i++) {
for (let j = 0; j < holeCols; j++) {
// 计算每个孔洞的中心位置
const x = -(width / 2) + rowSpacing * (i + 1) + holeDiameter * (i + 0.5);
const z = -(depth / 2) + colSpacing * (j + 1) + holeDiameter * (j + 0.5);
const holeGeometry = new THREE.BoxGeometry(holeDiameter, height, holeDiameter);
const holeBrush = new Brush(holeGeometry);
holeBrush.geometry.translate(x, 0, z);
holes.push(holeBrush);
}
}
// 计算所有孔洞的合并结果
let combinedHoles = holes[0];
for (let i = 1; i < holes.length; i++) {
combinedHoles = new Evaluator().evaluate(combinedHoles, holes[i], ADDITION);
}
// 执行布尔运算 (差集)
const resultBrush = new Evaluator().evaluate(baseBrush, combinedHoles, SUBTRACTION);
// 将布尔运算结果应用到当前 BufferGeometry
this.copy(resultBrush.geometry);
this.computeVertexNormals(); // 计算法线
}
}
export class 蜂窝育苗盘 extends THREE.BufferGeometry { }
export default { 方形育苗盘, 蜂窝育苗盘 };