【问题标题】:How to move camera along a simple path如何沿简单路径移动相机
【发布时间】:2014-06-20 00:31:09
【问题描述】:

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

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

【问题讨论】:

    标签: three.js


    【解决方案1】:

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

    这个简单的解决方案基于这本书:Learning Three.js: "The JavaScript 3D Library for WebGL";学三方面的东西很好,推荐大家读一读;首先我们将 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() 函数中使用这 3 行代码,以便相机实际上会根据时间移动:

    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

    【讨论】:

    猜你喜欢
    • 2013-08-14
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 2011-09-03
    • 2011-05-26
    • 2011-08-20
    • 2020-04-05
    • 2011-03-02
    相关资源
    最近更新 更多