11import { NgIf } from '@angular/common' ;
22import { Component , CUSTOM_ELEMENTS_SCHEMA , ElementRef , inject , OnDestroy , ViewChild } from '@angular/core' ;
3- import { extend , injectBeforeRender , injectNgtLoader , NgtArgs , NgtCanvas , NgtPush , NgtStore } from 'angular-three' ;
3+ import {
4+ extend ,
5+ injectBeforeRender ,
6+ injectNgtLoader ,
7+ NgtArgs ,
8+ NgtCanvas ,
9+ NgtPush ,
10+ NgtRenderState ,
11+ NgtStore ,
12+ } from 'angular-three' ;
413import { map } from 'rxjs' ;
514import * as THREE from 'three' ;
615import { CCDIKHelper , CCDIKSolver , DRACOLoader , GLTFLoader , IKS , OrbitControls , TransformControls } from 'three-stdlib' ;
@@ -94,30 +103,7 @@ export class Scene implements OnDestroy {
94103 this . gui . add ( this . config , 'turnHead' ) . name ( 'turn head' ) ;
95104 this . gui . add ( this . config , 'ikSolver' ) . name ( 'IK Auto update' ) ;
96105
97- injectBeforeRender ( ( { gl, scene } ) => {
98- const head = this . ooi [ 'head' ] ;
99- const sphere = this . ooi . sphere ;
100-
101- if ( sphere && this . cubeCamera ) {
102- sphere . visible = false ;
103- sphere . getWorldPosition ( this . cubeCamera . nativeElement . position ) ;
104- this . cubeCamera . nativeElement . update ( gl , scene ) ;
105- sphere . visible = true ;
106- }
107-
108- if ( sphere && this . config . followSphere ) {
109- sphere . getWorldPosition ( v0 ) ;
110- this . orbitControls ?. target . lerp ( v0 , 0.1 ) ;
111- }
112-
113- if ( head && sphere && this . config . turnHead ) {
114- sphere . getWorldPosition ( v0 ) ;
115- head . lookAt ( v0 ) ;
116- head . rotation . set ( head . rotation . x , head . rotation . y + Math . PI , head . rotation . z ) ;
117- }
118-
119- if ( this . config . ikSolver && this . solver ) this . solver . update ( ) ;
120- } ) ;
106+ injectBeforeRender ( this . onBeforeRender . bind ( this ) ) ;
121107 }
122108
123109 onAfterModelAttach ( ) {
@@ -144,6 +130,31 @@ export class Scene implements OnDestroy {
144130 this . gui . open ( ) ;
145131 }
146132
133+ private onBeforeRender ( { gl, scene } : NgtRenderState ) {
134+ const head = this . ooi [ 'head' ] ;
135+ const sphere = this . ooi . sphere ;
136+
137+ if ( sphere && this . cubeCamera ) {
138+ sphere . visible = false ;
139+ sphere . getWorldPosition ( this . cubeCamera . nativeElement . position ) ;
140+ this . cubeCamera . nativeElement . update ( gl , scene ) ;
141+ sphere . visible = true ;
142+ }
143+
144+ if ( sphere && this . config . followSphere ) {
145+ sphere . getWorldPosition ( v0 ) ;
146+ this . orbitControls ?. target . lerp ( v0 , 0.1 ) ;
147+ }
148+
149+ if ( head && sphere && this . config . turnHead ) {
150+ sphere . getWorldPosition ( v0 ) ;
151+ head . lookAt ( v0 ) ;
152+ head . rotation . set ( head . rotation . x , head . rotation . y + Math . PI , head . rotation . z ) ;
153+ }
154+
155+ if ( this . config . ikSolver && this . solver ) this . solver . update ( ) ;
156+ }
157+
147158 ngOnDestroy ( ) {
148159 this . gui . domElement . remove ( ) ;
149160 }
0 commit comments