forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderTexture.js
More file actions
1419 lines (1255 loc) · 49.7 KB
/
Copy pathRenderTexture.js
File metadata and controls
1419 lines (1255 loc) · 49.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var BlendModes = require('../../renderer/BlendModes');
var Camera = require('../../cameras/2d/BaseCamera');
var CanvasPool = require('../../display/canvas/CanvasPool');
var Class = require('../../utils/Class');
var Components = require('../components');
var CONST = require('../../const');
var Frame = require('../../textures/Frame');
var GameObject = require('../GameObject');
var NOOP = require('../../utils/NOOP');
var PIPELINE_CONST = require('../../renderer/webgl/pipelines/const');
var Render = require('./RenderTextureRender');
var RenderTarget = require('../../renderer/webgl/RenderTarget');
var Utils = require('../../renderer/webgl/Utils');
var UUID = require('../../utils/string/UUID');
/**
* @classdesc
* A Render Texture.
*
* A Render Texture is a special texture that allows any number of Game Objects to be drawn to it. You can take many complex objects and
* draw them all to this one texture, which can they be used as the texture for other Game Object's. It's a way to generate dynamic
* textures at run-time that are WebGL friendly and don't invoke expensive GPU uploads.
*
* Note that under WebGL a FrameBuffer, which is what the Render Texture uses internally, cannot be anti-aliased. This means
* that when drawing objects such as Shapes to a Render Texture they will appear to be drawn with no aliasing, however this
* is a technical limitation of WebGL. To get around it, create your shape as a texture in an art package, then draw that
* to the Render Texture.
*
* @class RenderTexture
* @extends Phaser.GameObjects.GameObject
* @memberof Phaser.GameObjects
* @constructor
* @since 3.2.0
*
* @extends Phaser.GameObjects.Components.Alpha
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.ComputedSize
* @extends Phaser.GameObjects.Components.Crop
* @extends Phaser.GameObjects.Components.Depth
* @extends Phaser.GameObjects.Components.Flip
* @extends Phaser.GameObjects.Components.GetBounds
* @extends Phaser.GameObjects.Components.Mask
* @extends Phaser.GameObjects.Components.Origin
* @extends Phaser.GameObjects.Components.Pipeline
* @extends Phaser.GameObjects.Components.ScrollFactor
* @extends Phaser.GameObjects.Components.Tint
* @extends Phaser.GameObjects.Components.Transform
* @extends Phaser.GameObjects.Components.Visible
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
* @param {number} [x=0] - The horizontal position of this Game Object in the world.
* @param {number} [y=0] - The vertical position of this Game Object in the world.
* @param {number} [width=32] - The width of the Render Texture.
* @param {number} [height=32] - The height of the Render Texture.
* @property {string} [key] - The texture key to make the RenderTexture from.
* @property {string} [frame] - the frame to make the RenderTexture from.
*/
var RenderTexture = new Class({
Extends: GameObject,
Mixins: [
Components.Alpha,
Components.BlendMode,
Components.ComputedSize,
Components.Crop,
Components.Depth,
Components.Flip,
Components.GetBounds,
Components.Mask,
Components.Origin,
Components.Pipeline,
Components.ScrollFactor,
Components.Tint,
Components.Transform,
Components.Visible,
Render
],
initialize:
function RenderTexture (scene, x, y, width, height, key, frame)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = 32; }
if (height === undefined) { height = 32; }
GameObject.call(this, scene, 'RenderTexture');
/**
* A reference to either the Canvas or WebGL Renderer that the Game instance is using.
*
* @name Phaser.GameObjects.RenderTexture#renderer
* @type {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)}
* @since 3.2.0
*/
this.renderer = scene.sys.renderer;
/**
* A reference to the Texture Manager.
*
* @name Phaser.GameObjects.RenderTexture#textureManager
* @type {Phaser.Textures.TextureManager}
* @since 3.12.0
*/
this.textureManager = scene.sys.textures;
/**
* The tint of the Render Texture when rendered.
*
* @name Phaser.GameObjects.RenderTexture#globalTint
* @type {number}
* @default 0xffffff
* @since 3.2.0
*/
this.globalTint = 0xffffff;
/**
* The alpha of the Render Texture when rendered.
*
* @name Phaser.GameObjects.RenderTexture#globalAlpha
* @type {number}
* @default 1
* @since 3.2.0
*/
this.globalAlpha = 1;
/**
* The HTML Canvas Element that the Render Texture is drawing to when using the Canvas Renderer.
*
* @name Phaser.GameObjects.RenderTexture#canvas
* @type {HTMLCanvasElement}
* @since 3.2.0
*/
this.canvas = null;
/**
* Is this Render Texture dirty or not? If not it won't spend time clearing or filling itself.
*
* @name Phaser.GameObjects.RenderTexture#dirty
* @type {boolean}
* @since 3.12.0
*/
this.dirty = false;
/**
* The internal crop data object, as used by `setCrop` and passed to the `Frame.setCropUVs` method.
*
* @name Phaser.GameObjects.RenderTexture#_crop
* @type {object}
* @private
* @since 3.12.0
*/
this._crop = this.resetCropObject();
/**
* The Texture corresponding to this Render Texture.
*
* @name Phaser.GameObjects.RenderTexture#texture
* @type {Phaser.Textures.Texture}
* @since 3.12.0
*/
this.texture = null;
/**
* The Frame corresponding to this Render Texture.
*
* @name Phaser.GameObjects.RenderTexture#frame
* @type {Phaser.Textures.Frame}
* @since 3.12.0
*/
this.frame = null;
/**
* Internal saved texture flag.
*
* @name Phaser.GameObjects.RenderTexture#_saved
* @type {boolean}
* @private
* @since 3.12.0
*/
this._saved = false;
if (key === undefined)
{
this.canvas = CanvasPool.create2D(this, width, height);
// Create a new Texture for this RenderTexture object
this.texture = scene.sys.textures.addCanvas(UUID(), this.canvas);
// Get the frame
this.frame = this.texture.get();
}
else
{
this.texture = scene.sys.textures.get(key);
// Get the frame
this.frame = this.texture.get(frame);
this.canvas = this.frame.source.image;
this._saved = true;
this.dirty = true;
this.width = this.frame.cutWidth;
this.height = this.frame.cutHeight;
}
/**
* A reference to the Rendering Context belonging to the Canvas Element this Render Texture is drawing to.
*
* @name Phaser.GameObjects.RenderTexture#context
* @type {CanvasRenderingContext2D}
* @since 3.2.0
*/
this.context = this.canvas.getContext('2d');
/**
* Internal erase mode flag.
*
* @name Phaser.GameObjects.RenderTexture#_eraseMode
* @type {boolean}
* @private
* @since 3.16.0
*/
this._eraseMode = false;
/**
* An internal Camera that can be used to move around the Render Texture.
* Control it just like you would any Scene Camera. The difference is that it only impacts the placement of what
* is drawn to the Render Texture. You can scroll, zoom and rotate this Camera.
*
* @name Phaser.GameObjects.RenderTexture#camera
* @type {Phaser.Cameras.Scene2D.BaseCamera}
* @since 3.12.0
*/
this.camera = new Camera(0, 0, width, height);
/**
* The Render Target that belongs to this Render Texture.
*
* A Render Target encapsulates a framebuffer and texture for the WebGL Renderer.
*
* This property remains `null` under Canvas.
*
* @name Phaser.GameObjects.RenderTexture#renderTarget
* @type {Phaser.Renderer.WebGL.RenderTarget}
* @since 3.50.0
*/
this.renderTarget = null;
var renderer = this.renderer;
if (!renderer)
{
this.drawGameObject = NOOP;
}
else if (renderer.type === CONST.WEBGL)
{
this.drawGameObject = this.batchGameObjectWebGL;
this.renderTarget = new RenderTarget(renderer, width, height, 1, 0, false);
}
else if (renderer.type === CONST.CANVAS)
{
this.drawGameObject = this.batchGameObjectCanvas;
}
this.camera.setScene(scene);
this.setPosition(x, y);
if (key === undefined)
{
this.setSize(width, height);
}
this.setOrigin(0, 0);
this.initPipeline(PIPELINE_CONST.SINGLE_PIPELINE);
},
/**
* Sets the size of this Game Object.
*
* @method Phaser.GameObjects.RenderTexture#setSize
* @since 3.0.0
*
* @param {number} width - The width of this Game Object.
* @param {number} height - The height of this Game Object.
*
* @return {this} This Game Object instance.
*/
setSize: function (width, height)
{
return this.resize(width, height);
},
/**
* Resizes the Render Texture to the new dimensions given.
*
* If Render Texture was created from specific frame, only the size of the frame will be changed. The size of the source
* texture will not change.
*
* If Render Texture was not created from specific frame, the following will happen:
*
* In WebGL it will destroy and then re-create the frame buffer being used by the Render Texture.
* In Canvas it will resize the underlying canvas element.
*
* Both approaches will erase everything currently drawn to the Render Texture.
*
* If the dimensions given are the same as those already being used, calling this method will do nothing.
*
* @method Phaser.GameObjects.RenderTexture#resize
* @since 3.10.0
*
* @param {number} width - The new width of the Render Texture.
* @param {number} [height=width] - The new height of the Render Texture. If not specified, will be set the same as the `width`.
*
* @return {this} This Render Texture.
*/
resize: function (width, height)
{
if (height === undefined) { height = width; }
var frame = this.frame;
if (width !== this.width || height !== this.height)
{
if (frame.name === '__BASE')
{
// Resize the texture
this.canvas.width = width;
this.canvas.height = height;
this.texture.width = width;
this.texture.height = height;
var renderTarget = this.renderTarget;
if (renderTarget)
{
renderTarget.resize(width, height);
frame.glTexture = renderTarget.texture;
frame.source.isRenderTexture = true;
frame.source.isGLTexture = true;
frame.source.glTexture = renderTarget.texture;
}
this.camera.setSize(width, height);
frame.source.width = width;
frame.source.height = height;
frame.setSize(width, height);
this.width = width;
this.height = height;
}
}
else
{
// Resize the frame
var baseFrame = this.texture.getSourceImage();
if (frame.cutX + width > baseFrame.width)
{
width = baseFrame.width - frame.cutX;
}
if (frame.cutY + height > baseFrame.height)
{
height = baseFrame.height - frame.cutY;
}
frame.setSize(width, height, frame.cutX, frame.cutY);
}
this.updateDisplayOrigin();
var input = this.input;
if (input && !input.customHitArea)
{
input.hitArea.width = width;
input.hitArea.height = height;
}
return this;
},
/**
* Set the tint to use when rendering this Render Texture.
*
* @method Phaser.GameObjects.RenderTexture#setGlobalTint
* @since 3.2.0
*
* @param {number} tint - The tint value.
*
* @return {this} This Render Texture.
*/
setGlobalTint: function (tint)
{
this.globalTint = tint;
return this;
},
/**
* Set the alpha to use when rendering this Render Texture.
*
* @method Phaser.GameObjects.RenderTexture#setGlobalAlpha
* @since 3.2.0
*
* @param {number} alpha - The alpha value.
*
* @return {this} This Render Texture.
*/
setGlobalAlpha: function (alpha)
{
this.globalAlpha = alpha;
return this;
},
/**
* Stores a copy of this Render Texture in the Texture Manager using the given key.
*
* After doing this, any texture based Game Object, such as a Sprite, can use the contents of this
* Render Texture by using the texture key:
*
* ```javascript
* var rt = this.add.renderTexture(0, 0, 128, 128);
*
* // Draw something to the Render Texture
*
* rt.saveTexture('doodle');
*
* this.add.image(400, 300, 'doodle');
* ```
*
* Updating the contents of this Render Texture will automatically update _any_ Game Object
* that is using it as a texture. Calling `saveTexture` again will not save another copy
* of the same texture, it will just rename the key of the existing copy.
*
* By default it will create a single base texture. You can add frames to the texture
* by using the `Texture.add` method. After doing this, you can then allow Game Objects
* to use a specific frame from a Render Texture.
*
* If you destroy this Render Texture, any Game Object using it via the Texture Manager will
* stop rendering. Ensure you remove the texture from the Texture Manager and any Game Objects
* using it first, before destroying this Render Texture.
*
* @method Phaser.GameObjects.RenderTexture#saveTexture
* @since 3.12.0
*
* @param {string} key - The unique key to store the texture as within the global Texture Manager.
*
* @return {Phaser.Textures.Texture} The Texture that was saved.
*/
saveTexture: function (key)
{
this.textureManager.renameTexture(this.texture.key, key);
this._saved = true;
return this.texture;
},
/**
* Fills the Render Texture with the given color.
*
* @method Phaser.GameObjects.RenderTexture#fill
* @since 3.2.0
*
* @param {number} rgb - The color to fill the Render Texture with.
* @param {number} [alpha=1] - The alpha value used by the fill.
* @param {number} [x=0] - The left coordinate of the fill rectangle.
* @param {number} [y=0] - The top coordinate of the fill rectangle.
* @param {number} [width=this.frame.cutWidth] - The width of the fill rectangle.
* @param {number} [height=this.frame.cutHeight] - The height of the fill rectangle.
*
* @return {this} This Render Texture instance.
*/
fill: function (rgb, alpha, x, y, width, height)
{
var frame = this.frame;
var camera = this.camera;
var renderer = this.renderer;
if (alpha === undefined) { alpha = 1; }
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = frame.cutWidth; }
if (height === undefined) { height = frame.cutHeight; }
var r = (rgb >> 16 & 0xFF) / 255;
var g = (rgb >> 8 & 0xFF) / 255;
var b = (rgb & 0xFF) / 255;
var renderTarget = this.renderTarget;
camera.preRender();
if (renderTarget)
{
renderTarget.bind(true);
var pipeline = this.pipeline;
pipeline.manager.set(pipeline);
var tw = renderTarget.width;
var th = renderTarget.height;
var rw = renderer.width;
var rh = renderer.height;
var sx = rw / tw;
var sy = rh / th;
pipeline.drawFillRect(
x * sx, y * sy, width * sx, height * sy,
Utils.getTintFromFloats(b, g, r, 1),
alpha
);
renderTarget.unbind(true);
}
else
{
var ctx = this.context;
renderer.setContext(ctx);
ctx.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + alpha + ')';
ctx.fillRect(x + frame.cutX, y + frame.cutY, width, height);
renderer.setContext();
}
this.dirty = true;
return this;
},
/**
* Clears the Render Texture.
*
* @method Phaser.GameObjects.RenderTexture#clear
* @since 3.2.0
*
* @return {this} This Render Texture instance.
*/
clear: function ()
{
if (this.dirty)
{
var renderTarget = this.renderTarget;
if (renderTarget)
{
renderTarget.clear();
}
else
{
var ctx = this.context;
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(this.frame.cutX, this.frame.cutY, this.frame.cutWidth, this.frame.cutHeight);
ctx.restore();
}
this.dirty = false;
}
return this;
},
/**
* Draws the given object, or an array of objects, to this Render Texture using a blend mode of ERASE.
* This has the effect of erasing any filled pixels in the objects from this Render Texture.
*
* It can accept any of the following:
*
* * Any renderable Game Object, such as a Sprite, Text, Graphics or TileSprite.
* * Tilemap Layers.
* * A Group. The contents of which will be iterated and drawn in turn.
* * A Container. The contents of which will be iterated fully, and drawn in turn.
* * A Scene's Display List. Pass in `Scene.children` to draw the whole list.
* * Another Render Texture.
* * A Texture Frame instance.
* * A string. This is used to look-up a texture from the Texture Manager.
*
* Note: You cannot erase a Render Texture from itself.
*
* If passing in a Group or Container it will only draw children that return `true`
* when their `willRender()` method is called. I.e. a Container with 10 children,
* 5 of which have `visible=false` will only draw the 5 visible ones.
*
* If passing in an array of Game Objects it will draw them all, regardless if
* they pass a `willRender` check or not.
*
* You can pass in a string in which case it will look for a texture in the Texture
* Manager matching that string, and draw the base frame.
*
* You can pass in the `x` and `y` coordinates to draw the objects at. The use of
* the coordinates differ based on what objects are being drawn. If the object is
* a Group, Container or Display List, the coordinates are _added_ to the positions
* of the children. For all other types of object, the coordinates are exact.
*
* Calling this method causes the WebGL batch to flush, so it can write the texture
* data to the framebuffer being used internally. The batch is flushed at the end,
* after the entries have been iterated. So if you've a bunch of objects to draw,
* try and pass them in an array in one single call, rather than making lots of
* separate calls.
*
* @method Phaser.GameObjects.RenderTexture#erase
* @since 3.16.0
*
* @param {any} entries - Any renderable Game Object, or Group, Container, Display List, other Render Texture, Texture Frame or an array of any of these.
* @param {number} [x] - The x position to draw the Frame at, or the offset applied to the object.
* @param {number} [y] - The y position to draw the Frame at, or the offset applied to the object.
*
* @return {this} This Render Texture instance.
*/
erase: function (entries, x, y)
{
this._eraseMode = true;
this.draw(entries, x, y, 1, 16777215);
this._eraseMode = false;
return this;
},
/**
* Draws the given object, or an array of objects, to this Render Texture.
*
* It can accept any of the following:
*
* * Any renderable Game Object, such as a Sprite, Text, Graphics or TileSprite.
* * Tilemap Layers.
* * A Group. The contents of which will be iterated and drawn in turn.
* * A Container. The contents of which will be iterated fully, and drawn in turn.
* * A Scene's Display List. Pass in `Scene.children` to draw the whole list.
* * Another Render Texture.
* * A Texture Frame instance.
* * A string. This is used to look-up a texture from the Texture Manager.
*
* Note: You cannot draw a Render Texture to itself.
*
* If passing in a Group or Container it will only draw children that return `true`
* when their `willRender()` method is called. I.e. a Container with 10 children,
* 5 of which have `visible=false` will only draw the 5 visible ones.
*
* If passing in an array of Game Objects it will draw them all, regardless if
* they pass a `willRender` check or not.
*
* You can pass in a string in which case it will look for a texture in the Texture
* Manager matching that string, and draw the base frame. If you need to specify
* exactly which frame to draw then use the method `drawFrame` instead.
*
* You can pass in the `x` and `y` coordinates to draw the objects at. The use of
* the coordinates differ based on what objects are being drawn. If the object is
* a Group, Container or Display List, the coordinates are _added_ to the positions
* of the children. For all other types of object, the coordinates are exact.
*
* The `alpha` and `tint` values are only used by Texture Frames.
* Game Objects use their own alpha and tint values when being drawn.
*
* Calling this method causes the WebGL batch to flush, so it can write the texture
* data to the framebuffer being used internally. The batch is flushed at the end,
* after the entries have been iterated. So if you've a bunch of objects to draw,
* try and pass them in an array in one single call, rather than making lots of
* separate calls.
*
* @method Phaser.GameObjects.RenderTexture#draw
* @since 3.2.0
*
* @param {any} entries - Any renderable Game Object, or Group, Container, Display List, other Render Texture, Texture Frame or an array of any of these.
* @param {number} [x] - The x position to draw the Frame at, or the offset applied to the object.
* @param {number} [y] - The y position to draw the Frame at, or the offset applied to the object.
* @param {number} [alpha] - The alpha value. Only used for Texture Frames and if not specified defaults to the `globalAlpha` property. Game Objects use their own current alpha value.
* @param {number} [tint] - WebGL only. The tint color value. Only used for Texture Frames and if not specified defaults to the `globalTint` property. Game Objects use their own current tint value.
*
* @return {this} This Render Texture instance.
*/
draw: function (entries, x, y, alpha, tint)
{
this.beginDraw();
this.batchDraw(entries, x, y, alpha, tint);
this.endDraw();
return this;
},
/**
* Draws the Texture Frame to the Render Texture at the given position.
*
* Textures are referenced by their string-based keys, as stored in the Texture Manager.
*
* ```javascript
* var rt = this.add.renderTexture(0, 0, 800, 600);
* rt.drawFrame(key, frame);
* ```
*
* You can optionally provide a position, alpha and tint value to apply to the frame
* before it is drawn.
*
* Calling this method will cause a batch flush, so if you've got a stack of things to draw
* in a tight loop, try using the `draw` method instead.
*
* If you need to draw a Sprite to this Render Texture, use the `draw` method instead.
*
* @method Phaser.GameObjects.RenderTexture#drawFrame
* @since 3.12.0
*
* @param {string} key - The key of the texture to be used, as stored in the Texture Manager.
* @param {(string|number)} [frame] - The name or index of the frame within the Texture.
* @param {number} [x=0] - The x position to draw the frame at.
* @param {number} [y=0] - The y position to draw the frame at.
* @param {number} [alpha] - The alpha to use. If not specified it uses the `globalAlpha` property.
* @param {number} [tint] - WebGL only. The tint color to use. If not specified it uses the `globalTint` property.
*
* @return {this} This Render Texture instance.
*/
drawFrame: function (key, frame, x, y, alpha, tint)
{
this.beginDraw();
this.batchDrawFrame(key, frame, x, y, alpha, tint);
this.endDraw();
return this;
},
/**
* Use this method if you need to batch draw a large number of Game Objects to
* this Render Texture in a single go, or on a frequent basis.
*
* This method starts the beginning of a batched draw.
*
* It is faster than calling `draw`, but you must be very careful to manage the
* flow of code and remember to call `endDraw()`. If you don't need to draw large
* numbers of objects it's much safer and easier to use the `draw` method instead.
*
* The flow should be:
*
* ```javascript
* // Call once:
* RenderTexture.beginDraw();
*
* // repeat n times:
* RenderTexture.batchDraw();
* // or
* RenderTexture.batchDrawFrame();
*
* // Call once:
* RenderTexture.endDraw();
* ```
*
* Do not call any methods other than `batchDraw`, `batchDrawFrame`, or `endDraw` once you
* have started a batch. Also, be very careful not to destroy this Render Texture while the
* batch is still open, or call `beginDraw` again.
*
* @method Phaser.GameObjects.RenderTexture#beginDraw
* @since 3.50.0
*
* @return {this} This Render Texture instance.
*/
beginDraw: function ()
{
var camera = this.camera;
var renderer = this.renderer;
var renderTarget = this.renderTarget;
camera.preRender();
if (renderTarget)
{
renderer.beginCapture(renderTarget.width, renderTarget.height);
}
else
{
renderer.setContext(this.context);
}
return this;
},
/**
* Use this method if you have already called `beginDraw` and need to batch
* draw a large number of objects to this Render Texture.
*
* This method batches the drawing of the given objects to this Render Texture,
* without causing a bind or batch flush.
*
* It is faster than calling `draw`, but you must be very careful to manage the
* flow of code and remember to call `endDraw()`. If you don't need to draw large
* numbers of objects it's much safer and easier to use the `draw` method instead.
*
* The flow should be:
*
* ```javascript
* // Call once:
* RenderTexture.beginDraw();
*
* // repeat n times:
* RenderTexture.batchDraw();
* // or
* RenderTexture.batchDrawFrame();
*
* // Call once:
* RenderTexture.endDraw();
* ```
*
* Do not call any methods other than `batchDraw`, `batchDrawFrame`, or `endDraw` once you
* have started a batch. Also, be very careful not to destroy this Render Texture while the
* batch is still open, or call `beginDraw` again.
*
* Draws the given object, or an array of objects, to this Render Texture.
*
* It can accept any of the following:
*
* * Any renderable Game Object, such as a Sprite, Text, Graphics or TileSprite.
* * Tilemap Layers.
* * A Group. The contents of which will be iterated and drawn in turn.
* * A Container. The contents of which will be iterated fully, and drawn in turn.
* * A Scene's Display List. Pass in `Scene.children` to draw the whole list.
* * Another Render Texture.
* * A Texture Frame instance.
* * A string. This is used to look-up a texture from the Texture Manager.
*
* Note: You cannot draw a Render Texture to itself.
*
* If passing in a Group or Container it will only draw children that return `true`
* when their `willRender()` method is called. I.e. a Container with 10 children,
* 5 of which have `visible=false` will only draw the 5 visible ones.
*
* If passing in an array of Game Objects it will draw them all, regardless if
* they pass a `willRender` check or not.
*
* You can pass in a string in which case it will look for a texture in the Texture
* Manager matching that string, and draw the base frame. If you need to specify
* exactly which frame to draw then use the method `drawFrame` instead.
*
* You can pass in the `x` and `y` coordinates to draw the objects at. The use of
* the coordinates differ based on what objects are being drawn. If the object is
* a Group, Container or Display List, the coordinates are _added_ to the positions
* of the children. For all other types of object, the coordinates are exact.
*
* The `alpha` and `tint` values are only used by Texture Frames.
* Game Objects use their own alpha and tint values when being drawn.
*
* @method Phaser.GameObjects.RenderTexture#batchDraw
* @since 3.50.0
*
* @param {any} entries - Any renderable Game Object, or Group, Container, Display List, other Render Texture, Texture Frame or an array of any of these.
* @param {number} [x] - The x position to draw the Frame at, or the offset applied to the object.
* @param {number} [y] - The y position to draw the Frame at, or the offset applied to the object.
* @param {number} [alpha] - The alpha value. Only used for Texture Frames and if not specified defaults to the `globalAlpha` property. Game Objects use their own current alpha value.
* @param {number} [tint] - WebGL only. The tint color value. Only used for Texture Frames and if not specified defaults to the `globalTint` property. Game Objects use their own current tint value.
*
* @return {this} This Render Texture instance.
*/
batchDraw: function (entries, x, y, alpha, tint)
{
if (alpha === undefined) { alpha = this.globalAlpha; }
if (tint === undefined)
{
tint = (this.globalTint >> 16) + (this.globalTint & 0xff00) + ((this.globalTint & 0xff) << 16);
}
else
{
tint = (tint >> 16) + (tint & 0xff00) + ((tint & 0xff) << 16);
}
if (!Array.isArray(entries))
{
entries = [ entries ];
}
this.batchList(entries, x, y, alpha, tint);
return this;
},
/**
* Use this method if you have already called `beginDraw` and need to batch
* draw a large number of texture frames to this Render Texture.
*
* This method batches the drawing of the given frames to this Render Texture,
* without causing a bind or batch flush.
*
* It is faster than calling `drawFrame`, but you must be very careful to manage the
* flow of code and remember to call `endDraw()`. If you don't need to draw large
* numbers of frames it's much safer and easier to use the `drawFrame` method instead.
*
* The flow should be:
*
* ```javascript
* // Call once:
* RenderTexture.beginDraw();
*
* // repeat n times:
* RenderTexture.batchDraw();
* // or
* RenderTexture.batchDrawFrame();
*
* // Call once:
* RenderTexture.endDraw();
* ```
*
* Do not call any methods other than `batchDraw`, `batchDrawFrame`, or `endDraw` once you
* have started a batch. Also, be very careful not to destroy this Render Texture while the
* batch is still open, or call `beginDraw` again.
*
* Draws the Texture Frame to the Render Texture at the given position.
*
* Textures are referenced by their string-based keys, as stored in the Texture Manager.
*
* ```javascript
* var rt = this.add.renderTexture(0, 0, 800, 600);
* rt.drawFrame(key, frame);
* ```
*
* You can optionally provide a position, alpha and tint value to apply to the frame
* before it is drawn.
*
* Calling this method will cause a batch flush, so if you've got a stack of things to draw
* in a tight loop, try using the `draw` method instead.
*
* If you need to draw a Sprite to this Render Texture, use the `draw` method instead.
*
* @method Phaser.GameObjects.RenderTexture#batchDrawFrame
* @since 3.50.0
*
* @param {string} key - The key of the texture to be used, as stored in the Texture Manager.
* @param {(string|number)} [frame] - The name or index of the frame within the Texture.
* @param {number} [x=0] - The x position to draw the frame at.
* @param {number} [y=0] - The y position to draw the frame at.
* @param {number} [alpha] - The alpha to use. If not specified it uses the `globalAlpha` property.
* @param {number} [tint] - WebGL only. The tint color to use. If not specified it uses the `globalTint` property.
*
* @return {this} This Render Texture instance.
*/
batchDrawFrame: function (key, frame, x, y, alpha, tint)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (alpha === undefined) { alpha = this.globalAlpha; }
if (tint === undefined)
{
tint = (this.globalTint >> 16) + (this.globalTint & 0xff00) + ((this.globalTint & 0xff) << 16);
}
else
{
tint = (tint >> 16) + (tint & 0xff00) + ((tint & 0xff) << 16);
}
var textureFrame = this.textureManager.getFrame(key, frame);
if (textureFrame)
{
if (this.renderTarget)
{
this.pipeline.batchTextureFrame(textureFrame, x, y, tint, alpha, this.camera.matrix, null);
}
else
{
this.batchTextureFrame(textureFrame, x + this.frame.cutX, y + this.frame.cutY, alpha, tint);
}
}
return this;
},
/**
* Use this method to finish batch drawing to this Render Texture.
*
* Never call this method without first calling `beginDraw`.
*
* It is faster than calling `draw`, but you must be very careful to manage the
* flow of code and remember to call `endDraw()`. If you don't need to draw large