three.js TypeError:无法读取未定义的属性"center"

Des*_*vet 8 three.js

我正在尝试使用node.js和three.js在服务器上导入OBJ(尝试不同) - 我在解析文件后得到此错误.这是我导入几何的当前代码:

    var loader = new THREE.OBJLoader();
    loader.load(modelPath, function (geometryObj) {
    var materialObj = new THREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors, overdraw: 0.5 } );
    mesh = new THREE.Mesh(geometryObj, materialObj);
    scene.add(mesh);
Run Code Online (Sandbox Code Playgroud)

这是调用堆栈:

this.center.copy( sphere.center );
TypeError: Cannot read property 'center' of undefined
at THREE.Sphere.copy (eval at <anonymous> (/lib/three.js:32:3), <anonymous>:6074:27)
at THREE.Frustum.intersectsObject (eval at <anonymous> (/lib/three.js:32:3), <anonymous>:6253:11)
at eval (eval at <anonymous> (/lib/three.js:32:3), <anonymous>:36578:53)
at THREE.Object3D.traverseVisible (eval at <anonymous> (/lib/three.js:32:3), <anonymous>:7943:3)
at THREE.Object3D.traverseVisible (eval at <anonymous> (/lib/three.js:32:3), <anonymous>:7947:23)
at projectScene (eval at <anonymous> (/lib/three.js:32:3), <anonymous>:36568:9)
at render (eval at <anonymous> (/lib/three.js:32:3), <anonymous>:35449:28)
Run Code Online (Sandbox Code Playgroud)

我知道这是已知问题https://github.com/mrdoob/three.js/pull/3748,但我无法弄清楚如何修复此错误.

Cob*_*way 17

我发现OBJLoader加载的对象已经是THREE.Mesh实例,我遇到了同样的问题.

所以你应该这样做:

var loader = new THREE.OBJLoader();
loader.load(modelPath, function(object) {

    // if you want to add your custom material
    var materialObj = new THREE.MeshBasicMaterial({
        vertexColors: THREE.FaceColors,
        overdraw: 0.5
    });
    object.traverse(function(child) {
        if (child instanceof THREE.Mesh) {
            child.material = materialObj;
        }
    });

    // then directly add the object
    scene.add(object);
});
Run Code Online (Sandbox Code Playgroud)

另请参阅three.js网站上的这个问题和这个例子.