New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

vue-3d-loader

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-3d-loader

vueJS + [threeJS](https://threejs.org/)整合的一个3d展示组件,支持dae/fbx/gltf(glb)/obj/ply/stl,并支持同一个场景导入多个不同3D模型,材质方面,支持mtl材质

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
457
increased by43.26%
Maintainers
1
Weekly downloads
 
Created
Source

vue-3d-loader

vueJS + threeJS整合的一个3d展示组件,支持dae/fbx/gltf(glb)/obj/ply/stl,并支持同一个场景导入多个不同3D模型,材质方面,支持mtl材质

demo gif image

当前仅支持vue2

安装

npm i vue-3d-loader -S

yarn add vue-3d-loader

使用

在入口文件中全局安装,代码如下:

import vue3dLoader from "vue-3d-loader";
Vue.use(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

属性

proptypedefaultdescription
filePathstring | array-文件路径,支持多个文件一起加载,注意:如果有每一个文件都对应一个材质,则也需要将材质对象mtlPath设置为对应的数组
mtlPathstring | array-.mtl材质路径,支持多个材质一起加载,设置此参数为数组,必须设置filePath为数组
textureImagestring | array-jpg/png纹理加载,与filePath一一对应,为数组时,必须设置filePath为数组
widthnumber-宽度
heightnumber-高度
positionobject{ x: 0, y: 0, z: 0 }物体位置
rotationobject{ x: 0, y: 0, z: 0 }旋转
cameraPositionobject{ x: 0, y: 0, z: 0 }摄像机位置
cameraRotationobject{ x: 0, y: 0, z: 0 }摄像机旋转
scaleobject{ x: 1, y: 1, z: 1 }缩放
lightsarray[{type: "AmbientLight",color: 0xaaaaaa,},{type: "DirectionalLight",position: { x: 1, y: 1, z: 1 },color: 0xffffff,intensity: 0.8,}]灯光,AmbientLight为环境光,DirectionalLight为定向光
backgroundColornumber | string0xffffff背景颜色0xffffff/#f00/rgb(255,255,255)
backgroundAlphanumber1背景透明度(范围0-1)
controlsOptionsobject-控制参数OrbitControls Properties
crossOriginstringanonymous跨域配置anonymous/use-credentials
requestHeaderobject-设置请求{ 'Authorization: Bearer token' }头
outputEncodingnumberTHREE.LinearEncoding渲染器的输出编码WebGLRenderer OutputEncoding
webGLRendererOptionsobject{ antialias: true, alpha: true }WebGLRenderer可选参数WebGLRenderer Parameters
showFpsbooleanfalse显示FPS等信息

事件

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>
  <vue3dLoader
    :filePath="filePath"
    :scale="{ x: 0.4, y: 0.4, z: 0.4 }"
    :cameraPosition="{ x: 100, y: 200, z: 30 }"
  ></vue3dLoader>
</template>
<script>
export default {
  data() {
    return {
      filePath: [
        "/models/fbx/Samba Dancing.fbx",
        "models/collada/pump/pump.dae",
      ],
    };
  },
};
</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>
export default {
  data() {
    return {
      enablePan: true,
      enableZoom: true,
      enableRotate: true,
    };
  },
};
</script>
6. 旋转模型
<template>
  <vue3dLoader
    :rotation="rotation"
    @load="onLoad()"
    filePath="/models/collada/elf/elf.dae"
  />
</template>
<script>
export default {
  data() {
    return {
      rotation: {
        x: -Math.PI / 2,
        y: 0,
        z: 0,
      },
    };
  },
  methods: {
    onLoad() {
      this.rotate();
    },
    rotate() {
      requestAnimationFrame(this.rotate);
      this.rotation.z += 0.01;
    },
  },
};
</script>
7. 事件
<template>
  <vue3dLoader filePath="/models/ply/Lucy100k.ply" @mousemove="onMouseMove" />
</template>
<script>
export default {
  data() {
    return {
      object: null,
    };
  },
  methods: {
    onMouseMove(event, intersected) {
      if (this.object) {
        this.object.material.color.setStyle("#fff");
      }
      if (intersected) {
        this.object = intersected.object;
        this.object.material.color.setStyle("#13ce66");
      }
    },
  },
};
</script>
8. 更多演示

点我查看更多演示代码

即将到来

  • 支持Vue3

bug提交

issues

感谢

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

Keywords

FAQs

Package last updated on 02 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc