首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Three.js(版本102)如何使用OrbitControls设置摄像头的默认位置和旋转

Three.js(版本102)如何使用OrbitControls设置摄像头的默认位置和旋转
EN

Stack Overflow用户
提问于 2019-03-23 11:44:45
回答 1查看 2.2K关注 0票数 0

我创建了一个场景,没有使用OrbitControls,一切都很好。当我使用OrbitControls时,我发现我的相机的位置和旋转已经改变了,我不能修改它。

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

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-24 00:00:15

OrbitControls需要target。设置target,使您获得相同的视图。

代码语言:javascript
运行
复制
  camera.position.set(1, 8, 7);

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

代码语言:javascript
运行
复制
'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();
代码语言:javascript
运行
复制
body { margin: 0; }
#c { width: 100vw; height: 100vh; display: block; }
代码语言:javascript
运行
复制
<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>

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

代码语言:javascript
运行
复制
  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个单位似乎是放置目标的合理位置。

代码语言:javascript
运行
复制
'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();
代码语言:javascript
运行
复制
body { margin: 0; }
#c { width: 100vw; height: 100vh; display: block; }
代码语言:javascript
运行
复制
<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>

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55310424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档