Skip to content

Commit

Permalink
create multiViewRenderer WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
spearwolf committed Mar 23, 2024
1 parent 8a15ff5 commit 9bc209e
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 7 deletions.
6 changes: 4 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# @spearwolf/visual-fx-web-components

> _Generated by [scripts/makeTODO.mjs](scripts/makeTODO.mjs) at 2024-03-23T15:46:34.496Z_
> _Generated by [scripts/makeTODO.mjs](scripts/makeTODO.mjs) at 2024-03-23T17:11:46.494Z_
This file contains an overview of all TODO, FIXME or XXX comments extracted from the source files of the packages.

Expand All @@ -18,11 +18,12 @@ This file contains an overview of all TODO, FIXME or XXX comments extracted from
### TODOs
| Filename | line # | TODO |
|:------|:------:|:------|
| [packages/vfx/src/worker-sample.js](packages/vfx/src/worker-sample.js#L8) | 8 | create and export types for onload function |
| [packages/vfx/src/worker-sample.js](packages/vfx/src/worker-sample.js#L9) | 9 | create and export types for onload function |
| [packages/vfx/src/elements/VfxCtxElement.js](packages/vfx/src/elements/VfxCtxElement.js#L279) | 279 | initialize worker ? |
| [packages/vfx/src/elements/VfxCtxElement.js](packages/vfx/src/elements/VfxCtxElement.js#L285) | 285 | this.actor.send({type: 'workerFailed'}) ? |
| [packages/vfx/src/elements/VfxCtxElement.js](packages/vfx/src/elements/VfxCtxElement.js#L299) | 299 | what should happen with the events ? |
| [packages/vfx/src/elements/VfxCtxElement.js](packages/vfx/src/elements/VfxCtxElement.js#L312) | 312 | find a more friendly way to destroy the worker before termination |
| [packages/vfx/src/shadow-objects/ThreeRenderView.js](packages/vfx/src/shadow-objects/ThreeRenderView.js#L2) | 2 | implement ThreeRenderView |
| [packages/vfx/src/shadow-objects/VfxDisplay.js](packages/vfx/src/shadow-objects/VfxDisplay.js#L26) | 26 | use shared vfx.canvas|multiViewRenderer -------- |
| [packages/vfx/src/worker/MessageRouter.js](packages/vfx/src/worker/MessageRouter.js#L16) | 16 | subscribe to kernel "message" events |
| [packages/vfx/src/worker/MessageRouter.js](packages/vfx/src/worker/MessageRouter.js#L73) | 73 | remember constructors from define() for later cleanup (src changed, maybe we should support hotswap here?) |
Expand All @@ -31,6 +32,7 @@ This file contains an overview of all TODO, FIXME or XXX comments extracted from
| Filename | line # | XXX |
|:------|:------:|:------|
| [packages/vfx/src/elements/VfxCtxElement.js](packages/vfx/src/elements/VfxCtxElement.js#L206) | 206 | do we need to set additional properties on view component ? |
| [packages/vfx/src/shadow-objects/ThreeMultiViewRenderer.js](packages/vfx/src/shadow-objects/ThreeMultiViewRenderer.js#L19) | 19 | how we can set webgl-renderer parameters? |
| [packages/vfx/src/worker/MessageRouter.js](packages/vfx/src/worker/MessageRouter.js#L58) | 58 | do we need cleanup here ? |


Expand Down
1 change: 1 addition & 0 deletions packages/vfx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@spearwolf/offscreen-display": "workspace:*",
"@spearwolf/shadow-ents": "workspace:*",
"@spearwolf/signalize": "^0.13.0",
"three": "^0.162.0",
"xstate": "^5.9.1"
}
}
69 changes: 69 additions & 0 deletions packages/vfx/src/shadow-objects/ThreeMultiViewRenderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {WebGLRenderer} from 'three';

const DEFAULT_WIDTH = 320;
const DEFAULT_HEIGHT = 240;

export class ThreeMultiViewRenderer {
#views = new Map();
#lastViewId = 0;

constructor({provideContext, onDestroy}) {
const [, setMultiViewRenderer] = provideContext('multiViewRenderer', this);

onDestroy(() => {
setMultiViewRenderer(null);
});

this.canvas = new OffscreenCanvas(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// XXX how we can set webgl-renderer parameters?
this.renderer = new WebGLRenderer({canvas: this.canvas});
this.renderer.setScissorTest(true);

console.log('ThreeMultiViewRenderer created', this);
}

createView(width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT) {
const viewId = ++this.#lastViewId;
const view = {viewId, width, height, viewport: [0, 0, width, height], scene: null, camera: null};
this.#views.set(viewId, view);
return view;
}

renderView(view) {
if (view?.scene == null || view?.camera == null) return;
if (!(view.width > 0 && view.height > 0)) return;
if (!(view.viewport[2] > 0 && view.viewport[3] > 0)) return;
if (this.#views.has(view.viewId) === false) {
throw new Error(`not my view: ${view.viewId}`);
}

this.updateSize();

this.renderer.setScissor(0, 0, view.width, view.height);
this.renderer.setViewport(...view.viewport);
this.renderer.render(view.scene, view.camera);
}

destroyView(view) {
const viewId = typeof view === 'number' ? view : view.viewId;
this.#views.delete(viewId);
}

updateSize() {
let width = DEFAULT_WIDTH;
let height = DEFAULT_HEIGHT;
for (const view of this.#views.values()) {
width = Math.max(width, view.width);
height = Math.max(height, view.height);
}
if (this.canvas.width === width && this.canvas.height === height) return;
this.renderer.setSize(width, height, false);
}

onDestroy() {
this.renderer.dispose();
this.renderer = null;
console.log('ThreeMultiViewRenderer destroyed', this);
}
}
15 changes: 15 additions & 0 deletions packages/vfx/src/shadow-objects/ThreeRenderView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function ThreeRenderView(/* {useContext, onDestroy, provideContext} */) {
// TODO implement ThreeRenderView
//
// - useContext('canvas');
// - create bitmapdrawingcontext2d..
// - useContext('canvasSize');
// - useContext('multiViewRenderer');
// - create render-view with multiViewRenderer.createView()
// - onRenderFrame() - call multiViewRenderer.renderView(view) and copy webgl-canvas-content to our canvas
//
// maybe not here but in another shadow-object:
// - provideContext('three.scene');
// - provideContext('three.camera');
// - setCamera(...)
}
10 changes: 5 additions & 5 deletions packages/vfx/src/shadow-objects/VfxCtxCanvas.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export class VfxCtxCanvas {
constructor({useProperty, provideContext}) {
constructor({useProperty, useContext}) {
const getBar = useProperty('bar');

getBar((val) => {
console.log('[VfxCtxCanvas] bar changed to', val);
console.debug('[VfxCtxCanvas] bar changed to', val);
});

console.debug('[VfxCtxCanvas] bar is initially set to', getBar());

// ----

provideContext('multiViewRenderer', this);
useContext('multiViewRenderer')((val) => {
console.log('[VfxCtxCanvas] multiViewRenderer context set to', val);
});
}
}
2 changes: 2 additions & 0 deletions packages/vfx/src/worker-sample.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {TestImage2OnCanvas2D} from './shadow-objects/TestImage2OnCanvas2D.js';
import {TestImageOnCanvas2D} from './shadow-objects/TestImageOnCanvas2D.js';
import {ThreeMultiViewRenderer} from './shadow-objects/ThreeMultiViewRenderer.js';
import {VfxCtxCanvas} from './shadow-objects/VfxCtxCanvas.js';
import {VfxDisplay} from './shadow-objects/VfxDisplay.js';

Expand All @@ -11,6 +12,7 @@ export const onload = ({shadowObjects, kernel, registry}) => {
console.debug('worker.onload', {shadowObjects, kernel, registry});

shadowObjects.define('vfx-ctx', VfxCtxCanvas);
shadowObjects.define('vfx-ctx', ThreeMultiViewRenderer);
shadowObjects.define('vfx-display', VfxDisplay);
shadowObjects.define('TestImageOnCanvas2D', TestImageOnCanvas2D);
shadowObjects.define('TestImage2OnCanvas2D', TestImage2OnCanvas2D);
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9bc209e

Please sign in to comment.