Skip to content

Commit

Permalink
WebGLCubeRenderTarget.depthTexture
Browse files Browse the repository at this point in the history
  • Loading branch information
mbredif committed Mar 1, 2024
1 parent 3b847c4 commit c9b9789
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 145 deletions.
32 changes: 24 additions & 8 deletions examples/webgl_materials_cubemap_dynamic.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,35 @@
minFilter: THREE.LinearMipmapLinearFilter,
encoding: THREE.sRGBEncoding // temporary -- to prevent the material's shader from recompiling every frame
} );

cubeCamera1 = new THREE.CubeCamera( 1, 1000, cubeRenderTarget1 );
cubeRenderTarget1.stencilBuffer = false;
cubeRenderTarget1.depthBuffer = true;
cubeRenderTarget1.depthTexture = new THREE.CubeTexture();
cubeRenderTarget1.depthTexture.format = THREE.DepthFormat;
cubeRenderTarget1.depthTexture.type = THREE.UnsignedShortType;
cubeRenderTarget1.depthTexture.minFilter = THREE.NearestFilter;
cubeRenderTarget1.depthTexture.magFilter = THREE.NearestFilter;
cubeRenderTarget1.depthTexture.generateMipmaps = false;
cubeRenderTarget1.depthTexture._needsFlipEnvMap = false;

cubeCamera1 = new THREE.CubeCamera( 10, 100, cubeRenderTarget1 );

cubeRenderTarget2 = new THREE.WebGLCubeRenderTarget( 256, {
format: THREE.RGBFormat,
generateMipmaps: true,
minFilter: THREE.LinearMipmapLinearFilter,
encoding: THREE.sRGBEncoding
} );

cubeCamera2 = new THREE.CubeCamera( 1, 1000, cubeRenderTarget2 );

//
cubeRenderTarget2.stencilBuffer = false;
cubeRenderTarget2.depthBuffer = true;
cubeRenderTarget2.depthTexture = new THREE.CubeTexture();
cubeRenderTarget2.depthTexture.format = THREE.DepthFormat;
cubeRenderTarget2.depthTexture.type = THREE.FloatType;
cubeRenderTarget2.depthTexture.minFilter = THREE.NearestFilter;
cubeRenderTarget2.depthTexture.magFilter = THREE.NearestFilter;
cubeRenderTarget2.depthTexture.generateMipmaps = false;
cubeRenderTarget2.depthTexture._needsFlipEnvMap = false;

cubeCamera2 = new THREE.CubeCamera( 10, 100, cubeRenderTarget2 );

material = new THREE.MeshBasicMaterial( {
envMap: cubeRenderTarget2.texture,
Expand Down Expand Up @@ -190,12 +206,12 @@
if ( count % 2 === 0 ) {

cubeCamera1.update( renderer, scene );
material.envMap = cubeRenderTarget1.texture;
material.envMap = cubeRenderTarget1.depthTexture;

} else {

cubeCamera2.update( renderer, scene );
material.envMap = cubeRenderTarget2.texture;
material.envMap = cubeRenderTarget2.depthTexture;

}

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

}

let generateDepthMipmaps = false;
if ( renderTarget.depthTexture ) {

generateDepthMipmaps = renderTarget.depthTexture.generateMipmaps;
renderTarget.depthTexture.generateMipmaps = false;

}

renderer.setRenderTarget( renderTarget, 0 );
renderer.render( scene, cameraPX );

Expand All @@ -102,6 +110,12 @@ class CubeCamera extends Object3D {

}

if ( renderTarget.depthTexture ) {

renderTarget.depthTexture.generateMipmaps = generateDepthMipmaps;

}

renderer.setRenderTarget( renderTarget, 5 );
renderer.render( scene, cameraNZ );

Expand Down
4 changes: 0 additions & 4 deletions src/renderers/WebGLCubeRenderTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class WebGLCubeRenderTarget extends WebGLRenderTarget {
options.texture = new CubeTexture( undefined, options.mapping, options.wrapS, options.wrapT, options.magFilter,
options.minFilter, options.format, options.type, options.anisotropy, options.encoding );

}

if ( options.texture._needsFlipEnvMap === undefined ) {

options.texture._needsFlipEnvMap = false;

}
Expand Down
46 changes: 23 additions & 23 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1918,22 +1918,30 @@ function WebGLRenderer( parameters = {} ) {
state.scissor( _currentScissor );
state.setScissorTest( _currentScissorTest );

// select active layer and level for 2D array textures and 3D textures
// 2D and textures need no update
// * framebufferTexture2D's level is fixed to 0
// * the active cube face is already selected by the active face framebuffer
if ( renderTarget ) {

const level = activeMipmapLevel || 0;
const layer = activeCubeFace || 0;
const textures = renderTarget.textures;
for ( let i = 0, il = textures.length; i < il; i ++ ) {

const texture = textures[ i ];
const __webglTexture = properties.get( texture ).__webglTexture;
const attachment = _gl.COLOR_ATTACHMENT0 + i;
if ( texture.isDataTexture3D || texture.isDataTexture2DArray ) {

_gl.framebufferTextureLayer( _gl.FRAMEBUFFER, attachment, __webglTexture, activeMipmapLevel, activeCubeFace );
const __webglTexture = properties.get( texture ).__webglTexture;
const attachment = _gl.COLOR_ATTACHMENT0 + i;
_gl.framebufferTextureLayer( _gl.FRAMEBUFFER, attachment, __webglTexture, level, layer );

}

}

const texture = renderTarget.depthTexture;
const __webglTexture = properties.get( texture ).__webglTexture;
const texture = renderTarget.depthTexture;
if ( texture && ( texture.isDataTexture3D || texture.isDataTexture2DArray ) ) {

let attachment = undefined;
if ( texture.format === DepthFormat ) {
Expand All @@ -1946,33 +1954,25 @@ function WebGLRenderer( parameters = {} ) {

} else {

throw new Error( 'Unsupported depthTexture format' );

if ( renderTarget.depthTexture ) {

const texture = renderTarget.depthTexture;
if ( texture.isDataTexture3D || texture.isDataTexture2DArray ) {

let attachment = undefined;
if ( texture.format === DepthFormat ) {

attachment = _gl.DEPTH_ATTACHMENT;
}

} else if ( texture.format === DepthStencilFormat ) {
const __webglTexture = properties.get( texture ).__webglTexture;
_gl.framebufferTextureLayer( _gl.FRAMEBUFFER, attachment, __webglTexture, level, layer );

attachment = _gl.DEPTH_STENCIL_ATTACHMENT;
}

} else {
}

throw new Error( 'Unknown depthTexture format' );

}
const framebufferStatus = _gl.checkFramebufferStatus( _gl.FRAMEBUFFER );
if ( framebufferStatus !== _gl.FRAMEBUFFER_COMPLETE ) {

const __webglTexture = properties.get( texture ).__webglTexture;
_gl.framebufferTextureLayer( _gl.FRAMEBUFFER, attachment, __webglTexture, activeMipmapLevel, activeCubeFace );
console.log( 'incomplete framebuffer', framebufferStatus, framebuffer );

}
}
}

};

this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex ) {
Expand Down
Loading

0 comments on commit c9b9789

Please sign in to comment.