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: InstancedMesh.setMatrixAt returns the mesh, as three.js does (#117)

Kevin Rajand90a85848a9d
diff --git a/packages/scene/src/core.ts b/packages/scene/src/core.ts
index fce557e..8ce7f37 100644
--- a/packages/scene/src/core.ts
+++ b/packages/scene/src/core.ts
@@ -1861,8 +1861,9 @@ export class InstancedMesh extends Object3D {
this._instData = a && this.own(a);
touched(this);
}
- setMatrixAt(index: number, matrix: Matrix4): void {
+ setMatrixAt(index: number, matrix: Matrix4): this {
this.instanceMatrix.array.set(matrix.elements, index * 16);
+ return this;
}
getMatrixAt(index: number, matrix: Matrix4): Matrix4 {
return matrix.fromArray(this.instanceMatrix.array, index * 16);
diff --git a/packages/scene/test/three-compat-instanced.test.ts b/packages/scene/test/three-compat-instanced.test.ts
new file mode 100644
index 0000000..fba3390
--- /dev/null
+++ b/packages/scene/test/three-compat-instanced.test.ts
@@ -0,0 +1,16 @@
+// three.js compatibility: InstancedMesh.setMatrixAt returns the mesh, 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("InstancedMesh: setMatrixAt returns the mesh for chaining, as three.js does", () => {
+ const mesh = new THREE.InstancedMesh(new THREE.BoxGeometry(), new THREE.MeshStandardMaterial(), 2);
+ const translation = new THREE.Matrix4().makeTranslation(2, 3, 4);
+ const result = mesh.setMatrixAt(1, translation);
+ assert.equal(result, mesh, "setMatrixAt returns the mesh, not undefined");
+ assert.deepEqual([...mesh.getMatrixAt(1, new THREE.Matrix4()).elements], [...translation.elements], "instance 1 receives its matrix");
+ assert.deepEqual([...mesh.getMatrixAt(0, new THREE.Matrix4()).elements], [...new THREE.Matrix4().elements], "instance 0 stays the identity");
+ const version = mesh.instanceMatrix.version;
+ mesh.instanceMatrix.needsUpdate = true;
+ assert.equal(mesh.instanceMatrix.version, version + 1, "the required upload flag still works");
+});