快速搭建vue3 threejs模型展示的库(适合多格式)

前言

之前做了个项目需要快速搭建展示threejs模型简单展示多种格式模型,时间紧任务重,所以上github找了这个蛮优秀的库解决我目前的问题,喜欢的可以去看看。

  • 优点:可以快速展示自己的模型,api用起来方便清楚,适合vue2/vue3,适合多格式
  • 缺点:限制比较多,比如动画自动播放没办法获取组对象进行播放,有的api按照文档添加后没有效果

github地址:https://github.com/king2088/vue-3d-loader

image.png

vue-3d-loader

vueJS + threeJS整合的一个3d展示组件。

支持.dae/.fbx/.gltf/.glb/.obj/.ply/.stl/.json,并支持同一个场景导入多个不同3D模型,支持mtl材质以及jpg/png等图片纹理

English

文档: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属性文档

文档地址

image.png

事件

eventdescription
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
  • 16
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
VueThree.js是两个热门的Web开发框架和,分别用于构建用户界面和创建3D场景。结合使用VueThree.js可以非常方便地搭建一个3D仓房。 首先,在Vue项目中安装并引入Three.js。可以使用npm或者直接在HTML中引入CDN链接。然后,使用Vue的组件化开发思想,创建一个3D仓房组件。 在Vue3D仓房组件中,可以使用Three.js的场景(Scene)、相机(Camera)、渲染器(Renderer)等基本元素来创建一个空白的3D场景。可以设定相机的位置和方向,调整渲染器的大小和样式。 接下来,可以使用Three.js提供的几何体(Geometry)和材质(Material)来创建具体的仓房模型。例如,可以使用BoxGeometry创建一个长方体模型,然后使用MeshBasicMaterial设置其颜色或者使用纹理材质来进行贴图。 在几何体和材质创建好之后,可以将其合并成一个网格(Mesh),并添加到场景中。 为了使3D场景更加生动,可以使用Three.js的灯光(Light)来设置光照效果。例如,太阳光照射到仓房模型上,可以使用光源和颜色来模拟阳光的效果。 最后,在Vue3D仓房组件中添加交互功能,例如旋转、缩放或者平移等,可以使用Three.js提供的控制器(Controller)或者自定义事件监听器来实现。 在Vue项目中的相应页面引入3D仓房组件,并传入相应的参数,即可在浏览器中看到搭建好的3D仓房场景。 总之,使用VueThree.js搭建3D仓房的过程大致如上所述,需要使用Vue的组件化开发和Three.js的渲染和建模功能来实现。这样可以充分利用两个框架和的优势,简化开发流程,创建出生动逼真的3D仓房场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嚣张农民

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值