2016-04-02 21 views
2

更新:通過這個插件https://github.com/mayognaise/aframe-mouse-cursor-componentAFRAME - 讓<a-cursor>後續鼠標在桌面上

新來這裏AFRAME解決的問題。我創建了一個場景,我希望用戶能夠在桌面瀏覽器上「點擊」對象,並且我不想花太多時間來混淆現有的外觀控件代碼或創建新組件,所以我看着從https://stackoverflow.com/a/36071100/5132437代碼和設計了一個解決方案,我儘量讓的位置跟隨鼠標的移動:

// code for moving the cursor 
var mouse = { 
    x: 0, 
    y: 0 
}; 
var camera = document.querySelector('#camera').components.camera.camera; 
console.log(camera.position); 

function onMouseMove(event) { 

    mouse.x = (event.clientX/window.innerWidth) * 2 - 1; 
    mouse.y = -(event.clientY/window.innerHeight) * 2 + 1; 

    var vector = new THREE.Vector3(mouse.x, mouse.y, -1); 
    vector.unproject(camera); 
    var dir = vector.sub(camera.position).normalize(); 
    var distance = -camera.position.z/dir.z; 
    var pos = camera.position.clone().add(dir.multiplyScalar(distance)); 
    positionStr = String(pos.x) + " " + String(pos.y) + " " + String(pos.z); 
    document.querySelector('#cursor').setAttribute('position', positionStr); 
} 

window.addEventListener('mousemove', onMouseMove, false); 

這裏的問題是,每一個鼠標移動光標時消失,位置(0,0,0 )。我知道可能存在一個問題,即在一幀中獲取正確的相機數據,但我不確定。

Full jsFiddle here

任何幫助,將不勝感激

+0

因爲插件的文檔中所提到的,此功能現已在A型架通過設置'<一個場景光標V0.6.1 = 「rayOrigin:mouse」>'。 – klaasman

回答