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格式文件,体积小,加载快,非常适合小程序使用

相关推荐
2401_8638014626 分钟前
不同类型的3D文件:简明易懂的指南
3d
木子啊28 分钟前
Uni-app导航栏适配终极避坑指南
uni-app·自定义导航栏·导航栏
新启航光学频率梳29 分钟前
深地钻探用钻杆深孔孔深光学3D轮廓测量-激光频率梳3D轮廓技术
科技·3d·制造
2501_9151063234 分钟前
iOS 如何绕过 ATS 发送请求,iOS调试
android·ios·小程序·https·uni-app·iphone·webview
多恩Stone36 分钟前
【3DV 进阶-12】Trellis.2 数据处理脚本细节
人工智能·pytorch·python·算法·3d·aigc
小宇的天下1 小时前
innovus User Guide---Design Methodology for 3D IC withThrough Silicon Via(11)
3d
esmap1 小时前
Clawdbot与ESMAP数字孪生技术融合分析
人工智能·计算机视觉·3d·ai·js
计算机毕设指导61 小时前
基于微信小程序的非物质文化遗产推广管理系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
说私域1 小时前
基于开源AI智能名片S2B2C商城小程序视角下的消费需求激发与企业机遇挖掘
人工智能·小程序·开源·流量运营·私域运营
我想吃辣条1 小时前
从 0 到 1 开发一款记账小程序的设计与实现
小程序·记账