Skip to content

Instantly share code, notes, and snippets.

@ayamflow
Forked from kenjiSpecial/screenToWorldAtZ.js
Created February 25, 2016 23:53
Show Gist options
  • Save ayamflow/131b3eff88a39da25773 to your computer and use it in GitHub Desktop.
Save ayamflow/131b3eff88a39da25773 to your computer and use it in GitHub Desktop.
function screenToWorldAtZ(positionX, positionY, z){
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 );
var camera = App.experience.curCamera;
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