threejs 世界坐标与屏幕坐标相互转换

屏幕坐标转世界坐标:

let pX = (screenPoint.x / this.scene.renderer.domElement.clientWidth) * 2 - 1;
let pY = - (screenPoint.y / this.scene.renderer.domElement.clientHeight) * 2 + 1;
 
//通过调用Vector3的unproject()方法(只有vector3能使用)
//注:pX, pY介于 -1 到1之间
let p = new THREE.Vector3(pX, pY, -1).unproject(this.scene.camera)
return  new THREE.Vector2(p.x,p.y);
 
 
世界坐标转屏幕坐标
let projector = new THREE.Projector();  

let world_vector = new THREE.Vector3(0,0,1);  

let vector = world_vector.project(this.camera);
let halfWidth = window.innerWidth / 2,

  halfHeight = window.innerHeight / 2;  

return {  

  x: Math.round(vector.x * halfWidth + halfWidth),  

  y: Math.round(-vector.y * halfHeight + halfHeight)  

}; 

 

posted @ 2018-01-18 11:46  善未易明  阅读(14215)  评论(2编辑  收藏  举报