BufferGeometryUtils
描述
- Three.js 提供的工具类,用于高效处理
BufferGeometry
的复杂操作,包括几何体合并、顶点优化、切线计算等。适用于需要高性能几何体处理的场景(如模型简化、LOD生成、动态几何体更新)。
导入
import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
方法
方法 | 参数 | 返回值 | 描述 |
---|
.computeMikkTSpaceTangents
(geometry, MikkTSpace, negateSign=true) | geometry : BufferGeometry 实例
MikkTSpace : mikktspace 模块实例(需 await MikkTSpace.ready)
negateSign : 是否反转切线符号(.w 分量,默认为 true) | 原 BufferGeometry | 使用 MikkTSpace 算法计算顶点切线,确保与其他建模工具生成的切线一致。适用于法线贴图材质,可避免镜像 UV 接缝处的视觉问题。注意:索引几何体会被解索引,需包含 position/normal/uv 属性。 |
.computeMorphedAttributes
(object) | object : Mesh/Line/Points 实例 | { positionAttribute, normalAttribute, morphedPositionAttribute, morphedNormalAttribute } | 返回变形/蒙皮对象的当前属性(含原始属性),用于射线检测或贴花(如 DecalGeometry)。 |
.estimateBytesUsed
(geometry) | geometry : BufferGeometry 实例 | Number | 估算几何体所有属性占用的内存字节数。 |
.interleaveAttributes
(attributes) | attributes : BufferAttribute 数组 | InterleavedBufferAttribute 或 null | 交叉存储一组属性(共享 InterleavedBuffer),要求属性类型兼容。失败返回 null。 |
.mergeAttributes
(attributes) | attributes : BufferAttribute 数组 | BufferAttribute 或 null | 合并一组属性为单一实例(不支持 InterleavedBufferAttributes)。失败返回 null。 |
.mergeGeometries
(geometries, useGroups) | geometries : BufferGeometry 数组
useGroups : 是否生成合并组 | BufferGeometry 或 null | 合并多个几何体为单一实例,要求属性兼容。失败返回 null。 |
.mergeGroups
(geometry) | geometry : BufferGeometry 实例 | BufferGeometry | 合并给定几何体的渲染组。 |
.mergeVertices
(geometry, tolerance=1e-4) | geometry : BufferGeometry 实例
tolerance : 顶点合并容差 | BufferGeometry | 返回合并相似顶点后的新几何体(容差范围内)。 |
.toCreasedNormals
(geometry, creaseAngle) | geometry : 输入几何体
creaseAngle : 折痕角度(弧度) | BufferGeometry | 生成折痕法线:仅在面夹角大于折痕角时保留硬边。非索引几何体会被直接修改。 |
.toTrianglesDrawMode
(geometry, drawMode) | geometry : BufferGeometry 实例
drawMode : TrianglesDrawMode 枚举值 | BufferGeometry | 将 TriangleStrip/TriangleFan 模式转换为 TrianglesDrawMode(对应 WebGL 的 gl.TRIANGLES)。 |
代码
const mergedGeometry = BufferGeometryUtils.mergeGeometries([geo1, geo2, geo3]);const optimizedGeo = BufferGeometryUtils.mergeVertices(mergedGeometry, 1e-4);const interleaved = BufferGeometryUtils.interleaveAttributes([geometry.getAttribute('position'),geometry.getAttribute('normal')
]);