javascript - Three.js(版本102)如何使用OrbitControls设置相机的默认位置和旋转

标签 javascript three.js orbit

我创建了一个场景,在不使用 OrbitControls 的情况下一切都很好。 当我使用OrbitControls时,我发现我的相机的位置和旋转发生了变化,并且无法修改。

有人可以告诉我如何使用 OrbitControls 设置相机的默认位置和旋转吗?

谢谢! three.js - OrbitControls

最佳答案

OrbitControls 需要一个目标。设置目标,以便获得相同的 View 。

  camera.position.set(1, 8, 7);

  const controls = new THREE.OrbitControls(camera, canvas);
  controls.target.set(0, 3, 0);
  controls.update();  

'use strict';

/* global THREE */

function main() {
  const canvas = document.querySelector('#c');
  const renderer = new THREE.WebGLRenderer({canvas: canvas});

  const fov = 75;
  const aspect = 2;  // the canvas default
  const near = 0.1;
  const far = 500;
  const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  camera.position.set(1, 8, 7);
  
  const controls = new THREE.OrbitControls(camera, canvas);
  controls.target.set(0, 3, 0);
  controls.update();  

  const scene = new THREE.Scene();

  {
    const color = 0xFFFFFF;
    const intensity = 1;
    const light = new THREE.DirectionalLight(color, intensity);
    light.position.set(-1, 2, 4);
    scene.add(light);
  }

  {
    const boxWidth = 1;
    const boxHeight = 10;
    const boxDepth = 1;
    const geometry = new THREE.BoxBufferGeometry(boxWidth, boxHeight, boxDepth);
    const material = new THREE.MeshPhongMaterial({color: 'red'});

    const cube = new THREE.Mesh(geometry, material);
    scene.add(cube);
    cube.position.y = .5;
  }
  
  {
    const geometry = new THREE.PlaneBufferGeometry(10, 10);
    const material = new THREE.MeshPhongMaterial({color: 'gray'});

    const plane = new THREE.Mesh(geometry, material);
    scene.add(plane);
    plane.rotation.x = Math.PI * -0.5;
  }

  function resizeRendererToDisplaySize(renderer) {
    const canvas = renderer.domElement;
    const width = canvas.clientWidth;
    const height = canvas.clientHeight;
    const needResize = canvas.width !== width || canvas.height !== height;
    if (needResize) {
      renderer.setSize(width, height, false);
    }
    return needResize;
  }

  function render(time) {
    if (resizeRendererToDisplaySize(renderer)) {
      const canvas = renderer.domElement;
      camera.aspect = canvas.clientWidth / canvas.clientHeight;
      camera.updateProjectionMatrix();
    }

    renderer.render(scene, camera);

    requestAnimationFrame(render);
  }

  requestAnimationFrame(render);
}

main();
body { margin: 0; }
#c { width: 100vw; height: 100vh; display: block; }
<canvas id="c"></canvas>
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r102/three.min.js"></script>
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r102/js/controls/OrbitControls.js"></script>

或者您可以根据当前摄像机 View 计算目标。 OrbitControls 围绕目标运行,因此您需要选择距相机的距离作为目标

  const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  camera.position.set(1, 2, 5);
  camera.rotation.set(.1, .2, 0);

  // get the direction of the camera
  const direction = new THREE.Vector3();
  camera.getWorldDirection(direction);

  const controls = new THREE.OrbitControls(camera, canvas);

  // point the target from the camera in the
  // target direction
  camera.getWorldPosition(controls.target);
  controls.target.addScaledVector(direction, 5);
  controls.update();  

请注意,addScaledVector 中的 5 表示目标将位于相机前面 5 个单位(沿相机面向的方向)。 5 是否合适取决于您。在我的示例场景中,相机从 z = 5 开始,因此相机前面的 5 个单位似乎是放置目标的合理位置

'use strict';

/* global THREE */

function main() {
  const canvas = document.querySelector('#c');
  const renderer = new THREE.WebGLRenderer({canvas: canvas});

  const fov = 75;
  const aspect = 2;  // the canvas default
  const near = 0.1;
  const far = 500;
  const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  camera.position.set(1, 2, 5);
  camera.rotation.set(.1, .2, 0);

  // compute a target direction
  const direction = new THREE.Vector3();
  camera.getWorldDirection(direction);

  const controls = new THREE.OrbitControls(camera, canvas);
  // point the target from the camera in the
  // target direction
  camera.getWorldPosition(controls.target);
  controls.target.addScaledVector(direction, 5);
  controls.update();  

  const scene = new THREE.Scene();

  {
    const color = 0xFFFFFF;
    const intensity = 1;
    const light = new THREE.DirectionalLight(color, intensity);
    light.position.set(-1, 2, 4);
    scene.add(light);
  }

  {
    const boxWidth = 1;
    const boxHeight = 10;
    const boxDepth = 1;
    const geometry = new THREE.BoxBufferGeometry(boxWidth, boxHeight, boxDepth);
    const material = new THREE.MeshPhongMaterial({color: 'red'});

    const cube = new THREE.Mesh(geometry, material);
    scene.add(cube);
    cube.position.y = .5;
  }
  
  {
    const geometry = new THREE.PlaneBufferGeometry(10, 10);
    const material = new THREE.MeshPhongMaterial({color: 'gray'});

    const plane = new THREE.Mesh(geometry, material);
    scene.add(plane);
    plane.rotation.x = Math.PI * -0.5;
  }

  function resizeRendererToDisplaySize(renderer) {
    const canvas = renderer.domElement;
    const width = canvas.clientWidth;
    const height = canvas.clientHeight;
    const needResize = canvas.width !== width || canvas.height !== height;
    if (needResize) {
      renderer.setSize(width, height, false);
    }
    return needResize;
  }

  function render(time) {
    if (resizeRendererToDisplaySize(renderer)) {
      const canvas = renderer.domElement;
      camera.aspect = canvas.clientWidth / canvas.clientHeight;
      camera.updateProjectionMatrix();
    }

    renderer.render(scene, camera);

    requestAnimationFrame(render);
  }

  requestAnimationFrame(render);
}

main();
body { margin: 0; }
#c { width: 100vw; height: 100vh; display: block; }
<canvas id="c"></canvas>
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r102/three.min.js"></script>
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r102/js/controls/OrbitControls.js"></script>

关于javascript - Three.js(版本102)如何使用OrbitControls设置相机的默认位置和旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55310424/

相关文章:

javascript - Jquery Slider 旋转但发生了一些奇怪的事情

javascript - 十六进制到 RGB 转换器

javascript - 获取 jQuery 创建的表格中某一列中所有单元格的数据

javascript - 导入 LOCAL .obj 以使用 React 使用 Three.js OBJLoader 加载

image - Three.js 纹理/图像在运行时更新

javascript - 三.js线宽矛盾

javascript - 2 个连续的 fetch PUT 请求是否可以乱序处理?

javascript - 如何使用 JavaScript 或 jQuery 将样式表中 CSS 类的属性直接加载到散列中?

javascript - 图像不会在轨道 slider 基础上滑动

jquery - slider 中垂直居中字幕