Skip to content
Esc

noctcore-three/update-projection-matrix-after-camera-change

A camera’s projection fields are followed by updateProjectionMatrix() in the same function.

A camera renders through its projection matrix, and only updateProjectionMatrix() rebuilds it. Assigning fov, aspect, near, far, zoom or an orthographic bound changes the property and nothing on screen: after a resize the scene stays stretched, a zoom animation does nothing, a clipping plane moved for a close-up never moves.

Incorrect
import { PerspectiveCamera, WebGLRenderer } from 'three';
const camera = new PerspectiveCamera(50, 1, 0.1, 100);
const renderer = new WebGLRenderer();
export function onResize(width: number, height: number) {
camera.aspect = width / height;
renderer.setSize(width, height);
}
Correct
import { PerspectiveCamera, WebGLRenderer } from 'three';
const camera = new PerspectiveCamera(50, 1, 0.1, 100);
const renderer = new WebGLRenderer();
export function onResize(width: number, height: number) {
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
}

An assignment (=, += and the rest) to fov, aspect, near, far, zoom, left, right, top, bottom, filmGauge or filmOffset on a receiver whose name matches cameraNames, when nothing after it in the same function updates the projection.

The receiver’s name is its last segment: camera, this.camera, state.camera and mainCamera all match the default, and cameraRef.current reads as camera.

Incorrect · 2 reports
import { useFrame } from '@react-three/fiber';
useFrame(({ camera }) => {
camera.zoom += 0.01;
});
export function closeUp() {
const camera = rig.camera;
camera.near = 0.01;
}
  • 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.
  • Any updateProjectionMatrix() call after the assignment in the same function, on any receiver, so an alias (const cam = ref.current) is fine.
  • setFocalLength(), setLens(), setViewOffset() and clearViewOffset() after it: they update the projection themselves.
  • Handing the camera (or the object it hangs from, such as R3F’s state) to any call after the assignment (syncCamera(camera)), which may update it.
  • A method of a class that calls updateProjectionMatrix() in any of its methods (a resize handler that sets the bounds, an update() that runs every frame).
  • A light’s shadow camera (light.shadow.camera, or a const read from it). The renderer calls updateProjectionMatrix() on it when it first allocates the shadow map, so bounds set before the first render need no call. Changing them after that does need one, and is not detected.
  • A function that writes the projection matrix by hand (projectionMatrix.copy(), .set(), .elements[i] = ...), as oblique clipping and off-axis projection code does.
  • Receivers whose names do not look like a camera (cameraSettings.fov, controls.zoom), and fields that do not feed the projection (position, focus).
Correct
import { useFrame } from '@react-three/fiber';
useFrame(({ camera }) => {
camera.zoom += 0.01;
camera.updateProjectionMatrix();
});
export function closeUp() {
const camera = rig.camera;
camera.near = 0.01;
camera.updateProjectionMatrix();
}

Outside a class, an update in a different function (a resize handler that sets aspect while a render loop calls updateProjectionMatrix() every frame) is reported: the rule reads one function at a time. Call the update where the change is made, or put that file in allowIn.

Option Type Default Meaning
cameraNames string (regular expression) ^(?:cam|camera|[A-Za-z_$][\w$]*Camera)$ What the receiver’s last name must match to count as a camera. Case sensitive, so cameraSettings does not match.
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/update-projection-matrix-after-camera-change': ['error', { cameraNames: '^(?:camera|eye|viewer)$' }]

A codebase that updates every camera’s projection once per frame in its render loop on purpose, and never relies on the update at the change.