Skip to content

Commit 272ed97

Browse files
committed
add <two5-glitch-pass> element
1 parent c409e1d commit 272ed97

File tree

10 files changed

+99
-11
lines changed

10 files changed

+99
-11
lines changed

packages/twopoint5d-elements-e2e/pages/post-processing.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
<x-display class="two5-display">
1313
<x-texture-store id="texstore" src="/assets/textures.json">
1414
<x-post-processing debug id="pp">
15-
<x-stage2d id="stage2d" projection-type="parallax" width="197" height="205"></x-stage2d>
15+
<x-stage2d id="stage2d" projection-type="parallax" width="197" height="205" />
16+
<x-glitch-pass debug />
1617
</x-post-processing>
1718
</x-texture-store>
1819
</x-display>

packages/twopoint5d-elements-e2e/src/post-processing.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import type {PostProcessingRenderer, TextureStore} from '@spearwolf/twopoint5d';
2-
import {DisplayElement, PostProcessingElement, Stage2DElement, TextureStoreElement} from '@spearwolf/twopoint5d-elements';
2+
import {
3+
DisplayElement,
4+
GlitchPassElement,
5+
PostProcessingElement,
6+
Stage2DElement,
7+
TextureStoreElement,
8+
} from '@spearwolf/twopoint5d-elements';
39
import {Color, Scene, Sprite, SpriteMaterial} from 'three';
4-
import {GlitchPass} from 'three/addons/postprocessing/GlitchPass.js';
510
import './display.css';
611
import './style.css';
712

@@ -12,6 +17,7 @@ const initialize = async (
1217
customElements.define('x-stage2d', Stage2DElement);
1318
customElements.define('x-texture-store', TextureStoreElement);
1419
customElements.define('x-post-processing', PostProcessingElement);
20+
customElements.define('x-glitch-pass', GlitchPassElement);
1521

1622
const [stageEl, storeEl, postProcessingEl] = await Promise.all([
1723
Stage2DElement.whenDefined(document.getElementById('stage2d')),
@@ -22,12 +28,9 @@ const initialize = async (
2228
action(await stageEl.firstFrame(), storeEl.store, postProcessingEl.renderer);
2329
};
2430

25-
initialize(({scene}, textureStore, postProcessing) => {
31+
initialize(({scene}, textureStore, _postProcessing) => {
2632
scene.background = new Color(0x212121);
2733

28-
const glitchPass = new GlitchPass();
29-
postProcessing.addPass(glitchPass);
30-
3134
const sprite = new Sprite();
3235
sprite.scale.set(197, 205, 1);
3336
scene.add(sprite);

packages/twopoint5d-elements/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,24 @@
4040
"./two5-post-processing.js": {
4141
"default": "./dist/src/two5-post-processing.js",
4242
"types": "./dist/src/two5-post-processing.d.ts"
43+
},
44+
"./two5-glitch-pass.js": {
45+
"default": "./dist/src/two5-glitch-pass.js",
46+
"types": "./dist/src/two5-glitch-pass.d.ts"
4347
}
4448
},
4549
"sideEffects": [
4650
"build/src/two5-display.js",
4751
"build/src/two5-stage2d.js",
4852
"build/src/two5-texture-store.js",
4953
"build/src/two5-post-processing.js",
54+
"build/src/two5-glitch-pass.js",
5055
"build/src/components/attachContextRoot.js",
5156
"dist/src/two5-display.js",
5257
"dist/src/two5-stage2d.js",
5358
"dist/src/two5-texture-store.js",
5459
"dist/src/two5-post-processing.js",
60+
"dist/src/two5-glitch-pass.js",
5561
"dist/src/components/attachContextRoot.js",
5662
"dist/src/bundle.js",
5763
"dist/bundle.js"

packages/twopoint5d-elements/package.override.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"src/two5-stage2d.js",
77
"src/two5-texture-store.js",
88
"src/two5-post-processing.js",
9+
"src/two5-glitch-pass.js",
910
"src/components/attachContextRoot.js"
1011
],
1112
"scripts": null,
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import './two5-display.js';
2+
import './two5-glitch-pass.js';
23
import './two5-post-processing.js';
34
import './two5-stage2d.js';
45
import './two5-texture-store.js';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {consume} from '@lit/context';
2+
import {effect, signal} from '@spearwolf/signalize/decorators';
3+
import {css, html} from 'lit';
4+
import {property} from 'lit/decorators.js';
5+
import {GlitchPass} from 'three/addons/postprocessing/GlitchPass.js';
6+
import {postProcessingContext, type IPostProcessingContext} from '../context/post-processing-context.js';
7+
import {whenDefined} from '../utils/whenDefined.js';
8+
import {TwoPoint5DElement} from './TwoPoint5DElement.js';
9+
10+
export class GlitchPassElement extends TwoPoint5DElement {
11+
static async whenDefined(el: any): Promise<GlitchPassElement> {
12+
await whenDefined(el);
13+
if (el instanceof GlitchPassElement) {
14+
return el;
15+
}
16+
throw new Error('not a GlitchPassElement');
17+
}
18+
19+
static override styles = css`
20+
:host {
21+
display: inline;
22+
}
23+
`;
24+
25+
@consume({context: postProcessingContext, subscribe: true})
26+
@property({attribute: false})
27+
@signal()
28+
accessor postProcessing: IPostProcessingContext | undefined;
29+
30+
@signal({readAsValue: true})
31+
accessor glitchPass = new GlitchPass();
32+
33+
@effect({deps: ['postProcessing', 'glitchPass']})
34+
onPostProcessingUpdate() {
35+
const postProcessing = this.postProcessing;
36+
if (postProcessing != null) {
37+
this.logger?.log('add glitchPass to postProcessing', {postProcessing, self: this});
38+
postProcessing.renderer.addPass(this.glitchPass);
39+
return () => {
40+
this.logger?.log('remove glitchPass from postProcessing', {postProcessing, self: this});
41+
postProcessing.renderer.removePass(this.glitchPass);
42+
};
43+
}
44+
}
45+
46+
constructor() {
47+
super();
48+
this.loggerNS = 'two5-glitch-pass';
49+
this.onPostProcessingUpdate();
50+
}
51+
52+
override render() {
53+
return html`<slot></slot>`;
54+
}
55+
}

packages/twopoint5d-elements/src/components/PostProcessingElement.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import {consume, provide} from '@lit/context';
1+
import {ContextProvider, consume, provide} from '@lit/context';
22
import {effect, signal} from '@spearwolf/signalize/decorators';
33
import {PostProcessingRenderer, type IStageRenderer} from '@spearwolf/twopoint5d';
44
import {css, html} from 'lit';
55
import {property} from 'lit/decorators.js';
6-
import {stageRendererContext} from '../index.js';
6+
import {postProcessingContext, type IPostProcessingContext} from '../context/post-processing-context.js';
7+
import {stageRendererContext} from '../context/stage-renderer-context.js';
78
import {whenDefined} from '../utils/whenDefined.js';
89
import {TwoPoint5DElement} from './TwoPoint5DElement.js';
910

10-
export class PostProcessingElement extends TwoPoint5DElement {
11+
export class PostProcessingElement extends TwoPoint5DElement implements IPostProcessingContext {
1112
static async whenDefined(el: any): Promise<PostProcessingElement> {
1213
await whenDefined(el);
1314
if (el instanceof PostProcessingElement) {
@@ -30,7 +31,7 @@ export class PostProcessingElement extends TwoPoint5DElement {
3031
@provide({context: stageRendererContext})
3132
accessor renderer = new PostProcessingRenderer();
3233

33-
@effect()
34+
@effect({signal: 'parentRenderer'})
3435
onParentRendererChange() {
3536
const parent = this.parentRenderer;
3637
if (parent) {
@@ -43,9 +44,15 @@ export class PostProcessingElement extends TwoPoint5DElement {
4344
}
4445
}
4546

47+
readonly #postProcessingProvider: ContextProvider<typeof postProcessingContext, typeof this>;
48+
4649
constructor() {
4750
super();
4851
this.loggerNS = 'two5-post-processing';
52+
53+
this.#postProcessingProvider = new ContextProvider(this, {context: postProcessingContext});
54+
this.#postProcessingProvider.setValue(this);
55+
4956
this.onParentRendererChange();
5057
}
5158

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {createContext} from '@lit/context';
2+
import type {PostProcessingRenderer} from '@spearwolf/twopoint5d';
3+
4+
export interface IPostProcessingContext {
5+
renderer: PostProcessingRenderer;
6+
}
7+
8+
export const postProcessingContext = createContext<IPostProcessingContext | undefined>(Symbol('post-processing'));

packages/twopoint5d-elements/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
export * from './components/DisplayElement.js';
2+
export * from './components/GlitchPassElement.js';
23
export * from './components/PostProcessingElement.js';
34
export * from './components/Stage2DElement.js';
45
export * from './components/TextureStoreElement.js';
56
export * from './components/TwoPoint5DElement.js';
67
export * from './context/display-context.js';
8+
export * from './context/post-processing-context.js';
79
export * from './context/stage-renderer-context.js';
810
export * from './context/texture-store-context.js';
911
export * from './utils/ConsoleLogger.js';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './components/attachContextRoot.js';
2+
import {GlitchPassElement} from './components/GlitchPassElement.js';
3+
4+
customElements.define('two5-glitch-pass', GlitchPassElement);

0 commit comments

Comments
 (0)