import { Directive , Input , OnChanges , OnInit , SimpleChanges } from '@angular/core' ; import * as THREE from 'three' ; import { RendererService } from '../renderer/renderer.service' ; @ Directive ( ) export abstract class AbstractCamera < T extends THREE . Camera > implements OnInit , OnChanges { camera : T ; @ Input ( ) positionX : number ; @ Input ( ) positionY : number ; @ Input ( ) positionZ : number ; @ Input ( ) zAxisUp = false ; @ Input ( ) layer : number ; constructor ( protected rendererService ) { } public ngOnInit ( ) { this . createCamera ( ) ; if ( this . layer ) { this . camera . layers . enable ( this . layer ) ; } else { this . camera . layers . enableAll ( ) ; } this . applyZAxisUp ( ) ; this . applyPosition ( ) ; this . rendererService . setCamera ( this ) ; } protected abstract createCamera ( ) ; public abstract updateAspectRatio ( aspect ) ; ngOnChanges ( changes ) { let mustRerender = false ; if ( [ 'positionX' , 'positionY' , 'positionZ' ] . some ( propName => propName in changes ) ) { this . applyPosition ( ) ; mustRerender = true ; } if ( mustRerender ) { this . rendererService . render ( ) ; } } protected applyPosition ( ) { if ( this . camera ) { this . camera . position . set ( this . positionX || 0 , this . positionY || 0 , this . positionZ || 0 ) ; } } protected applyZAxisUp ( ) { if ( this . camera && this . zAxisUp ) { this . camera . up . set ( 0 , 0 , 1 ) ; } } }