1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > threejs 将屏幕坐标转换成3d中的坐标。

threejs 将屏幕坐标转换成3d中的坐标。

时间:2023-07-30 12:29:34

相关推荐

threejs 将屏幕坐标转换成3d中的坐标。

将当前屏幕上的坐标转换成3d中的坐标pos 是当前canvas屏幕上的坐标,targetZ 是物体距离原点的距离private get3DPosByCanvasPos (pos: THREE.Vector2, targetZ: number) { let vec = new THREE.Vector3(); // create once and reuselet target = new THREE.Vector3(); // create once and reusevec.set(( pos.x / window.innerWidth ) * 2 - 1,- ( pos.y / window.innerHeight ) * 2 + 1,0.5 );let camera = this.camera;vec.unproject( camera );vec.sub( camera.position ).normalize();var distance = (targetZ - camera.position.z) / vec.z;target.copy( camera.position ).add( vec.multiplyScalar( distance ) );return target;}

如果想让创建的模型以右边界靠近屏幕可以使用

let vec2 = new THREE.Vector2(window.innerWidth, window.innerHeight / 2);let pos = Game3DMgr.i.get3DPosByCanvasPos(vec2, 0.2);// 这里计算了物体所在三维空间的大小// 将计算玩的左边减去所占最大和大小值的一般就是 物体当前体积的一般。this.puteBoundingBox();let halfWidth = (this.basketBg.geometry.boundingBox.max.x - this.basketBg.geometry.boundingBox.min.x) / 2;pos.x -= halfWidth;this.basketBg.position.set(pos.x, pos.y, pos.z);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。