Skip to content

Commit

Permalink
t
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett committed Sep 20, 2024
1 parent 944419f commit 8c8517c
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 20 deletions.
29 changes: 26 additions & 3 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { WebGLUtils } from './webgl/WebGLUtils.js';
import { WebXRManager } from './webxr/WebXRManager.js';
import { WebGLMaterials } from './webgl/WebGLMaterials.js';
import { WebGLUniformsGroups } from './webgl/WebGLUniformsGroups.js';
import { createCanvasElement, probeAsync, warnOnce } from '../utils.js';
import { createCanvasElement, probeAsync, toNormalizedProjectionMatrix, toReversedProjectionMatrix, warnOnce } from '../utils.js';
import { ColorManagement } from '../math/ColorManagement.js';

class WebGLRenderer {
Expand Down Expand Up @@ -196,6 +196,7 @@ class WebGLRenderer {

// camera matrices cache

const _currentProjectionMatrix = new Matrix4();
const _projScreenMatrix = new Matrix4();

const _vector3 = new Vector3();
Expand Down Expand Up @@ -291,6 +292,8 @@ class WebGLRenderer {

state = new WebGLState( _gl );

if ( capabilities.reverseDepthBuffer ) state.buffers.depth.setReversed( true );

info = new WebGLInfo( _gl );
properties = new WebGLProperties();
textures = new WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info );
Expand Down Expand Up @@ -590,7 +593,13 @@ class WebGLRenderer {

}

if ( depth ) bits |= _gl.DEPTH_BUFFER_BIT;
if ( depth ) {

bits |= _gl.DEPTH_BUFFER_BIT;
_gl.clearDepth( this.capabilities.reverseDepthBuffer ? 0 : 1 );

}

if ( stencil ) {

bits |= _gl.STENCIL_BUFFER_BIT;
Expand Down Expand Up @@ -1971,7 +1980,21 @@ class WebGLRenderer {

// common camera uniforms

p_uniforms.setValue( _gl, 'projectionMatrix', camera.projectionMatrix );
if ( capabilities.reverseDepthBuffer ) {

_currentProjectionMatrix.copy( camera.projectionMatrix );

toNormalizedProjectionMatrix( _currentProjectionMatrix );
toReversedProjectionMatrix( _currentProjectionMatrix );

p_uniforms.setValue( _gl, 'projectionMatrix', _currentProjectionMatrix );

} else {

p_uniforms.setValue( _gl, 'projectionMatrix', camera.projectionMatrix );

}

p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse );

const uCamPos = p_uniforms.map.cameraPosition;
Expand Down
9 changes: 9 additions & 0 deletions src/renderers/webgl/WebGLCapabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ function WebGLCapabilities( gl, extensions, parameters, utils ) {
}

const logarithmicDepthBuffer = parameters.logarithmicDepthBuffer === true;
const reverseDepthBuffer = parameters.reverseDepthBuffer === true && extensions.has( 'EXT_clip_control' );

if ( reverseDepthBuffer === true ) {

const ext = extensions.get( 'EXT_clip_control' );
ext.clipControlEXT( ext.LOWER_LEFT_EXT, ext.ZERO_TO_ONE_EXT );

}

const maxTextures = gl.getParameter( gl.MAX_TEXTURE_IMAGE_UNITS );
const maxVertexTextures = gl.getParameter( gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
Expand Down Expand Up @@ -119,6 +127,7 @@ function WebGLCapabilities( gl, extensions, parameters, utils ) {

precision: precision,
logarithmicDepthBuffer: logarithmicDepthBuffer,
reverseDepthBuffer: reverseDepthBuffer,

maxTextures: maxTextures,
maxVertexTextures: maxVertexTextures,
Expand Down
2 changes: 2 additions & 0 deletions src/renderers/webgl/WebGLProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) {
parameters.numLightProbes > 0 ? '#define USE_LIGHT_PROBES' : '',

parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '',
parameters.reverseDepthBuffer ? '#define USE_REVERSEDEPTHBUF' : '',

'uniform mat4 modelMatrix;',
'uniform mat4 modelViewMatrix;',
Expand Down Expand Up @@ -822,6 +823,7 @@ function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) {
parameters.decodeVideoTexture ? '#define DECODE_VIDEO_TEXTURE' : '',

parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '',
parameters.reverseDepthBuffer ? '#define USE_REVERSEDEPTHBUF' : '',

'uniform mat4 viewMatrix;',
'uniform vec3 cameraPosition;',
Expand Down
36 changes: 20 additions & 16 deletions src/renderers/webgl/WebGLPrograms.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
const programs = [];

const logarithmicDepthBuffer = capabilities.logarithmicDepthBuffer;
const reverseDepthBuffer = capabilities.reverseDepthBuffer;
const SUPPORTS_VERTEX_TEXTURES = capabilities.vertexTextures;

let precision = capabilities.precision;
Expand Down Expand Up @@ -305,6 +306,7 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities

sizeAttenuation: material.sizeAttenuation === true,
logarithmicDepthBuffer: logarithmicDepthBuffer,
reverseDepthBuffer: reverseDepthBuffer,

skinning: object.isSkinnedMesh === true,

Expand Down Expand Up @@ -524,38 +526,40 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
_programLayers.enable( 2 );
if ( parameters.logarithmicDepthBuffer )
_programLayers.enable( 3 );
if ( parameters.skinning )
if ( parameters.reverseDepthBuffer )
_programLayers.enable( 4 );
if ( parameters.morphTargets )
if ( parameters.skinning )
_programLayers.enable( 5 );
if ( parameters.morphNormals )
if ( parameters.morphTargets )
_programLayers.enable( 6 );
if ( parameters.morphColors )
if ( parameters.morphNormals )
_programLayers.enable( 7 );
if ( parameters.premultipliedAlpha )
if ( parameters.morphColors )
_programLayers.enable( 8 );
if ( parameters.shadowMapEnabled )
if ( parameters.premultipliedAlpha )
_programLayers.enable( 9 );
if ( parameters.doubleSided )
if ( parameters.shadowMapEnabled )
_programLayers.enable( 10 );
if ( parameters.flipSided )
if ( parameters.doubleSided )
_programLayers.enable( 11 );
if ( parameters.useDepthPacking )
if ( parameters.flipSided )
_programLayers.enable( 12 );
if ( parameters.dithering )
if ( parameters.useDepthPacking )
_programLayers.enable( 13 );
if ( parameters.transmission )
if ( parameters.dithering )
_programLayers.enable( 14 );
if ( parameters.sheen )
if ( parameters.transmission )
_programLayers.enable( 15 );
if ( parameters.opaque )
if ( parameters.sheen )
_programLayers.enable( 16 );
if ( parameters.pointsUvs )
if ( parameters.opaque )
_programLayers.enable( 17 );
if ( parameters.decodeVideoTexture )
if ( parameters.pointsUvs )
_programLayers.enable( 18 );
if ( parameters.alphaToCoverage )
if ( parameters.decodeVideoTexture )
_programLayers.enable( 19 );
if ( parameters.alphaToCoverage )
_programLayers.enable( 20 );

array.push( _programLayers.mask );

Expand Down
21 changes: 21 additions & 0 deletions src/renderers/webgl/WebGLState.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import { NotEqualDepth, GreaterDepth, GreaterEqualDepth, EqualDepth, LessEqualDe
import { Color } from '../../math/Color.js';
import { Vector4 } from '../../math/Vector4.js';

const reversedFuncs = {
[ NeverDepth ]: AlwaysDepth,
[ LessDepth ]: GreaterDepth,
[ EqualDepth ]: NotEqualDepth,
[ LessEqualDepth ]: GreaterEqualDepth,

[ AlwaysDepth ]: NeverDepth,
[ GreaterDepth ]: LessDepth,
[ NotEqualDepth ]: EqualDepth,
[ GreaterEqualDepth ]: LessEqualDepth,
};

function WebGLState( gl ) {

function ColorBuffer() {
Expand Down Expand Up @@ -66,13 +78,20 @@ function WebGLState( gl ) {
function DepthBuffer() {

let locked = false;
let reversed = false;

let currentDepthMask = null;
let currentDepthFunc = null;
let currentDepthClear = null;

return {

setReversed: function ( value ) {

reversed = value;

},

setTest: function ( depthTest ) {

if ( depthTest ) {
Expand Down Expand Up @@ -100,6 +119,8 @@ function WebGLState( gl ) {

setFunc: function ( depthFunc ) {

if ( reversed ) depthFunc = reversedFuncs[ depthFunc ];

if ( currentDepthFunc !== depthFunc ) {

switch ( depthFunc ) {
Expand Down
34 changes: 33 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,36 @@ function probeAsync( gl, sync, interval ) {

}

export { arrayMin, arrayMax, arrayNeedsUint32, getTypedArray, createElementNS, createCanvasElement, warnOnce, probeAsync };
function toNormalizedProjectionMatrix( projectionMatrix ) {

const m = projectionMatrix.elements;

// Convert [-1, 1] to [0, 1] projection matrix
m[ 2 ] = 0.5 * m[ 2 ] + 0.5 * m[ 3 ];
m[ 6 ] = 0.5 * m[ 6 ] + 0.5 * m[ 7 ];
m[ 10 ] = 0.5 * m[ 10 ] + 0.5 * m[ 11 ];
m[ 14 ] = 0.5 * m[ 14 ] + 0.5 * m[ 15 ];

}

function toReversedProjectionMatrix( projectionMatrix ) {

const m = projectionMatrix.elements;
const isPerspectiveMatrix = m[ 11 ] === - 1;

// Reverse [0, 1] projection matrix
if ( isPerspectiveMatrix ) {

m[ 10 ] = - m[ 10 ] - 1;
m[ 14 ] = - m[ 14 ];

} else {

m[ 10 ] = - m[ 10 ];
m[ 14 ] = - m[ 14 ] + 1;

}

}

export { arrayMin, arrayMax, arrayNeedsUint32, getTypedArray, createElementNS, createCanvasElement, warnOnce, probeAsync, toNormalizedProjectionMatrix, toReversedProjectionMatrix };
6 changes: 6 additions & 0 deletions test/unit/src/renderers/webgl/WebGLCapabilities.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export default QUnit.module( 'Renderers', () => {

} );

QUnit.todo( 'reverseDepthBuffer', ( assert ) => {

assert.ok( false, 'everything\'s gonna be alright' );

} );

QUnit.todo( 'maxTextures', ( assert ) => {

assert.ok( false, 'everything\'s gonna be alright' );
Expand Down

0 comments on commit 8c8517c

Please sign in to comment.