构造函数与threejs npm和Browserify有关

时间:2016-08-30 08:39:11

标签: constructor npm three.js browserify

我似乎无法使用threejs npm build和browserify来实例化THREEjs对象。

var THREE = require("three-js");
var camera = new THREE.PerspectiveCamera( 75, 1, 20, 2250 );

这会抛出错误

bundle.js:24 Uncaught TypeError: THREE.PerspectiveCamera is not a constructor

我可以看到构造函数存在于bundle.js

    THREE.PerspectiveCamera = function ( fov, aspect, near, far ) {

    THREE.Camera.call( this );

    this.type = 'PerspectiveCamera';

    this.fov = fov !== undefined ? fov : 50;
    this.zoom = 1;

    this.near = near !== undefined ? near : 0.1;
    this.far = far !== undefined ? far : 2000;
    this.focus = 10;

    this.aspect = aspect !== undefined ? aspect : 1;
    this.view = null;

    this.filmGauge = 35;    // width of the film (default in millimeters)
    this.filmOffset = 0;    // horizontal film offset (same unit as gauge)

    this.updateProjectionMatrix();

};

当我从正在运行的构建中移植此代码时,我怀疑我可能在使用Browserify时出错了。

npm Threejs构建版本79

1 个答案:

答案 0 :(得分:0)

问题不是正确构造three-js对象。来自https://www.npmjs.com/package/three-js

var THREE = require("three-js")();

我的例子中缺少额外的括号。

仅供参考:要让插件工作,请使用此结构

var oc = require("three-js/addons/OrbitControls");
var anotherAddon = require("three-js/addons/anotherAddon");
var THREE = require('three-js')([oc,anotherAddon]);