编程语言
首页 > 编程语言> > javascript-使用VRControls时无法更改相机位置

javascript-使用VRControls时无法更改相机位置

作者:互联网

我有以下代码:

...
camera = new THREE.PerspectiveCamera(75, screenRatio, 1, 10000 );
camera.position.z = -10; // position.set(0, 0, -10) also not working.
controls = new THREE.VRControls( camera );
effect = new THREE.VREffect( renderer );
effect.setSize( window.innerWidth, window.innerHeight );
...

VRControls正在与加速度计同步工作,但是我无法更改相机位置.似乎卡在了原点(0,0,0)中.在应用VRControls和VREffect之前,它工作得很好.

解决方法:

Mozilla VR Team demos的Sechelt演示中找到了解决方案.我在这里放一个代码段,以供其他VR初学者参考.

将摄像机添加到组而不是直接更新摄像机位置是移动摄像机的方法.

var scene, renderer, cameraRatio, camera, controls, effect, dolly;

function init() {
    scene = new THREE.Scene();

    renderer = new THREE.WebGLRenderer({ antialias: true });
    renderer.setSize( window.innerWidth, window.innerHeight );
    document.body.appendChild( renderer.domElement );

    cameraRatio = window.innerWidth / window.innerHeight;
    camera = new THREE.PerspectiveCamera( 75, cameraRatio, 1, 1000 );       

    controls = new THREE.VRControls( camera );
    effect = new THREE.VREffect( renderer );
    effect.setSize( window.innerWidth, window.innerHeight );

    // This helps move the camera
    dolly = new THREE.Group();
    dolly.position.set( 0, 0, 0 );
    scene.add( dolly );
    dolly.add( camera );

    ...
    // Of course, there should be lights, objects, etc
}

function animate() {
    dolly.position.x += 0.1;
    controls.update();
    effect.render( scene, camera );
}

init();
animate();

标签:javascript,three-js,virtual-reality
来源: https://codeday.me/bug/20191012/1900094.html