首页 > 解决方案 > Three.js 模型在调用 computeVertexNormals 后变黑?

问题描述

我正在使用 MTLLoader 和 OBJLoader 将 3D 模型导入到 three.js 中。.obj 文件不包含法线,因此生成的模型看起来是块状的。我试图通过将 OBJLoader 返回的 BufferGeometry 转换为 Geometry 对象来平滑它们,然后计算法线。这适用于许多对象,但其中一些对象几乎完全变黑

对此进行调查,我注意到在变黑的模型上,几乎每个顶点的顶点法线(由 computeVertexNormals() 计算)为 (0, 0, 0),而在看起来正常的模型上,它们是介于 -1 和1.

这是我的代码:

function loadModel() {

    // Load the materials first
    mtlLoader.load('texture.mtl', function (materials) {
        materials.preload();

        // Load the object and attach the materials
        objLoader
            .setMaterials(materials)
            .load('model.obj', function (object) {
                object.children[0].geometry = new THREE.Geometry().fromBufferGeometry(object.children[0].geometry);
                object.children[0].geometry.computeFaceNormals();
                object.children[0].geometry.mergeVertices();
                object.children[0].geometry.computeVertexNormals();

                scene.add(object);
            }, undefined, function (error) {
                console.log(error);
            });
        }, undefined, function (error) {
            console.log(error);
        }
    );
}

我该如何解决/防止这种情况?

标签: three.js

解决方案


推荐阅读