Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d01bce3

Browse files
ctranctran
ctran
authored and
ctran
committed
more postprocessing
1 parent 44ec83e commit d01bce3

File tree

6 files changed

+157
-3
lines changed

6 files changed

+157
-3
lines changed

apps/kitchen-sink/src/app/postprocessing/basic/experience.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component, ElementRef, viewChild } from '@angular/core';
22
import { NgtArgs, extend, injectBeforeRender } from 'angular-three';
3-
import { NgtpDepthOfField, NgtpEffectComposer, NgtpEffects } from 'angular-three-postprocessing';
3+
import { NgtpEffectComposer, NgtpEffects, NgtpGlitch } from 'angular-three-postprocessing';
44
import * as THREE from 'three';
55
import { Group, Vector2 } from 'three';
66

@@ -30,12 +30,12 @@ extend(THREE);
3030
3131
<ngtp-effect-composer>
3232
<ng-template effects>
33-
<ngtp-depth-of-field [options]="{ focusDistance: 0, focalLength: 0.02, bokehScale: 2 }" />
33+
<ngtp-glitch />
3434
</ng-template>
3535
</ngtp-effect-composer>
3636
`,
3737
schemas: [CUSTOM_ELEMENTS_SCHEMA],
38-
imports: [NgtpEffectComposer, NgtpEffects, NgtArgs, NgtpDepthOfField],
38+
imports: [NgtpEffectComposer, NgtpEffects, NgtArgs, NgtpGlitch],
3939
changeDetection: ChangeDetectionStrategy.OnPush,
4040
host: { class: 'experience-basic-postprocessing' },
4141
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component, inject, input } from '@angular/core';
2+
import { NgtArgs, extend } from 'angular-three';
3+
import { DotScreenEffect } from 'postprocessing';
4+
import { NgtpEffect, NgtpEffectBlendMode, NgtpEffectHostDirective } from '../effect';
5+
6+
extend({ DotScreenEffect });
7+
8+
export type DotScreenEffectOptions = Partial<NonNullable<ConstructorParameters<typeof DotScreenEffect>[0]>>;
9+
10+
@Component({
11+
selector: 'ngtp-dot-screen',
12+
template: `
13+
<ngt-dot-screen-effect *args="[options()]" [camera]="effect.camera()" [ref]="effect.effectRef()" ngtCompound>
14+
<ngtp-effect-blend-mode />
15+
<ng-content />
16+
</ngt-dot-screen-effect>
17+
`,
18+
standalone: true,
19+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
20+
changeDetection: ChangeDetectionStrategy.OnPush,
21+
imports: [NgtArgs, NgtpEffectBlendMode],
22+
hostDirectives: [NgtpEffectHostDirective],
23+
})
24+
export class NgtpDotScreen {
25+
effect = inject(NgtpEffect, { host: true });
26+
options = input({} as Omit<DotScreenEffectOptions, 'blendFunction'>);
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component, inject, input } from '@angular/core';
2+
import { NgtArgs, extend } from 'angular-three';
3+
import { FXAAEffect } from 'postprocessing';
4+
import { NgtpEffect, NgtpEffectBlendMode, NgtpEffectHostDirective } from '../effect';
5+
6+
extend({ FXAAEffect });
7+
8+
export type FXAAEffectOptions = Partial<NonNullable<ConstructorParameters<typeof FXAAEffect>[0]>>;
9+
10+
@Component({
11+
selector: 'ngtp-fxaa',
12+
template: `
13+
<ngt-fXAA-effect *args="[options()]" [camera]="effect.camera()" [ref]="effect.effectRef()" ngtCompound>
14+
<ngtp-effect-blend-mode />
15+
<ng-content />
16+
</ngt-fXAA-effect>
17+
`,
18+
standalone: true,
19+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
20+
changeDetection: ChangeDetectionStrategy.OnPush,
21+
imports: [NgtArgs, NgtpEffectBlendMode],
22+
hostDirectives: [NgtpEffectHostDirective],
23+
})
24+
export class NgtpFXAA {
25+
effect = inject(NgtpEffect, { host: true });
26+
options = input({} as Omit<FXAAEffectOptions, 'blendFunction'>);
27+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import {
2+
CUSTOM_ELEMENTS_SCHEMA,
3+
ChangeDetectionStrategy,
4+
Component,
5+
afterNextRender,
6+
computed,
7+
input,
8+
} from '@angular/core';
9+
import { NgtArgs, NgtVector2, injectNgtRef, injectNgtStore } from 'angular-three';
10+
import { injectAutoEffect } from 'ngxtension/auto-effect';
11+
import { mergeInputs } from 'ngxtension/inject-inputs';
12+
import { GlitchEffect, GlitchMode } from 'postprocessing';
13+
import { vector2 } from '../utils';
14+
15+
export type GlitchProps = NonNullable<ConstructorParameters<typeof GlitchEffect>[0]> &
16+
Partial<{
17+
mode: GlitchMode;
18+
active: boolean;
19+
delay: NgtVector2;
20+
duration: NgtVector2;
21+
chromaticAberrationOffset: NgtVector2;
22+
strength: NgtVector2;
23+
}>;
24+
25+
@Component({
26+
selector: 'ngtp-glitch',
27+
standalone: true,
28+
template: `
29+
<ngt-primitive *args="[effect()]" [ref]="effectRef()" [dispose]="null" ngtCompound />
30+
`,
31+
imports: [NgtArgs],
32+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
33+
changeDetection: ChangeDetectionStrategy.OnPush,
34+
})
35+
export class NgtpGlitch {
36+
autoEffect = injectAutoEffect();
37+
store = injectNgtStore();
38+
invalidate = this.store.select('invalidate');
39+
40+
effectRef = input(injectNgtRef<GlitchEffect>());
41+
options = input({ active: true } as GlitchProps, { transform: mergeInputs({ active: true }) });
42+
43+
active = computed(() => this.options().active);
44+
mode = computed(() => this.options().mode);
45+
46+
delay = vector2(this.options, 'delay');
47+
duration = vector2(this.options, 'duration');
48+
chromaticAberrationOffset = vector2(this.options, 'chromaticAberrationOffset');
49+
strength = vector2(this.options, 'strength');
50+
51+
effect = computed(() => {
52+
const [
53+
{ ratio, dtSize, columns, blendFunction, perturbationMap },
54+
delay,
55+
duration,
56+
chromaticAberrationOffset,
57+
strength,
58+
] = [this.options(), this.delay(), this.duration(), this.chromaticAberrationOffset(), this.strength()];
59+
return new GlitchEffect({
60+
ratio,
61+
dtSize,
62+
columns,
63+
blendFunction,
64+
perturbationMap,
65+
delay,
66+
duration,
67+
chromaticAberrationOffset,
68+
strength,
69+
});
70+
});
71+
72+
constructor() {
73+
afterNextRender(() => {
74+
this.autoEffect(() => {
75+
const effect = this.effect();
76+
return () => effect.dispose();
77+
});
78+
79+
this.autoEffect(() => {
80+
const [effect, invalidate, mode, active] = [this.effect(), this.invalidate(), this.mode(), this.active()];
81+
effect.mode = active ? mode || GlitchMode.SPORADIC : GlitchMode.DISABLED;
82+
invalidate();
83+
});
84+
});
85+
}
86+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ export * from './color-average';
66
export * from './color-depth';
77
export * from './depth';
88
export * from './depth-of-field';
9+
export * from './dot-screen';
10+
export * from './fxaa';
11+
export * from './glitch';

libs/postprocessing/src/lib/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Signal, computed } from '@angular/core';
2+
import { Vector2, Vector2Tuple } from 'three';
3+
4+
export function vector2<TObject extends object>(options: Signal<TObject>, key: keyof TObject) {
5+
return computed(() => {
6+
const value = options()[key];
7+
if (typeof value === 'number') return new Vector2(value, value);
8+
else if (value) return new Vector2(...(value as Vector2Tuple));
9+
else return new Vector2();
10+
});
11+
}

0 commit comments

Comments
 (0)