Skip to content

Commit bfb3dbb

Browse files
committed
refactor(postprocessing): update postprocessing with new renderer
1 parent b73211d commit bfb3dbb

31 files changed

+278
-204
lines changed

libs/postprocessing/n8ao/src/lib/n8ao.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { applyProps, NgtArgs, pick } from 'angular-three';
1414
import { NgtpEffectComposer } from 'angular-three-postprocessing';
1515
import { mergeInputs } from 'ngxtension/inject-inputs';
16-
import { ColorRepresentation } from 'three';
16+
import * as THREE from 'three';
1717

1818
export interface NgtpN8AOOptions {
1919
aoRadius: number;
@@ -25,7 +25,7 @@ export interface NgtpN8AOOptions {
2525
aoSamples: number;
2626
denoiseSamples: number;
2727
denoiseRadius: number;
28-
color: ColorRepresentation;
28+
color: THREE.ColorRepresentation;
2929
halfRes: boolean;
3030
depthAwareUpsampling: boolean;
3131
screenSpaceRadius: boolean;
@@ -78,7 +78,7 @@ export class NgtpN8AO {
7878

7979
private effectComposer = inject(NgtpEffectComposer);
8080

81-
effect = computed(() => {
81+
protected effect = computed(() => {
8282
const [scene, camera] = [this.effectComposer.scene(), this.effectComposer.camera()];
8383
return new N8AOPostPass(scene, camera);
8484
});

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
input,
1111
viewChild,
1212
} from '@angular/core';
13-
import { extend, getLocalState, injectBeforeRender, injectStore, pick } from 'angular-three';
13+
import { extend, getInstanceState, injectBeforeRender, injectStore, pick } from 'angular-three';
1414
import { mergeInputs } from 'ngxtension/inject-inputs';
1515
import {
1616
DepthDownsamplingPass,
@@ -22,7 +22,8 @@ import {
2222
Pass,
2323
RenderPass,
2424
} from 'postprocessing';
25-
import { Camera, Group, HalfFloatType, NoToneMapping, Scene, TextureDataType } from 'three';
25+
import * as THREE from 'three';
26+
import { Group } from 'three';
2627
import { isWebGL2Available } from 'three-stdlib';
2728

2829
export interface NgtpEffectComposerOptions {
@@ -34,18 +35,18 @@ export interface NgtpEffectComposerOptions {
3435
autoClear: boolean;
3536
resolutionScale?: number;
3637
multisampling: number;
37-
frameBufferType: TextureDataType;
38+
frameBufferType: THREE.TextureDataType;
3839
renderPriority: number;
39-
camera?: Camera;
40-
scene?: Scene;
40+
camera?: THREE.Camera;
41+
scene?: THREE.Scene;
4142
}
4243

4344
const defaultOptions: NgtpEffectComposerOptions = {
4445
enabled: true,
4546
renderPriority: 1,
4647
autoClear: true,
4748
multisampling: 8,
48-
frameBufferType: HalfFloatType,
49+
frameBufferType: THREE.HalfFloatType,
4950
};
5051

5152
function isConvolution(effect: Effect) {
@@ -67,10 +68,10 @@ export class NgtpEffectComposer {
6768

6869
private injector = inject(Injector);
6970
private store = injectStore();
70-
private size = this.store.select('size');
71-
private gl = this.store.select('gl');
72-
private defaultScene = this.store.select('scene');
73-
private defaultCamera = this.store.select('camera');
71+
// private size = this.store.select('size');
72+
// private gl = this.store.select('gl');
73+
// private defaultScene = this.store.select('scene');
74+
// private defaultCamera = this.store.select('camera');
7475

7576
private depthBuffer = pick(this.options, 'depthBuffer');
7677
private stencilBuffer = pick(this.options, 'stencilBuffer');
@@ -81,10 +82,10 @@ export class NgtpEffectComposer {
8182
private enabled = pick(this.options, 'enabled');
8283
private renderPriority = pick(this.options, 'renderPriority');
8384

84-
scene = computed(() => this.options().scene ?? this.defaultScene());
85-
camera = computed(() => this.options().camera ?? this.defaultCamera());
85+
scene = computed(() => this.options().scene ?? this.store.scene());
86+
camera = computed(() => this.options().camera ?? this.store.camera());
8687

87-
private groupRef = viewChild.required<ElementRef<Group>>('group');
88+
private groupRef = viewChild.required<ElementRef<THREE.Group>>('group');
8889

8990
private priority = computed(() => {
9091
const enabled = this.enabled();
@@ -105,7 +106,7 @@ export class NgtpEffectComposer {
105106
enableNormalPass,
106107
resolutionScale,
107108
] = [
108-
this.gl(),
109+
this.store.gl(),
109110
this.scene(),
110111
this.camera(),
111112
this.depthBuffer(),
@@ -149,16 +150,16 @@ export class NgtpEffectComposer {
149150

150151
// NOTE: Disable tone mapping because threejs disallows tonemapping on render targets
151152
effect((onCleanup) => {
152-
const gl = this.gl();
153+
const gl = this.store.gl();
153154
const currentTonemapping = gl.toneMapping;
154-
gl.toneMapping = NoToneMapping;
155+
gl.toneMapping = THREE.NoToneMapping;
155156
onCleanup(() => {
156157
gl.toneMapping = currentTonemapping;
157158
});
158159
});
159160

160161
effect(() => {
161-
const [{ composer }, { width, height }] = [this.composerData(), this.size()];
162+
const [{ composer }, width, height] = [this.composerData(), this.store.size.width(), this.store.size.height()];
162163
if (composer) {
163164
composer.setSize(width, height);
164165
}
@@ -174,10 +175,10 @@ export class NgtpEffectComposer {
174175
const passes: Pass[] = [];
175176

176177
if (composer) {
177-
const localState = getLocalState(group);
178-
if (!localState) return;
178+
const instanceState = getInstanceState(group);
179+
if (!instanceState) return;
179180

180-
const children = localState.nonObjects();
181+
const children = instanceState.nonObjects();
181182
for (let i = 0; i < children.length; i++) {
182183
const child = children[i];
183184
if (child instanceof Effect) {
@@ -222,7 +223,7 @@ export class NgtpEffectComposer {
222223
const [{ composer }, { enabled, autoClear, stencilBuffer }, gl] = [
223224
this.composerData(),
224225
this.options(),
225-
this.gl(),
226+
this.store.snapshot.gl,
226227
];
227228

228229
if (enabled) {

libs/postprocessing/src/lib/effect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ export class NgtpEffect {
3131
opacity = input(this.defaultEffectOptions?.opacity);
3232

3333
private store = injectStore();
34-
camera = this.store.select('camera');
35-
invalidate = this.store.select('invalidate');
34+
camera = this.store.camera;
35+
invalidate = this.store.invalidate;
3636
}

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component, computed, e
22
import { NgtArgs } from 'angular-three';
33
import { mergeInputs } from 'ngxtension/inject-inputs';
44
import { Effect } from 'postprocessing';
5-
import { CanvasTexture, Color, NearestFilter, RepeatWrapping, Texture, Uniform } from 'three';
5+
import * as THREE from 'three';
66

77
const fragment = /* language=glsl glsl */ `
88
uniform sampler2D uCharacters;
@@ -63,12 +63,12 @@ export class ASCIIEffect extends Effect {
6363
color = '#ffffff',
6464
invert = false,
6565
}: ASCIIEffectOptions = {}) {
66-
const uniforms = new Map<string, Uniform>([
67-
['uCharacters', new Uniform(new Texture())],
68-
['uCellSize', new Uniform(cellSize)],
69-
['uCharactersCount', new Uniform(characters.length)],
70-
['uColor', new Uniform(new Color(color))],
71-
['uInvert', new Uniform(invert)],
66+
const uniforms = new Map<string, THREE.Uniform>([
67+
['uCharacters', new THREE.Uniform(new THREE.Texture())],
68+
['uCellSize', new THREE.Uniform(cellSize)],
69+
['uCharactersCount', new THREE.Uniform(characters.length)],
70+
['uColor', new THREE.Uniform(new THREE.Color(color))],
71+
['uInvert', new THREE.Uniform(invert)],
7272
]);
7373

7474
super('ASCIIEffect', fragment, { uniforms });
@@ -81,14 +81,21 @@ export class ASCIIEffect extends Effect {
8181
}
8282

8383
/** Draws the characters on a Canvas and returns a texture */
84-
public createCharactersTexture(characters: string, font: string, fontSize: number): Texture {
84+
public createCharactersTexture(characters: string, font: string, fontSize: number): THREE.Texture {
8585
const canvas = document.createElement('canvas');
8686
const SIZE = 1024;
8787
const MAX_PER_ROW = 16;
8888
const CELL = SIZE / MAX_PER_ROW;
8989

9090
canvas.width = canvas.height = SIZE;
91-
const texture = new CanvasTexture(canvas, undefined, RepeatWrapping, RepeatWrapping, NearestFilter, NearestFilter);
91+
const texture = new THREE.CanvasTexture(
92+
canvas,
93+
undefined,
94+
THREE.RepeatWrapping,
95+
THREE.RepeatWrapping,
96+
THREE.NearestFilter,
97+
THREE.NearestFilter,
98+
);
9299
const context = canvas.getContext('2d');
93100

94101
if (!context) {
@@ -133,7 +140,8 @@ const defaultOptions: ASCIIEffectOptions = {
133140
})
134141
export class NgtpASCII {
135142
options = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
136-
effect = computed(() => new ASCIIEffect(this.options()));
143+
144+
protected effect = computed(() => new ASCIIEffect(this.options()));
137145

138146
constructor() {
139147
effect((onCleanup) => {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { NgtArgs, extend } from 'angular-three';
33
import { BlendFunction, BloomEffect, BloomEffectOptions } from 'postprocessing';
44
import { NgtpEffect, NgtpEffectBlendMode, provideDefaultEffectOptions } from '../effect';
55

6-
extend({ BloomEffect });
7-
86
@Component({
97
selector: 'ngtp-bloom',
108
template: `
@@ -20,6 +18,11 @@ extend({ BloomEffect });
2018
providers: [provideDefaultEffectOptions({ blendFunction: BlendFunction.ADD })],
2119
})
2220
export class NgtpBloom {
23-
effect = inject(NgtpEffect, { host: true });
2421
options = input({} as Omit<BloomEffectOptions, 'blendFunction'>);
22+
23+
protected effect = inject(NgtpEffect, { host: true });
24+
25+
constructor() {
26+
extend({ BloomEffect });
27+
}
2528
}

libs/postprocessing/src/lib/effects/brightness-contrast.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { NgtArgs, extend } from 'angular-three';
33
import { BrightnessContrastEffect } from 'postprocessing';
44
import { NgtpEffect, NgtpEffectBlendMode } from '../effect';
55

6-
extend({ BrightnessContrastEffect });
7-
86
export type BrightnessEffectOptions = NonNullable<ConstructorParameters<typeof BrightnessContrastEffect>[0]>;
97

108
@Component({
@@ -21,6 +19,10 @@ export type BrightnessEffectOptions = NonNullable<ConstructorParameters<typeof B
2119
hostDirectives: [{ directive: NgtpEffect, inputs: ['blendFunction', 'opacity'] }],
2220
})
2321
export class NgtpBrightnessContrast {
24-
effect = inject(NgtpEffect, { host: true });
2522
options = input({} as Omit<BrightnessEffectOptions, 'blendFunction'>);
23+
protected effect = inject(NgtpEffect, { host: true });
24+
25+
constructor() {
26+
extend({ BrightnessContrastEffect });
27+
}
2628
}

libs/postprocessing/src/lib/effects/chromatic-abberation.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { NgtArgs, extend } from 'angular-three';
33
import { ChromaticAberrationEffect } from 'postprocessing';
44
import { NgtpEffect, NgtpEffectBlendMode } from '../effect';
55

6-
extend({ ChromaticAberrationEffect });
7-
86
export type ChromaticAberrationEffectOptions = Partial<
97
NonNullable<ConstructorParameters<typeof ChromaticAberrationEffect>[0]>
108
>;
@@ -23,6 +21,10 @@ export type ChromaticAberrationEffectOptions = Partial<
2321
hostDirectives: [{ directive: NgtpEffect, inputs: ['blendFunction', 'opacity'] }],
2422
})
2523
export class NgtpChromaticAberration {
26-
effect = inject(NgtpEffect, { host: true });
2724
options = input({} as Omit<ChromaticAberrationEffectOptions, 'blendFunction'>);
25+
protected effect = inject(NgtpEffect, { host: true });
26+
27+
constructor() {
28+
extend({ ChromaticAberrationEffect });
29+
}
2830
}

libs/postprocessing/src/lib/effects/color-average.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { NgtArgs, extend } from 'angular-three';
33
import { mergeInputs } from 'ngxtension/inject-inputs';
44
import { BlendFunction, ColorAverageEffect } from 'postprocessing';
55

6-
extend({ ColorAverageEffect });
7-
86
@Component({
97
selector: 'ngtp-color-average',
108
template: `
@@ -21,4 +19,8 @@ export class NgtpColorAverage {
2119
{ blendFunction: BlendFunction.NORMAL },
2220
{ transform: mergeInputs({ blendFunction: BlendFunction.NORMAL }) },
2321
);
22+
23+
constructor() {
24+
extend({ ColorAverageEffect });
25+
}
2426
}

libs/postprocessing/src/lib/effects/color-depth.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { NgtArgs, extend } from 'angular-three';
33
import { ColorDepthEffect } from 'postprocessing';
44
import { NgtpEffect, NgtpEffectBlendMode } from '../effect';
55

6-
extend({ ColorDepthEffect });
7-
86
export type ColorDepthEffectOptions = Partial<NonNullable<ConstructorParameters<typeof ColorDepthEffect>[0]>>;
97

108
@Component({
@@ -21,6 +19,10 @@ export type ColorDepthEffectOptions = Partial<NonNullable<ConstructorParameters<
2119
hostDirectives: [{ directive: NgtpEffect, inputs: ['blendFunction', 'opacity'] }],
2220
})
2321
export class NgtpColorDepth {
24-
effect = inject(NgtpEffect, { host: true });
2522
options = input({} as Omit<ColorDepthEffectOptions, 'blendFunction'>);
23+
protected effect = inject(NgtpEffect, { host: true });
24+
25+
constructor() {
26+
extend({ ColorDepthEffect });
27+
}
2628
}

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {
99
} from '@angular/core';
1010
import { NgtAnyRecord, NgtArgs, NgtVector3 } from 'angular-three';
1111
import { DepthOfFieldEffect, MaskFunction } from 'postprocessing';
12-
import { DepthPackingStrategies, Texture, Vector3 } from 'three';
12+
import * as THREE from 'three';
1313
import { NgtpEffectComposer } from '../effect-composer';
1414

1515
type DOFOptions = NonNullable<ConstructorParameters<typeof DepthOfFieldEffect>[1]> &
16-
Partial<{ target: NgtVector3; depthTexture: { texture: Texture; packing: DepthPackingStrategies } }>;
16+
Partial<{ target: NgtVector3; depthTexture: { texture: THREE.Texture; packing: THREE.DepthPackingStrategies } }>;
1717

1818
@Component({
1919
selector: 'ngtp-depth-of-field',
@@ -27,20 +27,23 @@ type DOFOptions = NonNullable<ConstructorParameters<typeof DepthOfFieldEffect>[1
2727
export class NgtpDepthOfField {
2828
options = input({} as DOFOptions);
2929

30-
private autoFocus = computed(() => this.options().target != null);
31-
3230
private effectComposer = inject(NgtpEffectComposer);
3331

34-
effect = computed(() => {
35-
const [camera, options, autoFocus] = [this.effectComposer.camera(), this.options(), this.autoFocus()];
32+
protected effect = computed(() => {
33+
const [camera, options] = [this.effectComposer.camera(), this.options()];
34+
35+
const autoFocus = options.target != null;
3636

3737
const effect = new DepthOfFieldEffect(camera, options);
3838

39-
// Creating a target enables autofocus, R3F will set via props
40-
if (autoFocus) effect.target = new Vector3();
39+
// Creating a target enables autofocus, NGT will set via props
40+
if (autoFocus) effect.target = new THREE.Vector3();
4141
// Depth texture for depth picking with optional packing strategy
4242
if (options.depthTexture) {
43-
effect.setDepthTexture(options.depthTexture.texture, options.depthTexture.packing as DepthPackingStrategies);
43+
effect.setDepthTexture(
44+
options.depthTexture.texture,
45+
options.depthTexture.packing as THREE.DepthPackingStrategies,
46+
);
4447
}
4548
// Temporary fix that restores DOF 6.21.3 behavior, everything since then lets shapes leak through the blur
4649
const maskPass = (effect as NgtAnyRecord)['maskPass'];

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { NgtArgs, extend } from 'angular-three';
33
import { DepthEffect } from 'postprocessing';
44
import { NgtpEffect, NgtpEffectBlendMode } from '../effect';
55

6-
extend({ DepthEffect });
7-
86
export type DepthEffectOptions = Partial<NonNullable<ConstructorParameters<typeof DepthEffect>[0]>>;
97

108
@Component({
@@ -21,6 +19,10 @@ export type DepthEffectOptions = Partial<NonNullable<ConstructorParameters<typeo
2119
hostDirectives: [{ directive: NgtpEffect, inputs: ['blendFunction', 'opacity'] }],
2220
})
2321
export class NgtpDepth {
24-
effect = inject(NgtpEffect, { host: true });
2522
options = input({} as Omit<DepthEffectOptions, 'blendFunction'>);
23+
protected effect = inject(NgtpEffect, { host: true });
24+
25+
constructor() {
26+
extend({ DepthEffect });
27+
}
2628
}

0 commit comments

Comments
 (0)