三.js TypeError:无法读取未定义的属性“中心" [英] three.js TypeError: Cannot read property 'center' of undefined

查看:36
本文介绍了三.js TypeError:无法读取未定义的属性“中心"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying import OBJ (tried different) on server with node.js and three.js - I got this Error after parse file. This is current code how I import geometry:

    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);

这是调用堆栈:

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)

我知道这是已知问题 https://github.com/mrdoob/three.js/pull/3748 ,但我不知道如何解决这个错误.

I know that this was known issue https://github.com/mrdoob/three.js/pull/3748 , but I cannot figured out how to fix this error.

推荐答案

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

I've got the same problem since I discovered that objects loaded by OBJLoader are already a THREE.Mesh instance.

所以你应该这样做:

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);
});

另见这个问题three.js 网站上的此示例.

这篇关于三.js TypeError:无法读取未定义的属性“中心"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆