Skip to content

Commit

Permalink
add renderFrame event to stage2d
Browse files Browse the repository at this point in the history
  • Loading branch information
spearwolf committed Nov 2, 2023
1 parent 7634cb4 commit 3b35756
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/twopoint5d/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@spearwolf/twopoint5d",
"description": "a library to create 2.5d realtime graphics and pixelart with three.js",
"version": "0.3.2",
"version": "0.3.3",
"author": {
"name": "Wolfger Schramm",
"email": "[email protected]",
Expand Down
37 changes: 33 additions & 4 deletions packages/twopoint5d/src/stage/Stage2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import type {IProjection} from './IProjection.js';

export interface Stage2D extends Eventize {}

export interface Stage2DRenderFrameProps {
stage: Stage2D;
renderer: WebGLRenderer;

/**
* you do not need to call this callback yourself. it is normally done after the event.
* however, you can use this callback to control when the THREE.WebGLRenderer is called.
*/
renderFrame: () => void;
}

/**
* The `Stage2D` is a facade for a `THREE.Scene` with a `THREE.Camera`.
* The camera is managed by means of a *projection* description.
Expand All @@ -19,6 +30,10 @@ export interface Stage2D extends Eventize {}
* After the camera is created the scene can be rendered with the method `renderFrame(renderer: THREE.WebGLRenderer)`
*/
export class Stage2D {
static readonly Resize = 'resize';
static readonly AfterCameraChanged = 'afterCameraChanged';
static readonly RenderFrame = 'renderFrame';

scene: Scene;

autoClear = true;
Expand Down Expand Up @@ -98,7 +113,7 @@ export class Stage2D {
const prevCamera = this.camera;
updateCallback();
if (prevCamera !== this.camera) {
this.emit('afterCameraChanged', this, prevCamera);
this.emit(Stage2D.AfterCameraChanged, this, prevCamera);
}
};

Expand Down Expand Up @@ -136,17 +151,20 @@ export class Stage2D {

const prevWidth = this.#width;
const prevHeight = this.#height;

this.#width = w;
this.#height = h;

if (this.camera != null) {
this.projection!.updateCamera(this.camera);
} else {
this.#updateCamera(() => void (this.#cameraFromProjection = this.projection!.createCamera()));
this.#updateCamera(() => {
this.#cameraFromProjection = this.projection!.createCamera();
});
}

if (prevWidth !== w || prevHeight !== h) {
this.emit('resize', this);
this.emit(Stage2D.Resize, this);
}
};

Expand All @@ -157,7 +175,18 @@ export class Stage2D {
if (scene && camera) {
const wasPreviouslyAutoClear = renderer.autoClear;
renderer.autoClear = this.autoClear;
renderer.render(scene, camera);

let isRendered = false;
const renderFrame = () => {
if (!isRendered) {
isRendered = true;
renderer.render(scene, camera);
}
};

this.emit(Stage2D.RenderFrame, {stage: this, renderer, renderFrame} as Stage2DRenderFrameProps);
renderFrame();

renderer.autoClear = wasPreviouslyAutoClear;
} else if (!camera && ++this.#noCameraErrorCount === 100) {
this.#noCameraErrorCount = -1000;
Expand Down

0 comments on commit 3b35756

Please sign in to comment.