-
-
Save floz/9d6c36e8eb5ab5ae867d2c8e0419d8de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function screenToWorldAtZ(positionX, positionY, z, camera){ | |
var vector = new THREE.Vector3(); | |
var dX, dY, dZ; | |
if(this.curObject && this.curObject.parent ){ | |
dX = this.curObject.parent.position.x; | |
dY = this.curObject.parent.position.y; | |
dZ = this.curObject.parent.position.z; | |
}else{ | |
dX = 0; dY = 0, dZ = 0; | |
} | |
vector.set( | |
positionX, | |
positionY, | |
0.5 ); | |
camera.updateProjectionMatrix(); | |
camera.updateMatrixWorld(); | |
vector.unproject( camera ); | |
var dir = vector.sub( camera.position ).normalize(); | |
var distance = (z- camera.position.z) / dir.z; | |
var pos = camera.position.clone().add( dir.multiplyScalar( distance ) ); | |
pos.x -= dX; | |
pos.y -= dY; | |
pos.z -= dZ; | |
return pos; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment