@@ -12,6 +12,7 @@ var CONST = require('../../const');
1212var Frame = require ( '../../textures/Frame' ) ;
1313var GameObject = require ( '../GameObject' ) ;
1414var Render = require ( './RenderTextureRender' ) ;
15+ var UUID = require ( '../../utils/string/UUID' ) ;
1516
1617/**
1718 * @classdesc
@@ -52,6 +53,7 @@ var RenderTexture = new Class({
5253 Components . Alpha ,
5354 Components . BlendMode ,
5455 Components . ComputedSize ,
56+ Components . Crop ,
5557 Components . Depth ,
5658 Components . Flip ,
5759 Components . GetBounds ,
@@ -117,23 +119,22 @@ var RenderTexture = new Class({
117119
118120 /**
119121 * The HTML Canvas Element that the Render Texture is drawing to.
120- * This is only set if Phaser is running with the Canvas Renderer.
122+ * This is only populated if Phaser is running with the Canvas Renderer.
121123 *
122124 * @name Phaser.GameObjects.RenderTexture#canvas
123- * @type {? HTMLCanvasElement }
125+ * @type {HTMLCanvasElement }
124126 * @since 3.2.0
125127 */
126- this . canvas = null ;
128+ this . canvas = CanvasPool . create2D ( this , width , height ) ;
127129
128130 /**
129131 * A reference to the Rendering Context belonging to the Canvas Element this Render Texture is drawing to.
130- * This is only set if Phaser is running with the Canvas Renderer.
131132 *
132133 * @name Phaser.GameObjects.RenderTexture#context
133- * @type {? CanvasRenderingContext2D }
134+ * @type {CanvasRenderingContext2D }
134135 * @since 3.2.0
135136 */
136- this . context = null ;
137+ this . context = this . canvas . getContext ( '2d' ) ;
137138
138139 /**
139140 * A reference to the GL Frame Buffer this Render Texture is drawing to.
@@ -145,12 +146,50 @@ var RenderTexture = new Class({
145146 */
146147 this . framebuffer = null ;
147148
149+ /**
150+ * The internal crop data object, as used by `setCrop` and passed to the `Frame.setCropUVs` method.
151+ *
152+ * @name Phaser.GameObjects.RenderTexture#_crop
153+ * @type {object }
154+ * @private
155+ * @since 3.12.0
156+ */
157+ this . _crop = this . resetCropObject ( ) ;
158+
159+ // Create a Texture for this Text object
160+ this . texture = scene . sys . textures . addCanvas ( UUID ( ) , this . canvas ) ;
161+
162+ // Get the frame
163+ this . frame = this . texture . get ( ) ;
164+
165+ /**
166+ * An internal Camera that can be used to move around the Render Texture!
167+ *
168+ * @name Phaser.GameObjects.RenderTexture#camera
169+ * @type {object }
170+ * @since 3.12.0
171+ */
148172 this . camera = new Camera ( 0 , 0 , width , height ) ;
149173
150174 this . camera . setScene ( scene ) ;
151175
176+ /**
177+ * Is this Render Texture dirty or not? If not it won't spend time clearing or filling itself.
178+ *
179+ * @name Phaser.GameObjects.RenderTexture#dirty
180+ * @type {boolean }
181+ * @since 3.12.0
182+ */
152183 this . dirty = false ;
153184
185+ /**
186+ * [description]
187+ *
188+ * @name Phaser.GameObjects.RenderTexture#gl
189+ * @type {WebGLRenderingContext }
190+ * @default null
191+ * @since 3.0.0
192+ */
154193 this . gl = null ;
155194
156195 var renderer = this . renderer ;
@@ -161,21 +200,35 @@ var RenderTexture = new Class({
161200
162201 this . gl = gl ;
163202 this . drawGameObject = this . batchGameObjectWebGL ;
164- this . texture = renderer . createTexture2D ( 0 , gl . NEAREST , gl . NEAREST , gl . CLAMP_TO_EDGE , gl . CLAMP_TO_EDGE , gl . RGBA , null , width , height , false ) ;
165- this . framebuffer = renderer . createFramebuffer ( width , height , this . texture , false ) ;
203+ this . framebuffer = renderer . createFramebuffer ( width , height , this . frame . source . glTexture , false ) ;
166204 }
167205 else if ( renderer . type === CONST . CANVAS )
168206 {
169207 this . drawGameObject = this . batchGameObjectCanvas ;
170- this . canvas = CanvasPool . create2D ( this , width , height ) ;
171- this . context = this . canvas . getContext ( '2d' ) ;
172208 }
173209
174210 this . setPosition ( x , y ) ;
175211 this . setSize ( width , height ) ;
212+ this . setOrigin ( 0 , 0 ) ;
176213 this . initPipeline ( 'TextureTintPipeline' ) ;
177214 } ,
178215
216+ /**
217+ * Sets the size of this Game Object.
218+ *
219+ * @method Phaser.GameObjects.Components.Size#setSize
220+ * @since 3.0.0
221+ *
222+ * @param {number } width - The width of this Game Object.
223+ * @param {number } height - The height of this Game Object.
224+ *
225+ * @return {this } This Game Object instance.
226+ */
227+ setSize : function ( width , height )
228+ {
229+ return this . resize ( width , height ) ;
230+ } ,
231+
179232 /**
180233 * Resizes the Render Texture to the new dimensions given.
181234 *
@@ -208,14 +261,19 @@ var RenderTexture = new Class({
208261 {
209262 var gl = this . gl ;
210263
211- this . renderer . deleteTexture ( this . texture ) ;
264+ this . renderer . deleteTexture ( this . frame . source . glTexture ) ;
212265 this . renderer . deleteFramebuffer ( this . framebuffer ) ;
213266
214- this . texture = this . renderer . createTexture2D ( 0 , gl . NEAREST , gl . NEAREST , gl . CLAMP_TO_EDGE , gl . CLAMP_TO_EDGE , gl . RGBA , null , width , height , false ) ;
267+ this . frame . source . glTexture = this . renderer . createTexture2D ( 0 , gl . NEAREST , gl . NEAREST , gl . CLAMP_TO_EDGE , gl . CLAMP_TO_EDGE , gl . RGBA , null , width , height , false ) ;
215268 this . framebuffer = this . renderer . createFramebuffer ( width , height , this . texture , false ) ;
269+
270+ this . frame . glTexture = this . frame . source . glTexture ;
216271 }
217272
218- this . setSize ( width , height ) ;
273+ this . frame . setSize ( width , height ) ;
274+
275+ this . width = width ;
276+ this . height = height ;
219277 }
220278
221279 return this ;
@@ -259,7 +317,7 @@ var RenderTexture = new Class({
259317 * Stores a copy of this Render Texture in the Texture Manager using the given key.
260318 *
261319 * After doing this, any texture based Game Object, such as a Sprite, can use the contents of this
262- * Render Texture for its texture by using the texture key:
320+ * Render Texture by using the texture key:
263321 *
264322 * ```javascript
265323 * var rt = this.add.renderTexture(0, 0, 128, 128);
@@ -272,7 +330,8 @@ var RenderTexture = new Class({
272330 * ```
273331 *
274332 * Updating the contents of this Render Texture will automatically update _any_ Game Object
275- * that is using it as a texture.
333+ * that is using it as a texture. Calling `saveTexture` again will not save another copy
334+ * of the same texture, it will just rename the key of the existing copy.
276335 *
277336 * By default it will create a single base texture. You can add frames to the texture
278337 * by using the `Texture.add` method. After doing this, you can then allow Game Objects
@@ -283,11 +342,13 @@ var RenderTexture = new Class({
283342 *
284343 * @param {string } key - The unique key to store the texture as within the global Texture Manager.
285344 *
286- * @return {? Phaser.Textures.Texture } The Texture that was created, or `null` if it could not be saved.
345+ * @return {Phaser.Textures.Texture } The Texture that was saved.
287346 */
288347 saveTexture : function ( key )
289348 {
290- return this . textureManager . addRenderTexture ( key , this ) ;
349+ this . textureManager . renameTexture ( this . texture . key , key ) ;
350+
351+ return this . texture ;
291352 } ,
292353
293354 /**
@@ -376,7 +437,7 @@ var RenderTexture = new Class({
376437 * It can accept any of the following:
377438 *
378439 * * Any renderable Game Object, such as a Sprite, Text, Graphics or TileSprite.
379- * * Dynamic and Static Tilemap Layers
440+ * * Dynamic and Static Tilemap Layers.
380441 * * A Group. The contents of which will be iterated and drawn in turn.
381442 * * A Container. The contents of which will be iterated fully, and drawn in turn.
382443 * * A Scene's Display List. Pass in `Scene.children` to draw the whole list.
@@ -391,7 +452,7 @@ var RenderTexture = new Class({
391452 * 5 of which have `visible=false` will only draw the 5 visible ones.
392453 *
393454 * If passing in an array of Game Objects it will draw them all, regardless if
394- * they render or not.
455+ * they pass a `willRender` check or not.
395456 *
396457 * You can pass in a string in which case it will look for a texture in the Texture
397458 * Manager matching that string, and draw the base frame. If you need to specify
@@ -682,8 +743,6 @@ var RenderTexture = new Class({
682743 var prevX = gameObject . x ;
683744 var prevY = gameObject . y ;
684745
685- this . renderer . setBlendMode ( gameObject . blendMode ) ;
686-
687746 gameObject . setPosition ( x , y ) ;
688747
689748 gameObject . renderCanvas ( this . renderer , gameObject , 0 , this . camera , null ) ;
@@ -773,6 +832,8 @@ var RenderTexture = new Class({
773832 this . renderer . deleteTexture ( this . texture ) ;
774833 this . renderer . deleteFramebuffer ( this . framebuffer ) ;
775834 }
835+
836+ this . texture . destroy ( ) ;
776837 }
777838
778839} ) ;
0 commit comments