# vue-3d-loader **Repository Path**: gsang/vue-3d-loader ## Basic Information - **Project Name**: vue-3d-loader - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2025-04-09 - **Last Updated**: 2025-10-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # vue-3d-loader vueJS + threeJS整合的一个3d展示组件。 支持.dae/.fbx/.gltf/.glb/.obj/.ply/.stl/.json,并支持同一个场景导入多个不同3D模型,支持mtl材质以及jpg/png等图片纹理 [![Version](https://img.shields.io/npm/v/vue-3d-loader.svg)](https://www.npmjs.com/package/vue-3d-loader) [![License](https://img.shields.io/npm/l/vue-3d-loader.svg)](https://www.npmjs.com/package/vue-3d-loader) [![Downloads](https://img.shields.io/npm/dt/vue-3d-loader.svg)](https://www.npmjs.com/package/vue-3d-loader) [English](./readme.md) 文档: 演示动画 ![demo gif image](./demo.gif) vue3请安装**2.0.0**及以上版本,vue2请安装**1.x.x**版本 ## 功能支持列表 - 加载单个3D模型 - 同时加载多个3D模型 - 同时加载多个不同类型3D模型 - 加载Draco压缩gltf模型(使用方法请查看API) - 支持自定义文件类型(用于无文件后缀名url) - 设置场景宽高 - 设置材质及纹理 - 交互控制 - 鼠标事件 - 灯光 - 相机位置及旋转 - 添加标注点 ## 安装 ```shell npm i vue-3d-loader -S ``` 或 ```shell yarn add vue-3d-loader ``` ## 使用 全局使用,在入口文件中全局安装,代码如下: ```js /* vue2 */ import vue3dLoader from "vue-3d-loader"; Vue.use(vue3dLoader) /* vue3 */ import vue3dLoader from "vue-3d-loader"; createApp(App).use(vue3dLoader).mount('#app') ``` 非全局使用,在Vue文件中使用如下代码: ```js import { vue3dLoader } from "vue-3d-loader"; // 注意 vue3dLoader 写在 {...} 内 ``` 在组件中使用标签`` ```vue ``` ## API ### 属性
属性名 类型 默认值 描述
filePath string | array - ```js const filePath = './models/tree.obj' /* or */ const filePath = [ './models/tree.obj', './models/building.obj' ] ``` 文件路径,支持多个文件一起加载,注意:如果每个文件对应一个材质,需要将材质**mtlPath**设置为数组。图片纹理也一样需要设置**textureImage**为数组
fileType string | array - ```js const fileType = 'obj' /* or */ const fileType = ['obj', 'gltf'] ``` 文件类型,主要用于无法判断3d模型文件后缀名的url地址,与filePath一起使用,当filePath为数组时,此选项必须为数组
mtlPath string | array - ```js const mltPath = './models/tree.mlt' /* or */ const mltPath = [ './models/tree.mlt', './models/building.mlt' ] ``` .mtl材质路径,支持多个材质一起加载,设置此参数为数组,必须设置**filePath**为数组
textureImage string | array - ```js const textureImage = './texture/tree.jpg' /* or */ const textureImage = [ './texture/tree.jpg' ,null, './building.png' ] ``` 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} ```js const position = {x:0, y:0, z:0} // or const position = [ {x:10, y:10, z:10}, {x:50, y:50, z:50} ] ``` 模型位置坐标,filePath类型为array时可用array
rotation object | array {x:0, y:0, z:0} ```js const rotation = {x:0, y:0, z:0} // or const rotation = [ {x: 10, y:20, z:30}, {x: 0, y: 16, z: 20} ] ``` 模型旋转坐标,filePath类型为array时可用array
cameraPosition object {x:0, y:0, z:0} ```js const cameraPosition = {x:0, y:0, z:0} ``` 摄像机位置坐标
cameraRotation object {x:0, y:0, z:0} ```js const cameraRotation = {x:0, y:0, z:0} ``` 摄像机旋转坐标
scale object | array {x:1, y:1, z:1} ```js const scale = {x:1, y:2, z:1} // or const scale = [ {x:1, y:2, z:1}, {x:0.5, y:0.5, z:0.5} ] ``` 模型比例,filePath类型为array时可用array
lights array [{ type: "AmbientLight", color: 0xaaaaaa, }, { type: "DirectionalLight", position: { x: 1, y: 1, z: 1 }, color: 0xffffff, intensity: 0.8, }] ```js const lights = [ { type: "AmbientLight", color: "red", }, { type: "DirectionalLight", position: { x: 100, y: 10, z: 100 }, color: "green", intensity: 0.8, }, { type: "PointLight", color: "#000000", position: { x: 200, y: -200, z: 100 }, intensity: 1 }, { type: "HemisphereLight", skyColor: "#00FF00", groundColor: "#000000", position: { x: 200, y: -200, z: 100 } } ] ``` 灯光为数组,type为 环境光(AmbientLight) | 方向光(DirectionalLight) | 点光(PointLight) | 半球光(HemisphereLight)
backgroundColor number | string 0xffffff ```js const bgColor = 0xff00ff /* or */ const bgColor = 'red' /* or */ const bgColor = '#000000' /* or */ const bgColor = 'rgba(0, 0, 0, 0.5)' ``` 场景背景色
backgroundAlpha number 1 ```js const bgAlpha = 0.5 ``` 场景背景透明度, 值范围0-1
controlsOptions object - - 控制参数 [OrbitControls Properties](https://threejs.org/docs/#examples/en/controls/OrbitControls)
crossOrigin string anonymous anonymous | use-credentials 跨域配置
requestHeader object anonymous ```js const headers = { 'Authorization': 'Bearer token' } ``` 设置请求头
outputEncoding string linear liner 或 sRGB linear 是 LinearEncoding 线性编码, sRGB 即 sRGBEncoding rgb模式编码(sRGBEncoding能更好的还原材质颜色)。 渲染器的输出编码 [WebGLRenderer OutputEncoding](https://threejs.org/docs/index.html#api/en/renderers/WebGLRenderer.outputEncoding)
webGLRendererOptions object { antialias: true, alpha: true } - WebGLRenderer可选参数 [WebGLRenderer Parameters](https://threejs.org/docs/index.html#api/zh/renderers/WebGLRenderer)
showFps boolean false - 显示FPS等信息
clearScene boolean false - 清空场景中的内容
parallelLoad boolean false - 开启/关闭并行加载模型(仅在多模型下有效)**注意:使用此属性后,load进度process事件将不可预测**
labels array - ```js const labels = [ { image: "", text: "", textStyle: { fontFamily: "Arial", fontSize: 18, fontWeight: "normal", lineHeight: 1, color: "#ffffff", borderWidth: 8, borderRadius: 4, borderColor: "rgba(0,0,0,1)", backgroundColor: "rgba(0, 0, 0, 1)" }, position: {x:0, y:0, z:0}, scale:{x:1, y:1, z:0}, sid: null } ] ``` 添加图片/文字标签,设置image则显示图片标签。设置text显示文字标签,文字样式通过textStyle进行设置,样例可以查看[examples/add-label.vue](./src/examples/add-label.vue)文件
autoPlay boolean true - 播放/停止动画
enableDraco boolean false - 加载Gltf draco模型需开启draco解密,开启后需要[下载draco解密库](https://github.com/king2088/vue-3d-loader/blob/master/public/assets/draco.7z)放到默认目录assets中,默认目录为assets/draco/gltf/,如需修改默认目录,请查看参数dracoDir。[关于draco请查阅](https://threejs.org/docs/index.html?q=draco#examples/en/loaders/DRACOLoader)
dracoDir string assets/draco/gltf/ - draco解密库默认目录,可自行修改
intersectRecursive boolean false - 若为true,则同时也会检测所有物体的后代。否则将只会检测对象本身的相交部分。
enableDamping boolean false - [enableDamping](https://threejs.org/docs/index.html#examples/zh/controls/OrbitControls.enableDamping)设置为true以启用阻尼(惯性),这将给控制器带来重量感。
dampingFactor number - - enableDamping必须为true,此选项才有效。[dampingFactor](https://threejs.org/docs/index.html#examples/zh/controls/OrbitControls.dampingFactor)用于设置阻尼惯性大小。
verticalCtrl boolean | object false - 为true,仅能垂直旋转相机。
horizontalCtrl boolean | object false - 为true,仅能水平旋转相机。
plyMaterial string MeshStandardMaterial - 值仅有两个 `MeshStandardMaterial` 和 `MeshBasicMaterial`. 用于ply模型. [MeshStandardMaterial相关文档](https://threejs.org/docs/#api/en/materials/MeshStandardMaterial).[MeshBasicMaterial相关文档](https://threejs.org/docs/?q=MeshBasicMaterial#api/en/materials/MeshBasicMaterial)
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中任意一种 ```vue ``` #### 2. 同一个场景中加载多个模型 ```vue ``` #### 3. 材质及纹理加载 ```vue ``` #### 4. 背景颜色及透明度 ```vue ``` #### 5. 交互控制controls ```vue ``` #### 6. 旋转模型 ```vue ``` #### 7. 事件 ```vue ``` #### 8. 加载draco模型 此功能需要先下载draco库存储与本地项目的静态文件夹内,才可以正常加载,下载地址: ```vue ``` #### 9. 更多演示 [点我查看更多演示代码](https://github.com/king2088/vue-3d-loader/tree/master/src/examples) ### Docker部署examples ```shell docker build -t vue/vue-3d-loader . # 运行docker docker run -p 8010:80 vue/vue-3d-loader ``` ### 即将到来 - [x] 支持Vue3 ### bug提交 [issues](https://github.com/king2088/vue-3d-loader/issues) ### 捐助 ![微信支付](/images/wxpay.png) ![支付宝支付](/images/alipay.png) ### 感谢 本插件参考[vue-3d-model](https://github.com/hujiulong/vue-3d-model)实现