king2088/vue-3d-loader
关于
VueJS and threeJS 3d viewer plugin
网站与应用开发可以做什么
- On-demand rendering (pauses when the page/container is not visible)
- Lazy-loaded model loaders (three addons are code-split)
- Depends on three >= 0.160 as a peer dependency
实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
readme_CN.md
原文vue-3d-loader
vueJS + threeJS整合的一个3d展示组件。
支持.dae/.fbx/.gltf/.glb/.obj/.ply/.stl/.json,并支持同一个场景导入多个不同3D模型,支持mtl材质以及jpg/png等图片纹理
文档:https://king2088.github.io/vue-3d-loader-docs/zh/
演示动画

vue3请安装2.0.0及以上版本,vue2请安装1.x.x版本
功能支持列表
- 加载单个3D模型
- 同时加载多个3D模型
- 同时加载多个不同类型3D模型
- 加载Draco压缩gltf模型(使用方法请查看API)
- 支持自定义文件类型(用于无文件后缀名url)
- 设置场景宽高
- 设置材质及纹理
- 交互控制
- 鼠标事件
- 灯光
- 相机位置及旋转
- 添加标注点
安装
npm i vue-3d-loader -S
或
yarn add vue-3d-loader
使用
全局使用,在入口文件中全局安装,代码如下:
/* vue2 */
import vue3dLoader from "vue-3d-loader";
Vue.use(vue3dLoader)
/* vue3 */
import vue3dLoader from "vue-3d-loader";
createApp(App).use(vue3dLoader).mount('#app')
非全局使用,在Vue文件中使用如下代码:
import { vue3dLoader } from "vue-3d-loader"; // 注意 vue3dLoader 写在 {...} 内
在组件中使用标签<vue3dLoader></vue3dLoader>
<vue3dLoader
:height="200"
:showFps="true"
:filePath="['/fbx/1.fbx', '/obj/2.obj', '/gltf/3.gltf']"
:mtlPath="[null, '/obj/2.mtl', null]"
:backgroundColor="0xff00ff"
></vue3dLoader>
API
属性
| 属性名 | 类型 | 默认值 | 值 | 描述 |
|---|---|---|---|---|
| filePath | string | array | - |
|
文件路径,支持多个文件一起加载,注意:如果每个文件对应一个材质,需要将材质mtlPath设置为数组。图片纹理也一样需要设置textureImage为数组 |
| fileType | string | array | - |
|
文件类型,主要用于无法判断3d模型文件后缀名的url地址,与filePath一起使用,当filePath为数组时,此选项必须为数组 |
| mtlPath | string | array | - |
|
.mtl材质路径,支持多个材质一起加载,设置此参数为数组,必须设置filePath为数组 |
| textureImage | string | array | - |
|
jpg/png纹理加载,与filePath一一对应,为数组时,必须设置filePath为数组 |
| width | number | parent element width | 100 | 场景宽度 |
| height | number | parent element height | 100 | 场景高度 |
| position | object | array | {x:0, y:0, z:0} |
|
模型位置坐标,filePath类型为array时可用array |
| rotation | object | array | {x:0, y:0, z:0} |
|
模型旋转坐标,filePath类型为array时可用array |
| cameraPosition | object | {x:0, y:0, z:0} |
|
摄像机位置坐标 |
| cameraRotation | object | {x:0, y:0, z:0} |
|
摄像机旋转坐标 |
| scale | object | array | {x:1, y:1, z:1} |
|
模型比例,filePath类型为array时可用array |
| lights | array | [{ type: "AmbientLight", color: 0xaaaaaa, }, { type: "DirectionalLight", position: { x: 1, y: 1, z: 1 }, color: 0xffffff, intensity: 0.8, }] |
|
灯光为数组,type为 环境光(AmbientLight) | 方向光(DirectionalLight) | 点光(PointLight) | 半球光(HemisphereLight) |
| backgroundColor | number | string | 0xffffff |
|
场景背景色 |
| backgroundAlpha | number | 1 |
|
场景背景透明度, 值范围0-1 |
| controlsOptions | object | - | - | |
| crossOrigin | string | anonymous | anonymous | use-credentials | 跨域配置 |
| requestHeader | object | anonymous |
|
设置请求头 |
| outputEncoding | string | sRGB | liner 或 sRGB | sRGB 为默认值,即 SRGBColorSpace,能更好的还原材质颜色;linear 即 LinearSRGBColorSpace 线性编码。 渲染器的输出颜色空间 WebGLRenderer outputColorSpace(three r152+) |
| webGLRendererOptions | object | { antialias: true, alpha: true } | - | WebGLRenderer可选参数 WebGLRenderer Parameters |
| showFps | boolean | false | - | 显示FPS等信息 |
| clearScene | boolean | false | - | 清空场景中的内容 |
| parallelLoad | boolean | false | - | 开启/关闭并行加载模型(仅在多模型下有效)注意:使用此属性后,load进度process事件将不可预测 |
| labels | array | - |
|
添加图片/文字标签,设置image则显示图片标签。设置text显示文字标签,文字样式通过textStyle进行设置,样例可以查看examples/add-label.vue文件 |
| autoPlay | boolean | true | - | 播放/停止动画 |
| enableDraco | boolean | false | - | 加载Gltf draco模型需开启draco解密,开启后需要下载draco解密库放到默认目录assets中,默认目录为assets/draco/gltf/,如需修改默认目录,请查看参数dracoDir。关于draco请查阅 |
| dracoDir | string | assets/draco/gltf/ | - | draco解密库默认目录,可自行修改 |
| intersectRecursive | boolean | false | - | 若为true,则同时也会检测所有物体的后代。否则将只会检测对象本身的相交部分。 |
| enableDamping | boolean | false | - | enableDamping设置为true以启用阻尼(惯性),这将给控制器带来重量感。 |
| dampingFactor | number | - | - | enableDamping必须为true,此选项才有效。dampingFactor用于设置阻尼惯性大小。 |
| verticalCtrl | boolean | object | false | - | 为true,仅能垂直旋转相机。 |
| horizontalCtrl | boolean | object | false | - | 为true,仅能水平旋转相机。 |
| plyMaterial | string | MeshStandardMaterial | - | 值仅有两个 |
| enableAxesHelper | boolean | false | - | 开启并在场景中显示x,y,z轴 |
| axesHelperSize | number | 100 | - | x,y,z轴的尺寸 |
| enableGridHelper | boolean | false | - | 开启并在场景中显示网格 |
| minDistance | float | 0 | - | 你能够将相机向内移动多少,其默认值为0 |
| maxDistance | float | Infinity | - | 你能够将相机向外移动多少,其默认值为Infinity |
| pointLightFollowCamera | boolean | false | - | 开启点光源跟随相机 |
事件
| event | description |
|---|---|
| mousedown(event, intersects) | 鼠标按下, intersect:当前相交最近的物体 |
| mousemove(event, intersects) | 鼠标移动, intersect:当前相交最近的物体 |
| mouseup(event, intersects) | 鼠标放开, intersect:当前相交最近的物体 |
| click(event, intersects) | 点击, intersect:当前相交最近的物体 |
| load | 加载模型事件 |
| process(event, fileIndex) | 加载进度, fileIndex:当前加载模型的索引 |
| error(event) | 错误事件 |
使用样例
1. 加载一个3D模型
目前支持dae/fbx/gltf(glb)/obj/ply/stl中任意一种
<!-- 加载fbx模型 -->
<vue3dLoader filePath="models/collada/stormtrooper/stormtrooper.dae"></vue3dLoader>
<!-- 加载obj模型 -->
<vue3dLoader filePath="/obj/1.obj"></vue3dLoader>
2. 同一个场景中加载多个模型
<!--
可同时加载多个不同种类的模型,
支持单独设置每一个模型的位置/缩放/旋转
-->
<template>
<div class="check-box">
<input type="checkbox" @change="change($event, 'position')" checked /> Set
position
<input type="checkbox" @change="change($event, 'rotation')" checked /> Set
rotation
<input type="checkbox" @change="change($event, 'scale')" checked /> Set
scale
</div>
<vue3dLoader
:filePath="filePath"
:position="position"
:rotation="rotation"
:scale="scale"
:cameraPosition="{ x: -0, y: 0, z: -500 }"
/>
</template>
<script setup lang="ts">
import { ref } from "vue";
const filePath = ref();
filePath.value = [
"/models/fbx/Samba Dancing.fbx",
"/models/collada/pump/pump.dae",
];
const position = ref();
position.value = [
{ x: 0, y: 0, z: 0 },
{ x: 100, y: 100, z: 100 },
];
const rotation = ref();
rotation.value = [
{ x: 0, y: 0, z: 0 },
{ x: 10, y: 1, z: 1 },
];
const scale = ref();
scale.value = [
{ x: 0.4, y: 0.4, z: 0.4 },
{ x: 0.8, y: 0.8, z: 0.8 },
];
function change(event: any, type: string) {
const value = event.target.checked;
switch (type) {
case "position":
value
? (position.value = [
{ x: 0, y: 0, z: 0 },
{ x: 100, y: 100, z: 100 },
])
: (position.value = []);
break;
case "rotation":
value
? (rotation.value = [
{ x: 0, y: 0, z: 0 },
{ x: 10, y: 1, z: 1 },
])
: (rotation.value = []);
break;
case "scale":
value
? (scale.value = [
{ x: 0.4, y: 0.4, z: 0.4 },
{ x: 0.8, y: 0.8, z: 0.8 },
])
: (scale.value = []);
break;
}
}
</script>
3. 材质及纹理加载
<!-- obj加载mtl材质 -->
<vue3dLoader filePath="/obj/1.obj" mtlPath="/obj/1.mtl" ></vue3dLoader>
<!-- fbx图片纹理加载 -->
<vue3dLoader filePath="/fbx/1.fbx" textureImage="/fbx/1.png" ></vue3dLoader>
4. 背景颜色及透明度
<vue3dLoader filePath="/fbx/1.fbx" :backgroundAlpha="0.5" backgroundColor="red"></vue3dLoader>
5. 交互控制controls
<template>
<div class="controls">
<div class="buttons">
<!-- 禁止右键拖动 -->
<button @click="enablePan = !enablePan">
{{ enablePan ? "disable" : "enable" }} translation
</button>
<!-- 禁止缩放 -->
<button @click="enableZoom = !enableZoom">
{{ enableZoom ? "disable" : "enable" }} zoom
</button>
<!-- 禁止旋转 -->
<button @click="enableRotate = !enableRotate">
{{ enableRotate ? "disable" : "enable" }} rotation
</button>
</div>
<vue3dLoader
:filePath="'/models/collada/elf/elf.dae'"
:controlsOptions="{
enablePan,
enableZoom,
enableRotate,
}"
:cameraPosition="{ x: 0, y: -10, z: 13 }"
/>
</div>
</template>
<script setup lang="ts">
import { ref } from "vue";
const enablePan = ref(true);
const enableZoom = ref(true);
const enableRotate = ref(true);
</script>
6. 旋转模型
<template>
<vue3dLoader
:rotation="rotation"
@load="onLoad()"
filePath="/models/collada/elf/elf.dae"
/>
</template>
<script setup lang="ts">
import { ref } from "vue";
const rotation = ref();
rotation.value = {
x: -Math.PI / 2,
y: 0,
z: 0,
};
function onLoad() {
rotate();
}
function rotate() {
requestAnimationFrame(rotate);
rotation.value.z -= 0.01;
}
</script>
7. 事件
<template>
<vue3dLoader filePath="/models/ply/Lucy100k.ply" @mousemove="onMouseMove" />
</template>
<script setup lang="ts">
import { ref } from "vue";
const object = ref(null);
function onMouseMove(event: MouseEvent, intersected: any) {
if (object.value) {
(object.value as any).material.color.setStyle("#fff");
}
if (intersected) {
object.value = intersected.object;
(object.value as any).material.color.setStyle("#13ce66");
}
}
</script>
8. 加载draco模型
此功能需要先下载draco库存储与本地项目的静态文件夹内,才可以正常加载,下载地址:https://github.com/king2088/vue-3d-loader/blob/master/public/assets/draco.7z
<template>
<vue3dLoader
filePath="/models/gltf/LittlestTokyo.glb"
:cameraPosition="{ x: 10, y: 700, z: 1000 }"
:enableDraco="true"
dracoDir="/draco/"
outputEncoding="sRGB"
/>
</template>
9. 更多演示
Docker部署examples
docker build -t vue/vue-3d-loader .
# 运行docker
docker run -p 8010:80 vue/vue-3d-loader
即将到来
- 支持Vue3
bug提交
捐助


感谢
本插件参考vue-3d-model实现
探索相关项目
加载中…