【问题标题】:THREE.CameraDolly is not a constructor (Three.js & dolly.js)THREE.CameraDolly 不是构造函数(Three.js & dolly.js)
【发布时间】:2015-08-09 11:02:47
【问题描述】:

我真的很困惑,没有任何效果。我总是收到错误“THREE.CameraDolly 不是构造函数”。如果有人还没有注意到,我正在使用 Three.js。

我的脚本:

    var WIDTH          = window.innerWidth;
var HEIGHT         = window.innerHeight;
var scene          = new THREE.Scene();
var camera         = new THREE.PerspectiveCamera( 45, WIDTH / HEIGHT, 1, 1000 );
var cameraDolly    = new THREE.PerspectiveCamera( 45, WIDTH / HEIGHT, 1, 1000);
var renderer       = new THREE.WebGLRenderer();

...

//LookAt & Camera Position Points
var points = {
     "camera": [        
         {            
             "x": 100,            
             "y": 60,            
             "z": 40        
         },        
         {            
             "x": -20,            
             "y": 5,            
             "z": -10        
         },        
         {            
             "x": 30,            
             "y": 10,            
             "z": -5        
         }    
     ],    
    "lookat": [        
        {            
            "x": 50,            
            "y": 30,            
            "z": 35        
        },        
        {            
            "x": 0,            
            "y": 2,            
            "z": 0       
        },        
        {            
            "x": 12,            
            "y": 8,            
            "z": -0.2        
        }    
    ]    
};

//Clock
var clock = new THREE.Clock( true );

//CameraDolly

***var dolly = new THREE.CameraDolly ( cameraDolly, scene, points );***//Here I got the error

scene.add(dolly, 'cameraPosition', 0, 1);
scene.add(dolly, 'lookatPosition', 0, 1);

function update() {
    requestAnimationFrame(update);
    render( cameraDolly, 0.75, 0, 0.25, 0.25 );

    var delta = clock.getElapsedTime() * 0.2;
    var position = THREE.Math.mapLinear(Math.sin(delta), -1, 1, 0, 1);

    dolly.cameraPosition = position;
    dolly.lookatPosition = position;
    dolly.update();

};


function render() {       
    renderer.render(scene, cameraDolly);
};

render();
update();

还有 dolly.js:

/**
* @author DPR / http://ivxvixviii.io
 */

THREE.CameraDolly = function ( camera, scene, points ){

this.cameraPosition = 0;
this.lookatPosition = 0;

this.camera       = camera;
this.scene        = scene;
this.cameraPoints = points.camera;
this.lookatPoints = points.lookat;
this.bounds       = 100;
}

// Lookat position Marker

this.lookatPositionMarker = this.createMarker(0xFF0000);

this.scene.add(this.lookatPositionMarker);

// Camera path markers
this.markers = [];

if(this.gui){
    var cameraPointsFolder = this.gui.addFolder('cameraPointsFolder');
    cameraPointsFolder.open();
}

var _this = this;

for( var i = 0; i < this.cameraPoints.length; ++i){

    if(this.gui){
        var point = this.cameraPoints[i];

        var folder = cameraPointsFolder.addFolder('marker-' + i);
        folder.add(point, 'x', -this.bounds, this.bounds).onChange(function(){
            _this.createCurves();
        });
        folder.add(point, 'y', -this.bounds, this.bounds).onChange(function(){
            _this.createCurves();
        });
        folder.add(point, 'z', -this.bounds, this.bounds).onChange(function(){
            _this.createCurves();
        });

        // folder.open();
    }

    var marker = this.createMarker(0x00FF00);

    this.scene.add( marker );
    this.markers.push( marker );
};

// Camera lookat path markers
this.lookatMarkers = [];

if(this.gui){
    var lookatPointsFolder = this.gui.addFolder('lookatPointsFolder');
    lookatPointsFolder.open();
}

for( var i = 0; i < this.lookatPoints.length; ++i){

    if(this.gui){
        var point = this.lookatPoints[i];

        var folder = lookatPointsFolder.addFolder('marker-' + i);
        folder.add(point, 'x', -this.bounds, this.bounds).onChange(function(){
            _this.createCurves();
        });
        folder.add(point, 'y', -this.bounds, this.bounds).onChange(function(){
            _this.createCurves();
        });
        folder.add(point, 'z', -this.bounds, this.bounds).onChange(function(){
            _this.createCurves();
        });

        // folder.open();
    }

    var marker = this.createMarker(0x0000FF);

    this.scene.add( marker );
    this.lookatMarkers.push( marker );
};

this.createCurves();
this.update();
};

THREE.CameraDolly.prototype.createCurves = function(){

// Camera curve

this.scene.remove(this.pathCurve);

var points = [];

for (var i = 0; i < this.cameraPoints.length ; ++i) {
    var point = this.cameraPoints[i];
    var vec   = new THREE.Vector3( point.x, point.y, point.z );
    this.markers[i].position.set( point.x, point.y, point.z );
    points.push(vec);
};

var spline = this.createSpline( points );
var points = spline.getPoints( 50 );

this.cameraSpline = this.createSpline(points);

var geometry = new THREE.Geometry();
var material = new THREE.LineBasicMaterial( { 0xFFFFFF*Math.random() } /*{ transparent: true, opacity: 0 }*/ );

points.forEach(function(point){
    geometry.vertices.push( point.clone() );
});

this.pathCurve = new THREE.Line( geometry, material );

this.scene.add( this.pathCurve );


// Lookat curve

this.scene.remove(this.pathLookatCurve);

var points = [];

for (var i = 0; i < this.lookatPoints.length ; ++i) {
    var point = this.lookatPoints[i];
    var vec   = new THREE.Vector3( point.x, point.y, point.z );
    this.lookatMarkers[i].position.set( point.x, point.y, point.z );
    points.push(vec);
};

var spline = this.createSpline( points );
var points = spline.getPoints( 50 );

this.cameralookatSpline = this.createSpline(points);

var geometry = new THREE.Geometry();
var material = new THREE.LineBasicMaterial( { 0xFFFFFF*Math.random() } /*{ transparent: true, opacity: 0 }*/ );

points.forEach(function(point){
    geometry.vertices.push( point.clone() );
});

this.pathLookatCurve = new THREE.Line( geometry, material );

this.scene.add( this.pathLookatCurve );

this.update();
};


THREE.CameraDolly.prototype.createSpline = function( points ) {

var tmp = [];

for( var i = 0; i < points.length; ++i){
    tmp.push( points[i].clone() );
};

return new THREE.SplineCurve3( tmp );
}

THREE.CameraDolly.prototype.createMarker = function(color){
var geometry = new THREE.SphereGeometry( 1, 4, 4 );
var material = new THREE.MeshBasicMaterial({color: color /*transparent: true, opacity: 0*/ });
return new THREE.Mesh(geometry, material);
};

THREE.CameraDolly.prototype.update = function(){

var position = this.cameraSpline.getPointAt( this.cameraPosition );

this.camera.position.copy( position );

position = this.cameralookatSpline.getPointAt( this.lookatPosition );

this.lookatPositionMarker.position.copy( position );

this.camera.lookAt( this.lookatPositionMarker.position );
};

THREE.CameraDolly.prototype.exportPositions = function(){

var data = {
    camera: [],
    lookat: []
};

this.cameraPoints.forEach(function(point){
    data.camera.push({
        x: point.x,
        y: point.y,
        z: point.z
    })
});

this.lookatPoints.forEach(function(point){
    data.lookat.push({
        x: point.x,
        y: point.y,
        z: point.z
    })
});

var json = JSON.stringify( data, undefined, 4 );

window.prompt('Copy to clipboard: Ctrl+C, Enter', json );
};

我已经在 Google 上进行了查找,但没有找到任何可以帮助我持续进行的内容。我已经包含了所有内容,我还尝试了使用 onload 函数并重命名了变量!

【问题讨论】:

    标签: javascript constructor camera three.js typeerror


    【解决方案1】:
    1. 您的 dolly.js 已更改并包含一些错误。如果您对此负责,请先对其进行调试,或者将其替换为 original dolly.js 文件。
    2. 这样做你会遇到这个错误:

    THREE.Object3D.add:" Object { […] } "is not an instance of THREE.Object3D.

    您不能将小车对象添加到 THREE.Scene,此脚本的作者正在将其添加到他的 GUI。请查看the example he provided 并尝试了解发生了什么。从那里调整您的代码。

    dolly helper 是为 r68 编写的第三方插件,并没有被three.js 官方支持,所以你应该在项目 github 页面报告错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 2016-06-05
      • 2017-03-08
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 2021-01-11
      相关资源
      最近更新 更多