Skip to content

Commit 4d0918d

Browse files
committed
fix(postprocessing): remove afterNextRender and autoEffect
1 parent 7ecfb3f commit 4d0918d

File tree

8 files changed

+102
-158
lines changed

8 files changed

+102
-158
lines changed

libs/postprocessing/src/lib/effect-composer.ts

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import {
66
Injector,
77
afterNextRender,
88
computed,
9+
effect,
910
inject,
1011
input,
1112
viewChild,
1213
} from '@angular/core';
1314
import { extend, getLocalState, injectBeforeRender, injectStore, pick } from 'angular-three';
14-
import { injectAutoEffect } from 'ngxtension/auto-effect';
1515
import { mergeInputs } from 'ngxtension/inject-inputs';
1616
import {
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
}

libs/postprocessing/src/lib/effects/depth-of-field.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import {
22
CUSTOM_ELEMENTS_SCHEMA,
33
ChangeDetectionStrategy,
44
Component,
5-
afterNextRender,
65
computed,
6+
effect,
77
inject,
88
input,
99
} from '@angular/core';
1010
import { NgtAnyRecord, NgtArgs, NgtVector3 } from 'angular-three';
11-
import { injectAutoEffect } from 'ngxtension/auto-effect';
1211
import { DepthOfFieldEffect, MaskFunction } from 'postprocessing';
1312
import { DepthPackingStrategies, Texture, Vector3 } from 'three';
1413
import { NgtpEffectComposer } from '../effect-composer';
@@ -27,7 +26,6 @@ type DOFOptions = NonNullable<ConstructorParameters<typeof DepthOfFieldEffect>[1
2726
changeDetection: ChangeDetectionStrategy.OnPush,
2827
})
2928
export class NgtpDepthOfField {
30-
private autoEffect = injectAutoEffect();
3129
private effectComposer = inject(NgtpEffectComposer);
3230

3331
options = input({} as DOFOptions);
@@ -53,13 +51,9 @@ export class NgtpDepthOfField {
5351
});
5452

5553
constructor() {
56-
afterNextRender(() => {
57-
this.autoEffect(() => {
58-
const effect = this.effect();
59-
return () => {
60-
effect.dispose();
61-
};
62-
});
54+
effect((onCleanup) => {
55+
const depthOfFieldEffect = this.effect();
56+
onCleanup(() => depthOfFieldEffect.dispose());
6357
});
6458
}
6559
}

libs/postprocessing/src/lib/effects/glitch.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import {
2-
CUSTOM_ELEMENTS_SCHEMA,
3-
ChangeDetectionStrategy,
4-
Component,
5-
afterNextRender,
6-
computed,
7-
input,
8-
} from '@angular/core';
1+
import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component, computed, effect, input } from '@angular/core';
92
import { NgtArgs, NgtVector2, injectStore, pick, vector2 } from 'angular-three';
10-
import { injectAutoEffect } from 'ngxtension/auto-effect';
113
import { mergeInputs } from 'ngxtension/inject-inputs';
124
import { GlitchEffect, GlitchMode } from 'postprocessing';
135

@@ -32,7 +24,6 @@ export type GlitchOptions = NonNullable<ConstructorParameters<typeof GlitchEffec
3224
changeDetection: ChangeDetectionStrategy.OnPush,
3325
})
3426
export class NgtpGlitch {
35-
private autoEffect = injectAutoEffect();
3627
private store = injectStore();
3728
private invalidate = this.store.select('invalidate');
3829

@@ -68,17 +59,15 @@ export class NgtpGlitch {
6859
});
6960

7061
constructor() {
71-
afterNextRender(() => {
72-
this.autoEffect(() => {
73-
const effect = this.effect();
74-
return () => effect.dispose();
75-
});
62+
effect((onCleanup) => {
63+
const effect = this.effect();
64+
onCleanup(() => effect.dispose());
65+
});
7666

77-
this.autoEffect(() => {
78-
const [effect, invalidate, mode, active] = [this.effect(), this.invalidate(), this.mode(), this.active()];
79-
effect.mode = active ? mode || GlitchMode.SPORADIC : GlitchMode.DISABLED;
80-
invalidate();
81-
});
67+
effect(() => {
68+
const [glitchEffect, invalidate, mode, active] = [this.effect(), this.invalidate(), this.mode(), this.active()];
69+
glitchEffect.mode = active ? mode || GlitchMode.SPORADIC : GlitchMode.DISABLED;
70+
invalidate();
8271
});
8372
}
8473
}

libs/postprocessing/src/lib/effects/god-rays.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import {
33
ChangeDetectionStrategy,
44
Component,
55
ElementRef,
6-
afterNextRender,
76
computed,
7+
effect,
88
inject,
99
input,
1010
} from '@angular/core';
1111
import { NgtArgs, is } from 'angular-three';
12-
import { injectAutoEffect } from 'ngxtension/auto-effect';
1312
import { GodRaysEffect } from 'postprocessing';
1413
import { Mesh, Points } from 'three';
1514
import { NgtpEffectComposer } from '../effect-composer';
@@ -29,7 +28,6 @@ type GodRaysOptions = ConstructorParameters<typeof GodRaysEffect>[2] & {
2928
schemas: [CUSTOM_ELEMENTS_SCHEMA],
3029
})
3130
export class NgtpGodRays {
32-
private autoEffect = injectAutoEffect();
3331
private effectComposer = inject(NgtpEffectComposer);
3432

3533
options = input({} as GodRaysOptions);
@@ -40,11 +38,9 @@ export class NgtpGodRays {
4038
});
4139

4240
constructor() {
43-
afterNextRender(() => {
44-
this.autoEffect(() => {
45-
const [sun, effect] = [this.options().sun, this.effect()];
46-
effect.lightSource = is.ref(sun) ? sun.nativeElement : sun;
47-
});
41+
effect(() => {
42+
const [sun, godRaysEffect] = [this.options().sun, this.effect()];
43+
godRaysEffect.lightSource = is.ref(sun) ? sun.nativeElement : sun;
4844
});
4945
}
5046
}
Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import {
2-
CUSTOM_ELEMENTS_SCHEMA,
3-
ChangeDetectionStrategy,
4-
Component,
5-
afterNextRender,
6-
computed,
7-
input,
8-
} from '@angular/core';
1+
import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component, computed, effect, input } from '@angular/core';
92
import { NgtArgs, pick } from 'angular-three';
10-
import { injectAutoEffect } from 'ngxtension/auto-effect';
113
import { GridEffect } from 'postprocessing';
124

135
type GridOptions = NonNullable<ConstructorParameters<typeof GridEffect>[0]> &
@@ -24,8 +16,6 @@ type GridOptions = NonNullable<ConstructorParameters<typeof GridEffect>[0]> &
2416
schemas: [CUSTOM_ELEMENTS_SCHEMA],
2517
})
2618
export class NgtpGrid {
27-
private autoEffect = injectAutoEffect();
28-
2919
options = input({} as GridOptions);
3020
private size = pick(this.options, 'size');
3121

@@ -35,13 +25,11 @@ export class NgtpGrid {
3525
});
3626

3727
constructor() {
38-
afterNextRender(() => {
39-
this.autoEffect(() => {
40-
const [size, effect] = [this.size(), this.effect()];
41-
if (size) {
42-
effect.setSize(size.width, size.height);
43-
}
44-
});
28+
effect(() => {
29+
const [size, effect] = [this.size(), this.effect()];
30+
if (size) {
31+
effect.setSize(size.width, size.height);
32+
}
4533
});
4634
}
4735
}

libs/postprocessing/src/lib/effects/lens-flare.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import {
55
CUSTOM_ELEMENTS_SCHEMA,
66
ChangeDetectionStrategy,
77
Component,
8-
afterNextRender,
98
computed,
9+
effect,
1010
inject,
1111
input,
1212
} from '@angular/core';
1313
import { NgtArgs, injectBeforeRender, injectStore } from 'angular-three';
1414
import { easing } from 'maath';
15-
import { injectAutoEffect } from 'ngxtension/auto-effect';
1615
import { mergeInputs } from 'ngxtension/inject-inputs';
1716
import { BlendFunction, Effect } from 'postprocessing';
1817
import { Color, Mesh, Texture, Uniform, Vector2, Vector3, WebGLRenderTarget, WebGLRenderer } from 'three';
@@ -145,7 +144,6 @@ const defaultOptions: LensFlareOptions = {
145144
changeDetection: ChangeDetectionStrategy.OnPush,
146145
})
147146
export class NgtpLensFlare {
148-
private autoEffect = injectAutoEffect();
149147
private store = injectStore();
150148
private viewport = this.store.select('viewport');
151149
private raycaster = this.store.select('raycaster');
@@ -164,15 +162,13 @@ export class NgtpLensFlare {
164162
});
165163

166164
constructor() {
167-
afterNextRender(() => {
168-
this.autoEffect(() => {
169-
const [effect, viewport] = [this.effect(), this.viewport()];
170-
const iResolution = effect.uniforms.get('iResolution');
171-
if (iResolution) {
172-
iResolution.value.x = viewport.width;
173-
iResolution.value.y = viewport.height;
174-
}
175-
});
165+
effect(() => {
166+
const [lensFlareEffect, viewport] = [this.effect(), this.viewport()];
167+
const iResolution = lensFlareEffect.uniforms.get('iResolution');
168+
if (iResolution) {
169+
iResolution.value.x = viewport.width;
170+
iResolution.value.y = viewport.height;
171+
}
176172
});
177173

178174
injectBeforeRender(({ delta }) => {
Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import {
2-
CUSTOM_ELEMENTS_SCHEMA,
3-
ChangeDetectionStrategy,
4-
Component,
5-
afterNextRender,
6-
computed,
7-
input,
8-
} from '@angular/core';
1+
import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component, computed, effect, input } from '@angular/core';
92
import { NgtArgs, injectStore, pick } from 'angular-three';
10-
import { injectAutoEffect } from 'ngxtension/auto-effect';
113
import { BlendFunction, LUT3DEffect } from 'postprocessing';
124
import { Texture } from 'three';
135

@@ -28,7 +20,6 @@ export interface LUTOptions {
2820
changeDetection: ChangeDetectionStrategy.OnPush,
2921
})
3022
export class NgtpLUT {
31-
private autoEffect = injectAutoEffect();
3223
private store = injectStore();
3324
private invalidate = this.store.select('invalidate');
3425

@@ -41,18 +32,16 @@ export class NgtpLUT {
4132
});
4233

4334
constructor() {
44-
afterNextRender(() => {
45-
this.autoEffect(() => {
46-
const [effect, { lut, tetrahedralInterpolation }, invalidate] = [
47-
this.effect(),
48-
this.options(),
49-
this.invalidate(),
50-
];
35+
effect(() => {
36+
const [effect, { lut, tetrahedralInterpolation }, invalidate] = [
37+
this.effect(),
38+
this.options(),
39+
this.invalidate(),
40+
];
5141

52-
if (tetrahedralInterpolation) effect.tetrahedralInterpolation = tetrahedralInterpolation;
53-
if (lut) effect.lut = lut;
54-
invalidate();
55-
});
42+
if (tetrahedralInterpolation) effect.tetrahedralInterpolation = tetrahedralInterpolation;
43+
if (lut) effect.lut = lut;
44+
invalidate();
5645
});
5746
}
5847
}

0 commit comments

Comments
 (0)