【问题标题】:three.js TypeError: Cannot read property 'center' of undefinedthree.js TypeError:无法读取未定义的属性“中心”
【发布时间】:2015-11-09 10:48:48
【问题描述】:

我正在尝试使用 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);

这里是调用栈:

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,但我不知道如何解决此错误。

【问题讨论】:

  • 模型是在不使用材质的情况下加载的吗?如果不是,那么您的模型代码有问题
  • @mrapsogos 模型只是由 3dsmax 生成的几何体 (pastebin.com/6gAkVmE0),所以应该没问题。

标签: three.js


【解决方案1】:

我遇到了同样的问题,因为我发现 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);
});

另见this questionthis example on the three.js website

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多