Skip to content
Esc

noctcore-three/instanced-mesh-needs-update

Writes through setMatrixAt() and setColorAt() are followed by the attribute’s needsUpdate.

InstancedMesh.setMatrixAt() and setColorAt() write into a CPU-side array. The GPU copy is uploaded again only when the attribute’s needsUpdate flag is set, and the three.js docs state the flag is required after either call. Without it the instances stay where the last upload left them: an animation that never moves, a recolor that never shows.

Incorrect · src/swarm.ts
import { InstancedMesh, Object3D } from 'three';
const dummy = new Object3D();
export function updateSwarm(time: number) {
for (let i = 0; i < count; i++) {
dummy.position.set(Math.sin(time + i), i * 0.1, 0);
dummy.updateMatrix();
swarm.setMatrixAt(i, dummy.matrix);
}
}
Correct · src/swarm.ts
import { InstancedMesh, Object3D } from 'three';
const dummy = new Object3D();
export function updateSwarm(time: number) {
for (let i = 0; i < count; i++) {
dummy.position.set(Math.sin(time + i), i * 0.1, 0);
dummy.updateMatrix();
swarm.setMatrixAt(i, dummy.matrix);
}
swarm.instanceMatrix.needsUpdate = true;
}

The first setMatrixAt() (or setColorAt()) call in a function with no <mesh>.instanceMatrix.needsUpdate = ... (or <mesh>.instanceColor.needsUpdate = ...) assignment anywhere in that function. Array callbacks (forEach, map, reduce and the like) and local helpers (const place = (i) => mesh.setMatrixAt(i, m)) are read as part of the function around them, so a loop over instances followed by one flag is fine. A class method reads its whole class, so a push() that writes and an end() that sets the flag pass. Each function is reported once per attribute.

The rule runs only in files that name InstancedMesh (an import, a type, a new, or R3F’s <instancedMesh> element).

Incorrect · src/Grid.tsx
import { useLayoutEffect, useRef } from 'react';
import type { InstancedMesh } from 'three';
export function Grid({ layout }: { layout: Layout }) {
const ref = useRef<InstancedMesh>(null!);
useLayoutEffect(() => {
layout.cells.forEach((cell, i) => ref.current.setColorAt(i, cell.color));
}, [layout]);
return <instancedMesh ref={ref} args={[undefined, undefined, layout.cells.length]} />;
}
Correct · src/Grid.tsx
import { useLayoutEffect, useRef } from 'react';
import type { InstancedMesh } from 'three';
export function Grid({ layout }: { layout: Layout }) {
const ref = useRef<InstancedMesh>(null!);
useLayoutEffect(() => {
layout.cells.forEach((cell, i) => ref.current.setColorAt(i, cell.color));
if (ref.current.instanceColor) ref.current.instanceColor.needsUpdate = true;
}, [layout]);
return <instancedMesh ref={ref} args={[undefined, undefined, layout.cells.length]} />;
}
  • Files that import neither three (or a three/* subpath), three-stdlib, an @react-three/* package nor one of extraModules: the rule only runs in three.js code, so another engine’s API with the same names (a Phaser or Babylon camera) is never read as three.js. Only static import declarations count: a file that loads three.js only through import() or require() is skipped, which can hide a report but never adds one.

  • A function that receives the mesh as a parameter: a helper whose caller sets the flag.

  • A function that hands the mesh to another call (scene.add(mesh), markDirty(mesh)), returns it, or constructs it (const mesh = new InstancedMesh(...), this.mesh = new InstancedMesh(...)): a mesh still being built has not been uploaded yet, and a helper may set the flag.

  • BatchedMesh.setMatrixAt(), which uploads by itself. Files that never name InstancedMesh are not checked, receivers whose name contains batch are skipped, and so is a receiver the file shows to be a BatchedMesh. The receiver’s own variable is resolved through scope, and for this.mesh and this.#mesh the same class is read, then, for a public field the class says nothing about, the classes it extends in the same file. What counts as showing it:

    • a value built as one: new BatchedMesh(), useMemo(() => new BatchedMesh(), []), const [mesh] = useState(() => new BatchedMesh()) while its setter is never used, useRef(new BatchedMesh()) and useRef<BatchedMesh>() for .current, and one property of an object literal (meshes.props in { props: new BatchedMesh(), swarm: new InstancedMesh() });
    • a hook or factory named for the class, as the outermost call of the stored value. The names are exact: useBatchedMesh() (the value), useBatchedMeshRef() (its .current), createBatchedMesh(), makeBatchedMesh() and buildBatchedMesh(). useNonBatchedMesh(), a predicate (isBatchedMesh(x)) or a call named after the class inside the arguments is not one;
    • a type that is BatchedMesh itself, optionally | null | undefined: on a variable, a parameter, a class field or a constructor parameter property, as a cast where the value is stored (ref.current = found as BatchedMesh), and as RefObject<BatchedMesh> or MutableRefObject<BatchedMesh> for .current. Any other type that mentions the class (Exclude<AnyMesh, BatchedMesh>, BatchedMesh | Mesh, BatchedMesh[]) shows nothing.

    Every source has to agree, and the rule reads all of them: the declaration, every write to the same chain, and every write to a shorter chain that holds it (meshes = load() for meshes.props). A source that is an InstancedMesh always wins, also over a type or a cast (flag ? new BatchedMesh() : new InstancedMesh(), useRef<BatchedMesh | InstancedMesh>()). A value the file says nothing about (mesh = pool.pop(), either side of cached ?? { mesh: new BatchedMesh() }), or a write the rule cannot read ([mesh] = pool, for (mesh of swarms), mesh &&= next, a computed key on the holder such as meshes[key] = ..., state.mesh ??= ... on a holder the file cannot read, Object.assign(meshes, loaded), a direct alias such as const holder = meshes, a state setter that is used), leaves the receiver unknown, and an unknown receiver is reported, unless the declaration is typed as a BatchedMesh: the type then speaks for every value the rule cannot read. Two things are not followed, and can hide a report: a holder passed to a call (scene.add(this), register(meshes); helpers and GUI bindings receive holders everywhere and almost never replace the mesh inside), and an alias made any other way than a direct one (const [h] = [meshes], const h = flag ? meshes : other, const reg = { m: meshes }). Another component’s ref.current, another class’s this.mesh, a parent class imported from another file and a string that happens to say BatchedMesh do not count.

  • “The mesh” in the lines above is the receiver’s own variable (or the same class’s this.mesh): a variable of the same name declared in an inner block, or a nested class’s this.mesh, says nothing about it.

Correct · src/swarm.ts
import { InstancedMesh, Matrix4 } from 'three';
export function buildSwarm(count: number, matrix: Matrix4) {
const swarm = new InstancedMesh(geometry, material, count);
for (let i = 0; i < count; i++) swarm.setMatrixAt(i, matrix);
scene.add(swarm);
return swarm;
}

Outside a class, a flag set in a different function (writes in an event handler, the flag set every frame in the render loop) is reported. Set it where the writes are, or put the file in allowIn. Writes in a useLayoutEffect that runs on mount, before the first frame, are still reported: they do not strictly need the flag, because the attribute has not been uploaded yet, but the three.js docs state the flag as required after setMatrixAt() / setColorAt(), the same effect re-runs when its dependencies change, and setting it costs nothing.

Option Type Default Meaning
allowIn string[] (globs) [] Files the rule skips, matched against the absolute path and the path relative to the working directory or workspace root.
extraModules string[] [] More module specifiers that mark a file as three.js code (a project module that re-exports three.js, such as @acme/engine); subpaths match too, and their imports count as three.js classes and R3F hooks.
'noctcore-three/instanced-mesh-needs-update': ['error', { allowIn: ['**/legacy/**'] }]

A codebase that sets needsUpdate once per frame in a central render loop for every instanced mesh it owns.