更新记录
1.2.2(2025-11-27) 下载此版本
更新日志
- APP端删除stageApp.vue中无用代码,修复报错:THREE.GLTFLoader: setMeshoptDecoder must be called before loading compressed files
1.2.1(2025-11-19) 下载此版本
更新日志
- 修复h5环境下screenshot截图之后不再渲染问题
1.2.0(2025-11-18) 下载此版本
更新日志
-
修复readme中示例代码错误问题
// import useThreeJs from "@/components/threeJs/index.js"; old // new import { useThreeJs } from "@/components/threeJs/index.js"; -
loader模型加载方法兼容模型url后缀大写的情况
平台兼容性
uni-app(4.45)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| √ | √ | √ | √ | √ | × | √ | √ | - |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|
| √ | × | √ | × | × | × | - | × | × | × | × |
其他
| 多语言 | 暗黑模式 | 宽屏模式 |
|---|---|---|
| × | × | √ |
简介
此组件仅为快速展示 3D 模型的 demo。 使用 vue3 + three-platformize 实现。 h5 和 app 用的是官方的 threejs,app 是基于 renderjs 实现的 目前仅支持微信小程序、抖音小程序,其他小程序可以参考代码修改,或者查看 three-platformize 官网
相关库 github 地址 vue、three.js、three-platformize
使用说明
建议先下载示例代码运行试试,且内部包含完整组件,可直接使用
插件命名原则导致需要组件拼接一个前缀,请直接下载示例工程
- 将 components 下的 threeJs 组件移动到自己项目 components 下
- 如果是字节小程序需要把 patches 目录复制到自己项目根目录,在 package.json 下 scripts 添加"postinstall": "patch-package",
- 下载 相关依赖
npm i three-platformize@1.133.3 three
- 引入到组件和相关工具方法
<template>
<view class="container">
<!-- onload事件代表初始化完毕,此时可以加载模型等操作 -->
<Stage @onload="onload"> </Stage>
</view>
</template>
import * as THREE from "three-platformize";
// Vue2请使用同目录下的stage-v2.vue
import Stage from "@/components/threeJs/stage.vue";
import { useThreeJs } from "@/components/threeJs/index.js";
import { ref, getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();
const {
getInstance, // 获取renderer、camera、controls、等实例
centerAndScale, // 用于调整模型大小和位置
setEnv, // 设置环境贴图
screenshot, // 截图
fixFbxMaterial, // 如果fbx模型不能正常显示的话就执行这个方法,接受fbx模型对于的Object3D
} = useThreeJs();
const loadModel = () => {
const { group, loader } = getInstance();
// 如果是.gltf+.bin+texture 请手动转成glb,相关nodejs代码可以参考使用说明
loader
.load(proxy.$getUrl("/DamagedHelmet.gltf.glb"), (p) => {
// 这里是加载进度
// 需要后台正确的设置Content-Length才会有进度
})
.then((gltf) => {
group.add(gltf.scene);
// 居中并缩放到合适的位置
centerAndScale();
});
};
// 初始化完毕,开始设置环境贴图,并加载模型
const onload = async () => {
console.log("onload");
const { renderer } = getInstance();
// oppo find x8
// THREE.WebGLRenderer: OES_texture_half_float extension not supported. supportsHalfFloatTextures
// #ifdef MP
const isSupport = !!renderer.extensions.get("OES_texture_half_float");
// #endif
// #ifndef MP
const isSupport = true;
// #endif
if (isSupport) {
// 设置环境贴图,传入hdr文件在线地址即可,注意自己网页版跨域问题
await setEnv(proxy.$getUrl("/studio_small_09_1k.hdr"), false);
} else {
await setEnv(proxy.$getUrl("/de_balie-compress.jpg"), false);
}
// 加载模型逻辑
loadModel();
};
工具相关
- obj 转 glb
# 先安装obj2gltf
npm i obj2gltf
// nodejs
const obj2gltf = require("obj2gltf");
const fs = require("fs");
const path = require("path");
(async () => {
// obj 2 glb
const outPath = "需要导出的路径";
const inPath = "需要转换的obj文件路径";
const options = {
binary: true, // 不传默认导出gltf
};
const glb = await obj2gltf(inPath, options);
const outFile = path.join(
outPath,
`newmodel.${options.binary ? "glb" : "gltf"}`
);
if (options.binary) {
fs.writeFileSync(outFile, glb);
} else {
const data = Buffer.from(JSON.stringify(glb));
fs.writeFileSync(outFile, data);
}
})();
- gltf 转 glb
# 先安装gltf-pipeline
npm i gltf-pipeline
// nodejs
const gltfPipeline = require("gltf-pipeline");
const fsExtra = require("fs-extra");
const path = require("path");
(() => {
// gltf 2 glb
const outPath = "需要导出的路径";
const inPath = "需要转换的obj文件路径";
const gltfToGlb = gltfPipeline.gltfToGlb;
const gltf = fsExtra.readJsonSync(inPath);
const dir = path.dirname(inPath);
const options = { resourceDirectory: dir };
gltfToGlb(gltf, options).then(function (results) {
const outFile = path.join(outPath, `newmodel.glb`);
fsExtra.writeFileSync(outFile, results.glb);
});
})();
- glb 压缩(这种方式兼容各个平台),参考文档
# 先安装gltfpack
npm i -g gltfpack
# 执行cmd命令
gltfpack -i model.glb -o out.glb -cc
常见问题
-
ios 端 bounce 特性问题
主要是会与 OrbitControls 控制器冲突
// 在ios端即使页面尺寸刚好是100vw * 100vh 没有滚动条情况,也会出现拖动回弹的情况(就是页面可以拖动然后松开手指回到初始为止)
// 这时在pages.json中对应的page添加disableScroll即可
{
"pages": [
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "threeJs",
"navigationStyle": "custom", // 隐藏原生导航栏
"enablePullDownRefresh": false, // 禁止下拉刷新
"disableScroll": true // 在内容刚好沾满屏幕的时候,ios设备可以拖动然后立马回弹,有些情况不是很好用。禁止ios页面回弹(bounce效果)
}
}
]
}
-
在抖音小程序上使用本插件
- three-platformize 的 BytePlatform 代码中有个 bug,导致一直报错无法使用控制器
// 主要是修改/node_modules/three-platformize/src/BytePlatform/index.js 文件
// 找到dispatchTouchEvent方法,在if (changedTouches.length)上面加一行 const changedTouches = e.changedTouches;
dispatchTouchEvent(e = {}) {
const target = { ...this };
const event = {
changedTouches: e.changedTouches.map(touch => new Touch(touch)),
touches: e.touches.map(touch => new Touch(touch)),
targetTouches: Array.prototype.slice.call(
e.touches.map(touch => new Touch(touch)),
),
timeStamp: e.timeStamp,
target: target,
currentTarget: target,
type: e.type,
cancelBubble: false,
cancelable: false,
};
this.canvas.dispatchEvent(event);
const changedTouches = e.changedTouches; // 新加的
if (changedTouches.length) {
const touch = changedTouches[0];
const pointerEvent = {
pageX: touch.pageX,
pageY: touch.pageY,
pointerId: touch.identifier,
type: {
touchstart: 'pointerdown',
touchmove: 'pointermove',
touchend: 'pointerup',
}[e.type],
pointerType: 'touch',
};
this.canvas.dispatchEvent(pointerEvent);
}
}
- 如果你确定使用本插件,请使用 patch-package 给 three-platformize 打补丁(主要是解决重新下载 node_modules 会自动添加上次修改的内容)
# 1. 安装patch-package
npm install patch-package -D
# 2. 在package.json中增加脚本(每次npm i的时候会自动执行postinstall): "postinstall": "patch-package"
# 3. 创建补丁,会在文件目录中生成一个patches文件夹
npx patch-package three-platformize
- 如果你走到了这里说明那你成功了一大半了,我还要告诉你一些其他的问题
- 开发者工具抖音基础库 3.x.x.x 不会显示模型,具体需要你自己测试
- 开发者工具抖音基础库 2.97.0.2 以上控制器没有效果,但是真机调试没有问题
- 在组件内获取 canvas 开发者工具没有问题,但是真机调试会报错,如果你上传体验版没有问题可以忽略,否则请参考示例项目在组件外获取 canvas

收藏人数:
下载插件并导入HBuilderX
下载示例项目ZIP
赞赏(18)
下载 1077
赞赏 18
下载 11811951
赞赏 1818
赞赏
京公网安备:11010802035340号