Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

Scene: MeshStandardMaterial.dispose fires a dispose event, as three.js does (#118)

Kevin Rajand1f56d4e9349
diff --git a/packages/scene/src/core.ts b/packages/scene/src/core.ts
index 8ce7f37..274776b 100644
--- a/packages/scene/src/core.ts
+++ b/packages/scene/src/core.ts
@@ -1052,10 +1052,13 @@ export const FrontSide = 0;
export const BackSide = 1;
export const DoubleSide = 2;
+type MaterialEvent = { type: string; target?: MeshStandardMaterial | null };
+
export class MeshStandardMaterial {
readonly isMaterial = true;
readonly isMeshStandardMaterial = true;
readonly uuid = `mat${++uid}`;
+ private _listeners?: Map<string, Array<(event: MaterialEvent) => void>>;
type: "standard" | "physical" = "standard";
/** Colours, emission, roughness, metalness, opacity, maps and the material function binding are picked up by the
* engine binding automatically; set this after changing anything else (`side`, a map's `repeat`, ...). */
@@ -1113,12 +1116,39 @@ export class MeshStandardMaterial {
return this.side === "double" || this.side === DoubleSide || this.side === BackSide;
}
- /** three.js API; the engine keeps what the scene uses. */
- dispose(): void {}
+ /** three.js's material events are per instance, including dispose notifications. */
+ addEventListener(type: string, listener: (event: MaterialEvent) => void): void {
+ const all = (this._listeners ??= new Map());
+ const listeners = all.get(type);
+ if (!listeners) all.set(type, [listener]);
+ else if (!listeners.includes(listener)) listeners.push(listener);
+ }
+
+ hasEventListener(type: string, listener: (event: MaterialEvent) => void): boolean {
+ return this._listeners?.get(type)?.includes(listener) ?? false;
+ }
+
+ removeEventListener(type: string, listener: (event: MaterialEvent) => void): void {
+ const listeners = this._listeners?.get(type);
+ if (listeners?.includes(listener)) listeners.splice(listeners.indexOf(listener), 1);
+ }
+
+ dispatchEvent(event: MaterialEvent): void {
+ const listeners = this._listeners?.get(event.type);
+ if (!listeners?.length) return;
+ event.target = this;
+ for (const listener of [...listeners]) listener.call(this, event);
+ event.target = null;
+ }
+
+ /** Notify owners of this material; shared textures are not disposed. */
+ dispose(): void {
+ this.dispatchEvent({ type: "dispose" });
+ }
copy(source: MeshStandardMaterial): this {
for (const [k, v] of Object.entries(source)) {
- if (k === "uuid") continue;
+ if (k === "uuid" || k === "_listeners") continue;
(this as unknown as Record<string, unknown>)[k] = v instanceof Color ? v.clone() : Array.isArray(v) ? [...v] : v;
}
this.needsUpdate = true;
diff --git a/packages/scene/test/three-compat-dispose.test.ts b/packages/scene/test/three-compat-dispose.test.ts
new file mode 100644
index 0000000..e272c8d
--- /dev/null
+++ b/packages/scene/test/three-compat-dispose.test.ts
@@ -0,0 +1,38 @@
+// three.js compatibility: MeshStandardMaterial dispose events, as three.js r187 has it (the snippet-level suite is three-compat.test.ts).
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import * as THREE from "../src/index.ts";
+
+test("MeshStandardMaterial.dispose: notifies only its own listeners and keeps shared maps, as three.js does", () => {
+ const texture = new THREE.Texture("albedo.png");
+ const material = new THREE.MeshStandardMaterial({ map: texture });
+ const received: Array<{ type: string; target?: unknown }> = [];
+ const listener = function (this: THREE.MeshStandardMaterial, event: { type: string; target?: unknown }) {
+ assert.equal(this, material, "the listener is called with the material as this");
+ assert.equal(event.target, material, "the event targets the disposed material");
+ received.push(event);
+ };
+ material.addEventListener("dispose", listener);
+ material.addEventListener("dispose", listener);
+ assert.equal(material.hasEventListener("dispose", listener), true);
+
+ const clone = material.clone();
+ const copy = new THREE.MeshStandardMaterial().copy(material);
+ clone.dispose();
+ copy.dispose();
+ assert.equal(received.length, 0, "clone and copy do not inherit the original's listeners");
+
+ material.dispose();
+ assert.deepEqual(received.map((event) => event.type), ["dispose"], "duplicate registration fires once");
+ assert.equal(received[0].target, null, "EventDispatcher resets the target after delivery");
+ assert.equal(material.map, texture, "disposing a material does not dispose its map");
+ assert.equal(clone.map, texture, "the clone shares the same texture");
+ assert.equal(copy.map, texture, "the copy shares the same texture");
+
+ material.dispose();
+ assert.deepEqual(received.map((event) => event.type), ["dispose", "dispose"], "each call emits an event");
+ material.removeEventListener("dispose", listener);
+ assert.equal(material.hasEventListener("dispose", listener), false);
+ material.dispose();
+ assert.equal(received.length, 2, "removed listeners do not receive future disposal events");
+});