From c018f5131892052cc8bb816359b8c0efbe6514e2 Mon Sep 17 00:00:00 2001 From: Victor Giers Date: Tue, 28 Apr 2026 18:37:31 +0200 Subject: [PATCH] Implement depth texture and stencil buffer support for advanced rendering composer --- src/rendering/advanced-rendering.ts | 50 ++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/rendering/advanced-rendering.ts b/src/rendering/advanced-rendering.ts index 6684b653..1d056c0d 100644 --- a/src/rendering/advanced-rendering.ts +++ b/src/rendering/advanced-rendering.ts @@ -1,6 +1,9 @@ import { BasicShadowMap, + DepthStencilFormat, + DepthTexture, DirectionalLight, + FloatType, HalfFloatType, Mesh, NoToneMapping, @@ -13,7 +16,8 @@ import { type Material, type PerspectiveCamera, type Scene, - type WebGLRenderTarget, + UnsignedInt248Type, + WebGLRenderTarget, type WebGLRenderer, UnsignedByteType } from "three"; @@ -258,6 +262,49 @@ function getAmbientOcclusionSampleCount(samples: number) { return Math.max(samples, MIN_AMBIENT_OCCLUSION_SAMPLES); } +interface EffectComposerDepthTextureInternals extends EffectComposer { + depthTexture: DepthTexture | null; + depthRenderTarget: WebGLRenderTarget | null; + createDepthTexture(): DepthTexture; +} + +function configureEffectComposerDepthTextureIsolation( + composer: EffectComposer +) { + const composerInternals = composer as EffectComposerDepthTextureInternals; + + composerInternals.createDepthTexture = function createDepthTexture() { + const inputBuffer = this.inputBuffer; + const sourceDepthTexture = new DepthTexture(); + const stableDepthTexture = new DepthTexture(); + + this.depthTexture = sourceDepthTexture; + + if (inputBuffer.stencilBuffer) { + sourceDepthTexture.format = DepthStencilFormat; + sourceDepthTexture.type = UnsignedInt248Type; + stableDepthTexture.format = DepthStencilFormat; + stableDepthTexture.type = UnsignedInt248Type; + } else { + sourceDepthTexture.type = FloatType; + stableDepthTexture.type = FloatType; + } + + stableDepthTexture.name = "EffectComposer.StableDepth"; + this.depthRenderTarget = new WebGLRenderTarget( + inputBuffer.width, + inputBuffer.height, + { + depthBuffer: true, + stencilBuffer: inputBuffer.stencilBuffer, + depthTexture: stableDepthTexture + } + ); + + return stableDepthTexture; + }; +} + export function createAdvancedRenderingComposer( renderer: WebGLRenderer, scene: Scene, @@ -276,6 +323,7 @@ export function createAdvancedRenderingComposer( ? HalfFloatType : UnsignedByteType }); + configureEffectComposerDepthTextureIsolation(composer); const dynamicGlobalIlluminationParameters = resolveDynamicGlobalIlluminationParameters( settings.dynamicGlobalIllumination