其他
使用 Three.js 创建…
在 Three.js 项目中需要创建 3D 形状、手写顶点数据、生成文本或路径几何体、批量实例化对象并优化渲染性能时使用。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 35/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 39/100 ×0.15
适用场景
使用 Three.js 创建、编辑与优化特定
解决思路
- 导入 Three.js:`import * as THREE from 'three'`。
- 用内置几何体快速创建形状:`new THREE.BoxGeometry(1,1,1)`、`new THREE.SphereGeometry(0.5,32,32)`、`new THREE.PlaneGeometry(10,10)`。
- 创建网格并加入场景:用 `new THREE.MeshStandardMaterial({ color: 0x00ff00 })` 创建材质,`new THREE.Mesh(box, material)` 创建网格,`scene.add(mesh)` 添加到场景。
- BoxGeometry 参数:width, height, depth, widthSegments, heightSegments, depthSegments;示例 `new THREE.BoxGeometry(1,1,1,1,1,1)`。
- SphereGeometry 参数:radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength;示例 `new THREE.SphereGeometry(1,32,32)`;通过 `phiLength`/`thetaLength` 可做完整球体或半球 `new THREE.SphereGeometry(1,32,32,0,Math.PI*2,0,Math.PI)`、`new THREE.SphereGeometry(1,32,32,0,Math.PI)`。
- PlaneGeometry 参数:width, height, widthSegments, heightSegments;示例 `new THREE.PlaneGeometry(10,10,1,1)`。
- CircleGeometry 参数:radius, segments, thetaStart, thetaLength;示例 `new THREE.CircleGeometry(1,32)`,用 `thetaLength=Math.PI` 做半圆 `new THREE.CircleGeometry(1,32,0,Math.PI)`。
- CylinderGeometry 参数:radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded;示例 `new THREE.CylinderGeometry(1,1,2,32,1,false)`;用 `radiusTop=0` 做圆锥 `new THREE.CylinderGeometry(0,1,2,32)`,用 `radialSegments=6` 做六棱柱 `new THREE.CylinderGeometry(1,1,2,6)`。
- ConeGeometry 参数:radius, height, radialSegments, heightSegments, openEnded;示例 `new THREE.ConeGeometry(1,2,32,1,false)`。
- TorusGeometry 参数:radius, tube, radialSegments, tubularSegments, arc;示例 `new THREE.TorusGeometry(1,0.4,16,100)`。
- TorusKnotGeometry 参数:radius, tube, tubularSegments, radialSegments, p, q;示例 `new THREE.TorusKnotGeometry(1,0.4,100,16,2,3)`。
- RingGeometry 参数:innerRadius, outerRadius, thetaSegments, phiSegments;示例 `new THREE.RingGeometry(0.5,1,32,1)`。
- CapsuleGeometry 参数:radius, length, capSegments, radialSegments;示例 `new THREE.CapsuleGeometry(0.5,1,4,8)`。
- DodecahedronGeometry 参数:radius, detail;示例 `new THREE.DodecahedronGeometry(1,0)`。
- IcosahedronGeometry 参数:radius, detail,detail=0 为 20 个面,越大越平滑;示例 `new THREE.IcosahedronGeometry(1,0)`。
- OctahedronGeometry 参数:radius, detail;示例 `new THREE.OctahedronGeometry(1,0)`。
- TetrahedronGeometry 参数:radius, detail;示例 `new THREE.TetrahedronGeometry(1,0)`。
- PolyhedronGeometry 参数:vertices, indices, radius, detail;用顶点数组 `[1,1,1,-1,-1,1,-1,1,-1,1,-1,-1]` 与索引数组 `[2,1,0,0,3,2,1,3,0,2,3,1]` 创建 `new THREE.PolyhedronGeometry(vertices,indices,1,0)`。
- LatheGeometry:用 `THREE.Vector2` 点数组定义轮廓,参数 points, segments, phiStart, phiLength;示例 `new THREE.LatheGeometry(points,32)`。
- ExtrudeGeometry:用 `THREE.Shape` 的 `moveTo`/`lineTo` 定义形状,设置 `extrudeSettings` 的 steps、depth、bevelEnabled、bevelThickness、bevelSize、bevelSegments;示例 `new THREE.ExtrudeGeometry(shape, extrudeSettings)`。
- TubeGeometry:用 `THREE.CatmullRomCurve3` 定义路径,参数 path, tubularSegments, radius, radialSegments, closed;示例 `new THREE.TubeGeometry(curve,64,0.2,8,false)`。
- TextGeometry:从 `three/examples/jsm/loaders/FontLoader.js` 导入 `FontLoader`,从 `three/examples/jsm/geometries/TextGeometry.js` 导入 `TextGeometry`;`loader.load('fonts/helvetiker_regular.typeface.json', font => ...)` 加载字体。
- TextGeometry 选项:font, size, depth(旧版本为 height)、curveSegments、bevelEnabled、bevelThickness、bevelSize、bevelSegments;创建后调用 `geometry.computeBoundingBox()` 和 `geometry.center()` 居中文本。
- BufferGeometry 是所有几何体的基类,用 typed array 存储数据以提高 GPU 效率。
- 自定义 BufferGeometry:`const geometry = new THREE.BufferGeometry()`;用 `Float32Array` 存顶点,`geometry.setAttribute('position', new THREE.BufferAttribute(vertices,3))` 设置 position。
- 为自定义 BufferGeometry 设置索引:用 `Uint16Array` 存三角形索引,`geometry.setIndex(new THREE.BufferAttribute(indices,1))`,以便复用顶点。
- 为光照设置法线:`geometry.setAttribute('normal', new THREE.BufferAttribute(normals,3))`,每个顶点 3 个 float。
- 为纹理设置 UV:`geometry.setAttribute('uv', new THREE.BufferAttribute(uvs,2))`,每个顶点 2 个 float。
- 设置逐顶点颜色:用 `Float32Array` 存 r,g,b,`geometry.setAttribute('color', new THREE.BufferAttribute(colors,3))`,并设置 `material.vertexColors = true`。
- BufferAttribute 常见类型:`Float32Array` 用于 positions/normals/UVs;`Uint16Array` 用于最多 65535 顶点的索引;`Uint32Array` 用于更大网格的索引;`Uint8Array` 用于 0-255 范围的颜色。
- BufferAttribute 的 itemSize:Position 为 3(x,y,z),Normal 为 3,UV 为 2,Color 为 3 或 4,Index 为 1;用 `new THREE.BufferAttribute(array, itemSize)` 创建。
- 修改 BufferGeometry 顶点:用 `positions.setXYZ(index,x,y,z)` 修改,用 `positions.getX(index)`、`getY`、`getZ` 读取。
- 修改顶点后设置 `positions.needsUpdate = true` 通知 GPU 更新;位置变化后调用 `geometry.computeVertexNormals()` 重算法线。
- 修改几何体后重新计算包围盒/包围球:`geometry.computeBoundingBox()`、`geometry.computeBoundingSphere()`。
- 交错缓冲区:用 `new THREE.InterleavedBuffer(new Float32Array([...]), 5)`,stride 为每个顶点 float 数;用 `new THREE.InterleavedBufferAttribute(interleavedBuffer, 3, 0)` 设置 position,用 size 2、offset 3 设置 uv。
- EdgesGeometry 显示硬边:`new THREE.EdgesGeometry(boxGeometry, 15)`,其中 15 为阈值角度;用 `LineSegments` 与 `LineBasicMaterial` 渲染。
- WireframeGeometry 显示所有三角形线框:`new THREE.WireframeGeometry(boxGeometry)`,用 `LineSegments` 与 `LineBasicMaterial` 渲染。
- 创建点云:用 `BufferGeometry` 和 `Float32Array` 填充 1000 个随机位置,`geometry.setAttribute('position', new THREE.BufferAttribute(positions,3))`。
- 点云材质:`new THREE.PointsMaterial({ size: 0.1, sizeAttenuation: true, color: 0xffffff })`;用 `new THREE.Points(geometry, material)` 并 `scene.add(points)`。
- 创建 Line:用 `BufferGeometry().setFromPoints(points)` 和 `LineBasicMaterial`,再用 `new THREE.Line(geometry, material)` 连接点。
- 创建 LineLoop:用同一个 geometry 和 material 创建 `new THREE.LineLoop(geometry, material)` 形成闭合环。
- 创建 LineSegments:用 `BufferAttribute` 设置成对顶点的 position 数组,再 `new THREE.LineSegments(segmentsGeometry, material)`。
- InstancedMesh 高效渲染大量相同几何体:`new THREE.InstancedMesh(geometry, material, count)`。
- 为 InstancedMesh 设置每个实例变换:用 `THREE.Object3D` 的 `dummy` 设置 position、rotation、scale,调用 `dummy.updateMatrix()`,再 `instancedMesh.setMatrixAt(i, dummy.matrix)`。
- 设置实例矩阵后调用 `instancedMesh.instanceMatrix.needsUpdate = true`,并将 `instancedMesh` 加入场景。
- 为 InstancedMesh 设置逐实例颜色:创建 `instancedMesh.instanceColor = new THREE.InstancedBufferAttribute(new Float32Array(count*3),3)`,循环 `instancedMesh.setColorAt(i, new THREE.Color(...))`,最后 `instancedMesh.instanceColor.needsUpdate = true`。
- 运行时更新单个实例:`instancedMesh.getMatrixAt(index, matrix)` 读取矩阵,修改后 `instancedMesh.setMatrixAt(index, matrix)`,再 `instancedMesh.instanceMatrix.needsUpdate = true`。
- 对 InstancedMesh 做射线检测:`raycaster.intersectObject(instancedMesh)`,从 `intersects[0].instanceId` 获取实例 ID。
- InstancedBufferGeometry 用于自定义逐实例属性:`new THREE.InstancedBufferGeometry()`,先 `geometry.copy(new THREE.BoxGeometry(1,1,1))`,再用 `geometry.setAttribute('offset', new THREE.InstancedBufferAttribute(offsets,3))` 添加 offset。
- 在着色器中使用逐实例属性:声明 `attribute vec3 offset;`,计算 `vec3 transformed = position + offset;`。
- 合并几何体:用 `BufferGeometryUtils.mergeGeometries([geo1,geo2,geo3])` 合并相同属性的几何体;传入 `true` 可合并并保留 groups 以支持多材质。
- 为法线贴图计算切线:`BufferGeometryUtils.computeTangents(geometry)`。
- 交错属性提升性能:`BufferGeometryUtils.interleaveAttributes([geometry.attributes.position, geometry.attributes.normal, geometry.attributes.uv])`。
- 居中几何体:`geometry.computeBoundingBox(); geometry.center();` 将顶点移到原点中心。
- 缩放适配尺寸:`geometry.computeBoundingBox(); geometry.boundingBox.getSize(size); const maxDim = Math.max(size.x,size.y,size.z); geometry.scale(1/maxDim,1/maxDim,1/maxDim);`。
- 克隆并变换几何体:`const clone = geometry.clone(); clone.rotateX(Math.PI/2); clone.translate(0,1,0); clone.scale(2,2,2);`。
- Morph Targets:以 `new THREE.BoxGeometry(1,1,1,4,4,4)` 为基础,复制 `geometry.attributes.position.array` 并修改,设置 `geometry.morphAttributes.position = [new THREE.BufferAttribute(new Float32Array(morphPositions),3)]`;用 `mesh.morphTargetInfluences[0] = 0.5` 混合 50%。
- 性能:优先使用 indexed geometry,通过索引复用顶点。
- 性能:合并静态网格以减少 draw calls,使用 `mergeGeometries`。
- 性能:对大量相同对象使用 `InstancedMesh`。
- 性能:选择合适的分段数,更多分段更平滑但更慢;常用球体分段:`new THREE.SphereGeometry(1,32,32)` 质量好,`new THREE.SphereGeometry(1,64,64)` 高质量,`new THREE.SphereGeometry(1,16,16)` 性能模式。
- 性能:不再使用时调用 `geometry.dispose()` 释放几何体。
- 需要场景设置与 Object3D 时参考 `threejs-fundamentals`;需要材质类型时参考 `threejs-materials`;需要自定义顶点操作时参考 `threejs-shaders`。
适用边界 · 注意事项
- 不使用 Three.js 或 WebGL 渲染时,例如纯 2D Canvas、SVG、CSS 3D 或服务端纯几何计算。
- 需要复杂 CAD 级布尔运算、参数化建模或高精度 DCC 工作流时,应使用专业建模软件或几何库,而不是手写 Three.js BufferGeometry。
- 模型已由外部工具制作并导出为 glTF/OBJ 等格式时,优先加载现有模型,而不是在代码中重建几何体。