日常记录 vue three 屏幕坐标转世界坐标

11 篇文章 1 订阅

data中添加 mouse:null,

onDocumentMouseDown(event) {
      //屏幕坐标系转换为three.js坐标系  // 屏幕坐标为event.clientX   标准设备坐标event.clientX / window.innerWidth) * 2 - 1  //标准设备坐标(z=0.5这个值并没有一个具体的说法)
      let vector = new THREE.Vector3(
        (event.clientX / window.innerWidth) * 2 - 1,
        -(event.clientY / window.innerHeight) * 2 + 1,
        0.5
      )
      // 通过unproject方法,可以将标准设备坐标转世界坐标
      vector = vector.unproject(this.camera)
      // 进行剩下操作,比如判断鼠标是否选中某个物体
      console.log(vector)
      // 创建光线投射对象
      const raycaster = new THREE.Raycaster(
        this.camera.position,
        vector.sub(this.camera.position).normalize()
      )

      //射线只检查指定的成员-球体、圆柱、方块
      /*
      const intersects = raycaster.intersectObjects([
        this.sphere,
        //this.cylinder,
        this.cube
      ])
*/
      const intersects = raycaster.intersectObjects(this.scene.children, true)//射线检查所有指定对象的成员
      // 设置选中对象透明度为0.1
      if (intersects.length > 0) {
        intersects[0].object.material.transparent = true
        intersects[0].object.material.opacity = 0.1
        console.log('123',intersects[0].point)
        console.log(vector)
      }
    },

在mounted中添加鼠标点击事件

window.addEventListener('click', this.onDocumentMouseDown)//鼠标点击时间
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北漂研究生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值