如何通过three.js中的轨道控制旋转具有偏移的对象?

时间:2018-07-24 13:39:21

标签: javascript three.js

我想像这样控制对象: helpful diagram

现在相机正在绕Y坐标移动,但需要绕NEW Y进行移动。代码在这里:

var material = new THREE.MeshLambertMaterial({color: 0x00ff00});
var cube = new THREE.Mesh(geometry, material);
scene.add(cube);
var controls = new THREE.OrbitControls(camera);
cube.position.x = 300;
// controls.target = THREE.Vector3(300, 0, 0);

我发现target选项可以设置控件的焦点,但是如果添加以下代码:controls.target = THREE.Vector3(300, 0, 0);,则该对象回到屏幕中心,但是它应该在右侧。

>

此外,当我将new THREE.OrbitControls(camera);设置为new THREE.OrbitControls(cube);并添加controls.target = THREE.Vector3(300, 0, 0);时,控制不起作用,但是当我设置controls.target = THREE.Vector3(301, 0, 0);时,它是可管理的,但不像照相机

1 个答案:

答案 0 :(得分:1)

OrbitControls.target设置控件的焦点以及围绕该控件的.object轨道。但这是手动操作。如文档中所述,OrbitControls.update()必须在对摄像机的变换进行任何手动更改后调用。

例如

controls.target = new THREE.Vector3(1, 0, 0)
controls.update();

请参见示例:

(function onLoad() {
  var container, camera, scene, renderer, controls;
  
  init();
  animate();

  function init() {
    container = document.getElementById('container');
    
    renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMap.enabled = true;
    container.appendChild(renderer.domElement);

    camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 100);
    camera.position.set(0, 1.0, -4);
    
    scene = new THREE.Scene();
    scene.background = new THREE.Color(0xffffff);
    scene.add(camera);
    window.onresize = resize;
    
    var ambientLight = new THREE.AmbientLight(0x404040);
    scene.add(ambientLight);

    var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
    directionalLight.position.set(1,2,1.5);
    scene.add( directionalLight );

    controls = new THREE.OrbitControls(camera);
    controls.target = new THREE.Vector3(3, 0, 0)
    controls.update();
    controls.autoRotate = true;
    controls.autoRotateSpeed = 5.0
    		
    var axis = new THREE.AxesHelper(1000);
    scene.add(axis);

    var material = new THREE.MeshPhongMaterial({color:'#b090b0'});
    var geometry = new THREE.BoxGeometry( 1, 1, 1 );
    var mesh = new THREE.Mesh(geometry, material);

    scene.add(mesh);
  }


  function resize() {   
    var aspect = window.innerWidth / window.innerHeight;
    renderer.setSize(window.innerWidth, window.innerHeight);
    camera.aspect = aspect;
    camera.updateProjectionMatrix();
  }

  function animate() {
    requestAnimationFrame(animate);
    controls.update();
    render();
  }

  function render() {
    renderer.render(scene, camera);
  }
})();
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>

<div id="container"></div>