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
444 lines (354 loc) · 13.7 KB
/
Copy pathRenderTexture.js
File metadata and controls
444 lines (354 loc) · 13.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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* A RenderTexture is a special texture that allows any displayObject to be rendered to it. It allows you to take many complex objects and
* render them down into a single quad (on WebGL) which can then be used to texture other display objects with. A way of generating textures at run-time.
*
* @class Phaser.RenderTexture
* @constructor
* @extends PIXI.Texture
* @param {Phaser.Game} game - Current game instance.
* @param {number} [width=100] - The width of the render texture.
* @param {number} [height=100] - The height of the render texture.
* @param {string} [key=''] - The key of the RenderTexture in the Cache, if stored there.
* @param {number} [scaleMode=Phaser.scaleModes.DEFAULT] - One of the Phaser.scaleModes consts.
* @param {number} [resolution=1] - The resolution of the texture being generated.
*/
Phaser.RenderTexture = function (game, width, height, key, scaleMode, resolution, renderer, textureUnit) {
if (width === undefined) { width = 100; }
if (height === undefined) { height = 100; }
if (key === undefined) { key = ''; }
if (scaleMode === undefined) { scaleMode = Phaser.scaleModes.DEFAULT; }
if (resolution === undefined) { resolution = 1; }
if (renderer === undefined) { renderer = PIXI.defaultRenderer; }
if (textureUnit === undefined) { textureUnit = 0; }
/**
* @property {Phaser.Game} game - A reference to the currently running game.
*/
this.game = game;
/**
* @property {string} key - The key of the RenderTexture in the Cache, if stored there.
*/
this.key = key;
/**
* @property {number} type - Base Phaser object type.
*/
this.type = Phaser.RENDERTEXTURE;
/**
* @property {Phaser.Matrix} _tempMatrix - The matrix that is applied when display objects are rendered to this RenderTexture.
* @private
*/
this._tempMatrix = new Phaser.Matrix();
this.width = width;
this.height = height;
this.resolution = resolution;
this.frame = new Phaser.Rectangle(0, 0, this.width * this.resolution, this.height * this.resolution);
this.crop = this.frame.clone();
/**
* The base texture object that this texture uses
*
* @property baseTexture
* @type BaseTexture
*/
this.baseTexture = new PIXI.BaseTexture();
this.baseTexture.width = this.width * this.resolution;
this.baseTexture.height = this.height * this.resolution;
this.baseTexture._glTextures = null;
this.baseTexture.resolution = this.resolution;
this.baseTexture.scaleMode = scaleMode;
this.baseTexture.hasLoaded = true;
PIXI.Texture.call(this, this.baseTexture, this.frame.clone());
/**
* The renderer this RenderTexture uses. A RenderTexture can only belong to one renderer at the moment if its webGL.
*
* @property renderer
* @type CanvasRenderer|WebGLRenderer
*/
this.renderer = renderer;
if (this.renderer.type === Phaser.WEBGL)
{
var gl = this.renderer.gl;
this.baseTexture.textureIndex = textureUnit;
this.baseTexture._dirty[gl.id] = false;
this.textureBuffer = new PIXI.FilterTexture(gl, this.width, this.height, this.baseTexture.scaleMode, textureUnit);
this.baseTexture._glTextures = this.textureBuffer.texture;
this.projection = new Phaser.Point(this.width * 0.5, -this.height * 0.5);
}
else
{
this.textureBuffer = new PIXI.CanvasBuffer(this.width * this.resolution, this.height * this.resolution);
this.baseTexture.source = this.textureBuffer.canvas;
}
/**
* @property valid
* @type Boolean
*/
this.valid = true;
this.tempMatrix = new Phaser.Matrix();
this._updateUvs();
};
Phaser.RenderTexture.prototype = Object.create(PIXI.Texture.prototype);
Phaser.RenderTexture.prototype.constructor = Phaser.RenderTexture;
/**
* This function will draw the display object to the RenderTexture at the given coordinates.
*
* When the display object is drawn it takes into account scale and rotation.
*
* If you don't want those then use RenderTexture.renderRawXY instead.
*
* @method Phaser.RenderTexture.prototype.renderXY
* @param {Phaser.Sprite|Phaser.Image|Phaser.Text|Phaser.BitmapText|Phaser.Group} displayObject - The display object to render to this texture.
* @param {number} x - The x position to render the object at.
* @param {number} y - The y position to render the object at.
* @param {boolean} [clear=false] - If true the texture will be cleared before the display object is drawn.
*/
Phaser.RenderTexture.prototype.renderXY = function (displayObject, x, y, clear) {
displayObject.updateTransform();
this._tempMatrix.copyFrom(displayObject.worldTransform);
this._tempMatrix.tx = x;
this._tempMatrix.ty = y;
if (this.renderer.type === Phaser.WEBGL)
{
this._renderWebGL(displayObject, this._tempMatrix, clear);
}
else
{
this._renderCanvas(displayObject, this._tempMatrix, clear);
}
};
/**
* This function will draw the display object to the RenderTexture at the given coordinates.
*
* When the display object is drawn it doesn't take into account scale, rotation or translation.
*
* If you need those then use RenderTexture.renderXY instead.
*
* @method Phaser.RenderTexture.prototype.renderRawXY
* @param {Phaser.Sprite|Phaser.Image|Phaser.Text|Phaser.BitmapText|Phaser.Group} displayObject - The display object to render to this texture.
* @param {number} x - The x position to render the object at.
* @param {number} y - The y position to render the object at.
* @param {boolean} [clear=false] - If true the texture will be cleared before the display object is drawn.
*/
Phaser.RenderTexture.prototype.renderRawXY = function (displayObject, x, y, clear) {
this._tempMatrix.identity().translate(x, y);
if (this.renderer.type === Phaser.WEBGL)
{
this._renderWebGL(displayObject, this._tempMatrix, clear);
}
else
{
this._renderCanvas(displayObject, this._tempMatrix, clear);
}
};
/**
* This function will draw the display object to the RenderTexture.
*
* In versions of Phaser prior to 2.4.0 the second parameter was a Phaser.Point object.
* This is now a Matrix allowing you much more control over how the Display Object is rendered.
* If you need to replicate the earlier behavior please use Phaser.RenderTexture.renderXY instead.
*
* If you wish for the displayObject to be rendered taking its current scale, rotation and translation into account then either
* pass `null`, leave it undefined or pass `displayObject.worldTransform` as the matrix value.
*
* @method Phaser.RenderTexture.prototype.render
* @param {Phaser.Sprite|Phaser.Image|Phaser.Text|Phaser.BitmapText|Phaser.Group} displayObject - The display object to render to this texture.
* @param {Phaser.Matrix} [matrix] - Optional matrix to apply to the display object before rendering. If null or undefined it will use the worldTransform matrix of the given display object.
* @param {boolean} [clear=false] - If true the texture will be cleared before the display object is drawn.
*/
Phaser.RenderTexture.prototype.render = function (displayObject, matrix, clear) {
if (matrix === undefined || matrix === null)
{
this._tempMatrix.copyFrom(displayObject.worldTransform);
}
else
{
this._tempMatrix.copyFrom(matrix);
}
if (this.renderer.type === Phaser.WEBGL)
{
this._renderWebGL(displayObject, this._tempMatrix, clear);
}
else
{
this._renderCanvas(displayObject, this._tempMatrix, clear);
}
};
/**
* Resizes the RenderTexture.
*
* @method Phaser.RenderTexture.prototype.resize
* @param {number} width - The width to resize to.
* @param {number} height - The height to resize to.
* @param {boolean} updateBase - Should the baseTexture.width and height values be resized as well?
*/
Phaser.RenderTexture.prototype.resize = function (width, height, updateBase) {
if (width === this.width && height === this.height)
{
return;
}
this.valid = (width > 0 && height > 0);
this.width = width;
this.height = height;
this.frame.width = this.crop.width = width * this.resolution;
this.frame.height = this.crop.height = height * this.resolution;
if (updateBase)
{
this.baseTexture.width = this.width * this.resolution;
this.baseTexture.height = this.height * this.resolution;
}
if (this.renderer.type === Phaser.WEBGL)
{
this.projection.x = this.width / 2;
this.projection.y = -this.height / 2;
}
if (!this.valid)
{
return;
}
this.textureBuffer.resize(this.width, this.height);
};
/**
* Clears the RenderTexture.
*
* @method Phaser.RenderTexture.prototype.clear
*/
Phaser.RenderTexture.prototype.clear = function () {
if (!this.valid)
{
return;
}
if (this.renderer.type === Phaser.WEBGL)
{
this.renderer.gl.bindFramebuffer(this.renderer.gl.FRAMEBUFFER, this.textureBuffer.frameBuffer);
}
this.textureBuffer.clear();
};
/**
* This function will draw the display object to the texture.
*
* @private
* @method Phaser.RenderTexture.prototype._renderWebGL
* @param displayObject {DisplayObject} The display object to render this texture on
* @param [matrix] {Matrix} Optional matrix to apply to the display object before rendering.
* @param [clear] {Boolean} If true the texture will be cleared before the displayObject is drawn
* @private
*/
Phaser.RenderTexture.prototype._renderWebGL = function (displayObject, matrix, clear) {
if (!this.valid || displayObject.alpha === 0)
{
return;
}
// Let's create a nice matrix to apply to our display object.
// Frame buffers come in upside down so we need to flip the matrix.
var wt = displayObject.worldTransform;
wt.identity();
wt.translate(0, this.projection.y * 2);
if (matrix)
{
wt.append(matrix);
}
wt.scale(1, -1);
// Time to update all the children of the displayObject with the new matrix.
for (var i = 0; i < displayObject.children.length; i++)
{
displayObject.children[i].updateTransform();
}
// Time for the webGL fun stuff!
var gl = this.renderer.gl;
gl.viewport(0, 0, this.width * this.resolution, this.height * this.resolution);
gl.bindFramebuffer(gl.FRAMEBUFFER, this.textureBuffer.frameBuffer);
if (clear)
{
this.textureBuffer.clear();
}
this.renderer.spriteBatch.dirty = true;
this.renderer.renderDisplayObject(displayObject, this.projection, this.textureBuffer.frameBuffer, matrix);
this.renderer.spriteBatch.dirty = true;
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
};
/**
* This function will draw the display object to the texture.
*
* @private
* @method Phaser.RenderTexture.prototype._renderCanvas
* @param displayObject {DisplayObject} The display object to render this texture on
* @param [matrix] {Matrix} Optional matrix to apply to the display object before rendering.
* @param [clear] {Boolean} If true the texture will be cleared before the displayObject is drawn
*/
Phaser.RenderTexture.prototype._renderCanvas = function (displayObject, matrix, clear) {
if (!this.valid || displayObject.alpha === 0)
{
return;
}
// Let's create a nice matrix to apply to our display object.
// Frame buffers come in upside down so we need to flip the matrix.
var wt = displayObject.worldTransform;
wt.identity();
if (matrix)
{
wt.append(matrix);
}
// Time to update all the children of the displayObject with the new matrix (what new matrix? there isn't one!)
for (var i = 0; i < displayObject.children.length; i++)
{
displayObject.children[i].updateTransform();
}
if (clear)
{
this.textureBuffer.clear();
}
var realResolution = this.renderer.resolution;
this.renderer.resolution = this.resolution;
this.renderer.renderDisplayObject(displayObject, this.textureBuffer.context, matrix);
this.renderer.resolution = realResolution;
};
/**
* Will return a HTML Image of the texture
*
* @method Phaser.RenderTexture.prototype.getImage
* @return {Image}
*/
Phaser.RenderTexture.prototype.getImage = function () {
var image = new Image();
image.src = this.getBase64();
return image;
};
/**
* Will return a base64 encoded string of this texture. It works by calling RenderTexture.getCanvas and then running toDataURL on that.
*
* @method Phaser.RenderTexture.prototype.getBase64
* @return {String} A base64 encoded string of the texture.
*/
Phaser.RenderTexture.prototype.getBase64 = function () {
return this.getCanvas().toDataURL();
};
/**
* Creates a Canvas element, renders this RenderTexture to it and then returns it.
*
* @method Phaser.RenderTexture.prototype.getCanvas
* @return {HTMLCanvasElement} A Canvas element with the texture rendered on.
*/
Phaser.RenderTexture.prototype.getCanvas = function () {
if (this.renderer.type === Phaser.WEBGL)
{
var gl = this.renderer.gl;
var width = this.textureBuffer.width;
var height = this.textureBuffer.height;
var webGLPixels = new Uint8Array(4 * width * height);
gl.bindFramebuffer(gl.FRAMEBUFFER, this.textureBuffer.frameBuffer);
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, webGLPixels);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
var tempCanvas = new PIXI.CanvasBuffer(width, height);
var canvasData = tempCanvas.context.getImageData(0, 0, width, height);
canvasData.data.set(webGLPixels);
tempCanvas.context.putImageData(canvasData, 0, 0);
return tempCanvas.canvas;
}
else
{
return this.textureBuffer.canvas;
}
};