Skip to content

Commit

Permalink
+ Implemented events for bootstrap
Browse files Browse the repository at this point in the history
+ Some webgl renderer fixes
  • Loading branch information
DrA1ex committed Oct 4, 2023
1 parent 06cbaf8 commit f0fecf4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
29 changes: 27 additions & 2 deletions examples/common/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Particle} from "../../lib/render/particles/particle.js";
import {ParticleSystem} from "../../lib/render/particles/system.js";
import * as Utils from "../../lib/utils/common.js";
import * as CommonUtils from "./utils.js";
import {EventEmitter} from "../../lib/misc/event.js";


/**
Expand All @@ -28,7 +29,7 @@ const ShapeType = {
rect: 2
}

export class Bootstrap {
export class Bootstrap extends EventEmitter {
/** @type {State} */
#state = State.stop;
/** @type {Debug} */
Expand Down Expand Up @@ -71,11 +72,25 @@ export class Bootstrap {
#debug = false;
#slowMotion = 1;

/** @type {Event<number>} */
#onBeforePhysicsStep = this.createEvent("before_physics_step");
/** @type {Event<number>} */
#onAfterPhysicsStep = this.createEvent("after_physics_step");
/** @type {Event<number>} */
#onBeforeRenderStep = this.createEvent("before_render_step");
/** @type {Event<number>} */
#onAfterRenderStep = this.createEvent("after_render_step");

get onBeforePhysicsStep() {return this.#onBeforePhysicsStep;}
get onAfterPhysicsStep() {return this.#onAfterPhysicsStep;}
get onBeforeRenderStep() {return this.#onBeforeRenderStep;}
get onAfterRenderStep() {return this.#onAfterRenderStep;}

get state() {return this.#state; }

get renderer() { return this.#renderer; }
get solver() { return this.#solver; }
get debug() {return this.#debugInstance;}
get debug() { return this.#debug ? this.#debugInstance : null; }

/** @deprecated Use renderer instead */
get canvas() { return this.#renderer.canvas; }
Expand All @@ -101,6 +116,7 @@ export class Bootstrap {
* }} options
*/
constructor(renderer, options = {}) {
super();
this.#renderer = renderer;

this.#slowMotion = 1;
Expand Down Expand Up @@ -416,14 +432,22 @@ export class Bootstrap {
const delta = Math.max(0, Math.min(0.1, elapsed * this.#slowMotion));
if (delta === 0) return;

this.onBeforePhysicsStep.emit(delta);

for (const waiterCb of this.#physicsWaiters) waiterCb(delta);
this.#physicsWaiters.clear();

this.#particleSystem.step(delta);
this.#solver.solve(delta);

this.onAfterPhysicsStep.emit(delta);
}

#render(delta) {
if (this.state === State.play) {
this.onBeforeRenderStep.emit(delta);
}

if (!this.#debugInstance || this.#debugInstance.showBodies) {
this.#renderer.render(this.state === State.play ? delta : 1e-12);
} else {
Expand All @@ -433,6 +457,7 @@ export class Bootstrap {
if (this.state === State.play) {
for (const waiterCb of this.#renderWaiters) waiterCb(delta);
this.#renderWaiters.clear();
this.onAfterRenderStep.emit(delta);
}

if (this.#debugInstance || this.#shapeId > 0) {
Expand Down
5 changes: 3 additions & 2 deletions lib/render/renderer/webgl/objects/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class IRenderObject {
get body() {return this.#body;}

/** @description Value within interval [-1;1] */
z = 1;
z = 0;

/**
* @abstract
Expand All @@ -30,7 +30,8 @@ export class IRenderObject {
get key() {return this.geometry.key;}
get group() {return this.constructor.name;}

get isIOpaque() {return true;}
/** @return {boolean} */
get isOpaque() {return true;}

constructor(body) {
this.#body = body;
Expand Down
4 changes: 2 additions & 2 deletions lib/render/renderer/webgl/objects/common/colored.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export class ColoredObject extends IRenderObject {
get pointsCount() {}

#colorComponents = null;
#color = "#000000";
#color = "#626262";

opacity = 1;

get color() {return this.#color;}
get isIOpaque() {return this.opacity === 1;}
get isOpaque() {return this.opacity === 1;}

/**
* @param {string} value
Expand Down
3 changes: 3 additions & 0 deletions lib/render/renderer/webgl/objects/sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export class SpriteObject extends ColoredObject {
/*** @type {ITextureSource} */
texture;
scale = 1;
opaque = true;

get isOpaque() { return this.opaque && super.isOpaque; }

constructor(body) {
super(body);
Expand Down
6 changes: 3 additions & 3 deletions lib/render/renderer/webgl/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ export class WebglRenderer extends IRenderer {
#prepare() {
for (const sets of this.#objectsByType.values()) {
for (const obj of sets.opaque) {
if (!obj.isIOpaque) {
if (!obj.isOpaque) {
sets.opaque.delete(obj);
sets.transparent.add(obj);
}
}

for (const obj of sets.transparent) {
if (obj.isIOpaque) {
if (obj.isOpaque) {
sets.transparent.delete(obj);
sets.opaque.add(obj);
}
Expand Down Expand Up @@ -152,7 +152,7 @@ export class WebglRenderer extends IRenderer {
this.#objectsByType.set(obj.group, sets);
}

if (obj.isIOpaque) {
if (obj.isOpaque) {
sets.opaque.add(obj);
} else {
sets.transparent.add(obj);
Expand Down

0 comments on commit f0fecf4

Please sign in to comment.