THREE.Object3D.add:对象不是THREE.Object3D的实例。在必需的模块上

时间:2016-07-04 01:53:58

标签: javascript three.js browserify

是否存在需要Object3D的任何已知问题?请考虑以下代码

这是有效的

...
// THREE is imported globally somewhere else

var material = new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true});
var mesh = new THREE.Mesh(new THREE.BoxGeometry(200, 200, 200), material);

console.dir(mesh) // -> THREE.Mesh

scene.add(mesh);

...

这不起作用

Character.ts

export class Character {

    create() {

        var material = new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true});
        return new THREE.Mesh(new THREE.BoxGeometry(200, 200, 200), material);

    }

}

app.ts

import {Character} from "./Character.js;

var mesh = (new Character).create();

console.dir(mesh) // -> THREE.Mesh

scene.add(mesh);

错误

app.js:8861 THREE.Object3D.add:object不是THREE.Object3D的实例。

我正在使用browserify

THREE.js版本r78

1 个答案:

答案 0 :(得分:0)

我刚刚碰到了这个问题 - 我的问题是我在项目中加入了three.js两次。我自己包含了它,我还使用了包含three.js的库。由于对象构造函数被覆盖,这会产生一些错误,我认为这是导致我的错误的原因。考虑下面的示例(从here刷过此代码):

window.MyObject = function () {};
var v1 = new window.MyObject();
console.log( 'first object instanceof:', v1 instanceof window.MyObject ); // true

window.MyObject = function () {};
var v2 = new window.MyObject();
console.log( 'second object instanceof:', v2 instanceof window.MyObject ); // true
console.log( 'first object instanceof:', v1 instanceof window.MyObject ); // false!