计算孔洞间距
This commit is contained in:
20
index.html
20
index.html
@@ -13,8 +13,8 @@
|
|||||||
</style>
|
</style>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import * as THREE from 'three';
|
import * as THREE from 'three';
|
||||||
import { CSGHexNutGeometry } from './objects/育苗盘.js';
|
|
||||||
import { 六角螺丝帽 } from './objects/螺丝帽.js';
|
import { 六角螺丝帽 } from './objects/螺丝帽.js';
|
||||||
|
import { 方形育苗盘 } from './objects/育苗盘.js';
|
||||||
|
|
||||||
// 1. 初始化场景、相机和渲染器
|
// 1. 初始化场景、相机和渲染器
|
||||||
const scene = new THREE.Scene();
|
const scene = new THREE.Scene();
|
||||||
@@ -30,10 +30,6 @@
|
|||||||
//);
|
//);
|
||||||
//scene.add(cube);
|
//scene.add(cube);
|
||||||
|
|
||||||
//// 添加育苗盘
|
|
||||||
//const 育苗盘模型 = 育苗盘();
|
|
||||||
//scene.add(育苗盘模型);
|
|
||||||
|
|
||||||
// 添加螺丝帽
|
// 添加螺丝帽
|
||||||
const 螺丝帽模型 = new THREE.Mesh(
|
const 螺丝帽模型 = new THREE.Mesh(
|
||||||
new 六角螺丝帽(1, 0.5, 0.6),
|
new 六角螺丝帽(1, 0.5, 0.6),
|
||||||
@@ -41,6 +37,14 @@
|
|||||||
);
|
);
|
||||||
scene.add(螺丝帽模型);
|
scene.add(螺丝帽模型);
|
||||||
|
|
||||||
|
// 添加育苗盘
|
||||||
|
const 育苗盘模型 = new THREE.Mesh(
|
||||||
|
new 方形育苗盘(),
|
||||||
|
new THREE.MeshStandardMaterial({ color: 0x0000ff, roughness: 0.5, metalness: 0.8 })
|
||||||
|
);
|
||||||
|
//育苗盘模型.position.set(0, -1, 0); // 设置育苗盘位置
|
||||||
|
scene.add(育苗盘模型);
|
||||||
|
|
||||||
// 添加光源
|
// 添加光源
|
||||||
scene.add(new THREE.AmbientLight(0x404040, 1)); // 环境光,柔和的白光
|
scene.add(new THREE.AmbientLight(0x404040, 1)); // 环境光,柔和的白光
|
||||||
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); // 平行光
|
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); // 平行光
|
||||||
@@ -55,9 +59,9 @@
|
|||||||
//cube.rotation.x += 0.01;
|
//cube.rotation.x += 0.01;
|
||||||
//cube.rotation.y += 0.01;
|
//cube.rotation.y += 0.01;
|
||||||
|
|
||||||
//// 旋转育苗盘
|
// 旋转育苗盘
|
||||||
//育苗盘模型.rotation.x += 0.01;
|
育苗盘模型.rotation.x += 0.01;
|
||||||
//育苗盘模型.rotation.y += 0.01;
|
育苗盘模型.rotation.y += 0.01;
|
||||||
|
|
||||||
// 旋转螺丝帽
|
// 旋转螺丝帽
|
||||||
螺丝帽模型.rotation.x += 0.01;
|
螺丝帽模型.rotation.x += 0.01;
|
||||||
|
@@ -1,47 +1,59 @@
|
|||||||
import * as THREE from 'three';
|
import * as THREE from 'three';
|
||||||
import { SUBTRACTION, Brush, Evaluator } from 'three-bvh-csg';
|
import { SUBTRACTION, ADDITION, Brush, Evaluator } from 'three-bvh-csg';
|
||||||
|
|
||||||
class CSGHexNutGeometry extends THREE.BufferGeometry {
|
export class 方形育苗盘 extends THREE.BufferGeometry {
|
||||||
constructor(radius = 1, height = 0.5, holeRadius = 0.6) {
|
constructor(width = 10, depth = 15, height = 0.25, holeDiameter = 0.25, holeRadius = 1, holeRows = 4, holeCols = 4) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.parameters = {
|
this.parameters = {
|
||||||
radius: radius,
|
width: width,
|
||||||
|
depth: depth,
|
||||||
height: height,
|
height: height,
|
||||||
holeRadius: holeRadius
|
holeDiameter: holeDiameter,
|
||||||
|
holeRadius: holeRadius,
|
||||||
|
holeRows: holeRows,
|
||||||
|
holeCols: holeCols
|
||||||
};
|
};
|
||||||
|
|
||||||
this.buildGeometry();
|
this.buildGeometry();
|
||||||
}
|
}
|
||||||
|
|
||||||
buildGeometry() {
|
buildGeometry() {
|
||||||
const radius = this.parameters.radius;
|
const { width, depth, height, holeDiameter, holeRows, holeCols, } = this.parameters;
|
||||||
const height = this.parameters.height;
|
|
||||||
const holeRadius = this.parameters.holeRadius;
|
|
||||||
|
|
||||||
// 创建螺丝帽主体 (六棱柱)
|
// 创建长方体主体
|
||||||
const hexagonGeometry = new THREE.CylinderGeometry(radius, radius, height, 6, 1, false);
|
const baseBrush = new Brush(new THREE.BoxGeometry(width, height, depth));
|
||||||
const hexagonMesh = new THREE.Mesh(hexagonGeometry); // 注意这里没有材质
|
|
||||||
const hexagonBrush = new Brush(hexagonMesh.geometry); // 使用 Brush 构造函数
|
|
||||||
|
|
||||||
// 创建内部的孔洞 (圆柱体)
|
// 计算孔洞间距, (主体尺寸- 孔洞数量 * 孔洞直径) / (孔洞数量 + 1)
|
||||||
const holeHeight = height + 0.1;
|
const rowSpacing = (width - holeRows * holeDiameter) / (holeRows + 1); // 行间距
|
||||||
const holeGeometry = new THREE.CylinderGeometry(holeRadius, holeRadius, holeHeight, 32);
|
const colSpacing = (depth - holeCols * holeDiameter) / (holeCols + 1); // 列间距
|
||||||
const holeMesh = new THREE.Mesh(holeGeometry); // 注意这里没有材质
|
const holes = []
|
||||||
const holeBrush = new Brush(holeMesh.geometry); // 使用 Brush 构造函数
|
for (let i = 1; i <= holeRows; i++) {
|
||||||
|
for (let j = 1; 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 evaluator = new Evaluator();
|
const resultBrush = new Evaluator().evaluate(baseBrush, combinedHoles, SUBTRACTION);
|
||||||
const resultBrush = evaluator.evaluate(hexagonBrush, holeBrush, SUBTRACTION);
|
|
||||||
|
|
||||||
// 将布尔运算结果转换为 BufferGeometry
|
// 将布尔运算结果应用到当前 BufferGeometry
|
||||||
const finalGeometry = resultBrush.geometry;
|
this.copy(resultBrush.geometry);
|
||||||
|
|
||||||
// 将结果的属性复制到当前的 BufferGeometry 实例
|
|
||||||
this.setAttribute('position', finalGeometry.getAttribute('position'));
|
|
||||||
this.setIndex(finalGeometry.getIndex());
|
|
||||||
this.computeVertexNormals(); // 计算法线
|
this.computeVertexNormals(); // 计算法线
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { CSGHexNutGeometry };
|
export class 蜂窝育苗盘 extends THREE.BufferGeometry { }
|
||||||
|
|
||||||
|
export default { 方形育苗盘, 蜂窝育苗盘 };
|
Reference in New Issue
Block a user