Skip to content

WebGLRenderer: Move ReversedCoordinateSystem to camera.reversedDepth #31391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/webgl_reverse_depth_buffer.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
camera.position.z = 12;

reversedCamera = camera.clone();
reversedCamera.coordinateSystem = THREE.ReversedCoordinateSystem;
reversedCamera.reversedDepth = true;

scene = new THREE.Scene();

Expand Down
2 changes: 1 addition & 1 deletion examples/webgl_shadowmap.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@

if ( renderer.capabilities.reverseDepthBuffer ) {

camera.coordinateSystem = THREE.ReversedCoordinateSystem;
camera.reversedDepth = true;
camera.updateProjectionMatrix();

}
Expand Down
8 changes: 8 additions & 0 deletions src/cameras/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class Camera extends Object3D {

this.type = 'Camera';

/**
* The flag that indicates whether the camera uses a reversed depth buffer.
*
* @type {boolean}
* @default false
*/
this.reversedDepth = false;

/**
* The inverse of the camera's world matrix.
*
Expand Down
2 changes: 1 addition & 1 deletion src/cameras/OrthographicCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class OrthographicCamera extends Camera {

}

this.projectionMatrix.makeOrthographic( left, right, top, bottom, this.near, this.far, this.coordinateSystem );
this.projectionMatrix.makeOrthographic( left, right, top, bottom, this.near, this.far, this.coordinateSystem, this.reversedDepth );

this.projectionMatrixInverse.copy( this.projectionMatrix ).invert();

Expand Down
2 changes: 1 addition & 1 deletion src/cameras/PerspectiveCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ class PerspectiveCamera extends Camera {
const skew = this.filmOffset;
if ( skew !== 0 ) left += near * skew / this.getFilmWidth();

this.projectionMatrix.makePerspective( left, left + width, top, top - height, near, this.far, this.coordinateSystem );
this.projectionMatrix.makePerspective( left, left + width, top, top - height, near, this.far, this.coordinateSystem, this.reversedDepth );

this.projectionMatrixInverse.copy( this.projectionMatrix ).invert();

Expand Down
14 changes: 2 additions & 12 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -1564,31 +1564,21 @@ export const GLSL1 = '100';
export const GLSL3 = '300 es';

/**
* WebGL coordinate system [-1, 1].
* WebGL coordinate system.
*
* @type {number}
* @constant
*/
export const WebGLCoordinateSystem = 2000;

/**
* WebGPU coordinate system [0, 1].
* WebGPU coordinate system.
*
* @type {number}
* @constant
*/
export const WebGPUCoordinateSystem = 2001;

/**
* Reversed coordinate system [1, 0].
*
* Used for reverse depth buffer.
*
* @type {number}
* @constant
*/
export const ReversedCoordinateSystem = 2002;

/**
* Represents the different timestamp query types.
*
Expand Down
60 changes: 35 additions & 25 deletions src/math/Matrix4.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WebGLCoordinateSystem, WebGPUCoordinateSystem, ReversedCoordinateSystem } from '../constants.js';
import { WebGLCoordinateSystem, WebGPUCoordinateSystem } from '../constants.js';
import { Vector3 } from './Vector3.js';

/**
Expand Down Expand Up @@ -1103,9 +1103,10 @@ class Matrix4 {
* @param {number} near - The distance from the camera to the near plane.
* @param {number} far - The distance from the camera to the far plane.
* @param {(WebGLCoordinateSystem|WebGPUCoordinateSystem)} [coordinateSystem=WebGLCoordinateSystem] - The coordinate system.
* @param {boolean} [reversedDepth=false] - Whether to use a reversed depth.
* @return {Matrix4} A reference to this matrix.
*/
makePerspective( left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem ) {
makePerspective( left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem, reversedDepth = false ) {

const te = this.elements;
const x = 2 * near / ( right - left );
Expand All @@ -1116,24 +1117,28 @@ class Matrix4 {

let c, d;

if ( coordinateSystem === WebGLCoordinateSystem ) {
if ( reversedDepth ) {

c = - ( far + near ) / ( far - near );
d = ( - 2 * far * near ) / ( far - near );
c = far / ( far - near ) - 1;
d = ( far * near ) / ( far - near );

} else if ( coordinateSystem === WebGPUCoordinateSystem ) {
} else {

c = - far / ( far - near );
d = ( - far * near ) / ( far - near );
if ( coordinateSystem === WebGLCoordinateSystem ) {

} else if ( coordinateSystem === ReversedCoordinateSystem ) {
c = - ( far + near ) / ( far - near );
d = ( - 2 * far * near ) / ( far - near );

c = far / ( far - near ) - 1;
d = ( far * near ) / ( far - near );
} else if ( coordinateSystem === WebGPUCoordinateSystem ) {

} else {
c = - far / ( far - near );
d = ( - far * near ) / ( far - near );

} else {

throw new Error( 'THREE.Matrix4.makePerspective(): Invalid coordinate system: ' + coordinateSystem );
throw new Error( 'THREE.Matrix4.makePerspective(): Invalid coordinate system: ' + coordinateSystem );

}

}

Expand All @@ -1157,9 +1162,10 @@ class Matrix4 {
* @param {number} near - The distance from the camera to the near plane.
* @param {number} far - The distance from the camera to the far plane.
* @param {(WebGLCoordinateSystem|WebGPUCoordinateSystem)} [coordinateSystem=WebGLCoordinateSystem] - The coordinate system.
* @param {boolean} [reversedDepth=false] - Whether to use a reversed depth.
* @return {Matrix4} A reference to this matrix.
*/
makeOrthographic( left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem ) {
makeOrthographic( left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem, reversedDepth = false ) {

const te = this.elements;
const w = 1.0 / ( right - left );
Expand All @@ -1171,24 +1177,28 @@ class Matrix4 {

let z, zInv;

if ( coordinateSystem === WebGLCoordinateSystem ) {
if ( reversedDepth ) {

z = ( far + near ) * p;
zInv = - 2 * p;
z = - near * p - 1;
zInv = 1 * p;

} else if ( coordinateSystem === WebGPUCoordinateSystem ) {
} else {

z = near * p;
zInv = - 1 * p;
if ( coordinateSystem === WebGLCoordinateSystem ) {

} else if ( coordinateSystem === ReversedCoordinateSystem ) {
z = ( far + near ) * p;
zInv = - 2 * p;

z = - near * p - 1;
zInv = 1 * p;
} else if ( coordinateSystem === WebGPUCoordinateSystem ) {

} else {
z = near * p;
zInv = - 1 * p;

} else {

throw new Error( 'THREE.Matrix4.makeOrthographic(): Invalid coordinate system: ' + coordinateSystem );
throw new Error( 'THREE.Matrix4.makeOrthographic(): Invalid coordinate system: ' + coordinateSystem );

}

}

Expand Down
7 changes: 3 additions & 4 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import {
UnsignedInt248Type,
UnsignedShort4444Type,
UnsignedShort5551Type,
WebGLCoordinateSystem,
ReversedCoordinateSystem
WebGLCoordinateSystem
} from '../constants.js';
import { Color } from '../math/Color.js';
import { Frustum } from '../math/Frustum.js';
Expand Down Expand Up @@ -2383,10 +2382,10 @@ class WebGLRenderer {

const reverseDepthBuffer = state.buffers.depth.getReversed();

if ( reverseDepthBuffer && camera.coordinateSystem !== ReversedCoordinateSystem ) {
if ( reverseDepthBuffer && camera.reversedDepth === false ) {

// @deprecated, r179
warnOnce( 'THREE.WebGLRenderer: reverseDepthBuffer must be used with camera.coordinateSystem = THREE.ReversedCoordinateSystem for correct results. Automatic conversion will be removed in r189.' );
warnOnce( 'THREE.WebGLRenderer: reverseDepthBuffer must be used with `camera.reversedDepth = false` for correct results. Automatic conversion will be removed in r189.' );

_currentProjectionMatrix.copy( camera.projectionMatrix );

Expand Down
8 changes: 4 additions & 4 deletions src/renderers/webgl/WebGLShadowMap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FrontSide, BackSide, DoubleSide, NearestFilter, PCFShadowMap, VSMShadowMap, RGBADepthPacking, NoBlending, ReversedCoordinateSystem } from '../../constants.js';
import { FrontSide, BackSide, DoubleSide, NearestFilter, PCFShadowMap, VSMShadowMap, RGBADepthPacking, NoBlending } from '../../constants.js';
import { WebGLRenderTarget } from '../WebGLRenderTarget.js';
import { MeshDepthMaterial } from '../../materials/MeshDepthMaterial.js';
import { MeshDistanceMaterial } from '../../materials/MeshDistanceMaterial.js';
Expand Down Expand Up @@ -151,13 +151,13 @@ function WebGLShadowMap( renderer, objects, capabilities ) {
shadow.map.texture.name = light.name + '.shadowMap';

// @deprecated, r179
if ( capabilities.reverseDepthBuffer === true && camera.coordinateSystem !== ReversedCoordinateSystem ) {
if ( capabilities.reverseDepthBuffer === true && camera.reversedDepth === false ) {

shadow.camera.coordinateSystem = ReversedCoordinateSystem;
shadow.camera.reversedDepth = true;

} else {

shadow.camera.coordinateSystem = camera.coordinateSystem;
shadow.camera.reversedDepth = camera.reversedDepth;

}

Expand Down