Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/melonjs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
- Tile maps: `TMXTileMap` and `TMXTilesetGroup`, for `TileMap` and `TilesetGroup` (the same classes)
- Loader: `loader.getTMX`, for `loader.getTileMap` (the same function)

### Performance
- Rendering: sprites take about **45%** less CPU time to update and draw on WebGL and WebGPU (10,000 sprites on WebGL: 8.6 to 4.9 ms a frame), and about **60%** less when they rotate

### Fixed
- TypeScript: an asset's `data` accepts a parsed map object
- Tile maps: a floating tile layer disappeared once the camera moved
Expand Down
2 changes: 1 addition & 1 deletion packages/melonjs/skills/melonjs-math/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ another matrix:

```js
m.setTransform(a, b, c, d, e, f, g, h, i, j, k, l, p, q, r, s);
m.copy(other); // Float32Array.set under the hood
m.copy(other); // sixteen plain stores, nothing allocated
```

Both are fine occasionally. In a per-frame loop, prefer writing `val` or
Expand Down
44 changes: 20 additions & 24 deletions packages/melonjs/src/geometries/observablePoint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { Point } from "./point.ts";

/**
* What a revoked ObservablePoint holds instead of its Point: a proxy revoked
* here, so reading or writing the ObservablePoint after {@link ObservablePoint#revoke}
* throws a `TypeError`, as it does on any revoked proxy. A live ObservablePoint
* keeps a plain Point — no proxy on its hot path.
* @ignore
*/
const _revoked = Proxy.revocable(new Point(), {});
_revoked.revoke();

/**
* Represents a point in a 2D coordinate system that can be observed for changes.
*/
Expand All @@ -11,17 +21,12 @@ export class ObservablePoint {
* @ignore
* @internal
*/
private _callback: () => void;
private _callback: (() => void) | undefined;
/**
* @ignore
* @internal
*/
private _point: Point;
/**
* @ignore
* @internal
*/
private _revoke: () => void;

private callBackEnabled: boolean = true;

Expand All @@ -34,22 +39,7 @@ export class ObservablePoint {
* @param callback - The callback function to be called when the point changes. Default is undefined.
*/
constructor(x: number = 0, y: number = 0, callback?: () => void) {
const { proxy, revoke } = Proxy.revocable(new Point(x, y), {
set: (target, property, value) => {
if (property === "x" || property === "y") {
Reflect.set(target, property, value);
if (this.callBackEnabled) {
this._callback?.();
}
return true;
}
return false;
},
});

this.callBackEnabled = true;
this._revoke = revoke;
this._point = proxy;
this._point = new Point(x, y);

if (callback) {
this.setCallback(callback);
Expand Down Expand Up @@ -98,6 +88,9 @@ export class ObservablePoint {
*/
set x(value: number) {
this._point.x = value;
if (this.callBackEnabled) {
this._callback?.();
}
}

/**
Expand All @@ -113,6 +106,9 @@ export class ObservablePoint {
*/
set y(value: number) {
this._point.y = value;
if (this.callBackEnabled) {
this._callback?.();
}
}

/**
Expand Down Expand Up @@ -144,9 +140,9 @@ export class ObservablePoint {
}

/**
* Revokes the proxy object, preventing further access to the ObservablePoint instance.
* Revokes this instance: reading or writing it afterwards throws a `TypeError`.
*/
revoke() {
this._revoke();
this._point = _revoked.proxy;
}
}
9 changes: 8 additions & 1 deletion packages/melonjs/src/math/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,14 @@ export class Color {
if (typeof color === "string") {
return this.parseCSS(color);
} else {
this.normalizedRGBA.set(color.normalizedRGBA);
// by element: `TypedArray#set` is a builtin call, several times
// slower than four stores, and this runs for every save/restore
const a = this.normalizedRGBA;
const b = color.normalizedRGBA;
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
a[3] = b[3];
return this;
}
}
Expand Down
26 changes: 25 additions & 1 deletion packages/melonjs/src/math/matrix3d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,31 @@ export class Matrix3d {
* @returns Reference to this object for method chaining
*/
copy(m: Matrix3d) {
this.val.set(m.val);
// by element: `TypedArray#set` is a builtin call, about twice as slow
// as sixteen stores, and this runs for every save/restore
const a = this.val;
const b = m.val;
if (b.length !== 16) {
// not a 4x4 (a Matrix2d passed from JavaScript): as before
a.set(b);
return this;
}
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
a[3] = b[3];
a[4] = b[4];
a[5] = b[5];
a[6] = b[6];
a[7] = b[7];
a[8] = b[8];
a[9] = b[9];
a[10] = b[10];
a[11] = b[11];
a[12] = b[12];
a[13] = b[13];
a[14] = b[14];
a[15] = b[15];
return this;
}

Expand Down
55 changes: 19 additions & 36 deletions packages/melonjs/src/math/observableVector2d.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import { TupleToUnion } from "type-fest";
import { createPool } from "../system/pool.ts";
import { clamp, damp, lerp } from "./math.ts";
import { Vector2d } from "./vector2d.ts";

const propertiesToObserve = ["x", "y"] as const;
type ObservableProperty = keyof Pick<
Vector2d,
TupleToUnion<typeof propertiesToObserve>
>;
const setOfProperties = new Set<ObservableProperty>(propertiesToObserve);

const isObservableProperty = (
property: string | symbol,
): property is ObservableProperty => {
return setOfProperties.has(property as any);
};

/**
* Working room for {@link ObservableVector2d.slerp}, which has to arrive at
* the whole answer before publishing any of it.
* @ignore
*/
const _slerpScratch = new Vector2d();

/**
* What a revoked ObservableVector2d holds instead of its Vector2d: a proxy revoked
* here, so reading or writing the ObservableVector2d after {@link ObservableVector2d#revoke}
* throws a `TypeError`, as it does on any revoked proxy. A live ObservableVector2d
* keeps a plain Vector2d — no proxy on its hot path.
* @ignore
*/
const _revoked = Proxy.revocable(new Vector2d(), {});
_revoked.revoke();

/**
* Represents a point in a 2D coordinate vector that can be observed for changes.
* @category Math
Expand All @@ -38,11 +34,6 @@ export class ObservableVector2d {
* @internal
*/
private _vector2d: Vector2d;
/**
* @ignore
* @internal
*/
private _revoke: () => void;

private callBackEnabled: boolean = true;

Expand All @@ -53,21 +44,7 @@ export class ObservableVector2d {
* @param callback - The callback function to be called when the point changes. Default is undefined.
*/
constructor(x: number = 0, y: number = 0, callback?: () => void) {
const { proxy, revoke } = Proxy.revocable(new Vector2d(x, y), {
set: (target, property, value) => {
if (isObservableProperty(property)) {
Reflect.set(target, property, value);
if (this.callBackEnabled) {
this._callback?.();
}
return true;
}
return false;
},
});

this._revoke = revoke;
this._vector2d = proxy;
this._vector2d = new Vector2d(x, y);

if (callback) {
this.setCallback(callback);
Expand Down Expand Up @@ -116,6 +93,9 @@ export class ObservableVector2d {
*/
set x(value: number) {
this._vector2d.x = value;
if (this.callBackEnabled) {
this._callback?.();
}
}

/**
Expand All @@ -131,6 +111,9 @@ export class ObservableVector2d {
*/
set y(value: number) {
this._vector2d.y = value;
if (this.callBackEnabled) {
this._callback?.();
}
}

/**
Expand Down Expand Up @@ -555,10 +538,10 @@ export class ObservableVector2d {
}

/**
* Revokes the proxy object, preventing further access to the ObservablePoint instance.
* Revokes this instance: reading or writing it afterwards throws a `TypeError`.
*/
revoke() {
this._revoke();
this._vector2d = _revoked.proxy;
}
}

Expand Down
58 changes: 22 additions & 36 deletions packages/melonjs/src/math/observableVector3d.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
import { TupleToUnion } from "type-fest";
import { Point } from "../geometries/point.ts";
import { createPool } from "../system/pool.ts";
import { clamp, damp, lerp } from "./math.ts";
import { Vector2d } from "./vector2d";
import { Vector3d } from "./vector3d";

const propertiesToObserve = ["x", "y", "z"] as const;
type ObservableProperty = keyof Pick<
Vector3d,
TupleToUnion<typeof propertiesToObserve>
>;
const setOfProperties = new Set<ObservableProperty>(propertiesToObserve);

const isObservableProperty = (
property: string | symbol,
): property is ObservableProperty => {
return setOfProperties.has(property as any);
};

/**
* Working room for {@link ObservableVector3d.slerp}, which has to arrive at
* the whole answer before publishing any of it.
* @ignore
*/
const _slerpScratch = new Vector3d();

/**
* What a revoked ObservableVector3d holds instead of its Vector3d: a proxy revoked
* here, so reading or writing the ObservableVector3d after {@link ObservableVector3d#revoke}
* throws a `TypeError`, as it does on any revoked proxy. A live ObservableVector3d
* keeps a plain Vector3d — no proxy on its hot path.
* @ignore
*/
const _revoked = Proxy.revocable(new Vector3d(), {});
_revoked.revoke();

/**
* Represents a point in a 3D coordinate vector that can be observed for changes.
* @category Math
Expand All @@ -40,11 +36,6 @@ export class ObservableVector3d {
* @internal
*/
private _vector3d: Vector3d;
/**
* @ignore
* @internal
*/
private _revoke: () => void;

private callBackEnabled: boolean = true;

Expand All @@ -61,21 +52,7 @@ export class ObservableVector3d {
z: number = 0,
callback?: () => void,
) {
const { proxy, revoke } = Proxy.revocable(new Vector3d(x, y, z), {
set: (target, property, value) => {
if (isObservableProperty(property)) {
Reflect.set(target, property, value);
if (this.callBackEnabled) {
this._callback?.();
}
return true;
}
return false;
},
});

this._revoke = revoke;
this._vector3d = proxy;
this._vector3d = new Vector3d(x, y, z);

if (callback) {
this.setCallback(callback);
Expand Down Expand Up @@ -128,6 +105,9 @@ export class ObservableVector3d {
*/
set x(value: number) {
this._vector3d.x = value;
if (this.callBackEnabled) {
this._callback?.();
}
}

/**
Expand All @@ -143,6 +123,9 @@ export class ObservableVector3d {
*/
set y(value: number) {
this._vector3d.y = value;
if (this.callBackEnabled) {
this._callback?.();
}
}

get z() {
Expand All @@ -151,6 +134,9 @@ export class ObservableVector3d {

set z(value: number) {
this._vector3d.z = value;
if (this.callBackEnabled) {
this._callback?.();
}
}

/**
Expand Down Expand Up @@ -637,10 +623,10 @@ export class ObservableVector3d {
}

/**
* Revokes the proxy object, preventing further access to the ObservablePoint instance.
* Revokes this instance: reading or writing it afterwards throws a `TypeError`.
*/
revoke() {
this._revoke();
this._vector3d = _revoked.proxy;
}
}

Expand Down
Loading
Loading