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)  

}; 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM