threejs中引入draco压缩后的模型

draco是谷歌出的一款模型压缩工具,可将gltf格式的模型进行进一步压缩提高页面加载速的一种方法

一、通过Draco进行压缩

全局安装

1
2
3


npm install -g gltf-pipeline

压缩glb文件 -b表示输出glb格式, -d表示压缩

1
2
3


gltf-pipeline -i model.glb -b -d

压缩glb文件并将纹理图片分离出来

1
2
3


gltf-pipeline -i model.glb -b -d -t

更多参数查阅

1
2
3


gltf-pipeline -h

threejs中进行引用

首先在顶部引入"DRACOLoader",

1
2
3
4
5
6
7
8
9


import * as THREE from '../build/three.module.js'; 


import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';


import { DRACOLoader } from './jsm/loaders/DRACOLoader.js';

在threejs中进行加载,在draco文件中找到draco_decoder.js

 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


	 // // 创建加载器


	var gltfLoader = new GLTFLoader();


	const dracoLoader = new DRACOLoader();


	// 在draco文件中找到draco_decoder.js这个文件,如果是vue直接放在项目的public下  的'./gltfdraco/'目录下面即可,这个路径主要是放draco的一些js文件的


	dracoLoader.setDecoderPath('./gltfdraco/'); //这个路径是放draco_decoder.js这个文件的


	dracoLoader.setDecoderConfig({ type: 'js' });


	dracoLoader.preload();


	gltfLoader.setDRACOLoader(dracoLoader);


	// 然后直接加载模型即可


	gltfLoader.load('./model/zhuji08-draco.glb',function(gltf){


		scene.add(group)


	})

下面是完整代码

  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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270


// An highlighted block


<!DOCTYPE html>


<html lang="en">


	<head>


		<title>three.js webgl - post processing - Outline Pass</title>


		<meta charset="utf-8">


		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">


		<link type="text/css" rel="stylesheet" href="main.css">


	</head>


	<body>


		<div id="container">


		</div>


		<script type="module">





		import * as THREE from '../build/three.module.js';





		import Stats from './jsm/libs/stats.module.js';


		// import { GUI } from './jsm/libs/dat.gui.module.js';





		import { OrbitControls } from './jsm/controls/OrbitControls.js';


		import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';


		import { DRACOLoader } from './jsm/loaders/DRACOLoader.js';


	       /**


	     * 创建场景对象Scene


	     */


	    var scene = new THREE.Scene();


	    /**


	     * 光源设置


	     */


	    //点光源


	    var point = new THREE.PointLight(0xffffff, 1);


	    point.position.set(300, 1200, 300); //点光源位置


	    scene.add(point); //点光源添加到场景中


	    //环境光


	    var ambient = new THREE.AmbientLight(0xcecece,1);


	    ambient.position.set(0, 800, 0); //点光源位置


	    scene.add(ambient);


	    


	    var width = window.innerWidth; //窗口宽度


	    var height = window.innerHeight; //窗口高度


	    /**透视投影相机对象*/


	    var camera = new THREE.PerspectiveCamera(60, width / height, 1, 100000);


	    console.log('相机',camera)


	    camera.position.set(370, 60, 120); //设置相机位置


	    camera.lookAt(scene.position); //设置相机方向(指向的场景对象)


	    //设置窗口自适应


        window.onresize=function(){


      // 重置渲染器输出画布canvas尺寸


	      renderer.setSize(window.innerWidth,window.innerHeight);


	      // 全屏情况下:设置观察范围长宽比aspect为窗口宽高比


	      camera.aspect = window.innerWidth/window.innerHeight;


	      // 渲染器执行render方法的时候会读取相机对象的投影矩阵属性projectionMatrix


	      // 但是不会每渲染一帧,就通过相机的属性计算投影矩阵(节约计算资源)


	      // 如果相机的一些属性发生了变化,需要执行updateProjectionMatrix ()方法更新相机的投影矩阵


	      camera.updateProjectionMatrix ();





    


      // 创建加载器  DRACOLoader  重点


		var gltfLoader = new GLTFLoader();


		var group = new THREE.Group


		const dracoLoader = new DRACOLoader();


		// 设置解压路径,这个文件直接放在项目的public目录下面即可,后面会截图指明gltf目录所在位置


		dracoLoader.setDecoderPath('./gltfdraco/');


		dracoLoader.setDecoderConfig({ type: 'js' });


		dracoLoader.preload();


		gltfLoader.setDRACOLoader(dracoLoader);


		// 然后直接加载模型即可


		gltfLoader.load('./model/zhuji08-draco.glb',function(gltf){


		    console.log(gltf)


		    scene.add(gltf)


		})








	    var renderer = new THREE.WebGLRenderer();


	    renderer.setSize(width, height); //设置渲染区域尺寸


	    renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色


	    document.body.appendChild(renderer.domElement); //body元素中插入canvas对象


	    // 渲染函数


	    function render() {


	      renderer.render(scene, camera); //执行渲染操作


	      scene.rotateY(0.00005);//每次绕y轴旋转0.01弧度


	      requestAnimationFrame(render); //请求再次执行渲染函数render,渲染下一帧


	    }


	    render();


	    let controls


	    // //创建控件对象  相机对象camera作为参数   控件可以监听鼠标的变化,改变相机对象的属性


	    controls = new OrbitControls( camera, renderer.domElement );


		</script>


	</body>


</html>