2017-04-23 81 views
1

我只是想知道是否有一個內置功能來重置頭部旋轉。 我見過的大多數VR SDK(Oculus,Carboard等)都有一種將當前旋轉設置爲默認正向旋轉的方法。 有沒有一個簡單的方法來完成這一幀?有沒有辦法重新定位/重置方向?

回答

1

您可以在相機周圍添加包裝器實體,並且在事件中,將實體包裝器繞Y軸的旋轉設置爲相機繞Y軸旋轉的負值。

通過@jhsu使用shake.js下面是一個例子(但反應):

import React from 'react'; 
import Camera from './Camera'; 
import Shake from 'shake.js'; 

class Player extends Component { 
    componentDidMount() { 
    this.shaker = new Shake({ 
     threshold: 5, 
     timeout: 250, 
    }); 
    this.shaker.start(); 
    window.addEventListener('shake', this.reorient, false); 
    } 

    reorient() { 
    if (this.camera) { 
     const rotation = this.camera.getAttribute('rotation'); 
     this.setState({ 
     orientation: { x: 0, y: -1 * rotation.y, z: 0 } 
     }); 
    } 
    } 

    cameraMounted = (camera) => { 
    this.camera = camera; 
    } 

    render() { 
    return <Camera 
     rotation={this.state.orientation} 
     onMount={this.cameraMounted} 
    /> 
    } 
} 

的組件是:

AFRAME.registerComponent('resetorientation', { 
    init: function() { 
    var el = this.el; 
    el.addEventListener('resetorientation', function() { 
     var y = el.querySelector('[camera]').getAttribute('rotation').y; 
     el.setAttribute('rotation', {y: -1 * y}); 
    }); 
    } 
}); 

<a-entity id="cameraWrapper" resetorientation><a-camera></a-camera></a-entity> 

document.querySelector('#cameraWrapper').emit('resetorientation'); 
+0

太謝謝你了!這種方法爲我做了。 – makhras

相關問題