noctcore-three/instanced-mesh-needs-update
noctcore-three/instanced-mesh-needs-update
- Package
- three v0.1.0
- Preset
- recommended
- Severity
- error
- Autofix
- No
- Suggestions
- No
- Options
- Optional
- Type info
- Not needed
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.
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); }}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;}What it flags
Section titled “What it flags”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).
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]} />;}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]} />;}What it does not flag
Section titled “What it does not flag”-
Files that import neither
three(or athree/*subpath),three-stdlib, an@react-three/*package nor one ofextraModules: 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 staticimportdeclarations count: a file that loads three.js only throughimport()orrequire()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 nameInstancedMeshare not checked, receivers whose name containsbatchare skipped, and so is a receiver the file shows to be aBatchedMesh. The receiver’s own variable is resolved through scope, and forthis.meshandthis.#meshthe 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())anduseRef<BatchedMesh>()for.current, and one property of an object literal (meshes.propsin{ 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()andbuildBatchedMesh().useNonBatchedMesh(), a predicate (isBatchedMesh(x)) or a call named after the class inside the arguments is not one; - a type that is
BatchedMeshitself, 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 asRefObject<BatchedMesh>orMutableRefObject<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()formeshes.props). A source that is anInstancedMeshalways 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 ofcached ?? { 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 asmeshes[key] = ...,state.mesh ??= ...on a holder the file cannot read,Object.assign(meshes, loaded), a direct alias such asconst holder = meshes, a state setter that is used), leaves the receiver unknown, and an unknown receiver is reported, unless the declaration is typed as aBatchedMesh: 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’sref.current, another class’sthis.mesh, a parent class imported from another file and a string that happens to sayBatchedMeshdo not count. - a value built as one:
-
“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’sthis.mesh, says nothing about it.
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.
Options
Section titled “Options”| 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/**'] }]When not to use it
Section titled “When not to use it”A codebase that sets needsUpdate once per frame in a central render loop for every instanced
mesh it owns.