2014-11-04 73 views
0

大概只是在這裏丟失了一些簡單的東西。我有一個聚光燈指向相機後面的幾何平面,還有一些隨機放置的盒子。只是試圖做一個簡單的自上而下像演示。我在WASD上翻譯相機,複製位置,將Y設置爲零,然後讓相機查看該位置,然後將光線的目標轉換到該位置。相機移動正常,但光線不會改變目標。至少從我所知道的。Three.JS翻譯目標

創建我的光&目標:

this.playerLight = new THREE.SpotLight(0xffffff); 
this.playerLight.castShadow = true; 
this.playerLight.position.set(0, 40, 0); 
this.spotlightTarget = new THREE.Object3D(); 
this.spotlightTarget.position.set(0, 0, 0); 
this.playerLight.target = this.spotlightTarget; 
this.playerLight.shadowCameraVisible = true; 
this.scene.add(this.playerLight); 

然後我keydown事件處理:

window.addEventListener("keydown", function (e) { 
    var moved = false; 
    switch (event.keyCode) { 
    case 87: // W 
     e.preventDefault(); 
     _this.camera.position.x -= 0.2; 
     moved = true; 
     break; 
    case 65: // A 
     e.preventDefault(); 
     _this.camera.position.z += 0.2; 
     moved = true; 
     break; 
    case 83: // S 
     e.preventDefault(); 
     _this.camera.position.x += 0.2; 
     moved = true; 
     break; 
    case 68: // D 
     e.preventDefault(); 
     _this.camera.position.z -= 0.2; 
     moved = true; 
     break; 
    default: 
     return true; 
    } 

    if (moved) { 
    var lookAtPos = _this.camera.position.clone(); 
    lookAtPos.y = 0; 
    _this.camera.lookAt(lookAtPos); 
    _this.playerLight.position.x = lookAtPos.x; 
    _this.playerLight.position.z = lookAtPos.z; 
    _this.spotlightTarget.position.set(lookAtPos.x, lookAtPos.y, lookAtPos.z); 
    } 
}, false); 

任何想法?

+0

是否使用three.js所r.69?如果是這樣,它可能是https://github.com/mrdoob/three.js/issues/5555。 – WestLangley 2014-11-04 03:54:05

+0

是的,我正在使用r69。那麼當時的工作是否也將目標添加到現場? – agmcleod 2014-11-04 13:37:52

+0

你試過了嗎?它有用嗎? – WestLangley 2014-11-04 15:31:22

回答

1

您的聚光燈目標已被翻譯,但沒有關注聚光燈。

Spotlight.target是光的屬性,但它不是在three.js r.69場景圖的一部分。

因此,不幸的是,target.matrixtarget.matrixWorld沒有被更新。

解決方法是將spotlight.target添加到現場。另一個解決辦法是設置對象已在你的場景作爲目標:

spotLight.target = myObject; 

three.js所r.69