You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Three.js新手求助:如何简单创建楔形几何体?

用BoxGeometry实现楔形几何体的简便方案

当然可以用BoxGeometry实现这种楔形,完全没必要用PolyhedronGeometry绕弯路,给你几个新手友好的实用方案:

方案一:直接修改Box顶点(最快捷)

创建标准立方体后,调整部分顶点的坐标,把其中一侧压成斜面即可,适合快速生成规则楔形:

// 创建基础立方体,参数对应宽、高、深,按需调整
const boxGeo = new THREE.BoxGeometry(2, 1, 1);
// 获取顶点位置数组(r125+版本推荐用attributes.position操作)
const positions = boxGeo.attributes.position.array;

// 修改顶部的两个前侧顶点y坐标,形成斜面(索引对应可根据需求调整)
positions[1] = 0; // 顶点(0,1,0)的y值改为0
positions[7] = 0; // 顶点(2,1,0)的y值改为0

// 通知渲染器顶点数据已更新
boxGeo.attributes.position.needsUpdate = true;
// 重新计算法向量,确保光照显示正常
boxGeo.computeVertexNormals();

// 创建网格并添加到场景
const wedgeMesh = new THREE.Mesh(boxGeo, new THREE.MeshNormalMaterial());
scene.add(wedgeMesh);

方案二:用基础几何体拼接(最直观)

把楔形拆解成几个平面几何体(底面、侧面、斜面、端面),组合成一个Group,适合新手理解3D结构:

const wedgeGroup = new THREE.Group();

// 底面(PlaneGeometry参数:宽、高)
const bottomPlane = new THREE.Mesh(new THREE.PlaneGeometry(2, 1), new THREE.MeshNormalMaterial());
bottomPlane.rotation.x = -Math.PI / 2;
bottomPlane.position.y = -0.5;
wedgeGroup.add(bottomPlane);

// 后侧垂直面
const backPlane = new THREE.Mesh(new THREE.PlaneGeometry(2, 1), new THREE.MeshNormalMaterial());
backPlane.position.z = 0.5;
wedgeGroup.add(backPlane);

// 斜面(用斜边长度作为宽,这里斜边是√2≈1.414)
const slopePlane = new THREE.Mesh(new THREE.PlaneGeometry(Math.sqrt(2), 1), new THREE.MeshNormalMaterial());
slopePlane.rotation.y = -Math.PI / 4;
slopePlane.position.set(0, 0.25, -0.25);
wedgeGroup.add(slopePlane);

// 左侧三角形端面
const leftEnd = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), new THREE.MeshNormalMaterial());
leftEnd.rotation.z = Math.PI / 4;
leftEnd.position.set(-1, 0.25, 0);
wedgeGroup.add(leftEnd);

// 右侧三角形端面
const rightEnd = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), new THREE.MeshNormalMaterial());
rightEnd.rotation.z = -Math.PI / 4;
rightEnd.position.set(1, 0.25, 0);
wedgeGroup.add(rightEnd);

scene.add(wedgeGroup);

方案三:ExtrudeGeometry拉伸2D路径(最灵活)

先画一个对应楔形截面的2D图形(三角形/梯形),再拉伸成3D几何体,能精准控制楔形的角度和尺寸:

// 创建2D截面路径:这里是直角三角形,按需改成梯形即可
const wedgeShape = new THREE.Shape();
wedgeShape.moveTo(0, 0);
wedgeShape.lineTo(2, 0);
wedgeShape.lineTo(2, 1);
wedgeShape.lineTo(0, 0);

// 拉伸参数:depth是楔形的深度,关闭倒角
const extrudeSettings = {
  depth: 1,
  bevelEnabled: false
};

const wedgeGeo = new THREE.ExtrudeGeometry(wedgeShape, extrudeSettings);
const wedgeMesh = new THREE.Mesh(wedgeGeo, new THREE.MeshNormalMaterial());
scene.add(wedgeMesh);

优先推荐方案一或方案三,代码量少且易调试,完全能替代复杂的PolyhedronGeometry实现需求。

内容的提问来源于stack exchange,提问作者MoLt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 09:45:33