微信小程序集成three.js--5.加载外部模型

1.场景演示

小程序集成Three.js,加载外部模型,并执行模型动画

2.源码

(1)引入three.js库

import * as THREE from '../../libs/three.weapp.js'
import gLTF from '../../jsm/loaders/GLTFLoader'
import {
    OrbitControls
} from '../../jsm/controls/OrbitControls'
const app = getApp()

库文件下载及配置看这里icon-default.png?t=N7T8https://blog.csdn.net/weixin_39318421/article/details/128468409

(2)主要源码

 wx.createSelectorQuery()
            .select('#webgl')
            .node()
            .exec((res) => {
                let canvasId = String(res[0].node.id)
                const canvas = THREE.global.registerCanvas(canvasId, res[0].node)
                this.setData({
                    canvasId: canvasId
                })
                //相机
                const camera = new THREE.PerspectiveCamera(70, canvas.width / canvas.height, 1, 1000);
                //创建场景
                const scene = new THREE.Scene();
                scene.background = new THREE.Color(0xffffff);
                const renderer = new THREE.WebGLRenderer({
                    antialias: true
                });
                camera.position.set(10, 10, 10);

                //创建控制器
                const controls = new OrbitControls(camera, renderer.domElement);
                controls.enableDamping = true;
                controls.update();

                //创建光源
                const color = 0xFFFFFF;
                const intensity = 3;
                const light = new THREE.DirectionalLight(color, intensity);
                light.position.set(0, 0, 10);
                scene.add(light);

                //加载模型
                wx.showLoading({
                    title: '模型加载中',
                })
                const gltfLoader = new GLTFLoader()
            
                gltfLoader.load('https://file.balibali.work/2022/12/12/b22efd2840d3a58b80e17c835a772446.glb', (gltf) => {
                    wx.hideLoading({})
                    const model = gltf.scene
                    model.name = 'robot'
                    scene.add(model)
                    var states = ['Idle', 'Walking', 'Running', 'Dance', 'Death', 'Sitting', 'Standing'];
                    var emotes = ['Jump', 'Yes', 'No', 'Wave', 'Punch', 'ThumbsUp'];
                    //将模型绑定到动画混合器里面
                    mixer = new THREE.AnimationMixer(model)
                    actions = {}
                    //获取模型中所有的动画数组
                    gltfActions = gltf
                    animations = gltf.animations
                    console.log(animations)
                    dance = animations[0]
                    danceAction = mixer.clipAction(dance)
                    //通过动画混合器播放模型中的动画
                    danceAction.play()
                })

                //平面
                const planeGeometry = new THREE.PlaneGeometry(200, 150, 10, 10);
                const planeMaterial = new THREE.MeshBasicMaterial({
                    color: 0xc7c7c7,
                    wireframe: true
                });
                planeMaterial.side = THREE.DoubleSide;
                const plane = new THREE.Mesh(planeGeometry, planeMaterial)
                plane.rotation.x = Math.PI / 2
                plane.position.y = 0
                scene.add(plane)

                //辅助线
                const axesHelper = new THREE.AxesHelper(500);
                scene.add(axesHelper)

                renderer.setPixelRatio(wx.getSystemInfoSync().pixelRatio);
                renderer.setSize(canvas.width, canvas.height);

                function render() {
                    canvas.requestAnimationFrame(render);
                    //更新控制器
                    controls.update();
                    //更新动画
                    var time = clock.getDelta()
                    if (mixer) {
                        mixer.update(time)
                    }
                    renderer.render(scene, camera);
                }
                render()
            })

(3)源码解析

模型加载后,通过动画混合器THREE.AnimationMixer将模型中的动画绑定到混合器内。

然后就可以通过混合器中的play()方法,播放模型的动画

这里有个需要注意的地方,就是你的小程序需要配置合法域名,把你的模型文件存储到合法域名内的存储空间,这样,在真机预览时才能加载出来,如果没有配置合法域名,即使勾选了不校验合法域名,在真机预览时,手机上也是不会显示模型的。

源码中的模型文件包含了10多个动画效果,可以通过修改 animations[] 中的序号,展现不同的动画。

3.3D模型下载及ThreeJS演示小程序

微信小程序可以使用 socket.io-client 库来进行 Socket 通信。使用该库可以轻松地在微信小程序中实现实时通信功能。 首先,在微信小程序中引入 socket.io-client 库。可以通过 npm 安装,也可以直接下载并引入。 接着,在需要使用 Socket 通信的页面或组件中,引入 socket.io-client 库,并创建一个 Socket 实例: ```javascript import io from 'socket.io-client'; const socket = io('https://example.com'); socket.on('connect', () => { console.log('连接成功'); }); socket.on('message', (data) => { console.log('收到消息:', data); }); socket.emit('message', 'Hello, world!'); ``` 上面的代码中,我们创建了一个 Socket 实例,连接到了 https://example.com 地址。在连接成功后,会触发 connect 事件。当收到服务器发来的消息时,会触发 message 事件,并将消息数据作为参数传递给回调函数。我们也可以通过 emit 方法向服务器发送消息。 需要注意的是,在微信小程序中,WebSocket 连接默认是不允许跨域的。如果需要连接到不同域名的服务器,需要在微信小程序开发者工具中设置「不校验合法域名、web-view(业务域名)、TLS 版本以及 HTTPS 证书」选项。在上线前,还需要在微信公众平台中设置服务器域名白名单,否则连接会被拒绝。 另外,Socket.io 还提供了一些高级功能,比如房间、命名空间、中间件等。可以参考官方文档学习更多内容。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值