Skip to content

Commit 9eb7013

Browse files
committed
create <two5-texture-store> element
1 parent 698b8b3 commit 9eb7013

File tree

11 files changed

+118
-44
lines changed

11 files changed

+118
-44
lines changed

packages/twopoint5d-elements-e2e/pages/display-and-stage2d.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
<script type="module" src="/src/display-and-stage2d.ts"></script>
1111
<main class="two5-display-container">
1212
<two5-display class="two5-display">
13-
<x-texture-store src="/assets/textures.json">
13+
<two5-texture-store id="texstore" src="/assets/textures.json">
1414
<x-frame-buffer name="out1" width="640" height="480">
1515
<two5-stage2d id="stage2d" projection-type="parallax" width="197" height="205"></two5-stage2d>
1616
</x-frame-buffer>
17-
</x-texture-store>
17+
</two5-texture-store>
1818
</two5-display>
1919
</main>
2020
</body>

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
<script type="module" src="/src/post-processing.ts"></script>
1111
<main class="two5-display-container">
1212
<x-display class="two5-display">
13-
<x-post-processing>
14-
<x-stage2d id="stage2d" projection-type="parallax" width="197" height="205"></x-stage2d>
15-
</x-post-processing>
13+
<x-texture-store id="texstore" src="/assets/textures.json">
14+
<x-post-processing debug>
15+
<x-stage2d id="stage2d" projection-type="parallax" width="197" height="205"></x-stage2d>
16+
</x-post-processing>
17+
</x-texture-store>
1618
</x-display>
1719
</main>
1820
</body>
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
1-
import {TextureStore} from '@spearwolf/twopoint5d';
2-
import {Stage2DElement} from '@spearwolf/twopoint5d-elements';
1+
import {Stage2DElement, TextureStoreElement} from '@spearwolf/twopoint5d-elements';
32
import '@spearwolf/twopoint5d-elements/two5-display.js';
43
import '@spearwolf/twopoint5d-elements/two5-stage2d.js';
4+
import '@spearwolf/twopoint5d-elements/two5-texture-store.js';
55
import {Color, Scene, Sprite, SpriteMaterial} from 'three';
66
import './display.css';
77
import './style.css';
88

9-
console.log('hej ho!');
9+
const initialize = async (action: (stageEl: Stage2DElement, storeEl: TextureStoreElement) => void) => {
10+
const [stageEl, storeEl] = await Promise.all([
11+
Stage2DElement.whenDefined(document.getElementById('stage2d')),
12+
TextureStoreElement.whenDefined(document.getElementById('texstore')),
13+
]);
14+
action(stageEl, storeEl);
15+
};
1016

11-
const textures = new TextureStore().load('/assets/textures.json');
12-
13-
Stage2DElement.whenDefined(document.getElementById('stage2d')).then((el) => {
14-
// let renderFrameLogCount = 0;
15-
16-
// el.addEventListener(StageResize, (e: StageResizeEvent) => {
17-
// renderFrameLogCount = 0;
18-
// console.debug(StageResize, e.detail);
19-
// });
20-
21-
el.sceneReady().then((scene: Scene) => {
17+
initialize((stageEl, {store}) => {
18+
stageEl.sceneReady().then((scene: Scene) => {
2219
scene.background = new Color(0x212121);
23-
});
24-
25-
el.firstFrame().then(({renderer, scene}) => {
26-
textures.renderer = renderer;
2720

2821
const sprite = new Sprite();
2922
sprite.scale.set(197, 205, 1);
3023
scene.add(sprite);
3124

32-
textures.get('ballPatternRot', ['texture', 'imageCoords'], ([texture, imageCoords]) => {
25+
store.get('ballPatternRot', ['texture', 'imageCoords'], ([texture, imageCoords]) => {
3326
console.log('texture', {texture, imageCoords});
3427

3528
sprite.material?.dispose();
3629
sprite.material = new SpriteMaterial({map: texture});
3730
});
3831
});
39-
40-
// el.on(StageRenderFrame, (props: StageRenderFrameProps) => {
41-
// if (renderFrameLogCount++ < 3) {
42-
// console.debug(StageRenderFrame, props.frameNo, props);
43-
// }
44-
// });
4532
});

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

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
import {TextureStore} from '@spearwolf/twopoint5d';
2-
import {DisplayElement, Stage2DElement} from '@spearwolf/twopoint5d-elements';
1+
import {DisplayElement, Stage2DElement, TextureStoreElement} from '@spearwolf/twopoint5d-elements';
32
import {Color, Scene, Sprite, SpriteMaterial} from 'three';
43
import './display.css';
54
import './style.css';
65

7-
const initialize = async (action: (el: Stage2DElement) => void) => {
6+
const initialize = async (action: (stageEl: Stage2DElement, storeEl: TextureStoreElement) => void) => {
87
customElements.define('x-display', DisplayElement);
98
customElements.define('x-stage2d', Stage2DElement);
9+
customElements.define('x-texture-store', TextureStoreElement);
1010

11-
const el = await Stage2DElement.whenDefined(document.getElementById('stage2d'));
12-
action(el);
11+
const elems = await Promise.all([
12+
Stage2DElement.whenDefined(document.getElementById('stage2d')),
13+
TextureStoreElement.whenDefined(document.getElementById('texstore')),
14+
]);
15+
action(...elems);
1316
};
1417

15-
const textures = new TextureStore().load('/assets/textures.json');
16-
17-
initialize((el) => {
18-
el.sceneReady().then((scene: Scene) => {
18+
initialize((stageEl, {store}) => {
19+
stageEl.sceneReady().then((scene: Scene) => {
1920
scene.background = new Color(0x212121);
20-
});
21-
22-
el.firstFrame().then(({renderer, scene}) => {
23-
textures.renderer = renderer;
2421

2522
const sprite = new Sprite();
2623
sprite.scale.set(197, 205, 1);
2724
scene.add(sprite);
2825

29-
textures.get('ballPatternRot', ['texture', 'imageCoords'], ([texture, imageCoords]) => {
26+
store.get('ballPatternRot', ['texture', 'imageCoords'], ([texture, imageCoords]) => {
3027
console.log('texture', {texture, imageCoords});
3128

3229
sprite.material?.dispose();

packages/twopoint5d-elements/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,22 @@
3636
"./two5-stage2d.js": {
3737
"default": "./dist/src/two5-stage2d.js",
3838
"types": "./dist/src/two5-stage2d.d.ts"
39+
},
40+
"./two5-texture-store.js": {
41+
"default": "./dist/src/two5-texture-store.js",
42+
"types": "./dist/src/two5-texture-store.d.ts"
3943
}
4044
},
4145
"sideEffects": [
4246
"build/src/simple-greeting.js",
4347
"build/src/two5-display.js",
4448
"build/src/two5-stage2d.js",
49+
"build/src/two5-texture-store.js",
4550
"build/src/components/attachContextRoot.js",
4651
"dist/src/simple-greeting.js",
4752
"dist/src/two5-display.js",
4853
"dist/src/two5-stage2d.js",
54+
"dist/src/two5-texture-store.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/simple-greeting.js",
66
"src/two5-display.js",
77
"src/two5-stage2d.js",
8+
"src/two5-texture-store.js",
89
"src/components/attachContextRoot.js"
910
],
1011
"scripts": null,
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import './simple-greeting.js';
22
import './two5-display.js';
33
import './two5-stage2d.js';
4+
import './two5-texture-store.js';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {consume} from '@lit/context';
2+
import {eventize, type Eventize} from '@spearwolf/eventize';
3+
import {type SignalReader} from '@spearwolf/signalize';
4+
import {signal, signalReader} from '@spearwolf/signalize/decorators';
5+
import {Display, TextureStore} from '@spearwolf/twopoint5d';
6+
import {css, html} from 'lit';
7+
import {property} from 'lit/decorators.js';
8+
import {displayContext} from '../index.js';
9+
import {whenDefined} from '../utils/whenDefined.js';
10+
import {TwoPoint5DElement} from './TwoPoint5DElement.js';
11+
12+
export interface TextureStoreElement extends Eventize {}
13+
14+
export class TextureStoreElement extends TwoPoint5DElement {
15+
static async whenDefined(el: any): Promise<TextureStoreElement> {
16+
await whenDefined(el);
17+
if (el instanceof TextureStoreElement) {
18+
return el;
19+
}
20+
throw new Error('not a TextureStoreElement');
21+
}
22+
23+
static override styles = css`
24+
:host {
25+
display: inline;
26+
}
27+
`;
28+
29+
@property({type: String, reflect: true})
30+
accessor src: string | undefined;
31+
32+
@consume({context: displayContext, subscribe: true})
33+
@property({attribute: false})
34+
accessor displayCtx: Display | undefined;
35+
36+
@signal({readAsValue: true}) accessor display: Display | undefined;
37+
@signalReader() accessor display$: SignalReader<Display | undefined>;
38+
39+
@signal({readAsValue: true}) accessor href: string | undefined;
40+
@signalReader() accessor href$: SignalReader<string | undefined>;
41+
42+
readonly store = new TextureStore();
43+
44+
constructor() {
45+
super();
46+
eventize(this);
47+
48+
this.loggerNS = 'two5-texture-store';
49+
50+
this.display$((display) => {
51+
this.store.renderer = display?.renderer;
52+
this.logger?.log('received display', {display, el: this});
53+
});
54+
55+
this.href$((href) => {
56+
if (href?.trim()) {
57+
const url = new URL(href, window.location.href).href;
58+
this.logger?.log('load texture-store from', {url, el: this});
59+
this.store.load(url);
60+
}
61+
});
62+
}
63+
64+
override render() {
65+
this.display = this.displayCtx;
66+
this.href = this.src;
67+
68+
return html`<slot></slot>`;
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {createContext} from '@lit/context';
2+
import type {TextureStore} from '@spearwolf/twopoint5d';
3+
4+
export const textureStoreContext = createContext<TextureStore | undefined>(Symbol('texture-store'));

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';
22
export * from './components/SimpleGreeting.js';
33
export * from './components/Stage2DElement.js';
4+
export * from './components/TextureStoreElement.js';
45
export * from './components/TwoPoint5DElement.js';
56
export * from './context/display-context.js';
67
export * from './context/stage-renderer-context.js';
8+
export * from './context/texture-store-context.js';
79
export * from './utils/ConsoleLogger.js';
810
export * from './utils/asBoolean.js';
911
export * from './utils/readBooleanAttribute.js';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {TextureStoreElement} from './components/TextureStoreElement.js';
2+
import './components/attachContextRoot.js';
3+
4+
customElements.define('two5-texture-store', TextureStoreElement);

0 commit comments

Comments
 (0)