import { Directive , OnDestroy } from '@angular/core' ; import { AbstractObject3D } from './abstract-object-3d' ; import * as THREE from 'three' ; @ Directive ( ) export abstract class AbstractLazyObject3D extends AbstractObject3D < THREE . Object3D > implements OnDestroy { /**
     * Flag to signal whether the parentScene class instance AbstractObject3D called the
     * overwritten method  AbstractModelLoader yet.
     *
     * Unless that method was called, no methods and properties of  AbstractObject3D * may be safely accessed, especially  AbstractObject3D and
     *  AbstractObject3D . renderer .
      private parentInitialized = false ; /**
     * This is reference to lazy loaded Object3D (async after init)
     */ protected lazyObject : THREE . Object3D | undefined ; /**
     * Abstract method for lazy loading
     *
     */ protected abstract loadLazyObject ( ) ; protected afterInit ( ) { super . afterInit ( ) ; this . parentInitialized = true ; this . startLoading ( ) ; } protected startLoading ( ) { this . loadLazyObject ( ) . then ( obj => { if ( this . lazyObject ) { super . getObject ( ) . remove ( this . lazyObject ) ; } this . lazyObject = obj ; super . getObject ( ) . add ( obj ) ; this . rendererService . render ( ) ; setTimeout ( ( ) => { this . rendererService . render ( ) ; } , 10 ) ; } ) . catch ( err => { console . error ( err ) ; } ) ; } ngOnDestroy ( ) { if ( this . lazyObject ) { super . getObject ( ) . remove ( this . lazyObject ) ; } } protected newObject3DInstance ( ) { return new THREE . Object3D ( ) ; } }