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

相关推荐
游戏开发爱好者83 小时前
iOS 商店上架全流程解析 从工程准备到审核通过的系统化实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone
toooooop83 小时前
Vuex 中 state、mutations 和 actions 的原理和写法
前端·javascript·uni-app
hyswl6663 小时前
2025年开发小程序公司推荐
python·小程序
kdniao13 小时前
电商平台与小程序与快递鸟物流轨迹API
小程序
项目題供诗3 小时前
微信小程序黑马优购(项目)(一)
微信小程序·小程序
项目題供诗3 小时前
微信小程序黑马优购(项目)(三)
微信小程序·小程序
林_xi4 小时前
uniapp使用@uni-ku/root插件实现全局组件
前端·uni-app
计算机毕设定制辅导-无忧学长4 小时前
基于uni-app的“民族风韵”特色购物小程序
uni-app
一个处女座的程序猿O(∩_∩)O4 小时前
UniApp 生命周期全解析:从应用到页面,再到组件的完美协奏曲
前端·uni-app
hyswl6665 小时前
2025年郑州开发小程序公司推荐
python·小程序