Skip to content

Commit e069376

Browse files
committed
add <two5-post-processing>
1 parent 2fd3afe commit e069376

File tree

10 files changed

+102
-7
lines changed

10 files changed

+102
-7
lines changed

packages/twopoint5d-elements-e2e/pages/bundle.html

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
<two5-display debug data-testid="display"></two5-display>
1212
<two5-stage2d debug data-testid="stage2d"></two5-stage2d>
1313
<two5-texture-store debug data-testid="texture-store"></two5-texture-store>
14+
<two5-post-processing debug data-testid="post-processing"></two5-post-processing>
1415
</body>
1516
</html>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<main class="two5-display-container">
1212
<x-display class="two5-display">
1313
<x-texture-store id="texstore" src="/assets/textures.json">
14-
<x-post-processing debug>
14+
<x-post-processing debug id="pp">
1515
<x-stage2d id="stage2d" projection-type="parallax" width="197" height="205"></x-stage2d>
1616
</x-post-processing>
1717
</x-texture-store>

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

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1-
import type {TextureStore} from '@spearwolf/twopoint5d';
2-
import {DisplayElement, Stage2DElement, TextureStoreElement} from '@spearwolf/twopoint5d-elements';
1+
import type {PostProcessingRenderer, TextureStore} from '@spearwolf/twopoint5d';
2+
import {DisplayElement, PostProcessingElement, Stage2DElement, TextureStoreElement} from '@spearwolf/twopoint5d-elements';
33
import {Color, Scene, Sprite, SpriteMaterial} from 'three';
4+
import {GlitchPass} from 'three/addons/postprocessing/GlitchPass.js';
45
import './display.css';
56
import './style.css';
67

7-
const initialize = async (action: (scene: Scene, store: TextureStore) => void) => {
8+
const initialize = async (
9+
action: (frame: {scene: Scene}, store: TextureStore, postProcessing: PostProcessingRenderer) => void,
10+
) => {
811
customElements.define('x-display', DisplayElement);
912
customElements.define('x-stage2d', Stage2DElement);
1013
customElements.define('x-texture-store', TextureStoreElement);
14+
customElements.define('x-post-processing', PostProcessingElement);
1115

12-
const [stageEl, storeEl] = await Promise.all([
16+
const [stageEl, storeEl, postProcessingEl] = await Promise.all([
1317
Stage2DElement.whenDefined(document.getElementById('stage2d')),
1418
TextureStoreElement.whenDefined(document.getElementById('texstore')),
19+
PostProcessingElement.whenDefined(document.getElementById('pp')),
1520
]);
1621

17-
action(await stageEl.sceneReady(), storeEl.store);
22+
action(await stageEl.firstFrame(), storeEl.store, postProcessingEl.renderer);
1823
};
1924

20-
initialize((scene, textureStore) => {
25+
initialize(({scene}, textureStore, postProcessing) => {
2126
scene.background = new Color(0x212121);
2227

28+
const glitchPass = new GlitchPass();
29+
postProcessing.addPass(glitchPass);
30+
2331
const sprite = new Sprite();
2432
sprite.scale.set(197, 205, 1);
2533
scene.add(sprite);

packages/twopoint5d-elements-e2e/tests/bundle.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ test.describe('bundle', () => {
4444
expect(await whenDefined(page, 'two5-texture-store')).toBe(true);
4545
});
4646
});
47+
48+
test.describe('two5-post-processing', () => {
49+
test('has element', async ({page}) => {
50+
await expect(page.getByTestId('post-processing')).toBeAttached();
51+
});
52+
53+
test('custom element is defined', async ({page}) => {
54+
expect(await whenDefined(page, 'two5-post-processing')).toBe(true);
55+
});
56+
});
4757
});

packages/twopoint5d-elements/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,22 @@
3636
"./two5-texture-store.js": {
3737
"default": "./dist/src/two5-texture-store.js",
3838
"types": "./dist/src/two5-texture-store.d.ts"
39+
},
40+
"./two5-post-processing.js": {
41+
"default": "./dist/src/two5-post-processing.js",
42+
"types": "./dist/src/two5-post-processing.d.ts"
3943
}
4044
},
4145
"sideEffects": [
4246
"build/src/two5-display.js",
4347
"build/src/two5-stage2d.js",
4448
"build/src/two5-texture-store.js",
49+
"build/src/two5-post-processing.js",
4550
"build/src/components/attachContextRoot.js",
4651
"dist/src/two5-display.js",
4752
"dist/src/two5-stage2d.js",
4853
"dist/src/two5-texture-store.js",
54+
"dist/src/two5-post-processing.js",
4955
"dist/src/components/attachContextRoot.js",
5056
"dist/src/bundle.js",
5157
"dist/bundle.js"

packages/twopoint5d-elements/package.override.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"src/two5-display.js",
66
"src/two5-stage2d.js",
77
"src/two5-texture-store.js",
8+
"src/two5-post-processing.js",
89
"src/components/attachContextRoot.js"
910
],
1011
"scripts": null,
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import './two5-display.js';
2+
import './two5-post-processing.js';
23
import './two5-stage2d.js';
34
import './two5-texture-store.js';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {consume, provide} from '@lit/context';
2+
import {eventize, type Eventize} from '@spearwolf/eventize';
3+
import {createEffect, type SignalReader} from '@spearwolf/signalize';
4+
import {signal, signalReader} from '@spearwolf/signalize/decorators';
5+
import {PostProcessingRenderer, type IStageRenderer} from '@spearwolf/twopoint5d';
6+
import {css, html} from 'lit';
7+
import {property} from 'lit/decorators.js';
8+
import {stageRendererContext} from '../index.js';
9+
import {whenDefined} from '../utils/whenDefined.js';
10+
import {TwoPoint5DElement} from './TwoPoint5DElement.js';
11+
12+
export interface PostProcessingElement extends Eventize {}
13+
14+
export class PostProcessingElement extends TwoPoint5DElement {
15+
static async whenDefined(el: any): Promise<PostProcessingElement> {
16+
await whenDefined(el);
17+
if (el instanceof PostProcessingElement) {
18+
return el;
19+
}
20+
throw new Error('not a PostProcessingElement');
21+
}
22+
23+
static override styles = css`
24+
:host {
25+
display: inline;
26+
}
27+
`;
28+
29+
@consume({context: stageRendererContext, subscribe: true})
30+
@property({attribute: false})
31+
accessor stageRendererCtx: IStageRenderer | undefined;
32+
33+
@signal({readAsValue: true}) accessor parentRenderer: IStageRenderer | undefined;
34+
@signalReader() accessor parentRenderer$: SignalReader<IStageRenderer | undefined>;
35+
36+
@provide({context: stageRendererContext})
37+
accessor renderer = new PostProcessingRenderer();
38+
39+
constructor() {
40+
super();
41+
eventize(this);
42+
43+
this.loggerNS = 'two5-post-processing';
44+
45+
createEffect(() => {
46+
const parent = this.parentRenderer;
47+
if (parent) {
48+
parent.addStage(this.renderer);
49+
this.logger?.log('added renderer to parent', this);
50+
return () => {
51+
parent.removeStage(this.renderer);
52+
this.logger?.log('removed renderer from parent', {parent, self: this});
53+
};
54+
}
55+
}, [this.parentRenderer$]);
56+
}
57+
58+
override render() {
59+
this.parentRenderer = this.stageRendererCtx;
60+
61+
return html`<slot></slot>`;
62+
}
63+
}

packages/twopoint5d-elements/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './components/DisplayElement.js';
2+
export * from './components/PostProcessingElement.js';
23
export * from './components/Stage2DElement.js';
34
export * from './components/TextureStoreElement.js';
45
export * from './components/TwoPoint5DElement.js';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {PostProcessingElement} from './components/PostProcessingElement.js';
2+
import './components/attachContextRoot.js';
3+
4+
customElements.define('two5-post-processing', PostProcessingElement);

0 commit comments

Comments
 (0)