Skip to content

Commit 9d7ec13

Browse files
committed
Refactoring RenderTexture to remove the matrix stack and add in support for drawing Game Objects and arrays of them
1 parent be379fd commit 9d7ec13

3 files changed

Lines changed: 102 additions & 18 deletions

File tree

src/gameobjects/rendertexture/RenderTexture.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ var RenderTexture = new Class({
5757
Components.Flip,
5858
Components.GetBounds,
5959
Components.Mask,
60-
Components.MatrixStack,
6160
Components.Origin,
6261
Components.Pipeline,
6362
Components.ScaleMode,
@@ -77,7 +76,7 @@ var RenderTexture = new Class({
7776

7877
GameObject.call(this, scene, 'RenderTexture');
7978

80-
this.initMatrixStack();
79+
// this.initMatrixStack();
8180

8281
/**
8382
* A reference to either the Canvas or WebGL Renderer that the Game instance is using.
@@ -138,6 +137,8 @@ var RenderTexture = new Class({
138137
*/
139138
this.framebuffer = null;
140139

140+
this.currentMatrix = new Components.TransformMatrix();
141+
141142
if (this.renderer.type === CONST.WEBGL)
142143
{
143144
var gl = this.renderer.gl;
@@ -146,6 +147,8 @@ var RenderTexture = new Class({
146147
this.fill = RenderTextureWebGL.fill;
147148
this.clear = RenderTextureWebGL.clear;
148149
this.draw = RenderTextureWebGL.draw;
150+
this.drawFrame = RenderTextureWebGL.drawFrame;
151+
this.drawGameObject = RenderTextureWebGL.drawGameObject;
149152
this.texture = this.renderer.createTexture2D(0, gl.NEAREST, gl.NEAREST, gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE, gl.RGBA, null, width, height, false);
150153
this.framebuffer = this.renderer.createFramebuffer(width, height, this.texture, false);
151154
}

src/gameobjects/rendertexture/RenderTextureCanvas.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,33 @@ var RenderTextureCanvas = {
4242

4343
/**
4444
* Draws the Texture Frame to the Render Texture at the given position.
45+
*
46+
* You can either pass in a Game Object frame property:
47+
*
48+
* ```javascript
49+
* var rt = this.add.renderTexture(100, 100, 400, 300);
50+
* var bunny = this.make.sprite({ key: 'bunny' }, false);
51+
* rt.draw(bunny.frame, 0, 0);
52+
* ```
53+
*
54+
* Or get a frame reference from the Texture Manager:
55+
*
56+
* ```javascript
57+
* var rt = this.add.renderTexture(100, 100, 400, 300);
58+
* var bunny = this.textures.getFrame('bunny');
59+
* rt.draw(bunny, 0, 0);
60+
* ```
4561
*
4662
* @method Phaser.GameObjects.RenderTexture#draw
4763
* @since 3.2.0
4864
*
49-
* @param {Phaser.Textures.Texture} texture - Currently unused.
50-
* @param {Phaser.Textures.Frame} frame - The Texture Frame that will be drawn to the Render Texture. Get this from the Texture Manager via `this.textures.getFrame(key)`.
65+
* @param {Phaser.Textures.Frame} frame - The Texture Frame that will be drawn to the Render Texture.
5166
* @param {number} x - The x position to draw the frame at.
5267
* @param {number} y - The y position to draw the frame at.
5368
*
54-
* @return {Phaser.GameObjects.RenderTexture} This Game Object.
69+
* @return {this} This Game Object.
5570
*/
56-
draw: function (texture, frame, x, y)
71+
draw: function (frame, x, y)
5772
{
5873
var cd = frame.canvasData;
5974
var source = frame.source.image;

src/gameobjects/rendertexture/RenderTextureWebGL.js

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var Utils = require('../../renderer/webgl/Utils');
2+
13
var RenderTextureWebGL = {
24

35
/**
@@ -56,41 +58,105 @@ var RenderTextureWebGL = {
5658

5759
/**
5860
* Draws the Texture Frame to the Render Texture at the given position.
61+
*
62+
* You can either pass in a Game Object frame property:
63+
*
64+
* ```javascript
65+
* var rt = this.add.renderTexture(100, 100, 400, 300);
66+
* var bunny = this.make.sprite({ key: 'bunny' }, false);
67+
* rt.draw(bunny.frame, 0, 0);
68+
* ```
69+
*
70+
* Or get a frame reference from the Texture Manager:
71+
*
72+
* ```javascript
73+
* var rt = this.add.renderTexture(100, 100, 400, 300);
74+
* var bunny = this.textures.getFrame('bunny');
75+
* rt.draw(bunny, 0, 0);
76+
* ```
5977
*
6078
* @method Phaser.GameObjects.RenderTexture#draw
6179
* @since 3.2.0
6280
*
63-
* @param {Phaser.Textures.Texture} texture - Currently unused.
64-
* @param {Phaser.Textures.Frame} frame - The Texture Frame that will be drawn to the Render Texture. Get this from the Texture Manager via `this.textures.getFrame(key)`.
81+
* @param {Phaser.Textures.Frame} frame - The Texture Frame that will be drawn to the Render Texture.
6582
* @param {number} x - The x position to draw the frame at.
6683
* @param {number} y - The y position to draw the frame at.
84+
* @param {number} [tint] - A tint color to be applied to the frame drawn to the Render Texture.
6785
*
68-
* @return {Phaser.GameObjects.RenderTexture} This Game Object.
86+
* @return {this} This Game Object.
6987
*/
70-
draw: function (texture, frame, x, y, tint)
88+
draw: function (stamp, x, y, tint)
7189
{
72-
if (tint === undefined)
73-
{
74-
tint = (this.globalTint >> 16) + (this.globalTint & 0xff00) + ((this.globalTint & 0xff) << 16);
75-
}
76-
else
90+
if (x === undefined) { x = 0; }
91+
if (y === undefined) { y = 0; }
92+
93+
if (!Array.isArray(stamp))
7794
{
78-
tint = (tint >> 16) + (tint & 0xff00) + ((tint & 0xff) << 16);
95+
stamp = [ stamp ];
7996
}
8097

8198
this.renderer.setFramebuffer(this.framebuffer);
8299

83100
var pipeline = this.pipeline;
84101

85-
pipeline.projOrtho(0, pipeline.width, 0, pipeline.height, -1000.0, 1000.0);
102+
pipeline.projOrtho(0, this.width, 0, this.height, -1000.0, 1000.0);
86103

87-
pipeline.drawTextureFrame(frame, x, y, tint, this.globalAlpha, this.currentMatrix, null);
104+
for (var i = 0; i < stamp.length; i++)
105+
{
106+
if (stamp[i].frame)
107+
{
108+
this.drawGameObject(stamp[i], x, y);
109+
}
110+
{
111+
this.drawFrame(stamp[i], x, y, tint);
112+
}
113+
}
114+
115+
pipeline.flush();
88116

89117
this.renderer.setFramebuffer(null);
90118

91119
pipeline.projOrtho(0, pipeline.width, pipeline.height, 0, -1000.0, 1000.0);
92120

93121
return this;
122+
},
123+
124+
drawGameObject: function (gameObject, x, y)
125+
{
126+
var getTint = Utils.getTintAppendFloatAlpha;
127+
128+
this.pipeline.batchTextureFrame(
129+
gameObject,
130+
gameObject.frame,
131+
x, y,
132+
gameObject.width, gameObject.height,
133+
gameObject.scaleX, gameObject.scaleY,
134+
gameObject.rotation,
135+
gameObject.flipX, gameObject.flipY,
136+
gameObject.displayOriginX, gameObject.displayOriginY,
137+
getTint(gameObject._tintTL, this.alpha * gameObject._alphaTL),
138+
getTint(gameObject._tintTR, this.alpha * gameObject._alphaTR),
139+
getTint(gameObject._tintBL, this.alpha * gameObject._alphaBL),
140+
getTint(gameObject._tintBR, this.alpha * gameObject._alphaBR),
141+
(gameObject._isTinted && gameObject.tintFill),
142+
null
143+
);
144+
},
145+
146+
drawFrame: function (frame, x, y, tint)
147+
{
148+
if (tint === undefined)
149+
{
150+
tint = (this.globalTint >> 16) + (this.globalTint & 0xff00) + ((this.globalTint & 0xff) << 16);
151+
}
152+
else
153+
{
154+
tint = (tint >> 16) + (tint & 0xff00) + ((tint & 0xff) << 16);
155+
}
156+
157+
this.pipeline.drawTextureFrame(frame, x, y, tint, this.globalAlpha, this.currentMatrix, null);
158+
159+
// pipeline.drawTextureFrame(frame, x, y, tint, this.globalAlpha, [1,0,0,1,0,0,0,0,1], null);
94160
}
95161

96162
};

0 commit comments

Comments
 (0)