
微信小程序集成three.js--5.加载外部模型
three.js
JavaScript 3D Library.
项目地址:https://gitcode.com/gh_mirrors/th/three.js

·
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()
库文件下载及配置看这里
https://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()
})
three.js
JavaScript 3D Library.
项目地址:https://gitcode.com/gh_mirrors/th/three.js
(3)源码解析
模型加载后,通过动画混合器THREE.AnimationMixer将模型中的动画绑定到混合器内。
然后就可以通过混合器中的play()方法,播放模型的动画
这里有个需要注意的地方,就是你的小程序需要配置合法域名,把你的模型文件存储到合法域名内的存储空间,这样,在真机预览时才能加载出来,如果没有配置合法域名,即使勾选了不校验合法域名,在真机预览时,手机上也是不会显示模型的。
源码中的模型文件包含了10多个动画效果,可以通过修改 animations[] 中的序号,展现不同的动画。
3.3D模型下载及ThreeJS演示小程序
推荐内容
阅读全文
AI总结




JavaScript 3D Library.
最近提交(Master分支:24 天前 )
8a812ee2
Co-authored-by: Attila Schroeder <attila-schroeder.79@gmail.com> 9 小时前
8da96d09
9 小时前
更多推荐
相关推荐
查看更多
three.js

JavaScript 3D Library.
three.js

JavaScript 3D Library.
Three.js模型包

Three.js模型包为开发者提供了丰富的3D模型资源,助力快速上手Three.js网页开发。内含多种示例模型,涵盖不同场景,帮助用户深入理解Three.js的应用技巧。无论是学习还是实践,这些模型都能成为您的得力助手。使用前需具备Three.js基础知识,按照示例代码轻松导入并应用。请注意,本资源仅限学习和研究,严禁商业用途及侵权行为。开启您的3D创作之旅,让这些模型为您的项目增添无限可能!
热门开源项目
活动日历
查看更多
直播时间 2025-04-09 14:34:18

樱花限定季|G-Star校园行&华中师范大学专场
直播时间 2025-04-07 14:51:20

樱花限定季|G-Star校园行&华中农业大学专场
直播时间 2025-03-26 14:30:09

开源工业物联实战!
直播时间 2025-03-25 14:30:17

Heygem.ai数字人超4000颗星火燎原!
直播时间 2025-03-13 18:32:35

全栈自研企业级AI平台:Java核心技术×私有化部署实战
目录
所有评论(0)