微信小程序xr-frame实现交互(地月案例)

这篇具有很好参考价值的文章主要介绍了微信小程序xr-frame实现交互(地月案例)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  • 基础知识:

1.轮廓

  • 如果想要与场景中的物体进行互动,比如说点击、拖拽物体,那么这个物体得先拥有一个轮廓才行。
  • 轮廓是一个组件。与某个物体互动,实际上是在与这个物体的轮廓进行互动,轮廓让这个物体在物理世界中拥有了一个分身。
名称 标签属性名 组件数据 备注
球状轮廓 sphere-shape center, radius, autoFit
胶囊体轮廓 capsule-shape center, radius, height, autoFit
长方体轮廓 cube-shape center, size, autoFit
网格模型轮廓 mesh-shape - 自动适配元素下的Mesh和GLTF模型

2.创建轮廓

  • 创建轮廓非常简单,只要为xml标签添加上xxx-shape的属性即可。

例如:一个球体创建了一个球状轮廓

<xr-mesh node-id="mesh-sphere" geometry="sphere" sphere-shape></xr-mesh>

 注:任意一个场景标签都可以创建轮廓

3.轮廓交互

事件名 描述 事件的回调函数 备注
touch-shape 点击轮廓时触发 IShapeTouchEvent 如果有多个物体叠在一起,会点中最上层的。
drag-shape 点击轮廓后,手指不松开的情况下进行拖拽时触发 IShapeDragEvent 只有在先触发touch-shape之后才会触发这个事件,target和touch-shape保持一致
untouch-shape 点击轮廓后,手指松开时触发 IShapeTouchEvent 只有在先触发touch-shape之后才会触发这个事件。

整体代码以及效果:(来源于微信开放文档demo)

  • wxml部分
<xr-scene id="xr-scene" bind:tick="handleTick" bind:ready="handleReady">
  <xr-assets bind:progress="handleAssetsProgress" bind:loaded="handleAssetsLoaded">
    <xr-asset-load type="texture" asset-id="earth-texture" src="https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/2k_earth_daymap.jpeg" />
    <xr-asset-load type="texture" asset-id="moon-texture" src="https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/2k_moon.jpeg" />
    <xr-asset-load type="texture" asset-id="sky" src="https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/dark-cosmos.jpg" />
    <xr-asset-material asset-id="standard-mat" effect="standard" />
    <xr-asset-material asset-id="earth-mat" effect="standard" uniforms="u_baseColorMap: earth-texture" render-queue="501"/>
    <xr-asset-material asset-id="earth-silhouette" effect="simple" uniforms="u_baseColorFactor: 1.0 0.5 0 1.0" states="depthTestWrite: false" render-queue="500"/>
    <xr-asset-material asset-id="moon-mat" effect="standard" uniforms="u_baseColorMap: moon-texture" render-queue="503"/>
    <xr-asset-material asset-id="moon-silhouette" effect="simple" uniforms="u_baseColorFactor: 0.476 0.82 0.957 1.0" states="depthTestWrite: false" render-queue="502"/>
  </xr-assets>
  <xr-env sky-map="sky" is-sky2d/>
  <xr-node>
    <xr-mesh node-id="mesh-earth" position="0 0 0" scale="8 8 8" geometry="sphere" material="earth-mat" bind:touch-shape="handleTouchEarth" bind:untouch-shape="handleUntouchEarth" bind:drag-shape="handleEarthRotation" sphere-shape receive-shadow cast-shadow></xr-mesh>
    <xr-mesh node-id="earth-silhouette" scale="8.15 8.15 8.15" geometry="sphere" material="earth-silhouette" visible="{{touchingEarth}}"></xr-mesh>
    <xr-mesh node-id="mesh-moon" position="10 0 0" scale="1.5 1.5 1.5" rotation="0 90 0" geometry="sphere" material="moon-mat" bind:drag-shape="handleDragMoon" bind:touch-shape="handleTouchMoon" bind:untouch-shape="handleUntouchMoon" sphere-shape="radius: 1.5" receive-shadow cast-shadow>
    <xr-mesh node-id="moon-silhouette" scale="1.1 1.1 1.1" geometry="sphere" material="moon-silhouette" visible="{{touchingMoon}}"></xr-mesh>
    </xr-mesh>
    <xr-camera
      id="camera" node-id="camera" position="0 20 -35" clear-color="0 0 0 1"
      target="mesh-earth"
      background="skybox"
    ></xr-camera>
  </xr-node>
  <xr-node node-id="lights">
    <xr-light type="ambient" color="1 1 1" intensity="0.1" />
    <xr-light id="directional-light" type="directional" rotation="0 60 0" color="1 1 1" intensity="5"  shadow-distance="40" cast-shadow shadow-bias="0.004"/>
  </xr-node>
</xr-scene>
  • js部分
Component({
  properties: {
    a: Number,
  },
  data: {
    loaded: false,
    touchingMoon: false,
    touchingEarth: false,
    θ: Math.PI,
    r: 10,
    ω: 5e-4,
    outerRing: 20,
    innerRing: 10
  },
  lifetimes: {},
  methods: {
    handleReady({detail}) {
      const xrScene = this.scene = detail.value;
      console.log('xr-scene', xrScene);
    },
    handleAssetsProgress: function({detail}) {
      console.log('assets progress', detail.value);
    },
    handleAssetsLoaded: function({detail}) {
      console.log('assets loaded', detail.value);
      this.setData({loaded: true});
    },
    handleTouchEarth: function() {
      this.setData({
        touchingEarth: true
      });
    },
    handleUntouchEarth: function() {
      this.setData({
        touchingEarth: false
      });
    },
    handleEarthRotation: function({detail}) {
        const { target, deltaX } = detail.value;
        target._components.transform.rotation.y += deltaX / 100;
    },
    handleDragMoon: function({detail}) {
        const { dir, target, camera } = detail.value;
        const cameraPos = camera.el._components.transform.worldPosition;
        const k = -cameraPos.y / dir[1];
        const x = cameraPos.x + k * dir[0];
        const z = cameraPos.z + k * dir[2];
        const len = Math.sqrt(x * x + z * z);
        if (len > this.data.innerRing) {
            const transform = target._components.transform;
            const scale = len > this.data.outerRing ? this.data.outerRing / len : 1.0;
            transform.position.x = x * scale;
            transform.position.z = z * scale;
        }
    },
    handleTouchMoon: function() {
        this.setData({touchingMoon: true});
    },
    handleUntouchMoon: function() {
        const moon = this.scene.getNodeById("mesh-moon");
        const transform = moon.el._components.transform;
        const x = transform.position.x;
        const z = transform.position.z;
        const len = Math.sqrt(x * x + z * z);
        this.setData({
            r: len,
            θ: x < 0 ? Math.atan(z / x) + Math.PI : Math.atan(z / x),
            ω: Math.sqrt(2.5e-4 / (len * len * len))
        });
        this.setData({touchingMoon: false});
    },
    handleTick: function({detail}) {
        if (this.data.touchingMoon || !this.scene) return;
        const deltaTime = detail.value;
        const moon = this.scene.getNodeById("mesh-moon");
        const transform = moon.el._components.transform;
        const x = Math.cos(this.data.θ) * this.data.r;
        const z = Math.sin(this.data.θ) * this.data.r;
        transform.position.x = x;
        transform.position.z = z;
        transform.rotation.y -= this.data.ω * deltaTime;
        this.setData({
            θ: this.data.θ + this.data.ω * deltaTime
        });
    }
  }
})
  • 效果展示: 

微信小程序xr-frame实现交互(地月案例),xr,微信小程序,交互文章来源地址https://www.toymoban.com/news/detail-592610.html

到了这里,关于微信小程序xr-frame实现交互(地月案例)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • XR-FRAME 开始

    xr-frame 是一套小程序官方提供的XR/3D应用解决方案,基于混合方案实现,性能逼近原生、效果好、易用、强扩展、渐进式、遵循小程序开发标准。 在这一章中,我们将会带大家从头开始,用它构建一个XR小程序。 首先创建项目,让我们选择小程序工程: 之后先在 app.json 加上

    2024年02月12日
    浏览(44)
  • 【微信小程序】通过绑定点击事件来实现点击交互

    在微信小程序中,可以通过绑定点击事件来实现点击交互。以下是点击事件的实现步骤: 在WXML文件中,找到需要绑定点击事件的元素,例如按钮、图片等。 在该元素上添加 bindtap 属性,并指定一个对应的事件处理函数,例如: 在对应的页面或组件的JS文件中,定义事件处理

    2024年02月14日
    浏览(36)
  • 微信小程序适配iphoneX,XR,12及以上,获取底部安全区域

    iphone x及以上的异形屏top高度为44px(或以上),非异形屏为20px。 先判断是否为异形屏,如果是,获取到底部小黑条区域的高度。在页面设置底边距padding-bottom/margin-bottom,把内容区域隔开。 小黑条区域高度=屏幕高度-安全区域的bottom值,安全区域指的是内容可见区域 注意小

    2024年03月11日
    浏览(58)
  • 微信小程序——后台交互

    目录 后台准备 pom.xml 配置数据源 整合mtbatis 前后端交互  method1  method2 生成mapper接口、model实体类以及mapper映射文件 启动类 然后启动后台即可 首先在index.js中编写以下方法 然后在该页面下方生命周期函数——监听页面加载代码块下编写以下方法 由于后台是没有数据图片的,

    2024年02月06日
    浏览(35)
  • 微信小程序的页面交互2

    (1)定义: 微信小程序中的自定义属性实际上是由data-前缀加上一个自定义属性名组成。 (2)如何获取自定义属性的值? 用到target或currentTarget对象的dataset属性可以获取数据 (3)注意 :无论你在JavaScript代码中如何命名data对象中的属性(包括是否使用驼峰命名法),在W

    2024年04月26日
    浏览(24)
  • 微信小程序的页面交互1

    每个页面的s代码全部写入对应的js文件的page()函数里面。点击编译,就可以显示js代码的运行效果。注意,每个页面的page()函数是 唯一 的。 page()函数格式如下: 就是: 加载-》渲染-》销毁 的过程。 (1)每个页面都有生命周期。如果想要在某个特定的时机进行特定

    2024年04月12日
    浏览(22)
  • 微信小程序前后端数据交互

         目录 一 微信小程序发送请求 二 后端接口接受小程序请求并返回数据  三 最后的效果图     先简单说一下我写的这个微信小程序前后端交互的业务,主要是两个数据:supplyCount和wantBuyCount,分别代表我的车源和我的求购。目前的需求就是小程序向后端发送请求,然后

    2024年02月09日
    浏览(27)
  • 微信小程序之后台首页交互

    目录 一.与后台数据进行交互request封装 后台准备 测试结果 ​编辑   前端  测试结果  二.wxs的介绍以及入门  测试结果 后台准备 pom.xml文件编写 建立数据表 建立数据请求地址类  定义接口类  测试结果    前端 先关闭mock    先编写url地址  编写utils.js 编写index.js   编写

    2024年02月08日
    浏览(34)
  • “编辑微信小程序与后台数据交互与微信小程序wxs的使用“

    在现代移动应用开发中,微信小程序已经成为了一个非常流行和广泛使用的平台。为了使小程序能够展示丰富的内容和实现复杂的功能,与后台数据的交互是至关重要的。同时,微信小程序还提供了一种特殊的脚本语言——wxs,用于增强小程序的业务逻辑处理能力。本篇博客

    2024年02月08日
    浏览(35)
  • 微信小程序开发之微信小程序交互

    目录 一、小程序交互 前端: 1、先在登陆界面中编写代码 2、在前端中编写js代码 后端:           1、先导入依赖:           2、定义好配置文件           3、编写好实体类           4、将帮助类进行配置           5、编写mapper类           6、定义service层以及对应的

    2024年02月09日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包