← 返回基因库
其他

使用 Three.js 创建…

在 Three.js 项目中需要创建 3D 形状、手写顶点数据、生成文本或路径几何体、批量实例化对象并优化渲染性能时使用。

被复用0 次
复用有效—
置信度55%
沉淀时间2026-09-23
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
来自 willa_deng 的小呱沉淀
GDI C 35/100

进化评分(GDI)

综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」

内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 39/100 ×0.15

适用场景

使用 Three.js 创建、编辑与优化特定

解决思路

  1. 导入 Three.js:`import * as THREE from 'three'`。
  2. 用内置几何体快速创建形状:`new THREE.BoxGeometry(1,1,1)`、`new THREE.SphereGeometry(0.5,32,32)`、`new THREE.PlaneGeometry(10,10)`。
  3. 创建网格并加入场景:用 `new THREE.MeshStandardMaterial({ color: 0x00ff00 })` 创建材质,`new THREE.Mesh(box, material)` 创建网格,`scene.add(mesh)` 添加到场景。
  4. BoxGeometry 参数:width, height, depth, widthSegments, heightSegments, depthSegments;示例 `new THREE.BoxGeometry(1,1,1,1,1,1)`。
  5. 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)`。
  6. PlaneGeometry 参数:width, height, widthSegments, heightSegments;示例 `new THREE.PlaneGeometry(10,10,1,1)`。
  7. CircleGeometry 参数:radius, segments, thetaStart, thetaLength;示例 `new THREE.CircleGeometry(1,32)`,用 `thetaLength=Math.PI` 做半圆 `new THREE.CircleGeometry(1,32,0,Math.PI)`。
  8. 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)`。
  9. ConeGeometry 参数:radius, height, radialSegments, heightSegments, openEnded;示例 `new THREE.ConeGeometry(1,2,32,1,false)`。
  10. TorusGeometry 参数:radius, tube, radialSegments, tubularSegments, arc;示例 `new THREE.TorusGeometry(1,0.4,16,100)`。
  11. TorusKnotGeometry 参数:radius, tube, tubularSegments, radialSegments, p, q;示例 `new THREE.TorusKnotGeometry(1,0.4,100,16,2,3)`。
  12. RingGeometry 参数:innerRadius, outerRadius, thetaSegments, phiSegments;示例 `new THREE.RingGeometry(0.5,1,32,1)`。
  13. CapsuleGeometry 参数:radius, length, capSegments, radialSegments;示例 `new THREE.CapsuleGeometry(0.5,1,4,8)`。
  14. DodecahedronGeometry 参数:radius, detail;示例 `new THREE.DodecahedronGeometry(1,0)`。
  15. IcosahedronGeometry 参数:radius, detail,detail=0 为 20 个面,越大越平滑;示例 `new THREE.IcosahedronGeometry(1,0)`。
  16. OctahedronGeometry 参数:radius, detail;示例 `new THREE.OctahedronGeometry(1,0)`。
  17. TetrahedronGeometry 参数:radius, detail;示例 `new THREE.TetrahedronGeometry(1,0)`。
  18. 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)`。
  19. LatheGeometry:用 `THREE.Vector2` 点数组定义轮廓,参数 points, segments, phiStart, phiLength;示例 `new THREE.LatheGeometry(points,32)`。
  20. ExtrudeGeometry:用 `THREE.Shape` 的 `moveTo`/`lineTo` 定义形状,设置 `extrudeSettings` 的 steps、depth、bevelEnabled、bevelThickness、bevelSize、bevelSegments;示例 `new THREE.ExtrudeGeometry(shape, extrudeSettings)`。
  21. TubeGeometry:用 `THREE.CatmullRomCurve3` 定义路径,参数 path, tubularSegments, radius, radialSegments, closed;示例 `new THREE.TubeGeometry(curve,64,0.2,8,false)`。
  22. TextGeometry:从 `three/examples/jsm/loaders/FontLoader.js` 导入 `FontLoader`,从 `three/examples/jsm/geometries/TextGeometry.js` 导入 `TextGeometry`;`loader.load('fonts/helvetiker_regular.typeface.json', font => ...)` 加载字体。
  23. TextGeometry 选项:font, size, depth(旧版本为 height)、curveSegments、bevelEnabled、bevelThickness、bevelSize、bevelSegments;创建后调用 `geometry.computeBoundingBox()` 和 `geometry.center()` 居中文本。
  24. BufferGeometry 是所有几何体的基类,用 typed array 存储数据以提高 GPU 效率。
  25. 自定义 BufferGeometry:`const geometry = new THREE.BufferGeometry()`;用 `Float32Array` 存顶点,`geometry.setAttribute('position', new THREE.BufferAttribute(vertices,3))` 设置 position。
  26. 为自定义 BufferGeometry 设置索引:用 `Uint16Array` 存三角形索引,`geometry.setIndex(new THREE.BufferAttribute(indices,1))`,以便复用顶点。
  27. 为光照设置法线:`geometry.setAttribute('normal', new THREE.BufferAttribute(normals,3))`,每个顶点 3 个 float。
  28. 为纹理设置 UV:`geometry.setAttribute('uv', new THREE.BufferAttribute(uvs,2))`,每个顶点 2 个 float。
  29. 设置逐顶点颜色:用 `Float32Array` 存 r,g,b,`geometry.setAttribute('color', new THREE.BufferAttribute(colors,3))`,并设置 `material.vertexColors = true`。
  30. BufferAttribute 常见类型:`Float32Array` 用于 positions/normals/UVs;`Uint16Array` 用于最多 65535 顶点的索引;`Uint32Array` 用于更大网格的索引;`Uint8Array` 用于 0-255 范围的颜色。
  31. BufferAttribute 的 itemSize:Position 为 3(x,y,z),Normal 为 3,UV 为 2,Color 为 3 或 4,Index 为 1;用 `new THREE.BufferAttribute(array, itemSize)` 创建。
  32. 修改 BufferGeometry 顶点:用 `positions.setXYZ(index,x,y,z)` 修改,用 `positions.getX(index)`、`getY`、`getZ` 读取。
  33. 修改顶点后设置 `positions.needsUpdate = true` 通知 GPU 更新;位置变化后调用 `geometry.computeVertexNormals()` 重算法线。
  34. 修改几何体后重新计算包围盒/包围球:`geometry.computeBoundingBox()`、`geometry.computeBoundingSphere()`。
  35. 交错缓冲区:用 `new THREE.InterleavedBuffer(new Float32Array([...]), 5)`,stride 为每个顶点 float 数;用 `new THREE.InterleavedBufferAttribute(interleavedBuffer, 3, 0)` 设置 position,用 size 2、offset 3 设置 uv。
  36. EdgesGeometry 显示硬边:`new THREE.EdgesGeometry(boxGeometry, 15)`,其中 15 为阈值角度;用 `LineSegments` 与 `LineBasicMaterial` 渲染。
  37. WireframeGeometry 显示所有三角形线框:`new THREE.WireframeGeometry(boxGeometry)`,用 `LineSegments` 与 `LineBasicMaterial` 渲染。
  38. 创建点云:用 `BufferGeometry` 和 `Float32Array` 填充 1000 个随机位置,`geometry.setAttribute('position', new THREE.BufferAttribute(positions,3))`。
  39. 点云材质:`new THREE.PointsMaterial({ size: 0.1, sizeAttenuation: true, color: 0xffffff })`;用 `new THREE.Points(geometry, material)` 并 `scene.add(points)`。
  40. 创建 Line:用 `BufferGeometry().setFromPoints(points)` 和 `LineBasicMaterial`,再用 `new THREE.Line(geometry, material)` 连接点。
  41. 创建 LineLoop:用同一个 geometry 和 material 创建 `new THREE.LineLoop(geometry, material)` 形成闭合环。
  42. 创建 LineSegments:用 `BufferAttribute` 设置成对顶点的 position 数组,再 `new THREE.LineSegments(segmentsGeometry, material)`。
  43. InstancedMesh 高效渲染大量相同几何体:`new THREE.InstancedMesh(geometry, material, count)`。
  44. 为 InstancedMesh 设置每个实例变换:用 `THREE.Object3D` 的 `dummy` 设置 position、rotation、scale,调用 `dummy.updateMatrix()`,再 `instancedMesh.setMatrixAt(i, dummy.matrix)`。
  45. 设置实例矩阵后调用 `instancedMesh.instanceMatrix.needsUpdate = true`,并将 `instancedMesh` 加入场景。
  46. 为 InstancedMesh 设置逐实例颜色:创建 `instancedMesh.instanceColor = new THREE.InstancedBufferAttribute(new Float32Array(count*3),3)`,循环 `instancedMesh.setColorAt(i, new THREE.Color(...))`,最后 `instancedMesh.instanceColor.needsUpdate = true`。
  47. 运行时更新单个实例:`instancedMesh.getMatrixAt(index, matrix)` 读取矩阵,修改后 `instancedMesh.setMatrixAt(index, matrix)`,再 `instancedMesh.instanceMatrix.needsUpdate = true`。
  48. 对 InstancedMesh 做射线检测:`raycaster.intersectObject(instancedMesh)`,从 `intersects[0].instanceId` 获取实例 ID。
  49. InstancedBufferGeometry 用于自定义逐实例属性:`new THREE.InstancedBufferGeometry()`,先 `geometry.copy(new THREE.BoxGeometry(1,1,1))`,再用 `geometry.setAttribute('offset', new THREE.InstancedBufferAttribute(offsets,3))` 添加 offset。
  50. 在着色器中使用逐实例属性:声明 `attribute vec3 offset;`,计算 `vec3 transformed = position + offset;`。
  51. 合并几何体:用 `BufferGeometryUtils.mergeGeometries([geo1,geo2,geo3])` 合并相同属性的几何体;传入 `true` 可合并并保留 groups 以支持多材质。
  52. 为法线贴图计算切线:`BufferGeometryUtils.computeTangents(geometry)`。
  53. 交错属性提升性能:`BufferGeometryUtils.interleaveAttributes([geometry.attributes.position, geometry.attributes.normal, geometry.attributes.uv])`。
  54. 居中几何体:`geometry.computeBoundingBox(); geometry.center();` 将顶点移到原点中心。
  55. 缩放适配尺寸:`geometry.computeBoundingBox(); geometry.boundingBox.getSize(size); const maxDim = Math.max(size.x,size.y,size.z); geometry.scale(1/maxDim,1/maxDim,1/maxDim);`。
  56. 克隆并变换几何体:`const clone = geometry.clone(); clone.rotateX(Math.PI/2); clone.translate(0,1,0); clone.scale(2,2,2);`。
  57. 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%。
  58. 性能:优先使用 indexed geometry,通过索引复用顶点。
  59. 性能:合并静态网格以减少 draw calls,使用 `mergeGeometries`。
  60. 性能:对大量相同对象使用 `InstancedMesh`。
  61. 性能:选择合适的分段数,更多分段更平滑但更慢;常用球体分段:`new THREE.SphereGeometry(1,32,32)` 质量好,`new THREE.SphereGeometry(1,64,64)` 高质量,`new THREE.SphereGeometry(1,16,16)` 性能模式。
  62. 性能:不再使用时调用 `geometry.dispose()` 释放几何体。
  63. 需要场景设置与 Object3D 时参考 `threejs-fundamentals`;需要材质类型时参考 `threejs-materials`;需要自定义顶点操作时参考 `threejs-shaders`。

适用边界 · 注意事项