Three.js 透明模型纹理错误

Three.js transparency model texture bug

我搜索了整个互联网并尝试了很多方法,但没有结果.. 如何消除纹理透明度错误?查看图片

所以,在 3dsmax 模型上看起来不错。我已经使用 python 脚本从 .obj 转换为 .js 格式,以便 .png 文件透明(.obj 不透明)。

如何解决我的问题?谢谢

var loader = new THREE.JSONLoader();
loader.load('tree_model.js', function(geometry, materials) {
    var material = new THREE.MeshFaceMaterial(materials);
    var object = new THREE.Mesh(geometry, material);
    object.traverse( function ( child ) {
            if ( child.material instanceof THREE.MeshPhongMaterial ) {

                // this code is unattainable, but anyway without if (..) it does not work

                child.material.alphaTest = 0.5;
                child.material.depthWrite = false;
                child.material.depthTest = false;
                child.material.side = THREE.BackSide;
                child.material.transparent = true;
            }
        });
        scene.add(object);
    });
});

和渲染器:

renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, premultipliedAlpha: true });

您必须在 material 上设置 alphaTest。此外,将叶子和分支设置为 THREE.DoubleSide 可确保从另一侧查看时它们不会消失。

您发布的代码包含各种错误,因此将其替换为:

var loader = new THREE.JSONLoader();
loader.load('model/Elka.js', function(geometry, materials) {

    for( var i = 0; i < materials.length; i ++ ) {
        var material = materials[ i ];
        material.alphaTest = 0.5;
        material.side = THREE.DoubleSide;
        // not-so-good practice
        if ( material.name === "NorwaySpruceBark" ) {
            material.transparent = false;
        }
    }

    var material = new THREE.MeshFaceMaterial(materials);
    var object = new THREE.Mesh(geometry, material);
    scene.add(object);
});

要进一步减少透明度伪像,请将主干设置为不透明。您的模型应该包含正确的 material 设置,所以这是一种不好的做法。

编辑:renderer 中设置 alphapremultipliedAlpha 对于这个问题没有必要。

结果: