Skip to content
项目

关于

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等图片纹理

Version License Downloads

English

文档:https://king2088.github.io/vue-3d-loader-docs/zh/

演示动画

demo gif image

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 -
const filePath = './models/tree.obj'
/* or */
const filePath = [
  './models/tree.obj',
  './models/building.obj'
]

文件路径,支持多个文件一起加载,注意:如果每个文件对应一个材质,需要将材质mtlPath设置为数组。图片纹理也一样需要设置textureImage为数组

fileType string | array -
const fileType = 'obj'
/* or */
const fileType = ['obj', 'gltf']

文件类型,主要用于无法判断3d模型文件后缀名的url地址,与filePath一起使用,当filePath为数组时,此选项必须为数组

mtlPath string | array -
const mltPath = './models/tree.mlt'
/* or */
const mltPath = [
  './models/tree.mlt', 
  './models/building.mlt'
]

.mtl材质路径,支持多个材质一起加载,设置此参数为数组,必须设置filePath为数组

textureImage string | array -
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}
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}
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}
const cameraPosition = {x:0, y:0, z:0}
摄像机位置坐标
cameraRotation object {x:0, y:0, z:0}
const cameraRotation = {x:0, y:0, z:0}
摄像机旋转坐标
scale object | array {x:1, y:1, z:1}
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, }]
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
const bgColor = 0xff00ff
/* or */
const bgColor = 'red'
/* or */
const bgColor = '#000000'
/* or */
const bgColor = 'rgba(0, 0, 0, 0.5)'
场景背景色
backgroundAlpha number 1
const bgAlpha = 0.5
场景背景透明度, 值范围0-1
controlsOptions object - -

控制参数 OrbitControls Properties

crossOrigin string anonymous anonymous | use-credentials 跨域配置
requestHeader object anonymous
const headers = { 
  'Authorization': 'Bearer token'
}
设置请求头
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 -
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文件

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 -

值仅有两个 MeshStandardMaterial 和 MeshBasicMaterial. 用于ply模型. MeshStandardMaterial相关文档.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中任意一种

<!-- 加载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提交

issues

捐助

微信支付

支付宝支付

感谢

本插件参考vue-3d-model实现

探索相关项目

加载中…