Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

three.js TypeError: Cannot read property 'center' of undefined

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

Here is call stack:

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)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

So you should probably do that :

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

Also see this question and this example on the three.js website.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...