Skip to content

Commit ce0b9d9

Browse files
committed
Added option to clear frame before copy
1 parent b82ae85 commit ce0b9d9

3 files changed

Lines changed: 127 additions & 11 deletions

File tree

src/renderer/webgl/PipelineManager.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,15 +653,16 @@ var PipelineManager = new Class({
653653
* @param {Phaser.Renderer.WebGL.RenderTarget} source - The source Render Target.
654654
* @param {Phaser.Renderer.WebGL.RenderTarget} [target] - The target Render Target.
655655
* @param {number} [brightness=1] - The brightness value applied to the frame copy.
656+
* @param {boolean} [clear=true] - Clear the target before copying?
656657
* @param {boolean} [clearAlpha=true] - Clear the alpha channel when running `gl.clear` on the target?
657658
*
658659
* @return {this} This Pipeline Manager instance.
659660
*/
660-
copyFrame: function (source, target, brightness, clearAlpha)
661+
copyFrame: function (source, target, brightness, clear, clearAlpha)
661662
{
662663
var pipeline = this.setUtility(this.UTILITY_PIPELINE.copyShader);
663664

664-
pipeline.copyFrame(source, target, brightness, clearAlpha);
665+
pipeline.copyFrame(source, target, brightness, clear, clearAlpha);
665666

666667
return this;
667668
},
@@ -748,6 +749,22 @@ var PipelineManager = new Class({
748749
return this;
749750
},
750751

752+
/**
753+
* Clears the given Render Target.
754+
*
755+
* @method Phaser.Renderer.WebGL.Pipelines.UtilityPipeline#clearFrame
756+
* @since 3.50.0
757+
*
758+
* @param {Phaser.Renderer.WebGL.RenderTarget} target - The Render Target to clear.
759+
* @param {boolean} [clearAlpha=true] - Clear the alpha channel when running `gl.clear` on the target?
760+
*/
761+
clearFrame: function (target, clearAlpha)
762+
{
763+
this.UTILITY_PIPELINE.clearFrame(target, clearAlpha);
764+
765+
return this;
766+
},
767+
751768
/**
752769
* Returns `true` if the current pipeline is forced to use texture unit zero.
753770
*

src/renderer/webgl/pipelines/PostFXPipeline.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,12 @@ var PostFXPipeline = new Class({
184184
* @param {Phaser.Renderer.WebGL.RenderTarget} source - The source Render Target.
185185
* @param {Phaser.Renderer.WebGL.RenderTarget} [target] - The target Render Target.
186186
* @param {number} [brightness=1] - The brightness value applied to the frame copy.
187+
* @param {boolean} [clear=true] - Clear the target before copying?
187188
* @param {boolean} [clearAlpha=true] - Clear the alpha channel when running `gl.clear` on the target?
188189
*/
189-
copyFrame: function (source, target, brightness, clearAlpha)
190+
copyFrame: function (source, target, brightness, clear, clearAlpha)
190191
{
191-
this.manager.copyFrame(source, target, brightness, clearAlpha);
192+
this.manager.copyFrame(source, target, brightness, clear, clearAlpha);
192193
},
193194

194195
/**
@@ -295,6 +296,10 @@ var PostFXPipeline = new Class({
295296
{
296297
this.renderer.resetTextures();
297298
}
299+
else
300+
{
301+
gl.bindTexture(gl.TEXTURE_2D, null);
302+
}
298303
}
299304

300305
});

src/renderer/webgl/pipelines/UtilityPipeline.js

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)