@2112-lab/central-plant 0.3.16 → 0.3.18

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,683 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var THREE = require('three');
6
+
7
+ const _taskCache = new WeakMap();
8
+
9
+ /**
10
+ * A loader for the Draco format.
11
+ *
12
+ * [Draco]{@link https://google.github.io/draco/} is an open source library for compressing
13
+ * and decompressing 3D meshes and point clouds. Compressed geometry can be significantly smaller,
14
+ * at the cost of additional decoding time on the client device.
15
+ *
16
+ * Standalone Draco files have a `.drc` extension, and contain vertex positions, normals, colors,
17
+ * and other attributes. Draco files do not contain materials, textures, animation, or node hierarchies –
18
+ * to use these features, embed Draco geometry inside of a glTF file. A normal glTF file can be converted
19
+ * to a Draco-compressed glTF file using [glTF-Pipeline]{@link https://github.com/CesiumGS/gltf-pipeline}.
20
+ * When using Draco with glTF, an instance of `DRACOLoader` will be used internally by {@link GLTFLoader}.
21
+ *
22
+ * It is recommended to create one DRACOLoader instance and reuse it to avoid loading and creating
23
+ * multiple decoder instances.
24
+ *
25
+ * `DRACOLoader` will automatically use either the JS or the WASM decoding library, based on
26
+ * browser capabilities.
27
+ *
28
+ * ```js
29
+ * const loader = new DRACOLoader();
30
+ * loader.setDecoderPath( '/examples/jsm/libs/draco/' );
31
+ *
32
+ * const geometry = await dracoLoader.loadAsync( 'models/draco/bunny.drc' );
33
+ * geometry.computeVertexNormals(); // optional
34
+ *
35
+ * dracoLoader.dispose();
36
+ * ```
37
+ *
38
+ * @augments Loader
39
+ * @three_import import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
40
+ */
41
+ class DRACOLoader extends THREE.Loader {
42
+
43
+ /**
44
+ * Constructs a new Draco loader.
45
+ *
46
+ * @param {LoadingManager} [manager] - The loading manager.
47
+ */
48
+ constructor( manager ) {
49
+
50
+ super( manager );
51
+
52
+ this.decoderPath = '';
53
+ this.decoderConfig = {};
54
+ this.decoderBinary = null;
55
+ this.decoderPending = null;
56
+
57
+ this.workerLimit = 4;
58
+ this.workerPool = [];
59
+ this.workerNextTaskID = 1;
60
+ this.workerSourceURL = '';
61
+
62
+ this.defaultAttributeIDs = {
63
+ position: 'POSITION',
64
+ normal: 'NORMAL',
65
+ color: 'COLOR',
66
+ uv: 'TEX_COORD'
67
+ };
68
+ this.defaultAttributeTypes = {
69
+ position: 'Float32Array',
70
+ normal: 'Float32Array',
71
+ color: 'Float32Array',
72
+ uv: 'Float32Array'
73
+ };
74
+
75
+ }
76
+
77
+ /**
78
+ * Provides configuration for the decoder libraries. Configuration cannot be changed after decoding begins.
79
+ *
80
+ * @param {string} path - The decoder path.
81
+ * @return {DRACOLoader} A reference to this loader.
82
+ */
83
+ setDecoderPath( path ) {
84
+
85
+ this.decoderPath = path;
86
+
87
+ return this;
88
+
89
+ }
90
+
91
+ /**
92
+ * Provides configuration for the decoder libraries. Configuration cannot be changed after decoding begins.
93
+ *
94
+ * @param {{type:('js'|'wasm')}} config - The decoder config.
95
+ * @return {DRACOLoader} A reference to this loader.
96
+ */
97
+ setDecoderConfig( config ) {
98
+
99
+ this.decoderConfig = config;
100
+
101
+ return this;
102
+
103
+ }
104
+
105
+ /**
106
+ * Sets the maximum number of Web Workers to be used during decoding.
107
+ * A lower limit may be preferable if workers are also for other tasks in the application.
108
+ *
109
+ * @param {number} workerLimit - The worker limit.
110
+ * @return {DRACOLoader} A reference to this loader.
111
+ */
112
+ setWorkerLimit( workerLimit ) {
113
+
114
+ this.workerLimit = workerLimit;
115
+
116
+ return this;
117
+
118
+ }
119
+
120
+ /**
121
+ * Starts loading from the given URL and passes the loaded Draco asset
122
+ * to the `onLoad()` callback.
123
+ *
124
+ * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
125
+ * @param {function(BufferGeometry)} onLoad - Executed when the loading process has been finished.
126
+ * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
127
+ * @param {onErrorCallback} onError - Executed when errors occur.
128
+ */
129
+ load( url, onLoad, onProgress, onError ) {
130
+
131
+ const loader = new THREE.FileLoader( this.manager );
132
+
133
+ loader.setPath( this.path );
134
+ loader.setResponseType( 'arraybuffer' );
135
+ loader.setRequestHeader( this.requestHeader );
136
+ loader.setWithCredentials( this.withCredentials );
137
+
138
+ loader.load( url, ( buffer ) => {
139
+
140
+ this.parse( buffer, onLoad, onError );
141
+
142
+ }, onProgress, onError );
143
+
144
+ }
145
+
146
+ /**
147
+ * Parses the given Draco data.
148
+ *
149
+ * @param {ArrayBuffer} buffer - The raw Draco data as an array buffer.
150
+ * @param {function(BufferGeometry)} onLoad - Executed when the loading/parsing process has been finished.
151
+ * @param {onErrorCallback} onError - Executed when errors occur.
152
+ */
153
+ parse( buffer, onLoad, onError = ()=>{} ) {
154
+
155
+ this.decodeDracoFile( buffer, onLoad, null, null, THREE.SRGBColorSpace, onError ).catch( onError );
156
+
157
+ }
158
+
159
+ //
160
+
161
+ decodeDracoFile( buffer, callback, attributeIDs, attributeTypes, vertexColorSpace = THREE.LinearSRGBColorSpace, onError = () => {} ) {
162
+
163
+ const taskConfig = {
164
+ attributeIDs: attributeIDs || this.defaultAttributeIDs,
165
+ attributeTypes: attributeTypes || this.defaultAttributeTypes,
166
+ useUniqueIDs: !! attributeIDs,
167
+ vertexColorSpace: vertexColorSpace,
168
+ };
169
+
170
+ return this.decodeGeometry( buffer, taskConfig ).then( callback ).catch( onError );
171
+
172
+ }
173
+
174
+ decodeGeometry( buffer, taskConfig ) {
175
+
176
+ const taskKey = JSON.stringify( taskConfig );
177
+
178
+ // Check for an existing task using this buffer. A transferred buffer cannot be transferred
179
+ // again from this thread.
180
+ if ( _taskCache.has( buffer ) ) {
181
+
182
+ const cachedTask = _taskCache.get( buffer );
183
+
184
+ if ( cachedTask.key === taskKey ) {
185
+
186
+ return cachedTask.promise;
187
+
188
+ } else if ( buffer.byteLength === 0 ) {
189
+
190
+ // Technically, it would be possible to wait for the previous task to complete,
191
+ // transfer the buffer back, and decode again with the second configuration. That
192
+ // is complex, and I don't know of any reason to decode a Draco buffer twice in
193
+ // different ways, so this is left unimplemented.
194
+ throw new Error(
195
+
196
+ 'THREE.DRACOLoader: Unable to re-decode a buffer with different ' +
197
+ 'settings. Buffer has already been transferred.'
198
+
199
+ );
200
+
201
+ }
202
+
203
+ }
204
+
205
+ //
206
+
207
+ let worker;
208
+ const taskID = this.workerNextTaskID ++;
209
+ const taskCost = buffer.byteLength;
210
+
211
+ // Obtain a worker and assign a task, and construct a geometry instance
212
+ // when the task completes.
213
+ const geometryPending = this._getWorker( taskID, taskCost )
214
+ .then( ( _worker ) => {
215
+
216
+ worker = _worker;
217
+
218
+ return new Promise( ( resolve, reject ) => {
219
+
220
+ worker._callbacks[ taskID ] = { resolve, reject };
221
+
222
+ worker.postMessage( { type: 'decode', id: taskID, taskConfig, buffer }, [ buffer ] );
223
+
224
+ // this.debug();
225
+
226
+ } );
227
+
228
+ } )
229
+ .then( ( message ) => this._createGeometry( message.geometry ) );
230
+
231
+ // Remove task from the task list.
232
+ // Note: replaced '.finally()' with '.catch().then()' block - iOS 11 support (#19416)
233
+ geometryPending
234
+ .catch( () => true )
235
+ .then( () => {
236
+
237
+ if ( worker && taskID ) {
238
+
239
+ this._releaseTask( worker, taskID );
240
+
241
+ // this.debug();
242
+
243
+ }
244
+
245
+ } );
246
+
247
+ // Cache the task result.
248
+ _taskCache.set( buffer, {
249
+
250
+ key: taskKey,
251
+ promise: geometryPending
252
+
253
+ } );
254
+
255
+ return geometryPending;
256
+
257
+ }
258
+
259
+ _createGeometry( geometryData ) {
260
+
261
+ const geometry = new THREE.BufferGeometry();
262
+
263
+ if ( geometryData.index ) {
264
+
265
+ geometry.setIndex( new THREE.BufferAttribute( geometryData.index.array, 1 ) );
266
+
267
+ }
268
+
269
+ for ( let i = 0; i < geometryData.attributes.length; i ++ ) {
270
+
271
+ const result = geometryData.attributes[ i ];
272
+ const name = result.name;
273
+ const array = result.array;
274
+ const itemSize = result.itemSize;
275
+
276
+ const attribute = new THREE.BufferAttribute( array, itemSize );
277
+
278
+ if ( name === 'color' ) {
279
+
280
+ this._assignVertexColorSpace( attribute, result.vertexColorSpace );
281
+
282
+ attribute.normalized = ( array instanceof Float32Array ) === false;
283
+
284
+ }
285
+
286
+ geometry.setAttribute( name, attribute );
287
+
288
+ }
289
+
290
+ return geometry;
291
+
292
+ }
293
+
294
+ _assignVertexColorSpace( attribute, inputColorSpace ) {
295
+
296
+ // While .drc files do not specify colorspace, the only 'official' tooling
297
+ // is PLY and OBJ converters, which use sRGB. We'll assume sRGB when a .drc
298
+ // file is passed into .load() or .parse(). GLTFLoader uses internal APIs
299
+ // to decode geometry, and vertex colors are already Linear-sRGB in there.
300
+
301
+ if ( inputColorSpace !== THREE.SRGBColorSpace ) return;
302
+
303
+ const _color = new THREE.Color();
304
+
305
+ for ( let i = 0, il = attribute.count; i < il; i ++ ) {
306
+
307
+ _color.fromBufferAttribute( attribute, i );
308
+ THREE.ColorManagement.colorSpaceToWorking( _color, THREE.SRGBColorSpace );
309
+ attribute.setXYZ( i, _color.r, _color.g, _color.b );
310
+
311
+ }
312
+
313
+ }
314
+
315
+ _loadLibrary( url, responseType ) {
316
+
317
+ const loader = new THREE.FileLoader( this.manager );
318
+ loader.setPath( this.decoderPath );
319
+ loader.setResponseType( responseType );
320
+ loader.setWithCredentials( this.withCredentials );
321
+
322
+ return new Promise( ( resolve, reject ) => {
323
+
324
+ loader.load( url, resolve, undefined, reject );
325
+
326
+ } );
327
+
328
+ }
329
+
330
+ preload() {
331
+
332
+ this._initDecoder();
333
+
334
+ return this;
335
+
336
+ }
337
+
338
+ _initDecoder() {
339
+
340
+ if ( this.decoderPending ) return this.decoderPending;
341
+
342
+ const useJS = typeof WebAssembly !== 'object' || this.decoderConfig.type === 'js';
343
+ const librariesPending = [];
344
+
345
+ if ( useJS ) {
346
+
347
+ librariesPending.push( this._loadLibrary( 'draco_decoder.js', 'text' ) );
348
+
349
+ } else {
350
+
351
+ librariesPending.push( this._loadLibrary( 'draco_wasm_wrapper.js', 'text' ) );
352
+ librariesPending.push( this._loadLibrary( 'draco_decoder.wasm', 'arraybuffer' ) );
353
+
354
+ }
355
+
356
+ this.decoderPending = Promise.all( librariesPending )
357
+ .then( ( libraries ) => {
358
+
359
+ const jsContent = libraries[ 0 ];
360
+
361
+ if ( ! useJS ) {
362
+
363
+ this.decoderConfig.wasmBinary = libraries[ 1 ];
364
+
365
+ }
366
+
367
+ const fn = DRACOWorker.toString();
368
+
369
+ const body = [
370
+ '/* draco decoder */',
371
+ jsContent,
372
+ '',
373
+ '/* worker */',
374
+ fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) )
375
+ ].join( '\n' );
376
+
377
+ this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
378
+
379
+ } );
380
+
381
+ return this.decoderPending;
382
+
383
+ }
384
+
385
+ _getWorker( taskID, taskCost ) {
386
+
387
+ return this._initDecoder().then( () => {
388
+
389
+ if ( this.workerPool.length < this.workerLimit ) {
390
+
391
+ const worker = new Worker( this.workerSourceURL );
392
+
393
+ worker._callbacks = {};
394
+ worker._taskCosts = {};
395
+ worker._taskLoad = 0;
396
+
397
+ worker.postMessage( { type: 'init', decoderConfig: this.decoderConfig } );
398
+
399
+ worker.onmessage = function ( e ) {
400
+
401
+ const message = e.data;
402
+
403
+ switch ( message.type ) {
404
+
405
+ case 'decode':
406
+ worker._callbacks[ message.id ].resolve( message );
407
+ break;
408
+
409
+ case 'error':
410
+ worker._callbacks[ message.id ].reject( message );
411
+ break;
412
+
413
+ default:
414
+ console.error( 'THREE.DRACOLoader: Unexpected message, "' + message.type + '"' );
415
+
416
+ }
417
+
418
+ };
419
+
420
+ this.workerPool.push( worker );
421
+
422
+ } else {
423
+
424
+ this.workerPool.sort( function ( a, b ) {
425
+
426
+ return a._taskLoad > b._taskLoad ? - 1 : 1;
427
+
428
+ } );
429
+
430
+ }
431
+
432
+ const worker = this.workerPool[ this.workerPool.length - 1 ];
433
+ worker._taskCosts[ taskID ] = taskCost;
434
+ worker._taskLoad += taskCost;
435
+ return worker;
436
+
437
+ } );
438
+
439
+ }
440
+
441
+ _releaseTask( worker, taskID ) {
442
+
443
+ worker._taskLoad -= worker._taskCosts[ taskID ];
444
+ delete worker._callbacks[ taskID ];
445
+ delete worker._taskCosts[ taskID ];
446
+
447
+ }
448
+
449
+ debug() {
450
+
451
+ console.log( 'Task load: ', this.workerPool.map( ( worker ) => worker._taskLoad ) );
452
+
453
+ }
454
+
455
+ dispose() {
456
+
457
+ for ( let i = 0; i < this.workerPool.length; ++ i ) {
458
+
459
+ this.workerPool[ i ].terminate();
460
+
461
+ }
462
+
463
+ this.workerPool.length = 0;
464
+
465
+ if ( this.workerSourceURL !== '' ) {
466
+
467
+ URL.revokeObjectURL( this.workerSourceURL );
468
+
469
+ }
470
+
471
+ return this;
472
+
473
+ }
474
+
475
+ }
476
+
477
+ /* WEB WORKER */
478
+
479
+ function DRACOWorker() {
480
+
481
+ let decoderConfig;
482
+ let decoderPending;
483
+
484
+ onmessage = function ( e ) {
485
+
486
+ const message = e.data;
487
+
488
+ switch ( message.type ) {
489
+
490
+ case 'init':
491
+ decoderConfig = message.decoderConfig;
492
+ decoderPending = new Promise( function ( resolve/*, reject*/ ) {
493
+
494
+ decoderConfig.onModuleLoaded = function ( draco ) {
495
+
496
+ // Module is Promise-like. Wrap before resolving to avoid loop.
497
+ resolve( { draco: draco } );
498
+
499
+ };
500
+
501
+ DracoDecoderModule( decoderConfig ); // eslint-disable-line no-undef
502
+
503
+ } );
504
+ break;
505
+
506
+ case 'decode':
507
+ const buffer = message.buffer;
508
+ const taskConfig = message.taskConfig;
509
+ decoderPending.then( ( module ) => {
510
+
511
+ const draco = module.draco;
512
+ const decoder = new draco.Decoder();
513
+
514
+ try {
515
+
516
+ const geometry = decodeGeometry( draco, decoder, new Int8Array( buffer ), taskConfig );
517
+
518
+ const buffers = geometry.attributes.map( ( attr ) => attr.array.buffer );
519
+
520
+ if ( geometry.index ) buffers.push( geometry.index.array.buffer );
521
+
522
+ self.postMessage( { type: 'decode', id: message.id, geometry }, buffers );
523
+
524
+ } catch ( error ) {
525
+
526
+ console.error( error );
527
+
528
+ self.postMessage( { type: 'error', id: message.id, error: error.message } );
529
+
530
+ } finally {
531
+
532
+ draco.destroy( decoder );
533
+
534
+ }
535
+
536
+ } );
537
+ break;
538
+
539
+ }
540
+
541
+ };
542
+
543
+ function decodeGeometry( draco, decoder, array, taskConfig ) {
544
+
545
+ const attributeIDs = taskConfig.attributeIDs;
546
+ const attributeTypes = taskConfig.attributeTypes;
547
+
548
+ let dracoGeometry;
549
+ let decodingStatus;
550
+
551
+ const geometryType = decoder.GetEncodedGeometryType( array );
552
+
553
+ if ( geometryType === draco.TRIANGULAR_MESH ) {
554
+
555
+ dracoGeometry = new draco.Mesh();
556
+ decodingStatus = decoder.DecodeArrayToMesh( array, array.byteLength, dracoGeometry );
557
+
558
+ } else if ( geometryType === draco.POINT_CLOUD ) {
559
+
560
+ dracoGeometry = new draco.PointCloud();
561
+ decodingStatus = decoder.DecodeArrayToPointCloud( array, array.byteLength, dracoGeometry );
562
+
563
+ } else {
564
+
565
+ throw new Error( 'THREE.DRACOLoader: Unexpected geometry type.' );
566
+
567
+ }
568
+
569
+ if ( ! decodingStatus.ok() || dracoGeometry.ptr === 0 ) {
570
+
571
+ throw new Error( 'THREE.DRACOLoader: Decoding failed: ' + decodingStatus.error_msg() );
572
+
573
+ }
574
+
575
+ const geometry = { index: null, attributes: [] };
576
+
577
+ // Gather all vertex attributes.
578
+ for ( const attributeName in attributeIDs ) {
579
+
580
+ const attributeType = self[ attributeTypes[ attributeName ] ];
581
+
582
+ let attribute;
583
+ let attributeID;
584
+
585
+ // A Draco file may be created with default vertex attributes, whose attribute IDs
586
+ // are mapped 1:1 from their semantic name (POSITION, NORMAL, ...). Alternatively,
587
+ // a Draco file may contain a custom set of attributes, identified by known unique
588
+ // IDs. glTF files always do the latter, and `.drc` files typically do the former.
589
+ if ( taskConfig.useUniqueIDs ) {
590
+
591
+ attributeID = attributeIDs[ attributeName ];
592
+ attribute = decoder.GetAttributeByUniqueId( dracoGeometry, attributeID );
593
+
594
+ } else {
595
+
596
+ attributeID = decoder.GetAttributeId( dracoGeometry, draco[ attributeIDs[ attributeName ] ] );
597
+
598
+ if ( attributeID === - 1 ) continue;
599
+
600
+ attribute = decoder.GetAttribute( dracoGeometry, attributeID );
601
+
602
+ }
603
+
604
+ const attributeResult = decodeAttribute( draco, decoder, dracoGeometry, attributeName, attributeType, attribute );
605
+
606
+ if ( attributeName === 'color' ) {
607
+
608
+ attributeResult.vertexColorSpace = taskConfig.vertexColorSpace;
609
+
610
+ }
611
+
612
+ geometry.attributes.push( attributeResult );
613
+
614
+ }
615
+
616
+ // Add index.
617
+ if ( geometryType === draco.TRIANGULAR_MESH ) {
618
+
619
+ geometry.index = decodeIndex( draco, decoder, dracoGeometry );
620
+
621
+ }
622
+
623
+ draco.destroy( dracoGeometry );
624
+
625
+ return geometry;
626
+
627
+ }
628
+
629
+ function decodeIndex( draco, decoder, dracoGeometry ) {
630
+
631
+ const numFaces = dracoGeometry.num_faces();
632
+ const numIndices = numFaces * 3;
633
+ const byteLength = numIndices * 4;
634
+
635
+ const ptr = draco._malloc( byteLength );
636
+ decoder.GetTrianglesUInt32Array( dracoGeometry, byteLength, ptr );
637
+ const index = new Uint32Array( draco.HEAPF32.buffer, ptr, numIndices ).slice();
638
+ draco._free( ptr );
639
+
640
+ return { array: index, itemSize: 1 };
641
+
642
+ }
643
+
644
+ function decodeAttribute( draco, decoder, dracoGeometry, attributeName, attributeType, attribute ) {
645
+
646
+ const numComponents = attribute.num_components();
647
+ const numPoints = dracoGeometry.num_points();
648
+ const numValues = numPoints * numComponents;
649
+ const byteLength = numValues * attributeType.BYTES_PER_ELEMENT;
650
+ const dataType = getDracoDataType( draco, attributeType );
651
+
652
+ const ptr = draco._malloc( byteLength );
653
+ decoder.GetAttributeDataArrayForAllPoints( dracoGeometry, attribute, dataType, byteLength, ptr );
654
+ const array = new attributeType( draco.HEAPF32.buffer, ptr, numValues ).slice();
655
+ draco._free( ptr );
656
+
657
+ return {
658
+ name: attributeName,
659
+ array: array,
660
+ itemSize: numComponents
661
+ };
662
+
663
+ }
664
+
665
+ function getDracoDataType( draco, attributeType ) {
666
+
667
+ switch ( attributeType ) {
668
+
669
+ case Float32Array: return draco.DT_FLOAT32;
670
+ case Int8Array: return draco.DT_INT8;
671
+ case Int16Array: return draco.DT_INT16;
672
+ case Int32Array: return draco.DT_INT32;
673
+ case Uint8Array: return draco.DT_UINT8;
674
+ case Uint16Array: return draco.DT_UINT16;
675
+ case Uint32Array: return draco.DT_UINT32;
676
+
677
+ }
678
+
679
+ }
680
+
681
+ }
682
+
683
+ exports.DRACOLoader = DRACOLoader;
@@ -35,7 +35,7 @@ var CentralPlant = /*#__PURE__*/function (_BaseDisposable) {
35
35
  * Initialize the CentralPlant manager
36
36
  *
37
37
  * @constructor
38
- * @version 0.3.16
38
+ * @version 0.3.18
39
39
  * @updated 2025-10-22
40
40
  *
41
41
  * @description Creates a new CentralPlant instance and initializes internal managers and utilities.