@damienmortini/three 0.1.131

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,587 @@
1
+ import {
2
+ BufferAttribute,
3
+ BufferGeometry,
4
+ FileLoader,
5
+ Loader
6
+ } from 'three';
7
+
8
+ const _taskCache = new WeakMap();
9
+
10
+ class DRACOLoader extends Loader {
11
+
12
+ constructor( manager ) {
13
+
14
+ super( manager );
15
+
16
+ this.decoderPath = '';
17
+ this.decoderConfig = {};
18
+ this.decoderBinary = null;
19
+ this.decoderPending = null;
20
+
21
+ this.workerLimit = 4;
22
+ this.workerPool = [];
23
+ this.workerNextTaskID = 1;
24
+ this.workerSourceURL = '';
25
+
26
+ this.defaultAttributeIDs = {
27
+ position: 'POSITION',
28
+ normal: 'NORMAL',
29
+ color: 'COLOR',
30
+ uv: 'TEX_COORD'
31
+ };
32
+ this.defaultAttributeTypes = {
33
+ position: 'Float32Array',
34
+ normal: 'Float32Array',
35
+ color: 'Float32Array',
36
+ uv: 'Float32Array'
37
+ };
38
+
39
+ }
40
+
41
+ setDecoderPath( path ) {
42
+
43
+ this.decoderPath = path;
44
+
45
+ return this;
46
+
47
+ }
48
+
49
+ setDecoderConfig( config ) {
50
+
51
+ this.decoderConfig = config;
52
+
53
+ return this;
54
+
55
+ }
56
+
57
+ setWorkerLimit( workerLimit ) {
58
+
59
+ this.workerLimit = workerLimit;
60
+
61
+ return this;
62
+
63
+ }
64
+
65
+ load( url, onLoad, onProgress, onError ) {
66
+
67
+ const loader = new FileLoader( this.manager );
68
+
69
+ loader.setPath( this.path );
70
+ loader.setResponseType( 'arraybuffer' );
71
+ loader.setRequestHeader( this.requestHeader );
72
+ loader.setWithCredentials( this.withCredentials );
73
+
74
+ loader.load( url, ( buffer ) => {
75
+
76
+ const taskConfig = {
77
+ attributeIDs: this.defaultAttributeIDs,
78
+ attributeTypes: this.defaultAttributeTypes,
79
+ useUniqueIDs: false
80
+ };
81
+
82
+ this.decodeGeometry( buffer, taskConfig )
83
+ .then( onLoad )
84
+ .catch( onError );
85
+
86
+ }, onProgress, onError );
87
+
88
+ }
89
+
90
+ /** @deprecated Kept for backward-compatibility with previous DRACOLoader versions. */
91
+ decodeDracoFile( buffer, callback, attributeIDs, attributeTypes ) {
92
+
93
+ const taskConfig = {
94
+ attributeIDs: attributeIDs || this.defaultAttributeIDs,
95
+ attributeTypes: attributeTypes || this.defaultAttributeTypes,
96
+ useUniqueIDs: !! attributeIDs
97
+ };
98
+
99
+ this.decodeGeometry( buffer, taskConfig ).then( callback );
100
+
101
+ }
102
+
103
+ decodeGeometry( buffer, taskConfig ) {
104
+
105
+ // TODO: For backward-compatibility, support 'attributeTypes' objects containing
106
+ // references (rather than names) to typed array constructors. These must be
107
+ // serialized before sending them to the worker.
108
+ for ( const attribute in taskConfig.attributeTypes ) {
109
+
110
+ const type = taskConfig.attributeTypes[ attribute ];
111
+
112
+ if ( type.BYTES_PER_ELEMENT !== undefined ) {
113
+
114
+ taskConfig.attributeTypes[ attribute ] = type.name;
115
+
116
+ }
117
+
118
+ }
119
+
120
+ //
121
+
122
+ const taskKey = JSON.stringify( taskConfig );
123
+
124
+ // Check for an existing task using this buffer. A transferred buffer cannot be transferred
125
+ // again from this thread.
126
+ if ( _taskCache.has( buffer ) ) {
127
+
128
+ const cachedTask = _taskCache.get( buffer );
129
+
130
+ if ( cachedTask.key === taskKey ) {
131
+
132
+ return cachedTask.promise;
133
+
134
+ } else if ( buffer.byteLength === 0 ) {
135
+
136
+ // Technically, it would be possible to wait for the previous task to complete,
137
+ // transfer the buffer back, and decode again with the second configuration. That
138
+ // is complex, and I don't know of any reason to decode a Draco buffer twice in
139
+ // different ways, so this is left unimplemented.
140
+ throw new Error(
141
+
142
+ 'THREE.DRACOLoader: Unable to re-decode a buffer with different ' +
143
+ 'settings. Buffer has already been transferred.'
144
+
145
+ );
146
+
147
+ }
148
+
149
+ }
150
+
151
+ //
152
+
153
+ let worker;
154
+ const taskID = this.workerNextTaskID ++;
155
+ const taskCost = buffer.byteLength;
156
+
157
+ // Obtain a worker and assign a task, and construct a geometry instance
158
+ // when the task completes.
159
+ const geometryPending = this._getWorker( taskID, taskCost )
160
+ .then( ( _worker ) => {
161
+
162
+ worker = _worker;
163
+
164
+ return new Promise( ( resolve, reject ) => {
165
+
166
+ worker._callbacks[ taskID ] = { resolve, reject };
167
+
168
+ worker.postMessage( { type: 'decode', id: taskID, taskConfig, buffer }, [ buffer ] );
169
+
170
+ // this.debug();
171
+
172
+ } );
173
+
174
+ } )
175
+ .then( ( message ) => this._createGeometry( message.geometry ) );
176
+
177
+ // Remove task from the task list.
178
+ // Note: replaced '.finally()' with '.catch().then()' block - iOS 11 support (#19416)
179
+ geometryPending
180
+ .catch( () => true )
181
+ .then( () => {
182
+
183
+ if ( worker && taskID ) {
184
+
185
+ this._releaseTask( worker, taskID );
186
+
187
+ // this.debug();
188
+
189
+ }
190
+
191
+ } );
192
+
193
+ // Cache the task result.
194
+ _taskCache.set( buffer, {
195
+
196
+ key: taskKey,
197
+ promise: geometryPending
198
+
199
+ } );
200
+
201
+ return geometryPending;
202
+
203
+ }
204
+
205
+ _createGeometry( geometryData ) {
206
+
207
+ const geometry = new BufferGeometry();
208
+
209
+ if ( geometryData.index ) {
210
+
211
+ geometry.setIndex( new BufferAttribute( geometryData.index.array, 1 ) );
212
+
213
+ }
214
+
215
+ for ( let i = 0; i < geometryData.attributes.length; i ++ ) {
216
+
217
+ const attribute = geometryData.attributes[ i ];
218
+ const name = attribute.name;
219
+ const array = attribute.array;
220
+ const itemSize = attribute.itemSize;
221
+
222
+ geometry.setAttribute( name, new BufferAttribute( array, itemSize ) );
223
+
224
+ }
225
+
226
+ return geometry;
227
+
228
+ }
229
+
230
+ _loadLibrary( url, responseType ) {
231
+
232
+ const loader = new FileLoader( this.manager );
233
+ loader.setPath( this.decoderPath );
234
+ loader.setResponseType( responseType );
235
+ loader.setWithCredentials( this.withCredentials );
236
+
237
+ return new Promise( ( resolve, reject ) => {
238
+
239
+ loader.load( url, resolve, undefined, reject );
240
+
241
+ } );
242
+
243
+ }
244
+
245
+ preload() {
246
+
247
+ this._initDecoder();
248
+
249
+ return this;
250
+
251
+ }
252
+
253
+ _initDecoder() {
254
+
255
+ if ( this.decoderPending ) return this.decoderPending;
256
+
257
+ const useJS = typeof WebAssembly !== 'object' || this.decoderConfig.type === 'js';
258
+ const librariesPending = [];
259
+
260
+ if ( useJS ) {
261
+
262
+ librariesPending.push( this._loadLibrary( 'draco_decoder.js', 'text' ) );
263
+
264
+ } else {
265
+
266
+ librariesPending.push( this._loadLibrary( 'draco_wasm_wrapper.js', 'text' ) );
267
+ librariesPending.push( this._loadLibrary( 'draco_decoder.wasm', 'arraybuffer' ) );
268
+
269
+ }
270
+
271
+ this.decoderPending = Promise.all( librariesPending )
272
+ .then( ( libraries ) => {
273
+
274
+ const jsContent = libraries[ 0 ];
275
+
276
+ if ( ! useJS ) {
277
+
278
+ this.decoderConfig.wasmBinary = libraries[ 1 ];
279
+
280
+ }
281
+
282
+ const fn = DRACOWorker.toString();
283
+
284
+ const body = [
285
+ '/* draco decoder */',
286
+ jsContent,
287
+ '',
288
+ '/* worker */',
289
+ fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) )
290
+ ].join( '\n' );
291
+
292
+ this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
293
+
294
+ } );
295
+
296
+ return this.decoderPending;
297
+
298
+ }
299
+
300
+ _getWorker( taskID, taskCost ) {
301
+
302
+ return this._initDecoder().then( () => {
303
+
304
+ if ( this.workerPool.length < this.workerLimit ) {
305
+
306
+ const worker = new Worker( this.workerSourceURL );
307
+
308
+ worker._callbacks = {};
309
+ worker._taskCosts = {};
310
+ worker._taskLoad = 0;
311
+
312
+ worker.postMessage( { type: 'init', decoderConfig: this.decoderConfig } );
313
+
314
+ worker.onmessage = function ( e ) {
315
+
316
+ const message = e.data;
317
+
318
+ switch ( message.type ) {
319
+
320
+ case 'decode':
321
+ worker._callbacks[ message.id ].resolve( message );
322
+ break;
323
+
324
+ case 'error':
325
+ worker._callbacks[ message.id ].reject( message );
326
+ break;
327
+
328
+ default:
329
+ console.error( 'THREE.DRACOLoader: Unexpected message, "' + message.type + '"' );
330
+
331
+ }
332
+
333
+ };
334
+
335
+ this.workerPool.push( worker );
336
+
337
+ } else {
338
+
339
+ this.workerPool.sort( function ( a, b ) {
340
+
341
+ return a._taskLoad > b._taskLoad ? - 1 : 1;
342
+
343
+ } );
344
+
345
+ }
346
+
347
+ const worker = this.workerPool[ this.workerPool.length - 1 ];
348
+ worker._taskCosts[ taskID ] = taskCost;
349
+ worker._taskLoad += taskCost;
350
+ return worker;
351
+
352
+ } );
353
+
354
+ }
355
+
356
+ _releaseTask( worker, taskID ) {
357
+
358
+ worker._taskLoad -= worker._taskCosts[ taskID ];
359
+ delete worker._callbacks[ taskID ];
360
+ delete worker._taskCosts[ taskID ];
361
+
362
+ }
363
+
364
+ debug() {
365
+
366
+ console.log( 'Task load: ', this.workerPool.map( ( worker ) => worker._taskLoad ) );
367
+
368
+ }
369
+
370
+ dispose() {
371
+
372
+ for ( let i = 0; i < this.workerPool.length; ++ i ) {
373
+
374
+ this.workerPool[ i ].terminate();
375
+
376
+ }
377
+
378
+ this.workerPool.length = 0;
379
+
380
+ return this;
381
+
382
+ }
383
+
384
+ }
385
+
386
+ /* WEB WORKER */
387
+
388
+ function DRACOWorker() {
389
+
390
+ let decoderConfig;
391
+ let decoderPending;
392
+
393
+ onmessage = function ( e ) {
394
+
395
+ const message = e.data;
396
+
397
+ switch ( message.type ) {
398
+
399
+ case 'init':
400
+ decoderConfig = message.decoderConfig;
401
+ decoderPending = new Promise( function ( resolve/*, reject*/ ) {
402
+
403
+ decoderConfig.onModuleLoaded = function ( draco ) {
404
+
405
+ // Module is Promise-like. Wrap before resolving to avoid loop.
406
+ resolve( { draco: draco } );
407
+
408
+ };
409
+
410
+ DracoDecoderModule( decoderConfig ); // eslint-disable-line no-undef
411
+
412
+ } );
413
+ break;
414
+
415
+ case 'decode':
416
+ const buffer = message.buffer;
417
+ const taskConfig = message.taskConfig;
418
+ decoderPending.then( ( module ) => {
419
+
420
+ const draco = module.draco;
421
+ const decoder = new draco.Decoder();
422
+ const decoderBuffer = new draco.DecoderBuffer();
423
+ decoderBuffer.Init( new Int8Array( buffer ), buffer.byteLength );
424
+
425
+ try {
426
+
427
+ const geometry = decodeGeometry( draco, decoder, decoderBuffer, taskConfig );
428
+
429
+ const buffers = geometry.attributes.map( ( attr ) => attr.array.buffer );
430
+
431
+ if ( geometry.index ) buffers.push( geometry.index.array.buffer );
432
+
433
+ self.postMessage( { type: 'decode', id: message.id, geometry }, buffers );
434
+
435
+ } catch ( error ) {
436
+
437
+ console.error( error );
438
+
439
+ self.postMessage( { type: 'error', id: message.id, error: error.message } );
440
+
441
+ } finally {
442
+
443
+ draco.destroy( decoderBuffer );
444
+ draco.destroy( decoder );
445
+
446
+ }
447
+
448
+ } );
449
+ break;
450
+
451
+ }
452
+
453
+ };
454
+
455
+ function decodeGeometry( draco, decoder, decoderBuffer, taskConfig ) {
456
+
457
+ const attributeIDs = taskConfig.attributeIDs;
458
+ const attributeTypes = taskConfig.attributeTypes;
459
+
460
+ let dracoGeometry;
461
+ let decodingStatus;
462
+
463
+ const geometryType = decoder.GetEncodedGeometryType( decoderBuffer );
464
+
465
+ if ( geometryType === draco.TRIANGULAR_MESH ) {
466
+
467
+ dracoGeometry = new draco.Mesh();
468
+ decodingStatus = decoder.DecodeBufferToMesh( decoderBuffer, dracoGeometry );
469
+
470
+ } else if ( geometryType === draco.POINT_CLOUD ) {
471
+
472
+ dracoGeometry = new draco.PointCloud();
473
+ decodingStatus = decoder.DecodeBufferToPointCloud( decoderBuffer, dracoGeometry );
474
+
475
+ } else {
476
+
477
+ throw new Error( 'THREE.DRACOLoader: Unexpected geometry type.' );
478
+
479
+ }
480
+
481
+ if ( ! decodingStatus.ok() || dracoGeometry.ptr === 0 ) {
482
+
483
+ throw new Error( 'THREE.DRACOLoader: Decoding failed: ' + decodingStatus.error_msg() );
484
+
485
+ }
486
+
487
+ const geometry = { index: null, attributes: [] };
488
+
489
+ // Gather all vertex attributes.
490
+ for ( const attributeName in attributeIDs ) {
491
+
492
+ const attributeType = self[ attributeTypes[ attributeName ] ];
493
+
494
+ let attribute;
495
+ let attributeID;
496
+
497
+ // A Draco file may be created with default vertex attributes, whose attribute IDs
498
+ // are mapped 1:1 from their semantic name (POSITION, NORMAL, ...). Alternatively,
499
+ // a Draco file may contain a custom set of attributes, identified by known unique
500
+ // IDs. glTF files always do the latter, and `.drc` files typically do the former.
501
+ if ( taskConfig.useUniqueIDs ) {
502
+
503
+ attributeID = attributeIDs[ attributeName ];
504
+ attribute = decoder.GetAttributeByUniqueId( dracoGeometry, attributeID );
505
+
506
+ } else {
507
+
508
+ attributeID = decoder.GetAttributeId( dracoGeometry, draco[ attributeIDs[ attributeName ] ] );
509
+
510
+ if ( attributeID === - 1 ) continue;
511
+
512
+ attribute = decoder.GetAttribute( dracoGeometry, attributeID );
513
+
514
+ }
515
+
516
+ geometry.attributes.push( decodeAttribute( draco, decoder, dracoGeometry, attributeName, attributeType, attribute ) );
517
+
518
+ }
519
+
520
+ // Add index.
521
+ if ( geometryType === draco.TRIANGULAR_MESH ) {
522
+
523
+ geometry.index = decodeIndex( draco, decoder, dracoGeometry );
524
+
525
+ }
526
+
527
+ draco.destroy( dracoGeometry );
528
+
529
+ return geometry;
530
+
531
+ }
532
+
533
+ function decodeIndex( draco, decoder, dracoGeometry ) {
534
+
535
+ const numFaces = dracoGeometry.num_faces();
536
+ const numIndices = numFaces * 3;
537
+ const byteLength = numIndices * 4;
538
+
539
+ const ptr = draco._malloc( byteLength );
540
+ decoder.GetTrianglesUInt32Array( dracoGeometry, byteLength, ptr );
541
+ const index = new Uint32Array( draco.HEAPF32.buffer, ptr, numIndices ).slice();
542
+ draco._free( ptr );
543
+
544
+ return { array: index, itemSize: 1 };
545
+
546
+ }
547
+
548
+ function decodeAttribute( draco, decoder, dracoGeometry, attributeName, attributeType, attribute ) {
549
+
550
+ const numComponents = attribute.num_components();
551
+ const numPoints = dracoGeometry.num_points();
552
+ const numValues = numPoints * numComponents;
553
+ const byteLength = numValues * attributeType.BYTES_PER_ELEMENT;
554
+ const dataType = getDracoDataType( draco, attributeType );
555
+
556
+ const ptr = draco._malloc( byteLength );
557
+ decoder.GetAttributeDataArrayForAllPoints( dracoGeometry, attribute, dataType, byteLength, ptr );
558
+ const array = new attributeType( draco.HEAPF32.buffer, ptr, numValues ).slice();
559
+ draco._free( ptr );
560
+
561
+ return {
562
+ name: attributeName,
563
+ array: array,
564
+ itemSize: numComponents
565
+ };
566
+
567
+ }
568
+
569
+ function getDracoDataType( draco, attributeType ) {
570
+
571
+ switch ( attributeType ) {
572
+
573
+ case Float32Array: return draco.DT_FLOAT32;
574
+ case Int8Array: return draco.DT_INT8;
575
+ case Int16Array: return draco.DT_INT16;
576
+ case Int32Array: return draco.DT_INT32;
577
+ case Uint8Array: return draco.DT_UINT8;
578
+ case Uint16Array: return draco.DT_UINT16;
579
+ case Uint32Array: return draco.DT_UINT32;
580
+
581
+ }
582
+
583
+ }
584
+
585
+ }
586
+
587
+ export { DRACOLoader };