@@ -147,42 +147,6 @@ export class EffectComposer {
147147
148148 }
149149
150- /**
151- * Retrieves the most relevant depth texture for the pass at the given index.
152- *
153- * @private
154- * @param {Number } index - The index of the pass that needs a depth texture.
155- * @return {DepthTexture } The depth texture, or null if there is none.
156- */
157-
158- getDepthTexture ( index ) {
159-
160- const passes = this . passes ;
161-
162- let depthTexture = null ;
163- let inputBuffer = true ;
164- let i , pass ;
165-
166- for ( i = 0 ; i < index ; ++ i ) {
167-
168- pass = passes [ i ] ;
169-
170- if ( pass . needsSwap ) {
171-
172- inputBuffer = ! inputBuffer ;
173-
174- } else if ( pass instanceof RenderPass ) {
175-
176- depthTexture = ( inputBuffer ? this . inputBuffer : this . outputBuffer ) . depthTexture ;
177-
178- }
179-
180- }
181-
182- return depthTexture ;
183-
184- }
185-
186150 /**
187151 * Creates two depth texture attachments, one for the input buffer and one for
188152 * the output buffer.
@@ -216,6 +180,52 @@ export class EffectComposer {
216180
217181 }
218182
183+ /**
184+ * Sets the correct depth texture for each pass.
185+ *
186+ * @private
187+ */
188+
189+ updateDepthTextures ( ) {
190+
191+ let depthTextureRequired = false ;
192+ let depthTexture = null ;
193+ let inputBuffer = true ;
194+
195+ for ( const pass of this . passes ) {
196+
197+ if ( pass . needsDepthTexture && pass . getDepthTexture ( ) !== depthTexture ) {
198+
199+ pass . setDepthTexture ( depthTexture ) ;
200+
201+ }
202+
203+ if ( pass . needsSwap ) {
204+
205+ inputBuffer = ! inputBuffer ;
206+
207+ } else if ( pass instanceof RenderPass ) {
208+
209+ depthTexture = ( inputBuffer ? this . inputBuffer : this . outputBuffer ) . depthTexture ;
210+
211+ }
212+
213+ depthTextureRequired = ( depthTextureRequired || pass . needsDepthTexture ) ;
214+
215+ }
216+
217+ if ( ! depthTextureRequired ) {
218+
219+ this . inputBuffer . depthTexture . dispose ( ) ;
220+ this . outputBuffer . depthTexture . dispose ( ) ;
221+
222+ this . inputBuffer . depthTexture = null ;
223+ this . outputBuffer . depthTexture = null ;
224+
225+ }
226+
227+ }
228+
219229 /**
220230 * Creates a new render target by replicating the renderer's canvas.
221231 *
@@ -263,25 +273,31 @@ export class EffectComposer {
263273 pass . setSize ( drawingBufferSize . width , drawingBufferSize . height ) ;
264274 pass . initialize ( renderer , renderer . context . getContextAttributes ( ) . alpha ) ;
265275
276+ if ( pass . needsDepthTexture && this . inputBuffer . depthTexture === null ) {
277+
278+ this . createDepthTexture ( ) ;
279+
280+ }
281+
266282 if ( index !== undefined ) {
267283
268284 this . passes . splice ( index , 0 , pass ) ;
269285
270- } else {
286+ if ( this . inputBuffer . depthTexture !== null ) {
271287
272- index = this . passes . push ( pass ) - 1 ;
288+ this . updateDepthTextures ( ) ;
273289
274- }
290+ }
275291
276- if ( pass . needsDepthTexture ) {
292+ } else {
277293
278- if ( this . inputBuffer . depthTexture === null ) {
294+ this . passes . push ( pass ) ;
279295
280- this . createDepthTexture ( ) ;
296+ if ( pass . needsDepthTexture ) {
281297
282- }
298+ this . updateDepthTextures ( ) ;
283299
284- pass . setDepthTexture ( this . getDepthTexture ( index ) ) ;
300+ }
285301
286302 }
287303
@@ -295,7 +311,13 @@ export class EffectComposer {
295311
296312 removePass ( pass ) {
297313
298- this . passes . splice ( this . passes . indexOf ( pass ) , 1 ) ;
314+ const removed = ( this . passes . splice ( this . passes . indexOf ( pass ) , 1 ) . length > 0 ) ;
315+
316+ if ( removed && this . inputBuffer . depthTexture !== null ) {
317+
318+ this . updateDepthTextures ( ) ;
319+
320+ }
299321
300322 }
301323
0 commit comments