我是靠谱客的博主 野性百合,这篇文章主要介绍Vue使用Three.js加载glTF模型的方法详解,现在分享给大家,希望可以做个参考。

前言

Three.js是一个跨浏览器的脚本,使用JavaScript函数库或API来在网页浏览器中创建和展示动画的三维计算机图形,基于WebGL实现,对WebGL进行了进一步的封装,简化了多数复杂的接口。

Three.js支持包括 .obj、.gltf等类型的模型结构。glTF(GL传输格式)是Khronos的一个开放项目,它为3D资产提供了一种通用的、可扩展的格式,这种格式既高效又与现代web技术高度互操作。

obj格式的模型只支持顶点、法线、纹理坐标和基本材质,而glTF模型除上述所有内容外,glTF还提供了如下功能:

层级对象
场景信息(光源,相机)
骨骼结构与动画
更可靠的材质和着色器

一、安装引入Three.js

复制代码
1
npm install three

在需要使用3D模型的页面导入包:

复制代码
1
import * as Three from "three"

在Vue中导入glTF模型需要使用 Three.js 中的 GLTFLoader:

复制代码
1
2
3
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader" // 导入轨道模型控制器 import { OrbitControls } from "three/examples/jsm/controls/OrbitControls

二、页面DOM元素渲染

在Vue中,我们需要使用一个 div 元素来作为3D模型的容器:

复制代码
1
<div id="container"></div>

页面打开之后,Three.js会给 div 元素添加一个 canvas 子元素用来作为3D模型的画布。

三、初始化

Three.js中最重要的三大组件:

场景——Scene

相机——Camera

渲染器——Renderer

初始化:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
mounted(){ this.initScene() this.initContainer() this.initCamera() this.initRenderer() this.initControls() }, methods:{ initModelContainer() { this.model_container = document.getElementById("container"); this.model_container.style.height = window.innerHeight + "px"; this.model_container.style.width = window.innerWidth + "px"; this.height = this.model_container.clientHeight; this.width = this.model_container.clientWidth; }, initScene() { this.scene = new Three.Scene(); }, initCamera() { // 照相机 this.camera = new Three.PerspectiveCamera(70, this.width / this.height, 0.01, 1000); this.camera.position.set(-100, 60, 0); }, initRenderer() { this.renderer = new Three.WebGLRenderer({ antialias: true, alpha: true }); this.renderer.setSize(this.width, this.height); // 兼容高清屏幕 this.renderer.setPixelRatio(window.devicePixelRatio); // 消除canvas的外边框 this.renderer.domElement.style.outline = "none"; this.model_container.appendChild(this.renderer.domElement); }, initControls() { this.orbitControls = new OrbitControls( this.camera, this.renderer.domElement ); // 惯性 this.orbitControls.enableDamping = true; // 动态阻尼系数 this.orbitControls.dampingFactor = 0.25; // 缩放 this.orbitControls.enableZoom = true; // 右键拖拽 this.orbitControls.enablePan = true; // 水平旋转范围 this.orbitControls.maxAzimuthAngle = Math.PI / 6; this.orbitControls.minAzimuthAngle = -Math.PI / 6; // 垂直旋转范围 this.orbitControls.maxPolarAngle = Math.PI / 6; this.orbitControls.minPolarAngle = -Math.PI / 6; }, }

四、导入glTF模型

将你的 gltf 模型放在 Vue 项目中的 public 文件夹下,注意,只有将 gltf 模型放在静态资源文件夹下才能被访问到。

在钩子函数 mounted 中进行模型加载:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
mounted(){ this.loadModel() }, methods:{ loadModel(){ let that = this // gltf模型加载器 let loader = new GLTFLoader() return new Promise(function(resolve, reject){ loader.load( // 模型在 /public/static/building/文件夹下 "static/building/scene.gltf", gltf => { console.log(gltf) gltf.scene.traverse(object => { // 修改模型材质 let material = ... object.material = material }) let group = new Three.Group() group.add(gltf.scene) let box = new Three.Box3() box.setFromObject(group) let wrapper = new Three.Object3D() wrapper.add(group) // 根据自己模型的大小设置位置 wrapper.position.set(100, -300, 120) // 将模型加入到场景中 ! important that.scene.add(wrapper) }, xhr => { // 模型加载期间的回调函数 console.log(`${(xhr.loaded / xhr.total) * 100% building model loaded` ); }, error => { // 模型加载出错的回调函数 console.log("error while loading", error); reject("load model error", error); } ) }) } }

启动项目,模型导入成功,可以根据自己的需求为模型渲染材质。

总结

到此这篇关于Vue使用Three.js加载glTF模型的文章就介绍到这了,更多相关Vue用Three.js加载glTF模型内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是野性百合最近收集整理的关于Vue使用Three.js加载glTF模型的方法详解的全部内容,更多相关Vue使用Three.js加载glTF模型内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(1929)

评论列表共有 0 条评论

立即
投稿
返回
顶部