@@ -20,6 +20,7 @@ import {
2020 type OnInit ,
2121} from '@angular/core' ;
2222import * as THREE from 'three' ;
23+ import { injectBeforeRender } from './di/before-render' ;
2324import { injectNgtRef } from './di/ref' ;
2425import { SPECIAL_INTERNAL_ADD_COMMENT } from './renderer/utils' ;
2526import { NgtSignalStore } from './stores/signal.store' ;
@@ -28,6 +29,7 @@ import type { NgtEventManager, NgtRenderState, NgtSize, NgtState } from './types
2829import { getLocalState , prepare } from './utils/instance' ;
2930import { is } from './utils/is' ;
3031import { safeDetectChanges } from './utils/safe-detect-changes' ;
32+ import { queueMicrotaskInInjectionContext } from './utils/timing' ;
3133import { updateCamera } from './utils/update' ;
3234
3335const privateKeys = [
@@ -57,7 +59,7 @@ export interface NgtPortalInputs {
5759}
5860
5961@Directive ( { selector : '[ngtPortalBeforeRender]' , standalone : true } )
60- export class NgtPortalBeforeRender implements OnInit {
62+ export class NgtPortalBeforeRender {
6163 readonly #portalStore = inject ( NgtStore ) ;
6264
6365 @Input ( ) renderPriority = 1 ;
@@ -66,36 +68,29 @@ export class NgtPortalBeforeRender implements OnInit {
6668
6769 @Output ( ) beforeRender = new EventEmitter < NgtRenderState > ( ) ;
6870
69- #subscription?: ( ) => void ;
70-
7171 constructor ( ) {
72- inject ( DestroyRef ) . onDestroy ( ( ) => {
73- this . #subscription?.( ) ;
74- } ) ;
75- }
76-
77- ngOnInit ( ) {
7872 let oldClear : boolean ;
79- this . #subscription = this . #portalStore. get ( 'internal' ) . subscribe (
80- ( { delta, frame } ) => {
81- this . beforeRender . emit ( { ...this . #portalStore. get ( ) , delta, frame } ) ;
82- const { gl, scene, camera } = this . #portalStore. get ( ) ;
83- oldClear = gl . autoClear ;
84- if ( this . renderPriority === 1 ) {
85- // clear scene and render with default
86- gl . autoClear = true ;
87- gl . render ( this . parentScene , this . parentCamera ) ;
88- }
89- // disable cleaning
90- gl . autoClear = false ;
91- gl . clearDepth ( ) ;
92- gl . render ( scene , camera ) ;
93- // restore
94- gl . autoClear = oldClear ;
95- } ,
96- this . renderPriority ,
97- this . #portalStore
98- ) ;
73+ queueMicrotaskInInjectionContext ( ( ) => {
74+ injectBeforeRender (
75+ ( { delta, frame } ) => {
76+ this . beforeRender . emit ( { ...this . #portalStore. get ( ) , delta, frame } ) ;
77+ const { gl, scene, camera } = this . #portalStore. get ( ) ;
78+ oldClear = gl . autoClear ;
79+ if ( this . renderPriority === 1 ) {
80+ // clear scene and render with default
81+ gl . autoClear = true ;
82+ gl . render ( this . parentScene , this . parentCamera ) ;
83+ }
84+ // disable cleaning
85+ gl . autoClear = false ;
86+ gl . clearDepth ( ) ;
87+ gl . render ( scene , camera ) ;
88+ // restore
89+ gl . autoClear = oldClear ;
90+ } ,
91+ { priority : this . renderPriority }
92+ ) ;
93+ } ) ;
9994 }
10095}
10196
@@ -133,7 +128,7 @@ export class NgtPortal extends NgtSignalStore<NgtPortalInputs> implements OnInit
133128 this . set ( { container } ) ;
134129 }
135130
136- @Input ( ) set state ( state : NgtPortalInputs [ 'state' ] ) {
131+ @Input ( ) set portalState ( state : NgtPortalInputs [ 'state' ] ) {
137132 this . set ( { state } ) ;
138133 }
139134
0 commit comments