@@ -6,12 +6,12 @@ import {
66 Injector ,
77 afterNextRender ,
88 computed ,
9+ effect ,
910 inject ,
1011 input ,
1112 viewChild ,
1213} from '@angular/core' ;
1314import { extend , getLocalState , injectBeforeRender , injectStore , pick } from 'angular-three' ;
14- import { injectAutoEffect } from 'ngxtension/auto-effect' ;
1515import { mergeInputs } from 'ngxtension/inject-inputs' ;
1616import {
1717 DepthDownsamplingPass ,
@@ -68,7 +68,6 @@ export class NgtpEffectComposer {
6868 options = input ( defaultOptions , { transform : mergeInputs ( defaultOptions ) } ) ;
6969
7070 private injector = inject ( Injector ) ;
71- private autoEffect = injectAutoEffect ( ) ;
7271 private store = injectStore ( ) ;
7372 private size = this . store . select ( 'size' ) ;
7473 private gl = this . store . select ( 'gl' ) ;
@@ -141,55 +140,24 @@ export class NgtpEffectComposer {
141140 constructor ( ) {
142141 extend ( { Group } ) ;
143142
144- afterNextRender ( ( ) => {
145- this . disableToneMapping ( ) ;
146- this . setComposerSize ( ) ;
147- this . updatePasses ( ) ;
148-
149- injectBeforeRender (
150- ( { delta } ) => {
151- const [ { composer } , { enabled, autoClear, stencilBuffer } , gl ] = [
152- this . composerData ( ) ,
153- this . options ( ) ,
154- this . gl ( ) ,
155- ] ;
156-
157- if ( enabled ) {
158- const currentAutoClear = gl . autoClear ;
159- gl . autoClear = autoClear ;
160- if ( stencilBuffer && ! autoClear ) gl . clearStencil ( ) ;
161- composer . render ( delta ) ;
162- gl . autoClear = currentAutoClear ;
163- }
164- } ,
165- { injector : this . injector , priority : this . options ( ) . enabled ? this . options ( ) . renderPriority : 0 } ,
166- ) ;
167- } ) ;
168- }
169-
170- // NOTE: Disable tone mapping because threejs disallows tonemapping on render targets
171- private disableToneMapping ( ) {
172- this . autoEffect ( ( ) => {
143+ // NOTE: Disable tone mapping because threejs disallows tonemapping on render targets
144+ effect ( ( onCleanup ) => {
173145 const gl = this . gl ( ) ;
174146 const currentTonemapping = gl . toneMapping ;
175147 gl . toneMapping = NoToneMapping ;
176- return ( ) => {
148+ onCleanup ( ( ) => {
177149 gl . toneMapping = currentTonemapping ;
178- } ;
150+ } ) ;
179151 } ) ;
180- }
181152
182- private setComposerSize ( ) {
183- this . autoEffect ( ( ) => {
153+ effect ( ( ) => {
184154 const [ { composer } , { width, height } ] = [ this . composerData ( ) , this . size ( ) ] ;
185155 if ( composer ) {
186156 composer . setSize ( width , height ) ;
187157 }
188158 } ) ;
189- }
190159
191- private updatePasses ( ) {
192- this . autoEffect ( ( ) => {
160+ effect ( ( onCleanup ) => {
193161 const [ group , { composer, normalPass, downSamplingPass } , camera ] = [
194162 this . groupRef ( ) ,
195163 this . composerData ( ) ,
@@ -232,11 +200,33 @@ export class NgtpEffectComposer {
232200 if ( downSamplingPass ) downSamplingPass . enabled = true ;
233201 }
234202
235- return ( ) => {
203+ onCleanup ( ( ) => {
236204 for ( const pass of passes ) composer ?. removePass ( pass ) ;
237205 if ( normalPass ) normalPass . enabled = false ;
238206 if ( downSamplingPass ) downSamplingPass . enabled = false ;
239- } ;
207+ } ) ;
208+ } ) ;
209+
210+ // NOTE: register beforeRender afterNextRender to ensure input is ready
211+ afterNextRender ( ( ) => {
212+ injectBeforeRender (
213+ ( { delta } ) => {
214+ const [ { composer } , { enabled, autoClear, stencilBuffer } , gl ] = [
215+ this . composerData ( ) ,
216+ this . options ( ) ,
217+ this . gl ( ) ,
218+ ] ;
219+
220+ if ( enabled ) {
221+ const currentAutoClear = gl . autoClear ;
222+ gl . autoClear = autoClear ;
223+ if ( stencilBuffer && ! autoClear ) gl . clearStencil ( ) ;
224+ composer . render ( delta ) ;
225+ gl . autoClear = currentAutoClear ;
226+ }
227+ } ,
228+ { injector : this . injector , priority : this . options ( ) . enabled ? this . options ( ) . renderPriority : 0 } ,
229+ ) ;
240230 } ) ;
241231 }
242232}
0 commit comments