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

three.js r133版本运行UV分配代码报错faceVertexUvs未定义求助

问题原因

Three.js 从 r125 版本开始正式移除了旧版 Geometry 类,仅保留性能更优的 BufferGeometry 作为唯一几何体类。你之前代码中用到的 faceVertexUvs、faces、vertices 都是旧 Geometry 专属的属性,在新版中不存在,因此会抛出属性不存在的错误。

适配方案

你需要将函数改造为适配 BufferGeometry 结构的版本,改造后代码如下:

assignUVs(geometry) {
    // 获取顶点位置属性
    const posAttribute = geometry.getAttribute('position');
    const vertexCount = posAttribute.count;
    // 初始化UV数组,每个顶点对应2个分量
    const uvArray = new Float32Array(vertexCount * 2);
    // 处理索引:兼容带索引和不带索引的BufferGeometry
    const indexAttr = geometry.getIndex();
    const indices = indexAttr ? indexAttr.array : Array.from({length: vertexCount}, (_, i) => i);
    const faceCount = indices.length / 3;
    // 临时变量复用
    const p1 = new THREE.Vector3(), p2 = new THREE.Vector3(), p3 = new THREE.Vector3();
    const faceNormal = new THREE.Vector3();
    for (let i = 0; i < faceCount; i++) {
        // 取出当前面的三个顶点索引
        const vi1 = indices[i * 3], vi2 = indices[i * 3 + 1], vi3 = indices[i * 3 + 2];
        // 读取三个顶点的坐标
        p1.fromBufferAttribute(posAttribute, vi1);
        p2.fromBufferAttribute(posAttribute, vi2);
        p3.fromBufferAttribute(posAttribute, vi3);
        // 计算面法向量,和原有逻辑保持一致
        THREE.Triangle.getNormal(p1, p2, p3, faceNormal);
        // 按法向量分量绝对值排序,选择投影轴
        const components = ['x', 'y', 'z'].sort((a, b) => {
            return Math.abs(faceNormal[a]) > Math.abs(faceNormal[b]) ? -1 : 1;
        });
        // 给三个顶点赋值UV
        uvArray[vi1 * 2] = p1[components[0]];
        uvArray[vi1 * 2 + 1] = p1[components[1]];
        uvArray[vi2 * 2] = p2[components[0]];
        uvArray[vi2 * 2 + 1] = p2[components[1]];
        uvArray[vi3 * 2] = p3[components[0]];
        uvArray[vi3 * 2 + 1] = p3[components[1]];
    }
    // 给几何体设置UV属性
    geometry.setAttribute('uv', new THREE.BufferAttribute(uvArray, 2));
}

关键改动说明

  • 不再操作已废弃的旧API,直接读写BufferGeometry的属性数组,性能更高
  • 自动兼容带索引、不带索引两种BufferGeometry格式
  • 移除了不再需要的uvsNeedUpdate标记,新赋值的属性会自动生效,如果你后续修改已有UV属性,只需设置geometry.getAttribute('uv').needsUpdate = true即可

内容的提问来源于stack exchange,提问作者Michaël

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 16:57:03