【问题标题】:Threejs - rotating the camera by 90 degrees, not objectsThreejs - 将相机旋转 90 度,而不是物体
【发布时间】:2021-01-25 21:40:14
【问题描述】:

在我的项目中,场景中有很多物体,所以我宁愿旋转相机而不是物体。我怎样才能做到这一点?我使用的是 OrthographicCamera,这意味着我在 2D 上工作。

当前相机位置(左示例)

const camera =  new OrthographicCamera(0, width, 0, height, 0, 20);
camera.position.set(x, y, 1);
camera.clearViewOffset();
camera.updateProjectionMatrix();

右边是我想要实现的……

当我使用camera.rotation.z = Math.PI * 0.5; 时,对象消失在视口后面,因为我的相机设置在左上角。不幸的是,相机设置是由于它从 BE 获得的坐标。

【问题讨论】:

    标签: javascript three.js webgl


    【解决方案1】:

    试试看:

    camera.rotation.z = Math.PI * 0.5;

    const aspect = window.innerWidth / window.innerHeight;
    const frustumSize = 2;
    
    const camera = new THREE.OrthographicCamera(0.5 * frustumSize * aspect / -2, 0.5 * frustumSize * aspect / 2, frustumSize / 2, frustumSize / -2, 0.01, 10);
    camera.position.z = 1;
    camera.rotation.z = Math.PI * 0.5;
    
    const scene = new THREE.Scene();
    
    scene.add(new THREE.AxesHelper());
    
    const renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);
    
    renderer.render(scene, camera);
    body {
          margin: 0;
    }
    <script src="https://cdn.jsdelivr.net/npm/three@0.124/build/three.js"></script>

    【讨论】:

    • 您的想法旋转了相机,但物体消失了(它们在某个高处)。发生这种情况是因为这些对象的坐标是根据左上角定义的(我无法更改):/ new OrthographicCamera(left: 0, right: width, top: 0, bottom: height, near: 0, far: 20);
    • 还有其他想法吗? :)
    【解决方案2】:

    因为你的平截头体在左上角有 0,0,你需要添加一些其他的父母到相机移动它的旋转点。

    const {
      Object3D,
      OrthographicCamera,
      Scene,
      Renderer,
      GridHelper,
      WebGLRenderer,
      MathUtils,
    } = THREE;
    const {
      lerp,
    } = MathUtils;
    
    const canvas = document.querySelector('canvas')
    const {width, height} = canvas.getBoundingClientRect();
    const scene = new Scene();
    
    const camera = new OrthographicCamera(0, width, 0, height, 0, 20);
    // cameras look down -Z so this camera is looking from 20 to 0
    camera.position.z = 20;
    
    const cameraBase = new Object3D();
    const cameraPivot = new Object3D();
    cameraBase.add(cameraPivot);
    cameraBase.position.set(width / 2, height / 2, 0);
    cameraPivot.add(camera);
    cameraPivot.position.set(-width / 2, -height / 2, 0);
    scene.add(cameraBase);
    
    
    addRect('red',    -10, -10, 5,  20,  20);
    addRect('cyan',    20,  20, 5,  20,  20);
    addRect('green',  100,  50, 5,  25,  30);
    addRect('yellow', 130,  70, 6, 100,  50);
    addRect('orange', 170,  90, 7,  50,  75);
    addRect('white',    1,   1, 5, 298, 148);
    
    function addRect(color, x, y, z, width, height) {
      // make a 1x1 unit grid
      const grid = new GridHelper(1, 1, color, color);
      // move it so it starts at 0,0 and goes to 1,1
      grid.geometry.applyMatrix4(new THREE.Matrix4().makeTranslation(0.5, 0, 0.5));
      // grids are in x,z plane. rotate to x,y plane
      grid.rotation.x = Math.PI * -0.5;
      // position it
      grid.position.set(x, y, z);
      // make it the size we want in pixel units
      grid.scale.set(width, 1, height);
      scene.add(grid);
    }
    
    const renderer = new WebGLRenderer({canvas});
    
    
    function render(now) {
      cameraBase.rotation.z = (now * 0.001);
      renderer.render(scene, camera);
      requestAnimationFrame(render);
    }
    requestAnimationFrame(render);
    body { background: #444; }
    <script src="https://cdn.jsdelivr.net/npm/three@0.124/build/three.js"></script>
    <canvas></canvas>

    【讨论】:

    • 酷!我们很亲密!你能简化一下你的代码吗?摆脱动画,因为我的版本是静态的(点击按钮后我旋转相机),我真的不明白你为什么要制作 object3d
    • 也许你应该read some tutorialsObject3Ds 让我们将相机移动到中心,旋转它,然后将它移回左上角,以便它适用于给定的投影矩阵。另外,不,我不会更改代码。删除动画并根据您想要的任何输入将cameraBase.rotation.z 设置为Math.PI * 0.5Math.PIMath.PI * 1.5 应该是微不足道的。
    • 现在我有另一个问题.. 一切正常,但是当我用threejs(作为图层)传输照片时,threejs 不跟随照片:/
    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    相关资源
    最近更新 更多