@@ -292,11 +292,13 @@ var UtilityPipeline = new Class({
292292 * @param {Phaser.Renderer.WebGL.RenderTarget } source - The source Render Target.
293293 * @param {Phaser.Renderer.WebGL.RenderTarget } [target] - The target Render Target.
294294 * @param {number } [brightness=1] - The brightness value applied to the frame copy.
295+ * @param {boolean } [clear=true] - Clear the target before copying?
295296 * @param {boolean } [clearAlpha=true] - Clear the alpha channel when running `gl.clear` on the target?
296297 */
297- copyFrame : function ( source , target , brightness , clearAlpha )
298+ copyFrame : function ( source , target , brightness , clear , clearAlpha )
298299 {
299300 if ( brightness === undefined ) { brightness = 1 ; }
301+ if ( clear === undefined ) { clear = true ; }
300302 if ( clearAlpha === undefined ) { clearAlpha = true ; }
301303
302304 var gl = this . gl ;
@@ -318,23 +320,79 @@ var UtilityPipeline = new Class({
318320 gl . viewport ( 0 , 0 , source . width , source . height ) ;
319321 }
320322
321- if ( clearAlpha )
323+ if ( clear )
322324 {
323- gl . clearColor ( 0 , 0 , 0 , 0 ) ;
325+ if ( clearAlpha )
326+ {
327+ gl . clearColor ( 0 , 0 , 0 , 0 ) ;
328+ }
329+ else
330+ {
331+ gl . clearColor ( 0 , 0 , 0 , 1 ) ;
332+ }
333+
334+ gl . clear ( gl . COLOR_BUFFER_BIT ) ;
335+ }
336+
337+ gl . bufferData ( gl . ARRAY_BUFFER , this . vertexData , gl . STATIC_DRAW ) ;
338+ gl . drawArrays ( gl . TRIANGLES , 0 , 6 ) ;
339+
340+ gl . bindFramebuffer ( gl . FRAMEBUFFER , null ) ;
341+ gl . bindTexture ( gl . TEXTURE_2D , null ) ;
342+ } ,
343+
344+ /**
345+ * Binds this pipeline and draws the `source` Render Target to the `target` Render Target.
346+ *
347+ * If no `target` is specified, it will pop the framebuffer from the Renderers FBO stack
348+ * and use that instead, which should be done when you need to draw the final results of
349+ * this pipeline to the game canvas.
350+ *
351+ * You can optionally set the shader to be used for the draw here, if this is a multi-shader
352+ * pipeline. By default `currentShader` will be used. If you need to set a shader but not
353+ * a target, just pass `null` as the `target` parameter.
354+ *
355+ * @method Phaser.Renderer.WebGL.Pipelines.PostFXPipeline#bindAndDraw
356+ * @since 3.50.0
357+ *
358+ * @param {Phaser.Renderer.WebGL.RenderTarget } source - The Render Target to draw from.
359+ * @param {Phaser.Renderer.WebGL.RenderTarget } [target] - The Render Target to draw to. If not set, it will pop the fbo from the stack.
360+ * @param {Phaser.Renderer.WebGL.WebGLShader } [currentShader] - The shader to use during the draw.
361+ bindAndDraw: function (source, target)
362+ {
363+ var gl = this.gl;
364+
365+ this.bind(currentShader);
366+
367+ this.set1i('uMainSampler', 0);
368+
369+ if (target)
370+ {
371+ gl.viewport(0, 0, target.width, target.height);
372+ gl.bindFramebuffer(gl.FRAMEBUFFER, target.framebuffer);
373+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, target.texture, 0);
324374 }
325375 else
326376 {
327- gl . clearColor ( 0 , 0 , 0 , 1 ) ;
377+ this.renderer.popFramebuffer( );
328378 }
329379
330- gl . clear ( gl . COLOR_BUFFER_BIT ) ;
380+ gl.activeTexture(gl.TEXTURE0);
381+ gl.bindTexture(gl.TEXTURE_2D, source.texture);
331382
332383 gl.bufferData(gl.ARRAY_BUFFER, this.vertexData, gl.STATIC_DRAW);
333384 gl.drawArrays(gl.TRIANGLES, 0, 6);
334385
335- gl . bindFramebuffer ( gl . FRAMEBUFFER , null ) ;
336- gl . bindTexture ( gl . TEXTURE_2D , null ) ;
386+ if (!target)
387+ {
388+ this.renderer.resetTextures();
389+ }
390+ else
391+ {
392+ gl.bindTexture(gl.TEXTURE_2D, null);
393+ }
337394 },
395+ */
338396
339397 /**
340398 * Copy the `source` Render Target to the `target` Render Target, using the
@@ -453,6 +511,7 @@ var UtilityPipeline = new Class({
453511 gl . bufferData ( gl . ARRAY_BUFFER , this . vertexData , gl . STATIC_DRAW ) ;
454512 gl . drawArrays ( gl . TRIANGLES , 0 , 6 ) ;
455513
514+ gl . bindFramebuffer ( gl . FRAMEBUFFER , null ) ;
456515 gl . bindTexture ( gl . TEXTURE_2D , null ) ;
457516 } ,
458517
@@ -472,6 +531,41 @@ var UtilityPipeline = new Class({
472531 blendFramesAdditive : function ( source1 , source2 , target , strength , clearAlpha )
473532 {
474533 this . blendFrames ( source1 , source2 , target , strength , clearAlpha , this . addShader ) ;
534+ } ,
535+
536+ /**
537+ * Clears the given Render Target.
538+ *
539+ * @method Phaser.Renderer.WebGL.Pipelines.UtilityPipeline#clearFrame
540+ * @since 3.50.0
541+ *
542+ * @param {Phaser.Renderer.WebGL.RenderTarget } target - The Render Target to clear.
543+ * @param {boolean } [clearAlpha=true] - Clear the alpha channel when running `gl.clear` on the target?
544+ */
545+ clearFrame : function ( target , clearAlpha )
546+ {
547+ if ( clearAlpha === undefined ) { clearAlpha = true ; }
548+
549+ var gl = this . gl ;
550+
551+ gl . viewport ( 0 , 0 , target . width , target . height ) ;
552+
553+ gl . bindFramebuffer ( gl . FRAMEBUFFER , target . framebuffer ) ;
554+
555+ if ( clearAlpha )
556+ {
557+ gl . clearColor ( 0 , 0 , 0 , 0 ) ;
558+ }
559+ else
560+ {
561+ gl . clearColor ( 0 , 0 , 0 , 1 ) ;
562+ }
563+
564+ gl . clear ( gl . COLOR_BUFFER_BIT ) ;
565+
566+ var fbo = this . renderer . currentFramebuffer ;
567+
568+ gl . bindFramebuffer ( gl . FRAMEBUFFER , fbo ) ;
475569 }
476570
477571} ) ;
0 commit comments