Skip to content

Commit ceb9910

Browse files
committed
Added erase method for clearing parts of a Render Texture.
1 parent 7d1f990 commit ceb9910

1 file changed

Lines changed: 91 additions & 1 deletion

File tree

src/gameobjects/rendertexture/RenderTexture.js

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var BlendModes = require('../../renderer/BlendModes');
78
var Camera = require('../../cameras/2d/BaseCamera');
89
var CanvasPool = require('../../display/canvas/CanvasPool');
910
var Class = require('../../utils/Class');
@@ -188,6 +189,16 @@ var RenderTexture = new Class({
188189
*/
189190
this._saved = false;
190191

192+
/**
193+
* Internal erase mode flag.
194+
*
195+
* @name Phaser.GameObjects.RenderTexture#_eraseMode
196+
* @type {boolean}
197+
* @private
198+
* @since 3.16.0
199+
*/
200+
this._eraseMode = false;
201+
191202
/**
192203
* An internal Camera that can be used to move around the Render Texture.
193204
* Control it just like you would any Scene Camera. The difference is that it only impacts the placement of what
@@ -464,6 +475,70 @@ var RenderTexture = new Class({
464475
return this;
465476
},
466477

478+
/**
479+
* Draws the given object, or an array of objects, to this Render Texture using a blend mode of ERASE.
480+
* This has the effect of erasing any filled pixels in the objects from this Render Texture.
481+
*
482+
* It can accept any of the following:
483+
*
484+
* * Any renderable Game Object, such as a Sprite, Text, Graphics or TileSprite.
485+
* * Dynamic and Static Tilemap Layers.
486+
* * A Group. The contents of which will be iterated and drawn in turn.
487+
* * A Container. The contents of which will be iterated fully, and drawn in turn.
488+
* * A Scene's Display List. Pass in `Scene.children` to draw the whole list.
489+
* * Another Render Texture.
490+
* * A Texture Frame instance.
491+
* * A string. This is used to look-up a texture from the Texture Manager.
492+
*
493+
* Note: You cannot erase a Render Texture from itself.
494+
*
495+
* If passing in a Group or Container it will only draw children that return `true`
496+
* when their `willRender()` method is called. I.e. a Container with 10 children,
497+
* 5 of which have `visible=false` will only draw the 5 visible ones.
498+
*
499+
* If passing in an array of Game Objects it will draw them all, regardless if
500+
* they pass a `willRender` check or not.
501+
*
502+
* You can pass in a string in which case it will look for a texture in the Texture
503+
* Manager matching that string, and draw the base frame.
504+
*
505+
* You can pass in the `x` and `y` coordinates to draw the objects at. The use of
506+
* the coordinates differ based on what objects are being drawn. If the object is
507+
* a Group, Container or Display List, the coordinates are _added_ to the positions
508+
* of the children. For all other types of object, the coordinates are exact.
509+
*
510+
* Calling this method causes the WebGL batch to flush, so it can write the texture
511+
* data to the framebuffer being used internally. The batch is flushed at the end,
512+
* after the entries have been iterated. So if you've a bunch of objects to draw,
513+
* try and pass them in an array in one single call, rather than making lots of
514+
* separate calls.
515+
*
516+
* @method Phaser.GameObjects.RenderTexture#erase
517+
* @since 3.16.0
518+
*
519+
* @param {any} entries - Any renderable Game Object, or Group, Container, Display List, other Render Texture, Texture Frame or an array of any of these.
520+
* @param {number} [x] - The x position to draw the Frame at, or the offset applied to the object.
521+
* @param {number} [y] - The y position to draw the Frame at, or the offset applied to the object.
522+
*
523+
* @return {this} This Render Texture instance.
524+
*/
525+
erase: function (entries, x, y)
526+
{
527+
this._eraseMode = true;
528+
529+
var blendMode = this.renderer.currentBlendMode;
530+
531+
this.renderer.setBlendMode(BlendModes.ERASE);
532+
533+
this.draw(entries, x, y, 1, 16777215);
534+
535+
this.renderer.setBlendMode(blendMode);
536+
537+
this._eraseMode = false;
538+
539+
return this;
540+
},
541+
467542
/**
468543
* Draws the given object, or an array of objects, to this Render Texture.
469544
*
@@ -748,7 +823,10 @@ var RenderTexture = new Class({
748823
var prevX = gameObject.x;
749824
var prevY = gameObject.y;
750825

751-
this.renderer.setBlendMode(gameObject.blendMode);
826+
if (!this._eraseMode)
827+
{
828+
this.renderer.setBlendMode(gameObject.blendMode);
829+
}
752830

753831
gameObject.setPosition(x, y);
754832

@@ -828,7 +906,19 @@ var RenderTexture = new Class({
828906

829907
if (this.gl)
830908
{
909+
if (this._eraseMode)
910+
{
911+
var blendMode = this.renderer.currentBlendMode;
912+
913+
this.renderer.setBlendMode(BlendModes.ERASE);
914+
}
915+
831916
this.pipeline.batchTextureFrame(textureFrame, x, y, tint, alpha, this.camera.matrix, null);
917+
918+
if (this._eraseMode)
919+
{
920+
this.renderer.setBlendMode(blendMode);
921+
}
832922
}
833923
else
834924
{

0 commit comments

Comments
 (0)