2018-03-05 02:24:47 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
var CanvasPool = require('../../display/canvas/CanvasPool');
|
2018-02-23 03:44:22 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var Components = require('../components');
|
2018-03-05 02:24:47 +00:00
|
|
|
var CONST = require('../../const');
|
2018-02-23 03:44:22 +00:00
|
|
|
var GameObject = require('../GameObject');
|
|
|
|
var Render = require('./RenderTextureRender');
|
2018-03-05 02:24:47 +00:00
|
|
|
var RenderTextureCanvas = require('./RenderTextureCanvas');
|
|
|
|
var RenderTextureWebGL = require('./RenderTextureWebGL');
|
2018-02-23 03:44:22 +00:00
|
|
|
|
2018-03-05 02:24:47 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* A 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
|
2018-03-29 11:54:33 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.ComputedSize
|
2018-03-05 02:24:47 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Depth
|
|
|
|
* @extends Phaser.GameObjects.Components.Flip
|
|
|
|
* @extends Phaser.GameObjects.Components.GetBounds
|
2018-04-20 17:57:49 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Mask
|
2018-03-05 02:24:47 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.MatrixStack
|
|
|
|
* @extends Phaser.GameObjects.Components.Origin
|
|
|
|
* @extends Phaser.GameObjects.Components.Pipeline
|
|
|
|
* @extends Phaser.GameObjects.Components.ScaleMode
|
|
|
|
* @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 - The horizontal position of this Game Object in the world.
|
|
|
|
* @param {number} y - The vertical position of this Game Object in the world.
|
|
|
|
* @param {integer} [width=32] - The width of the Render Texture.
|
|
|
|
* @param {integer} [height=32] - The height of the Render Texture.
|
|
|
|
*/
|
2018-02-23 03:44:22 +00:00
|
|
|
var RenderTexture = new Class({
|
|
|
|
|
|
|
|
Extends: GameObject,
|
|
|
|
|
|
|
|
Mixins: [
|
|
|
|
Components.Alpha,
|
|
|
|
Components.BlendMode,
|
2018-03-23 10:00:25 +00:00
|
|
|
Components.ComputedSize,
|
2018-02-23 03:44:22 +00:00
|
|
|
Components.Depth,
|
|
|
|
Components.Flip,
|
|
|
|
Components.GetBounds,
|
2018-04-20 17:57:49 +00:00
|
|
|
Components.Mask,
|
2018-02-23 03:44:22 +00:00
|
|
|
Components.Origin,
|
|
|
|
Components.Pipeline,
|
|
|
|
Components.ScaleMode,
|
|
|
|
Components.ScrollFactor,
|
|
|
|
Components.Tint,
|
|
|
|
Components.Transform,
|
|
|
|
Components.Visible,
|
|
|
|
Render
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2018-03-05 01:40:11 +00:00
|
|
|
function RenderTexture (scene, x, y, width, height)
|
2018-02-23 03:44:22 +00:00
|
|
|
{
|
2018-03-05 02:24:47 +00:00
|
|
|
if (width === undefined) { width = 32; }
|
|
|
|
if (height === undefined) { height = 32; }
|
|
|
|
|
2018-02-23 03:44:22 +00:00
|
|
|
GameObject.call(this, scene, 'RenderTexture');
|
2018-03-05 02:24:47 +00:00
|
|
|
|
2018-07-31 16:10:26 +00:00
|
|
|
// this.initMatrixStack();
|
2018-02-23 03:44:22 +00:00
|
|
|
|
2018-03-23 10:00:25 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-02-23 03:44:22 +00:00
|
|
|
this.renderer = scene.sys.game.renderer;
|
2018-03-23 10:00:25 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-06 13:23:39 +00:00
|
|
|
* The tint of the Render Texture when rendered.
|
2018-03-23 10:00:25 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.RenderTexture#globalTint
|
|
|
|
* @type {number}
|
|
|
|
* @default 0xffffff
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2018-03-06 18:56:33 +00:00
|
|
|
this.globalTint = 0xffffff;
|
2018-03-23 10:00:25 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-06 13:23:39 +00:00
|
|
|
* The alpha of the Render Texture when rendered.
|
2018-03-23 10:00:25 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.RenderTexture#globalAlpha
|
2018-06-06 13:23:39 +00:00
|
|
|
* @type {number}
|
2018-03-23 10:00:25 +00:00
|
|
|
* @default 1
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
this.globalAlpha = 1;
|
2018-03-20 14:56:31 +00:00
|
|
|
|
2018-05-29 11:07:40 +00:00
|
|
|
/**
|
|
|
|
* The HTML Canvas Element that the Render Texture is drawing to.
|
|
|
|
* This is only set if Phaser is running with the Canvas Renderer.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.RenderTexture#canvas
|
|
|
|
* @type {?HTMLCanvasElement}
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
this.canvas = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the Rendering Context belonging to the Canvas Element this Render Texture is drawing to.
|
|
|
|
* This is only set if Phaser is running with the Canvas Renderer.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.RenderTexture#context
|
|
|
|
* @type {?CanvasRenderingContext2D}
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
this.context = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the GL Frame Buffer this Render Texture is drawing to.
|
|
|
|
* This is only set if Phaser is running with the WebGL Renderer.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.RenderTexture#framebuffer
|
|
|
|
* @type {?WebGLFramebuffer}
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
this.framebuffer = null;
|
|
|
|
|
2018-07-31 16:10:26 +00:00
|
|
|
this.currentMatrix = new Components.TransformMatrix();
|
|
|
|
|
2018-03-05 02:24:47 +00:00
|
|
|
if (this.renderer.type === CONST.WEBGL)
|
2018-02-23 03:44:22 +00:00
|
|
|
{
|
|
|
|
var gl = this.renderer.gl;
|
2018-03-23 10:00:25 +00:00
|
|
|
|
2018-02-23 03:44:22 +00:00
|
|
|
this.gl = gl;
|
|
|
|
this.fill = RenderTextureWebGL.fill;
|
|
|
|
this.clear = RenderTextureWebGL.clear;
|
|
|
|
this.draw = RenderTextureWebGL.draw;
|
2018-07-31 16:10:26 +00:00
|
|
|
this.drawFrame = RenderTextureWebGL.drawFrame;
|
|
|
|
this.drawGameObject = RenderTextureWebGL.drawGameObject;
|
2018-02-23 03:44:22 +00:00
|
|
|
this.texture = this.renderer.createTexture2D(0, gl.NEAREST, gl.NEAREST, gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE, gl.RGBA, null, width, height, false);
|
|
|
|
this.framebuffer = this.renderer.createFramebuffer(width, height, this.texture, false);
|
|
|
|
}
|
2018-03-05 02:24:47 +00:00
|
|
|
else if (this.renderer.type === CONST.CANVAS)
|
2018-02-23 03:44:22 +00:00
|
|
|
{
|
2018-02-24 00:05:15 +00:00
|
|
|
this.fill = RenderTextureCanvas.fill;
|
|
|
|
this.clear = RenderTextureCanvas.clear;
|
|
|
|
this.draw = RenderTextureCanvas.draw;
|
2018-04-13 15:48:34 +00:00
|
|
|
this.canvas = CanvasPool.create2D(this, width, height);
|
2018-03-05 01:40:11 +00:00
|
|
|
this.context = this.canvas.getContext('2d');
|
2018-02-23 03:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setPosition(x, y);
|
|
|
|
this.setSize(width, height);
|
|
|
|
this.initPipeline('TextureTintPipeline');
|
|
|
|
},
|
|
|
|
|
2018-05-29 11:07:40 +00:00
|
|
|
/**
|
|
|
|
* Resizes the Render Texture to the new dimensions given.
|
2018-06-06 13:23:39 +00:00
|
|
|
*
|
2018-05-29 11:07:40 +00:00
|
|
|
* 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] - 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; }
|
|
|
|
|
|
|
|
if (width !== this.width || height !== this.height)
|
|
|
|
{
|
|
|
|
if (this.canvas)
|
|
|
|
{
|
|
|
|
this.canvas.width = width;
|
|
|
|
this.canvas.height = height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.renderer.deleteTexture(this.texture);
|
|
|
|
this.renderer.deleteFramebuffer(this.framebuffer);
|
|
|
|
|
|
|
|
var gl = this.renderer.gl;
|
|
|
|
|
|
|
|
this.texture = this.renderer.createTexture2D(0, gl.NEAREST, gl.NEAREST, gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE, gl.RGBA, null, width, height, false);
|
|
|
|
this.framebuffer = this.renderer.createFramebuffer(width, height, this.texture, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setSize(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-03-05 19:57:41 +00:00
|
|
|
/**
|
2018-06-06 13:23:39 +00:00
|
|
|
* Set the tint to use when rendering this Render Texture.
|
2018-03-05 19:57:41 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.RenderTexture#setGlobalTint
|
|
|
|
* @since 3.2.0
|
|
|
|
*
|
2018-06-06 13:23:39 +00:00
|
|
|
* @param {integer} tint - The tint value.
|
2018-03-05 19:57:41 +00:00
|
|
|
*
|
2018-06-06 13:23:39 +00:00
|
|
|
* @return {this} This Render Texture.
|
2018-03-05 19:57:41 +00:00
|
|
|
*/
|
|
|
|
setGlobalTint: function (tint)
|
|
|
|
{
|
|
|
|
this.globalTint = tint;
|
2018-03-23 10:00:25 +00:00
|
|
|
|
2018-03-05 19:57:41 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-06-06 13:23:39 +00:00
|
|
|
* Set the alpha to use when rendering this Render Texture.
|
2018-03-05 19:57:41 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.RenderTexture#setGlobalAlpha
|
|
|
|
* @since 3.2.0
|
|
|
|
*
|
2018-06-06 13:23:39 +00:00
|
|
|
* @param {number} alpha - The alpha value.
|
2018-03-05 19:57:41 +00:00
|
|
|
*
|
2018-06-06 13:23:39 +00:00
|
|
|
* @return {this} This Render Texture.
|
2018-03-05 19:57:41 +00:00
|
|
|
*/
|
|
|
|
setGlobalAlpha: function (alpha)
|
|
|
|
{
|
|
|
|
this.globalAlpha = alpha;
|
2018-03-23 10:00:25 +00:00
|
|
|
|
2018-03-05 19:57:41 +00:00
|
|
|
return this;
|
2018-05-22 14:55:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal destroy handler, called as part of the destroy process.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.RenderTexture#preDestroy
|
|
|
|
* @protected
|
|
|
|
* @since 3.9.0
|
|
|
|
*/
|
|
|
|
preDestroy: function ()
|
|
|
|
{
|
2018-07-29 11:20:09 +00:00
|
|
|
if (this.canvas)
|
|
|
|
{
|
|
|
|
CanvasPool.remove(this.canvas);
|
|
|
|
}
|
|
|
|
|
2018-05-23 13:55:51 +00:00
|
|
|
if (this.renderer && this.renderer.gl)
|
2018-05-22 14:55:22 +00:00
|
|
|
{
|
|
|
|
this.renderer.deleteTexture(this.texture);
|
|
|
|
this.renderer.deleteFramebuffer(this.framebuffer);
|
|
|
|
}
|
2018-03-05 21:49:08 +00:00
|
|
|
}
|
2018-02-23 03:44:22 +00:00
|
|
|
|
2018-03-05 02:24:47 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.RenderTexture} This Game Object.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the Render Texture.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.RenderTexture#clear
|
|
|
|
* @since 3.2.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.RenderTexture} This Game Object.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws a texture frame to the Render Texture at the given position.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.RenderTexture#draw
|
|
|
|
* @since 3.2.0
|
|
|
|
*
|
|
|
|
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
|
2018-03-20 14:56:31 +00:00
|
|
|
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
|
2018-03-05 02:24:47 +00:00
|
|
|
* @param {number} x - The x position to draw the frame at.
|
|
|
|
* @param {number} y - The y position to draw the frame at.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.RenderTexture} This Game Object.
|
|
|
|
*/
|
|
|
|
|
2018-02-23 03:44:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = RenderTexture;
|