/
TextureSource.js
360 lines (320 loc) · 11.1 KB
/
TextureSource.js
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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var CanvasPool = require('../display/canvas/CanvasPool');
var Class = require('../utils/Class');
var IsSizePowerOfTwo = require('../math/pow2/IsSizePowerOfTwo');
var ScaleModes = require('../renderer/ScaleModes');
/**
* @classdesc
* A Texture Source is the encapsulation of the actual source data for a Texture.
*
* This is typically an Image Element, loaded from the file system or network, a Canvas Element or a Video Element.
*
* A Texture can contain multiple Texture Sources, which only happens when a multi-atlas is loaded.
*
* @class TextureSource
* @memberof Phaser.Textures
* @constructor
* @since 3.0.0
*
* @param {Phaser.Textures.Texture} texture - The Texture this TextureSource belongs to.
* @param {(HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|Phaser.GameObjects.RenderTexture|WebGLTexture|Phaser.Types.Textures.CompressedTextureData|Phaser.Textures.DynamicTexture)} source - The source image data.
* @param {number} [width] - Optional width of the source image. If not given it's derived from the source itself.
* @param {number} [height] - Optional height of the source image. If not given it's derived from the source itself.
* @param {boolean} [flipY=false] - Sets the `UNPACK_FLIP_Y_WEBGL` flag the WebGL Texture uses during upload.
*/
var TextureSource = new Class({
initialize:
function TextureSource (texture, source, width, height, flipY)
{
if (flipY === undefined) { flipY = false; }
var game = texture.manager.game;
/**
* A reference to the Canvas or WebGL Renderer.
*
* @name Phaser.Textures.TextureSource#renderer
* @type {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)}
* @since 3.7.0
*/
this.renderer = game.renderer;
/**
* The Texture this TextureSource instance belongs to.
*
* @name Phaser.Textures.TextureSource#texture
* @type {Phaser.Textures.Texture}
* @since 3.0.0
*/
this.texture = texture;
/**
* The source of the image data.
*
* This is either an Image Element, a Canvas Element, a Video Element, a RenderTexture or a WebGLTexture.
*
* In Phaser 3.60 and above it can also be a Compressed Texture data object.
*
* @name Phaser.Textures.TextureSource#source
* @type {(HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|Phaser.GameObjects.RenderTexture|WebGLTexture|Phaser.Types.Textures.CompressedTextureData|Phaser.Textures.DynamicTexture)}
* @since 3.12.0
*/
this.source = source;
/**
* The image data.
*
* This is either an Image element, Canvas element or a Video Element.
*
* @name Phaser.Textures.TextureSource#image
* @type {(HTMLImageElement|HTMLCanvasElement|HTMLVideoElement)}
* @since 3.0.0
*/
this.image = (source.compressed) ? null : source;
/**
* Holds the compressed textured algorithm, or `null` if it's not a compressed texture.
*
* Prior to Phaser 3.60 this value always held `null`.
*
* @name Phaser.Textures.TextureSource#compressionAlgorithm
* @type {number}
* @default null
* @since 3.0.0
*/
this.compressionAlgorithm = (source.compressed) ? source.format : null;
/**
* The resolution of the source image.
*
* @name Phaser.Textures.TextureSource#resolution
* @type {number}
* @default 1
* @since 3.0.0
*/
this.resolution = 1;
/**
* The width of the source image. If not specified in the constructor it will check
* the `naturalWidth` and then `width` properties of the source image.
*
* @name Phaser.Textures.TextureSource#width
* @type {number}
* @since 3.0.0
*/
this.width = width || source.naturalWidth || source.videoWidth || source.width || 0;
/**
* The height of the source image. If not specified in the constructor it will check
* the `naturalHeight` and then `height` properties of the source image.
*
* @name Phaser.Textures.TextureSource#height
* @type {number}
* @since 3.0.0
*/
this.height = height || source.naturalHeight || source.videoHeight || source.height || 0;
/**
* The Scale Mode the image will use when rendering.
* Either Linear or Nearest.
*
* @name Phaser.Textures.TextureSource#scaleMode
* @type {number}
* @since 3.0.0
*/
this.scaleMode = ScaleModes.DEFAULT;
/**
* Is the source image a Canvas Element?
*
* @name Phaser.Textures.TextureSource#isCanvas
* @type {boolean}
* @since 3.0.0
*/
this.isCanvas = (source instanceof HTMLCanvasElement);
/**
* Is the source image a Video Element?
*
* @name Phaser.Textures.TextureSource#isVideo
* @type {boolean}
* @since 3.20.0
*/
this.isVideo = (window.hasOwnProperty('HTMLVideoElement') && source instanceof HTMLVideoElement);
/**
* Is the source image a Render Texture?
*
* @name Phaser.Textures.TextureSource#isRenderTexture
* @type {boolean}
* @since 3.12.0
*/
this.isRenderTexture = (source.type === 'RenderTexture' || source.type === 'DynamicTexture');
/**
* Is the source image a WebGLTexture?
*
* @name Phaser.Textures.TextureSource#isGLTexture
* @type {boolean}
* @since 3.19.0
*/
this.isGLTexture = (window.hasOwnProperty('WebGLTexture') && source instanceof WebGLTexture);
/**
* Are the source image dimensions a power of two?
*
* @name Phaser.Textures.TextureSource#isPowerOf2
* @type {boolean}
* @since 3.0.0
*/
this.isPowerOf2 = IsSizePowerOfTwo(this.width, this.height);
/**
* The WebGL Texture of the source image. If this TextureSource is driven from a WebGLTexture
* already, then this is a reference to that WebGLTexture.
*
* @name Phaser.Textures.TextureSource#glTexture
* @type {?WebGLTexture}
* @default null
* @since 3.0.0
*/
this.glTexture = null;
/**
* Sets the `UNPACK_FLIP_Y_WEBGL` flag the WebGL Texture uses during upload.
*
* @name Phaser.Textures.TextureSource#flipY
* @type {boolean}
* @since 3.20.0
*/
this.flipY = flipY;
this.init(game);
},
/**
* Creates a WebGL Texture, if required, and sets the Texture filter mode.
*
* @method Phaser.Textures.TextureSource#init
* @since 3.0.0
*
* @param {Phaser.Game} game - A reference to the Phaser Game instance.
*/
init: function (game)
{
var renderer = this.renderer;
if (renderer)
{
var source = this.source;
if (renderer.gl)
{
var image = this.image;
var flipY = this.flipY;
var width = this.width;
var height = this.height;
var scaleMode = this.scaleMode;
if (this.isCanvas)
{
this.glTexture = renderer.createCanvasTexture(image, false, flipY);
}
else if (this.isVideo)
{
this.glTexture = renderer.createVideoTexture(image, false, flipY);
}
else if (this.isRenderTexture)
{
this.glTexture = renderer.createTextureFromSource(null, width, height, scaleMode);
}
else if (this.isGLTexture)
{
this.glTexture = source;
}
else if (this.compressionAlgorithm)
{
this.glTexture = renderer.createTextureFromSource(source);
}
else
{
this.glTexture = renderer.createTextureFromSource(image, width, height, scaleMode);
}
if (typeof WEBGL_DEBUG)
{
// eslint-disable-next-line camelcase
this.glTexture.__SPECTOR_Metadata = { textureKey: this.texture.key };
}
}
else if (this.isRenderTexture)
{
this.image = source.canvas;
}
}
if (!game.config.antialias)
{
this.setFilter(1);
}
},
/**
* Sets the Filter Mode for this Texture.
*
* The mode can be either Linear, the default, or Nearest.
*
* For pixel-art you should use Nearest.
*
* @method Phaser.Textures.TextureSource#setFilter
* @since 3.0.0
*
* @param {Phaser.Textures.FilterMode} filterMode - The Filter Mode.
*/
setFilter: function (filterMode)
{
if (this.renderer.gl)
{
this.renderer.setTextureFilter(this.glTexture, filterMode);
}
this.scaleMode = filterMode;
},
/**
* Sets the `UNPACK_FLIP_Y_WEBGL` flag for the WebGL Texture during texture upload.
*
* @method Phaser.Textures.TextureSource#setFlipY
* @since 3.20.0
*
* @param {boolean} [value=true] - Should the WebGL Texture be flipped on the Y axis on texture upload or not?
*/
setFlipY: function (value)
{
if (value === undefined) { value = true; }
this.flipY = value;
return this;
},
/**
* If this TextureSource is backed by a Canvas and is running under WebGL,
* it updates the WebGLTexture using the canvas data.
*
* @method Phaser.Textures.TextureSource#update
* @since 3.7.0
*/
update: function ()
{
var renderer = this.renderer;
var image = this.image;
var flipY = this.flipY;
var gl = renderer.gl;
if (gl && this.isCanvas)
{
this.glTexture = renderer.updateCanvasTexture(image, this.glTexture, flipY);
}
else if (gl && this.isVideo)
{
this.glTexture = renderer.updateVideoTexture(image, this.glTexture, flipY);
}
},
/**
* Destroys this Texture Source and nulls the references.
*
* @method Phaser.Textures.TextureSource#destroy
* @since 3.0.0
*/
destroy: function ()
{
if (this.glTexture)
{
this.renderer.deleteTexture(this.glTexture, true);
}
if (this.isCanvas)
{
CanvasPool.remove(this.image);
}
this.renderer = null;
this.texture = null;
this.source = null;
this.image = null;
this.glTexture = null;
}
});
module.exports = TextureSource;