uniApp使用XR-Frame创建3D场景(7)加入点击交互

上篇文章讲述了如何将XR-Frame作为子组件集成到uniApp中使用

这篇我们讲解如何与场景中的模型交互(点击识别)

先看源码

XML 复制代码
<xr-scene render-system="alpha:true" bind:ready="handleReady">
  <xr-node>
    <xr-mesh id="1" node-id="mesh-sphere" position="-0.4 0 -0.4" scale="0.3 0.3 0.3" geometry="sphere" mesh-shape bind:touch-shape="handleTouchModel"  uniforms="u_baseColorFactor:0.3 0.5 0.8 1,u_metallicRoughnessValues: 0.3 0.2"></xr-mesh>
    <xr-mesh id="2" node-id="mesh-sphere2" position="0.4 0 0.4" scale="0.3 0.3 0.3" geometry="sphere" mesh-shape bind:touch-shape="handleTouchModel"  uniforms="u_baseColorFactor:0.5 0.4 0.6 1,u_metallicRoughnessValues: 0.3 0.7"></xr-mesh>
  </xr-node>
  <xr-camera id="camera"  clear-color="0 0 0 0" position="1 1 2" target="mesh-sphere" camera-orbit-control/>
</xr-scene>

这里与前几章的代码没有太大区别

要实现点击物体的交互,关键就是在**<xr-mesh>**中,加入mesh-shape属性。然后在加入绑定事件bind:touch-shape="handleTouchModel"。

当在页面中点击了这个mesh,就会触发handleTouchModel这个回调函数。

我们在index.js文件中编写handleTouchModel的代码

javascript 复制代码
handleTouchModel: function ({detail}) {
      const {target} = detail.value;
      const id = target.id; 
      wx.showToast({
        title: `点击了模型:${id}`,
        icon:'none'
        });
    },

代码中通过获取点击模型的id识别出点击的是哪个物体,然后便可以根据自己业务的需求进一步完善代码。

这里给大家推荐一个微信小程序 3D模型素材库,这个小程序中的模型都是针对小程序优化后的glb格式文件,体积小,加载快,非常适合小程序使用

相关推荐
PedroQue992 小时前
uni-router新推useUniEventChannel,彻底解决页面通信难题
前端·uni-app
蓝速科技4 小时前
蓝速科技视觉 3D 全息仓 AI 数字人一体机深度评测
人工智能·科技·3d
码农客栈13 小时前
小程序学习(二十六)之“订单详情”
小程序
chaoyuanl14 小时前
现有游乐设施 XR 数字化升级改造方案
大数据·科技·3d·xr·娱乐
2601_9571909014 小时前
中小景区升级首选|XR 无轨黑暗乘骑,破解文旅同质化与低复购难题
xr·娱乐
3D小将15 小时前
3D格式转换之3MF模型转GLB通用技术
3d·solidworks模型·ug模型·rhino模型·catia模型
weixin_4400583115 小时前
2026知识付费小程序:年费全包零抽成,录播题库全功能覆盖
小程序·apache·小程序开发
小羊Yveesss17 小时前
2026年微信小程序后端怎么搭建?后台、数据、接口和运维怎么选
运维·微信小程序·小程序
小皮虾17 小时前
小程序首页性能优化实战:从 4 秒到 1.8 秒
前端·微信小程序
韭菜钟18 小时前
利用OpenCV进行3d相机手眼标定(眼在手上/眼在手外)
opencv·3d