Skip to content

Commit c2bce98

Browse files
committed
Added copyFrameRect method
1 parent cb71fe3 commit c2bce98

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

src/renderer/webgl/pipelines/UtilityPipeline.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var WebGLPipeline = require('../WebGLPipeline');
2929
* via the following methods in the Pipeline Manager:
3030
*
3131
* `copyFrame`
32+
* `copyFrameRect`
3233
* `drawFrame`
3334
* `blendFrames`
3435
* `blendFramesAdditive`
@@ -319,6 +320,58 @@ var UtilityPipeline = new Class({
319320
gl.bindTexture(gl.TEXTURE_2D, null);
320321
},
321322

323+
/**
324+
* Binds the `source` Render Target and then copies a section of it to the `target` Render Target.
325+
*
326+
* This method is extremely fast because it uses `gl.copyTexSubImage2D` and doesn't
327+
* require the use of any shaders. Remember the coordinates are given in standard WebGL format,
328+
* where x and y specify the lower-left corner of the section, not the top-left.
329+
*
330+
* @method Phaser.Renderer.WebGL.Pipelines.UtilityPipeline#copyFrameRect
331+
* @since 3.50.0
332+
*
333+
* @param {Phaser.Renderer.WebGL.RenderTarget} source - The source Render Target.
334+
* @param {Phaser.Renderer.WebGL.RenderTarget} target - The target Render Target.
335+
* @param {number} x - The x coordinate of the lower left corner where to start copying.
336+
* @param {number} y - The y coordinate of the lower left corner where to start copying.
337+
* @param {number} width - The width of the texture.
338+
* @param {number} height - The height of the texture.
339+
* @param {boolean} [clear=true] - Clear the target before copying?
340+
* @param {boolean} [clearAlpha=true] - Clear the alpha channel when running `gl.clear` on the target?
341+
*/
342+
copyFrameRect: function (source, target, x, y, width, height, clear, clearAlpha)
343+
{
344+
if (clear === undefined) { clear = true; }
345+
if (clearAlpha === undefined) { clearAlpha = true; }
346+
347+
var gl = this.gl;
348+
349+
gl.bindFramebuffer(gl.FRAMEBUFFER, source.framebuffer);
350+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, source.texture, 0);
351+
352+
if (clear)
353+
{
354+
if (clearAlpha)
355+
{
356+
gl.clearColor(0, 0, 0, 0);
357+
}
358+
else
359+
{
360+
gl.clearColor(0, 0, 0, 1);
361+
}
362+
363+
gl.clear(gl.COLOR_BUFFER_BIT);
364+
}
365+
366+
gl.activeTexture(gl.TEXTURE0);
367+
gl.bindTexture(gl.TEXTURE_2D, target.texture);
368+
369+
gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, x, y, width, height);
370+
371+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
372+
gl.bindTexture(gl.TEXTURE_2D, null);
373+
},
374+
322375
/**
323376
* Pops the framebuffer from the renderers FBO stack and sets that as the active target,
324377
* then draws the `source` Render Target to it. It then resets the renderer textures.

0 commit comments

Comments
 (0)