Vue使用vue-3d-model组件预览3D三维文件、立体文件,支持旋转、自动播放
实现效果
Tips:先泼个冷水,这个预览3D组件有个致命的缺陷——不能设置材质、皮肤文件的目录路径,必须要和3d文件放在同一个目录,如果项目是用hash模式(url后面会有/#/这种井号),就会导致无法读取根目录的材质文件。所以推荐了解下 vue-3d-loader
安装先
npm install vue-3d-model --save
属性
prop | type | default | example |
---|---|---|---|
src | string | - | './exapmle.obj' |
width | number | - | 300 |
height | number | - | 300 |
position | object | { x: 0, y: 0, z: 0 } | { x: 100, y: 20, z: -10 } |
rotation | object | { x: 0, y: 0, z: 0 } | { x: Math.PI / 2, y: 0, z: - Math.PI / 4 } |
cameraPosition | object | { x: 0, y: 0, z: 0 } | { x: 1, y: 2, z: -3 } |
cameraRotation | object | { x: 0, y: 0, z: 0 } | { x: 3, y: 2, z: -1 } |
scale | object | { x: 1, y: 1, z: 1 } | { x: 2, y: 2, z: 3 } |
lights | array | - | |
backgroundColor | number/string | 0xffffff | 0xffffff/'#f00'/'rgb(255,255,255)' |
backgroundAlpha | number | 1 | 0.5 |
controlsOptions | object | - | see OrbitControls Propertiesopen in new window |
crossOrigin | string | anonymous | anonymous/use-credentials |
requestHeader | object | - | { 'Authorization: Bearer token' } |
outputEncoding | number | THREE.LinearEncoding | see WebGLRenderer OutputEncodingopen in new window |
glOptions | object | { antialias: true, alpha: true } | see WebGLRenderer Parametersopen in new window |
事件
event |
---|
mousedown |
mousemove |
mouseup |
click |
load |
progress |
error |
插槽
slots |
---|
progress-bar |
poster |
-
<template>
-
<div class="threeDPreview">
-
-
<!-- 3D立体文件预览 -->
-
<div if="threeDComponents" class="threeD">
-
<component
-
:is="threeDComponents.componentName"
-
:backgroundAlpha="1"
-
:backgroundColor="'#000'"
-
:rotation="ratation"
-
:src="`${encodeURIComponent(_fileURL)}`"
-
@load="onLoad" />
-
</div>
-
-
</div>
-
</template>
-
-
<script>
-
//引入3D预览插件 npm install vue-3d-model --save
-
import { ModelCollada, ModelFbx, ModelGltf, ModelThree, ModelObj, ModelPly, ModelStl, } from 'vue-3d-model';
-
-
export default {
-
components: {
-
ModelCollada, ModelFbx, ModelGltf, ModelThree, ModelObj, ModelPly, ModelStl,
-
},
-
data() {
-
return {
-
load: null,
-
threeDfileTypes: [
-
{ label: 'dae', value: 1, iconURL: 'static/img/fileType/3D/dae.svg', componentName: 'ModelCollada' },
-
{ label: 'fbx', value: 2, iconURL: 'static/img/fileType/3D/fbx.svg', componentName: 'ModelFbx' },
-
{ label: 'gltf', value: 3, iconURL: 'static/img/fileType/3D/gltf.svg', componentName: 'ModelGltf' },
-
{ label: 'json', value: 4, iconURL: 'static/img/fileType/3D/json.svg', componentName: 'ModelThree' },
-
{ label: 'obj', value: 5, iconURL: 'static/img/fileType/3D/obj.svg', componentName: 'ModelObj' },
-
{ label: 'ply', value: 6, iconURL: 'static/img/fileType/3D/ply.svg', componentName: 'ModelPly' },
-
{ label: 'stl', value: 7, iconURL: 'static/img/fileType/3D/stl.svg', componentName: 'ModelStl' },
-
],
-
ratation: {
-
x: -Math.PI / 2,
-
y: 0,
-
z: 0,
-
},
-
_fileURL: '',
-
_fileType: '',
-
};
-
},
-
props: ["fileURL", "fileType"],
-
watch: {
-
fileURL: {
-
handler(d) {
-
this._fileURL = decodeURIComponent(d || this.$route.query.fileURL);
-
}, deep: true, immediate: true,
-
},
-
fileType: {
-
handler(d) {
-
this._fileType = d || this._fileURL.split('.').slice(-1)[0];
-
// 如果是3D文件个后缀名格式
-
if (this.threeDfileTypes.find(v => v.label == this._fileType)) {
-
this.showLoad();
-
}
-
}, deep: true, immediate: true,
-
},
-
},
-
computed: {
-
threeDComponents() {
-
return this.threeDfileTypes.find(v => v.label == this._fileType);
-
},
-
},
-
methods: {
-
showLoad() { this.load = this.$loading({ text: "加载中…" }); },
-
hideLoad() { this.load && this.load.close(); },
-
onLoad(d) {
-
this.rotate();
-
this.hideLoad();
-
},
-
rotate() {
-
requestAnimationFrame(this.rotate);//实现自动旋转效果
-
this.rotation.z = 0.01;
-
},
-
}
-
};
-
</script>
-
-
<style lang="scss" scoped>
-
.threeDPreview {
-
position: relative;
-
.threeD {
-
width: 100%;
-
height: calc(100vh - 60px);
-
}
-
}
-
</style>
相关API文档来源:
安装 | Vue 3D ModelVue 3D Modelhttps://vue-3d-model.netlify.app/zh/guide/installation/
可能会遇到的报错
其他可以预览3D的组件,前列腺推荐!
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhficgbj
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13