Skip to content

Commit 0342d33

Browse files
committed
rework: theater zones and establish big wip dir
1 parent a4b4d0b commit 0342d33

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+203
-172
lines changed

README.md

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,70 +21,74 @@ The theater coordinates your rendering thread and displays the results onto a ca
2121

2222
The theater is split into two main parts. Each will live in its own separate bundle.
2323
- *The Frontstage* hosts the visible canvas in the dom. It doesn't import Babylon, and will be lean in filesize.
24-
- *The Backstage* run inside a web worker, and performs rendering work. It imports Babylon, and will be fat in filesize.
24+
- *The Backstage* runs inside a web worker, and performs rendering work. It imports Babylon, and will be fat in filesize.
2525

2626
### Theater project setup
2727
1. Write your `spec.ts` type, which describes your renderable world
2828
```ts
29+
import {AsFigmentSpec} from "@benev/marduk/x/theater/index.pure.js"
30+
2931
export type MySpec = AsFigmentSpec<{
3032

31-
// Describe all the renderable objects in your world,
33+
// Describe a renderable object in your world,
3234
// in terms of serializable data.
3335
hippo: {hungry: boolean}
3436
}>
3537
```
38+
- notice the specific locations from where we're importing things
3639
1. Establish your `backstage.ts` module (this will be a web worker)
3740
```ts
3841
import {MySpec} from "./spec.js"
39-
import {theaterWorker, babylonBackstage} from "@benev/marduk/x/theater/index.back.babylon.js"
42+
import {babylonBackstage, theaterWorker} from "@benev/marduk/x/theater/index.babylon.js"
4043

4144
void async function() {
42-
await theaterWorker(
43-
await babylonBackstage<MySpec>(async stagecraft => ({
44-
45-
// now we define a renderer for this object
46-
hippo: (id, data) => {
47-
console.log("spawn", id, data)
48-
return {
49-
update: data => console.log("update", id, data),
50-
dispose: () => console.log("dispose", id, data),
51-
}
52-
},
53-
}))
54-
)
45+
46+
// defining a babylon backstage
47+
// (you could make a backstage for a different kind of renderer)
48+
const backstage = await babylonBackstage<MySpec>(async stagecraft => ({
49+
50+
// now we define a renderer for this object
51+
hippo: (id, data) => {
52+
console.log("spawn", id, data)
53+
return {
54+
update: data => console.log("update", id, data),
55+
dispose: () => console.log("dispose", id, data),
56+
}
57+
},
58+
}))
59+
60+
// establishing the web worker
61+
await theaterWorker(backstage)
5562
}()
5663
```
5764
- now bundle this module as `backstage.bundle.js` using `rollup`, `esbuild`, `parcel`, `vite`, or whatever
58-
1. Establish your `frontstage.ts` module (this will be your app's main entrypoint)
65+
1. Create your `frontstage.ts` module (this will be your app's main entrypoint)
5966
```ts
6067
import {MySpec} from "./spec.js"
61-
import {theaterHost, Frontstage, theaterElement, register} from "@benev/marduk/x/theater/index.front.js"
68+
import {Frontstage, register} from "@benev/marduk/x/theater/index.dom.js"
6269
6370
void async function() {
6471
const workerUrl = new URL("./backstage.bundle.js", import.meta.url)
65-
const theater = await theaterHost<MySpec>({workerUrl})
66-
const frontstage = new Frontstage(theater)
72+
const frontstage = await Frontstage.make<MySpec>({workerUrl})
6773
register(frontstage.getElements())
6874
6975
// okay, now you're ready to manipulate your world
7076
71-
// spawn a hippo
72-
await frontstage.theater.backstage.setFigments([[0, ["hippo", {hungry: false}]]])
73-
74-
// update the hippo
75-
await frontstage.theater.backstage.setFigments([[0, ["hippo", {hungry: true}]]])
76-
77-
// delete the hippo
78-
await frontstage.theater.backstage.setFigments([[0, undefined]])
77+
// spawn, then update, then delete a hippo (lol, just to exercise the full lifecycle)
78+
await frontstage.syncFigments([[0, ["hippo", {hungry: false}]]])
79+
await frontstage.syncFigments([[0, ["hippo", {hungry: true}]]])
80+
await frontstage.syncFigments([[0, undefined]])
7981
}()
8082
```
8183
- now bundle this module as `frontstage.bundle.js`
82-
1. Load the frontstage onto your web page
84+
1. Load the frontstage bundle onto your web page
8385
```html
8486
<script type=module src="./frontstage.bundle.js"></script>
8587
```
8688
1. Place the `<marduk-theater>` element in your html body
8789
```html
8890
<marduk-theater></marduk-theater>
8991
```
92+
1. 🎉 You're done!
93+
- If you run the page, you should see the console logging about the hippo lifecycle events.
9094

s/demo/main.bundle.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11

22
import {register} from "@benev/slate"
33
import {demoFrontstage} from "./theater/front.js"
4-
import {theaterElement} from "../theater/element/element.js"
54

65
const frontstage = await demoFrontstage()
7-
const MardukTheater = theaterElement(frontstage)
8-
register({MardukTheater})
6+
register(frontstage.getElements())
97
console.log("👁️")
108

s/demo/theater/back.worker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
import {DemoFigmentSpec} from "./spec.js"
3-
import {theaterWorker} from "../../theater/theater-worker.js"
4-
import {babylonBackstage} from "../../theater/babylon-backstage.js"
3+
import {theaterWorker, babylonBackstage} from "../../theater/index.babylon.js"
54

65
theaterWorker(
76
await babylonBackstage<DemoFigmentSpec>(async _stagecraft => ({

s/demo/theater/front.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11

22
import {DemoFigmentSpec} from "./spec.js"
3-
import {theaterHost} from "../../theater/theater-host.js"
4-
import {Frontstage} from "../../theater/parts/frontstage.js"
3+
import {Frontstage} from "../../theater/index.dom.js"
54

65
export async function demoFrontstage() {
76
const hash = document.head.querySelector("[data-commit-hash]")!.getAttribute("data-commit-hash")
87

98
const workerUrl = new URL(`./back.worker.bundle.js?v=${hash}`, import.meta.url)
10-
const theater = await theaterHost<DemoFigmentSpec>({workerUrl})
11-
const frontstage = new Frontstage(theater)
9+
const frontstage = await Frontstage.make<DemoFigmentSpec>({workerUrl})
1210

1311
// test full lifecycle
14-
await theater.backstage.setFigments([[0, ["hippo", {hungry: false}]]])
15-
await theater.backstage.setFigments([[0, ["hippo", {hungry: true}]]])
16-
await theater.backstage.setFigments([[0, undefined]])
12+
await frontstage.syncFigments([[0, ["hippo", {hungry: false}]]])
13+
await frontstage.syncFigments([[0, ["hippo", {hungry: true}]]])
14+
await frontstage.syncFigments([[0, undefined]])
1715

1816
return frontstage
1917
}

s/demo/theater/spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import {AsFigmentSpec} from "../../theater/parts/types.js"
2+
import {AsFigmentSpec} from "../../theater/index.pure.js"
33

44
export type DemoFigmentSpec = AsFigmentSpec<{
55
hippo: {hungry: boolean}

s/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11

2-
export * from "./babylon/index.js"
3-
export * from "./buckets/index.js"
4-
export * from "./nametag/index.js"
5-
export * from "./theater/parts/types.js"
2+
export * from "./wip/babylon/index.js"
3+
export * from "./wip/buckets/index.js"
4+
export * from "./wip/nametag/index.js"
65

7-
export * from "./tools/loading/load-image.js"
8-
export * from "./tools/babyloid.js"
9-
export * from "./tools/label.js"
10-
export * from "./tools/obtain_babylon_quaternion_from_mesh.js"
6+
export * from "./wip/tools/loading/load-image.js"
7+
export * from "./wip/tools/babyloid.js"
8+
export * from "./wip/tools/label.js"
9+
export * from "./wip/tools/obtain_babylon_quaternion_from_mesh.js"
1110
export * from "./tools/quick_hash.js"
1211
export * from "./tools/uncapitalize.js"
1312

s/tests.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import {Science} from "@e280/science"
3-
import buckets from "./buckets/buckets.test.js"
4-
import nametag from "./nametag/nametag.test.js"
3+
import buckets from "./wip/buckets/buckets.test.js"
4+
import nametag from "./wip/nametag/nametag.test.js"
55

66
await Science.run({
77
buckets,

s/theater/babylon-backstage.ts renamed to s/theater/babylon/babylon-backstage.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11

22
import {sub} from "@e280/stz"
33
import {Vec4} from "@benev/math"
4-
import {Scene} from "@babylonjs/core/scene.js"
5-
import {Engine} from "@babylonjs/core/Engines/engine.js"
6-
import {WebGPUEngine} from "@babylonjs/core/Engines/webgpuEngine.js"
74

8-
import {Backstage} from "./parts/backstage.js"
9-
import {make_scene} from "../babylon/iron/scene.js"
10-
import {Gameloop} from "../babylon/iron/gameloop.js"
11-
import {make_engine} from "../babylon/iron/engine.js"
12-
import {consolidateSpawners} from "./parts/spawners.js"
13-
import {Rendering} from "../babylon/iron/rendering/rendering.js"
14-
import {CanvasDetails, FigmentSpawners, FigmentSpec, Frame} from "./parts/types.js"
5+
import type {Scene} from "@babylonjs/core/scene.js"
6+
import type {Engine} from "@babylonjs/core/Engines/engine.js"
7+
import type {WebGPUEngine} from "@babylonjs/core/Engines/webgpuEngine.js"
8+
9+
import {consolidateSpawners} from "../browser/spawners.js"
10+
import {make_scene} from "../../wip/babylon/iron/scene.js"
11+
import {Gameloop} from "../../wip/babylon/iron/gameloop.js"
12+
import {make_engine} from "../../wip/babylon/iron/engine.js"
13+
import {FigmentSpawners, FigmentSpec} from "../pure/types.js"
14+
import {Backstage, CanvasDetails, Frame} from "../browser/types.js"
15+
import {Rendering} from "../../wip/babylon/iron/rendering/rendering.js"
1516

1617
export type BabylonStagecraft = {
1718
canvas: OffscreenCanvas
@@ -23,7 +24,7 @@ export type BabylonStagecraft = {
2324

2425
export async function babylonBackstage<Fs extends FigmentSpec>(
2526
establish: (stagecraft: BabylonStagecraft) => Promise<FigmentSpawners<Fs>>,
26-
) {
27+
): Promise<Backstage<Fs> & BabylonStagecraft> {
2728

2829
let frameCount = 0
2930
const onFrame = sub<[Frame]>()
@@ -53,7 +54,7 @@ export async function babylonBackstage<Fs extends FigmentSpec>(
5354
onFrame,
5455
updateCanvas,
5556
spawn: consolidateSpawners(spawners),
56-
} satisfies Backstage<Fs> & BabylonStagecraft
57+
}
5758
}
5859

5960
async function prepareBabylonStagecraft(): Promise<BabylonStagecraft> {

s/theater/parts/spawners.ts renamed to s/theater/browser/spawners.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import {Spawn} from "../../tools/lifecycler.js"
3-
import {FigmentId, FigmentSpawners, FigmentSpec, FigmentTuple} from "./types.js"
3+
import {FigmentId, FigmentSpawners, FigmentSpec, FigmentTuple} from "../pure/types.js"
44

55
export function consolidateSpawners<Fs extends FigmentSpec>(
66
spawners: FigmentSpawners<Fs>

s/theater/theater-worker.ts renamed to s/theater/browser/theater-worker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
import {Comrade, tune} from "@e280/comrade"
3-
import {Backstage} from "./parts/backstage.js"
4-
import {Lifecycler} from "../tools/lifecycler.js"
5-
import {FigmentId, FigmentSpec, FigmentTuple, TheaterSchematic} from "./parts/types.js"
3+
import {Lifecycler} from "../../tools/lifecycler.js"
4+
import {Backstage, TheaterSchematic} from "./types.js"
5+
import {FigmentId, FigmentSpec, FigmentTuple} from "../pure/types.js"
66

77
export async function theaterWorker<Fs extends FigmentSpec>(
88
backstage: Backstage<Fs>
@@ -16,7 +16,7 @@ export async function theaterWorker<Fs extends FigmentSpec>(
1616
async setCanvasDetails(details) {
1717
backstage.updateCanvas(details)
1818
},
19-
async setFigments(entries) {
19+
async syncFigments(entries) {
2020
for (const [id, tuple] of entries)
2121
lifecycler.sync(id, tuple)
2222
},

0 commit comments

Comments
 (0)