如何沿着简单的路径移动相机

时间:2014-05-03 10:19:04

标签: three.js

如何沿简单路径(由顶点/点数组创建)移动相机?我需要自动移动它,而不是像第一人称射击游戏那样通过键盘/鼠标事件移动它?

注意这个例子:http://threejs.org/examples/webgl_geometry_extrude_splines.html它真的很好(也很复杂),但我需要一些简单的东西,作为一个刚开始学习Three.js的人

1 个答案:

答案 0 :(得分:5)

因此,最好和最简单的解决方案(基于我的错误和研究 - 也许你甚至可以找到一个更简单的解决方案)是使用PathControls;你可以在这里找到图书馆:https://github.com/mrdoob/three.js/blob/master/examples/js/controls/PathControls.js

这个简单的解决方案基于以下书:学习Three.js:“用于WebGL的JavaScript 3D库”;在Three上学习一些东西非常好,我建议你阅读它;首先,我们将PathControls.js添加到我们的文档中:

<script src="js/PathControls.js"></script>

然后我们在init()函数之前添加一些变量:

var controls;
var clock = new THREE.Clock();
var pathControls;

现在,在创建场景,相机,灯光等之后,我们需要在init()函数上做一些工作:

controls = new function () {
this.numberOfPoints = 5;
this.segments = 64;
this.radius = 3;
this.radiusSegments = 5;
this.closed = false;
this.points = getPoints();

//you can take out this last one: it shows you the path on which the camera is moving
generatePoints(this.points, this.segments, this.radius, this.radiusSegments, this.closed);
}
pathControls = new THREE.PathControls(camera);

// configure the controller
pathControls.duration = 70

//speed, so you will not have the dash effect on a curve
pathControls.useConstantSpeed = true;
pathControls.lookSpeed = 0.1;
pathControls.lookVertical = true;
pathControls.lookHorizontal = true;
pathControls.verticalAngleMap = {srcRange: [ 0, 2 * Math.PI ], dstRange: [ 1.1, 3.8 ]};
pathControls.horizontalAngleMap = {srcRange: [ 0, 2 * Math.PI ], dstRange: [ 0.3, Math.PI - 0.3 ]};
pathControls.lon = 300;
pathControls.lat = 40;

// add the path
controls.points.forEach(function(e) {
  pathControls.waypoints.push([e.x, e.y, e.z]) });

// when done configuring init the control
pathControls.init();

// add the animationParent to the scene and start the animation
scene.add(pathControls.animationParent);
pathControls.animation.play(true, 0 );

最后你需要在你的animate()函数中使用这三行,这样相机实际上会根据时间移动:

var delta = clock.getDelta();
THREE.AnimationHandler.update(delta);
pathControls.update(delta);

关于支持功能(我有一个只有5个点的阵列,但是你可以使用越来越复杂的东西:这取决于你):

function getPoints() {
    var points = [];
    points.push(new THREE.Vector3(0, 20, 0));
    points.push(new THREE.Vector3(100, 25, 0));
    points.push(new THREE.Vector3(100, 20, 100));
    points.push(new THREE.Vector3(0, 25, 100));
    points.push(new THREE.Vector3(0, 20, 0));
    return points;
}

这些是在您选择的路径上显示/绘制管的功能,这样您就可以看到相机实际上是如何移动的(整个代码不需要工作):

function generatePoints(points, segments, radius, radiusSegments, closed) {
  var tube = new THREE.TubeGeometry(new THREE.SplineCurve3(points), segments, radius, radiusSegments, false, closed);
  var tubeMesh = createMesh(tube);
  scene.add(tubeMesh);
}

function createMesh(geom) {
  mesh = THREE.SceneUtils.createMultiMaterialObject( geom, [
    new THREE.MeshLambertMaterial({color: 0x00ff00, transparent: true}),
    new THREE.MeshBasicMaterial({color: 0x000000, opacity: 0.3, wireframe: true, transparent: true})]);
 return mesh;
}

希望它对某人有用;对于整个代码,您可以在此处找到它:https://github.com/MarcinKwiatkowski1988/learningThreeJs/blob/master/movingCameraOnPath/myTry1_simply.html