1. 程式人生 > >threejs 世界坐標與屏幕坐標相互轉換

threejs 世界坐標與屏幕坐標相互轉換

log style new t hal dom camera element his 互轉

屏幕坐標轉世界坐標:

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 世界坐標與屏幕坐標相互轉換