@@ -300,6 +300,99 @@ export class Entity extends Group {
300
300
301
301
}
302
302
303
+ toJSON ( ) {
304
+
305
+ const output = { } ;
306
+
307
+ output . uuid = this . uuid ;
308
+ output . matrix = this . matrix . toArray ( ) ;
309
+
310
+ if ( this . name !== '' ) output . name = this . name ;
311
+ if ( this . castShadow === true ) output . castShadow = true ;
312
+ if ( this . receiveShadow === true ) output . receiveShadow = true ;
313
+ if ( this . visible === false ) this . visible = false ;
314
+ if ( this . _enabled === false ) output . enabled = false ;
315
+
316
+ if ( this . componentData !== undefined ) {
317
+
318
+ const array = JSON . parse ( JSON . stringify ( this . componentData ) ) ;
319
+
320
+ output . components = [ ] ;
321
+
322
+ // sorting array to place non-dependency components last
323
+ array . sort ( ( a , b ) => {
324
+
325
+ const dependenciesA = ComponentManager . components [ a . type ] . config . dependencies ;
326
+ const dependenciesB = ComponentManager . components [ b . type ] . config . dependencies ;
327
+
328
+ if ( dependenciesA === undefined && dependenciesB !== undefined )
329
+ return 1 ;
330
+ return - 1 ;
331
+
332
+ } ) ;
333
+
334
+ while ( array . length > 0 ) {
335
+
336
+ let i = array . length ;
337
+
338
+ while ( i -- ) {
339
+
340
+ const dependencies = ComponentManager . components [ array [ i ] . type ] . config . dependencies ;
341
+
342
+ if ( dependencies !== undefined ) {
343
+
344
+ let wait = false ;
345
+ for ( let j = 0 , len = dependencies . length ; j < len ; j ++ ) {
346
+
347
+ if ( array . includes ( dependencies [ j ] ) ) {
348
+
349
+ wait = true ;
350
+ break ;
351
+
352
+ }
353
+
354
+ }
355
+
356
+ if ( wait === true ) {
357
+
358
+ continue ;
359
+
360
+ }
361
+
362
+ }
363
+
364
+ output . components . push ( array [ i ] ) ;
365
+ array . splice ( i , 1 ) ;
366
+
367
+ }
368
+
369
+ }
370
+
371
+ } else if ( this . components . length > 0 ) {
372
+
373
+ output . components = [ ] ;
374
+
375
+ for ( let i = 0 ; i < this . components . length ; i ++ ) {
376
+ if ( typeof this . components [ i ] . toJSON === 'function' )
377
+ output . components . push ( this . components . toJSON ( ) )
378
+
379
+ }
380
+
381
+ }
382
+
383
+ const children = this . getEntities ( ) ;
384
+ if ( children . length > 0 ) {
385
+
386
+ output . children = [ ] ;
387
+ for ( let i = 0 , len = children . length ; i < len ; i ++ )
388
+ output . children . push ( this . parseEntity ( children [ i ] ) ) ;
389
+
390
+ }
391
+
392
+ return output ;
393
+
394
+ }
395
+
303
396
get app ( ) {
304
397
305
398
return this . scene . app ;
0 commit comments