如何沿着简单的路径移动摄像机(由顶点/点数组创建)?我需要自动移动它,而不是像第一人称射击游戏那样通过键盘/鼠标事件移动它?
看看这个例子:http://threejs.org/examples/webgl_geometry_extrude_splines.html,它真的很好(也很复杂),但我需要一些简单的东西,作为一个刚刚开始学习Three.js的人
因此,最好和最简单的解决方案(基于我的错误和研究 - 也许你甚至可以找到一个更简单的解决方案)是使用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>
Run Code Online (Sandbox Code Playgroud)
然后我们在输出init()函数之前添加一些变量:
var controls;
var clock = new THREE.Clock();
var pathControls;
Run Code Online (Sandbox Code Playgroud)
现在我们需要在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 );
Run Code Online (Sandbox Code Playgroud)
最后你需要在你的animate()函数中使用这3行,这样相机实际上会根据时间移动:
var delta = clock.getDelta();
THREE.AnimationHandler.update(delta);
pathControls.update(delta);
Run Code Online (Sandbox Code Playgroud)
关于支持函数(我有一个只是一个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;
}
Run Code Online (Sandbox Code Playgroud)
这些是在您选择的路径上显示/绘制管的功能,这样您就可以看到相机实际上是如何移动的(整个代码不需要工作):
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;
}
Run Code Online (Sandbox Code Playgroud)
希望它对某人有用; 对于整个代码,你可以在这里找到它:https://github.com/MarcinKwiatkowski1988/learningThreeJs/blob/master/movingCameraOnPath/myTry1_simply.html
| 归档时间: |
|
| 查看次数: |
3701 次 |
| 最近记录: |