2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2018-04-23 22:41:05 +00:00
|
|
|
var CanvasPool = require('../display/canvas/CanvasPool');
|
2017-07-04 12:23:58 +00:00
|
|
|
var Class = require('../utils/Class');
|
2016-12-08 16:21:16 +00:00
|
|
|
var IsSizePowerOfTwo = require('../math/pow2/IsSizePowerOfTwo');
|
2017-07-04 12:23:58 +00:00
|
|
|
var ScaleModes = require('../renderer/ScaleModes');
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* A Texture Source is the encapsulation of the actual source data for a Texture.
|
2019-10-03 16:50:18 +00:00
|
|
|
*
|
|
|
|
* This is typically an Image Element, loaded from the file system or network, a Canvas Element or a Video Element.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* A Texture can contain multiple Texture Sources, which only happens when a multi-atlas is loaded.
|
|
|
|
*
|
|
|
|
* @class TextureSource
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.Textures
|
2018-02-08 04:01:44 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Textures.Texture} texture - The Texture this TextureSource belongs to.
|
2019-10-03 16:50:18 +00:00
|
|
|
* @param {(HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|Phaser.GameObjects.RenderTexture|WebGLTexture)} source - The source image data.
|
2018-02-08 04:01:44 +00:00
|
|
|
* @param {integer} [width] - Optional width of the source image. If not given it's derived from the source itself.
|
|
|
|
* @param {integer} [height] - Optional height of the source image. If not given it's derived from the source itself.
|
2019-10-10 11:26:25 +00:00
|
|
|
* @param {boolean} [flipY=false] - Sets the `UNPACK_FLIP_Y_WEBGL` flag the WebGL Texture uses during upload.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
var TextureSource = new Class({
|
2016-12-06 16:18:28 +00:00
|
|
|
|
2017-07-04 12:23:58 +00:00
|
|
|
initialize:
|
2017-06-07 23:12:22 +00:00
|
|
|
|
2019-10-10 11:26:25 +00:00
|
|
|
function TextureSource (texture, source, width, height, flipY)
|
2017-07-04 12:23:58 +00:00
|
|
|
{
|
2019-10-10 11:26:25 +00:00
|
|
|
if (flipY === undefined) { flipY = false; }
|
|
|
|
|
2017-09-13 20:54:32 +00:00
|
|
|
var game = texture.manager.game;
|
|
|
|
|
2018-04-23 17:37:30 +00:00
|
|
|
/**
|
|
|
|
* The Texture this TextureSource belongs to.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureSource#renderer
|
|
|
|
* @type {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)}
|
2018-05-04 17:51:02 +00:00
|
|
|
* @since 3.7.0
|
2018-04-23 17:37:30 +00:00
|
|
|
*/
|
|
|
|
this.renderer = game.renderer;
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* The Texture this TextureSource belongs to.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureSource#texture
|
2018-08-02 11:34:57 +00:00
|
|
|
* @type {Phaser.Textures.Texture}
|
2018-02-08 04:01:44 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.texture = texture;
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-08-02 15:16:46 +00:00
|
|
|
* The source of the image data.
|
2019-10-03 16:50:18 +00:00
|
|
|
*
|
|
|
|
* This is either an Image Element, a Canvas Element, a Video Element, a RenderTexture or a WebGLTexture.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
2018-08-02 15:16:46 +00:00
|
|
|
* @name Phaser.Textures.TextureSource#source
|
2019-10-03 16:50:18 +00:00
|
|
|
* @type {(HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|Phaser.GameObjects.RenderTexture|WebGLTexture)}
|
2018-08-02 15:16:46 +00:00
|
|
|
* @since 3.12.0
|
|
|
|
*/
|
|
|
|
this.source = source;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The image data.
|
2019-10-03 16:50:18 +00:00
|
|
|
*
|
|
|
|
* This is either an Image element, Canvas element or a Video Element.
|
2018-08-02 15:16:46 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureSource#image
|
2019-10-03 16:50:18 +00:00
|
|
|
* @type {(HTMLImageElement|HTMLCanvasElement|HTMLVideoElement)}
|
2018-02-08 04:01:44 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.image = source;
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* Currently un-used.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureSource#compressionAlgorithm
|
|
|
|
* @type {integer}
|
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.compressionAlgorithm = null;
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* The resolution of the source image.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureSource#resolution
|
|
|
|
* @type {number}
|
|
|
|
* @default 1
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.resolution = 1;
|
2018-02-08 04:01:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2019-10-03 16:50:18 +00:00
|
|
|
this.width = width || source.naturalWidth || source.videoWidth || source.width || 0;
|
2017-07-04 12:23:58 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* 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 {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2019-10-03 16:50:18 +00:00
|
|
|
this.height = height || source.naturalHeight || source.videoHeight || source.height || 0;
|
2017-07-04 12:23:58 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* The Scale Mode the image will use when rendering.
|
|
|
|
* Either Linear or Nearest.
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureSource#scaleMode
|
2018-03-19 01:03:17 +00:00
|
|
|
* @type {number}
|
2018-02-08 04:01:44 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
this.scaleMode = ScaleModes.DEFAULT;
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* Is the source image a Canvas Element?
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureSource#isCanvas
|
|
|
|
* @type {boolean}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2019-10-18 04:03:11 +00:00
|
|
|
this.isCanvas = (source instanceof HTMLCanvasElement);
|
2017-09-20 23:15:31 +00:00
|
|
|
|
2019-10-03 01:28:47 +00:00
|
|
|
/**
|
|
|
|
* Is the source image a Video Element?
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureSource#isVideo
|
|
|
|
* @type {boolean}
|
|
|
|
* @since 3.20.0
|
|
|
|
*/
|
2019-10-15 07:24:36 +00:00
|
|
|
this.isVideo = (window.hasOwnProperty('HTMLVideoElement') && source instanceof HTMLVideoElement);
|
2019-10-03 01:28:47 +00:00
|
|
|
|
2018-08-02 11:34:57 +00:00
|
|
|
/**
|
|
|
|
* Is the source image a Render Texture?
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureSource#isRenderTexture
|
|
|
|
* @type {boolean}
|
|
|
|
* @since 3.12.0
|
|
|
|
*/
|
|
|
|
this.isRenderTexture = (source.type === 'RenderTexture');
|
|
|
|
|
2019-06-21 15:34:47 +00:00
|
|
|
/**
|
|
|
|
* Is the source image a WebGLTexture?
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureSource#isGLTexture
|
|
|
|
* @type {boolean}
|
|
|
|
* @since 3.19.0
|
|
|
|
*/
|
2019-10-01 12:46:37 +00:00
|
|
|
this.isGLTexture = (window.hasOwnProperty('WebGLTexture') && source instanceof WebGLTexture);
|
2019-06-21 15:34:47 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* Are the source image dimensions a power of two?
|
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureSource#isPowerOf2
|
|
|
|
* @type {boolean}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-08-01 12:10:08 +00:00
|
|
|
this.isPowerOf2 = IsSizePowerOfTwo(this.width, this.height);
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2019-06-21 15:08:47 +00:00
|
|
|
* The WebGL Texture of the source image. If this TextureSource is driven from a WebGLTexture
|
|
|
|
* already, then this is a reference to that WebGLTexture.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Textures.TextureSource#glTexture
|
2018-03-19 01:03:17 +00:00
|
|
|
* @type {?WebGLTexture}
|
2018-02-08 04:01:44 +00:00
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-29 23:38:27 +00:00
|
|
|
this.glTexture = null;
|
|
|
|
|
2019-10-10 11:26:25 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2017-09-13 20:54:32 +00:00
|
|
|
this.init(game);
|
|
|
|
},
|
2017-07-04 12:23:58 +00:00
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-09-13 20:54:32 +00:00
|
|
|
init: function (game)
|
|
|
|
{
|
2018-08-02 15:16:46 +00:00
|
|
|
if (this.renderer)
|
2017-07-04 12:23:58 +00:00
|
|
|
{
|
2018-08-02 15:16:46 +00:00
|
|
|
if (this.renderer.gl)
|
2018-04-23 17:37:30 +00:00
|
|
|
{
|
2019-10-03 16:50:18 +00:00
|
|
|
if (this.isCanvas)
|
2018-08-02 15:16:46 +00:00
|
|
|
{
|
2019-10-10 11:26:25 +00:00
|
|
|
this.glTexture = this.renderer.createCanvasTexture(this.image, false, this.flipY);
|
2018-08-02 15:16:46 +00:00
|
|
|
}
|
2019-10-03 16:50:18 +00:00
|
|
|
else if (this.isVideo)
|
|
|
|
{
|
2019-10-10 11:26:25 +00:00
|
|
|
this.glTexture = this.renderer.createVideoTexture(this.image, false, this.flipY);
|
2019-10-03 16:50:18 +00:00
|
|
|
}
|
2018-08-02 15:16:46 +00:00
|
|
|
else if (this.isRenderTexture)
|
|
|
|
{
|
|
|
|
this.image = this.source.canvas;
|
2018-08-03 17:51:07 +00:00
|
|
|
|
|
|
|
this.glTexture = this.renderer.createTextureFromSource(null, this.width, this.height, this.scaleMode);
|
2018-08-02 15:16:46 +00:00
|
|
|
}
|
2019-06-21 15:34:47 +00:00
|
|
|
else if (this.isGLTexture)
|
2018-08-02 15:16:46 +00:00
|
|
|
{
|
2019-06-21 15:34:47 +00:00
|
|
|
this.glTexture = this.source;
|
2018-08-02 15:16:46 +00:00
|
|
|
}
|
2019-06-21 15:08:47 +00:00
|
|
|
else
|
|
|
|
{
|
2019-06-21 15:34:47 +00:00
|
|
|
this.glTexture = this.renderer.createTextureFromSource(this.image, this.width, this.height, this.scaleMode);
|
2019-06-21 15:08:47 +00:00
|
|
|
}
|
2018-04-23 17:37:30 +00:00
|
|
|
}
|
2018-08-02 11:34:57 +00:00
|
|
|
else if (this.isRenderTexture)
|
|
|
|
{
|
2018-08-02 15:16:46 +00:00
|
|
|
this.image = this.source.canvas;
|
2018-04-23 17:37:30 +00:00
|
|
|
}
|
2017-07-04 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 14:27:16 +00:00
|
|
|
if (!game.config.antialias)
|
2017-07-04 12:23:58 +00:00
|
|
|
{
|
|
|
|
this.setFilter(1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2018-03-29 15:42:20 +00:00
|
|
|
* @param {Phaser.Textures.FilterMode} filterMode - The Filter Mode.
|
2018-02-08 04:01:44 +00:00
|
|
|
*/
|
2017-07-04 12:23:58 +00:00
|
|
|
setFilter: function (filterMode)
|
2017-05-20 01:16:45 +00:00
|
|
|
{
|
2018-04-23 17:37:30 +00:00
|
|
|
if (this.renderer.gl)
|
|
|
|
{
|
|
|
|
this.renderer.setTextureFilter(this.glTexture, filterMode);
|
|
|
|
}
|
2019-10-01 13:42:30 +00:00
|
|
|
|
|
|
|
this.scaleMode = filterMode;
|
2018-04-23 17:37:30 +00:00
|
|
|
},
|
2017-07-04 12:23:58 +00:00
|
|
|
|
2019-10-10 11:26:25 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
},
|
|
|
|
|
2018-04-23 17:37:30 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2018-05-04 17:51:02 +00:00
|
|
|
* @since 3.7.0
|
2018-04-23 17:37:30 +00:00
|
|
|
*/
|
|
|
|
update: function ()
|
|
|
|
{
|
2019-10-03 16:50:18 +00:00
|
|
|
var gl = this.renderer.gl;
|
|
|
|
|
|
|
|
if (gl && this.isCanvas)
|
2017-07-04 12:23:58 +00:00
|
|
|
{
|
2019-10-10 11:26:25 +00:00
|
|
|
this.glTexture = this.renderer.updateCanvasTexture(this.image, this.glTexture, this.flipY);
|
2017-07-04 12:23:58 +00:00
|
|
|
}
|
2019-10-03 16:50:18 +00:00
|
|
|
else if (gl && this.isVideo)
|
|
|
|
{
|
2019-10-10 11:26:25 +00:00
|
|
|
this.glTexture = this.renderer.updateVideoTexture(this.image, this.glTexture, this.flipY);
|
2019-10-03 16:50:18 +00:00
|
|
|
}
|
2018-01-31 03:38:10 +00:00
|
|
|
},
|
|
|
|
|
2018-02-08 04:01:44 +00:00
|
|
|
/**
|
2018-04-23 17:37:30 +00:00
|
|
|
* Destroys this Texture Source and nulls the references.
|
2018-02-08 04:01:44 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Textures.TextureSource#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-31 03:38:10 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
2018-04-23 17:37:30 +00:00
|
|
|
if (this.glTexture)
|
|
|
|
{
|
|
|
|
this.renderer.deleteTexture(this.glTexture);
|
|
|
|
}
|
2018-01-31 03:38:10 +00:00
|
|
|
|
2018-04-23 22:41:05 +00:00
|
|
|
if (this.isCanvas)
|
|
|
|
{
|
|
|
|
CanvasPool.remove(this.image);
|
|
|
|
}
|
|
|
|
|
2018-04-23 17:37:30 +00:00
|
|
|
this.renderer = null;
|
|
|
|
this.texture = null;
|
2018-08-02 15:16:46 +00:00
|
|
|
this.source = null;
|
2018-01-31 03:38:10 +00:00
|
|
|
this.image = null;
|
2018-08-02 15:16:46 +00:00
|
|
|
this.glTexture = null;
|
2017-05-20 01:16:45 +00:00
|
|
|
}
|
2017-07-04 12:23:58 +00:00
|
|
|
|
|
|
|
});
|
2017-05-20 01:16:45 +00:00
|
|
|
|
2016-12-06 16:18:28 +00:00
|
|
|
module.exports = TextureSource;
|