码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-01-18 13:38:48      阅读:1899      评论:0      收藏:0      [点我收藏+]

标签:blog   element   col   return   坐标   attr   world   npoi   世界   

屏幕坐标转世界坐标:

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 = projector.projectVector(world_vector,this.scene.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)  

}; 

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

标签:blog   element   col   return   坐标   attr   world   npoi   世界   

原文地址:https://www.cnblogs.com/brainworld/p/8309415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!