2020-11-06 11:42:39 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2020 Photon Storm Ltd.
|
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* A Render Target encapsulates a WebGL framebuffer and the WebGL Texture that displays it.
|
|
|
|
*
|
2020-11-06 12:24:46 +00:00
|
|
|
* Instances of this class are created by, and belong to WebGL Pipelines.
|
2020-11-06 11:42:39 +00:00
|
|
|
*
|
|
|
|
* @class RenderTarget
|
|
|
|
* @memberof Phaser.Renderer.WebGL
|
|
|
|
* @constructor
|
|
|
|
* @since 3.50.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Renderer.WebGL.WebGLPipeline} pipeline - The WebGLPipeline to which this Render Target belongs.
|
|
|
|
* @param {number} width - The width of the WebGL Pipeline.
|
|
|
|
* @param {number} height - The height of the WebGL Pipeline.
|
2020-11-06 12:24:46 +00:00
|
|
|
* @param {number} scale - A value between 0 and 1. Controls the size of this Render Target in relation to the Renderer.
|
|
|
|
* @param {number} minFilter - The minFilter mode of the texture when created. 0 is `LINEAR`, 1 is `NEAREST`.
|
|
|
|
* @param {boolean} autoClear - Automatically clear this framebuffer when bound?
|
2020-11-06 11:42:39 +00:00
|
|
|
*/
|
|
|
|
var RenderTarget = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2020-11-06 12:24:46 +00:00
|
|
|
function RenderTarget (pipeline, width, height, scale, minFilter, autoClear)
|
2020-11-06 11:42:39 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A reference to the WebGLPipeline that owns this Render Target.
|
|
|
|
*
|
|
|
|
* A Render Target class can only belong to a single pipeline.
|
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.RenderTarget#pipeline
|
|
|
|
* @type {Phaser.Renderer.WebGL.WebGLPipeline}
|
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
|
|
|
this.pipeline = pipeline;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the WebGLRenderer instance.
|
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.RenderTarget#renderer
|
|
|
|
* @type {Phaser.Renderer.WebGL.WebGLRenderer}
|
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
|
|
|
this.renderer = pipeline.renderer;
|
|
|
|
|
|
|
|
/**
|
2020-11-06 12:24:46 +00:00
|
|
|
* The WebGLFramebuffer of this Render Target.
|
|
|
|
*
|
|
|
|
* This is created in the `RenderTarget.resize` method.
|
2020-11-06 11:42:39 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.RenderTarget#framebuffer
|
|
|
|
* @type {WebGLFramebuffer}
|
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
|
|
|
this.framebuffer = null;
|
|
|
|
|
|
|
|
/**
|
2020-11-06 12:24:46 +00:00
|
|
|
* The WebGLTexture of this Render Target.
|
|
|
|
*
|
|
|
|
* This is created in the `RenderTarget.resize` method.
|
2020-11-06 11:42:39 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.RenderTarget#texture
|
|
|
|
* @type {WebGLTexture}
|
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
|
|
|
this.texture = null;
|
|
|
|
|
2020-11-11 17:45:58 +00:00
|
|
|
/**
|
|
|
|
* The width of the texture.
|
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.RenderTarget#width
|
|
|
|
* @type {number}
|
|
|
|
* @readonly
|
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
|
|
|
this.width = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The height of the texture.
|
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.RenderTarget#height
|
|
|
|
* @type {number}
|
|
|
|
* @readonly
|
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
|
|
|
this.height = 0;
|
|
|
|
|
2020-11-06 11:42:39 +00:00
|
|
|
/**
|
2020-11-06 12:24:46 +00:00
|
|
|
* A value between 0 and 1. Controls the size of this Render Target in relation to the Renderer.
|
2020-11-06 11:42:39 +00:00
|
|
|
*
|
2020-11-06 12:24:46 +00:00
|
|
|
* A value of 1 matches it. 0.5 makes the Render Target half the size of the renderer, etc.
|
2020-11-06 11:42:39 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.RenderTarget#scale
|
|
|
|
* @type {number}
|
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
|
|
|
this.scale = scale;
|
|
|
|
|
2020-11-06 12:24:46 +00:00
|
|
|
/**
|
|
|
|
* The minFilter mode of the texture. 0 is `LINEAR`, 1 is `NEAREST`.
|
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.RenderTarget#minFilter
|
|
|
|
* @type {number}
|
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
|
|
|
this.minFilter = minFilter;
|
|
|
|
|
2020-11-06 11:42:39 +00:00
|
|
|
/**
|
|
|
|
* Controls if this Render Target is automatically cleared (via `gl.COLOR_BUFFER_BIT`)
|
2020-11-06 12:24:46 +00:00
|
|
|
* during the `RenderTarget.bind` method.
|
2020-11-06 11:42:39 +00:00
|
|
|
*
|
|
|
|
* If you need more control over how, or if, the target is cleared, you can disable
|
2020-11-06 12:24:46 +00:00
|
|
|
* this via the config on creation, or even toggle it directly at runtime.
|
2020-11-06 11:42:39 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.RenderTarget#autoClear
|
|
|
|
* @type {boolean}
|
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
|
|
|
this.autoClear = autoClear;
|
|
|
|
|
|
|
|
this.resize(width, height);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizes this Render Target.
|
|
|
|
*
|
|
|
|
* Deletes both the frame buffer and texture, if they exist and then re-creates
|
|
|
|
* them using the new sizes.
|
|
|
|
*
|
|
|
|
* This method is called automatically by the pipeline during its resize handler.
|
|
|
|
*
|
|
|
|
* @method Phaser.Renderer.WebGL.RenderTarget#resize
|
|
|
|
* @since 3.50.0
|
|
|
|
*
|
|
|
|
* @param {number} width - The new width of the WebGL Pipeline.
|
|
|
|
* @param {number} height - The new height of the WebGL Pipeline.
|
|
|
|
*
|
|
|
|
* @return {this} This RenderTarget instance.
|
|
|
|
*/
|
|
|
|
resize: function (width, height)
|
|
|
|
{
|
|
|
|
var renderer = this.renderer;
|
|
|
|
|
|
|
|
renderer.deleteFramebuffer(this.framebuffer);
|
|
|
|
|
|
|
|
renderer.deleteTexture(this.texture);
|
|
|
|
|
|
|
|
width *= this.scale;
|
|
|
|
height *= this.scale;
|
|
|
|
|
2020-11-06 12:24:46 +00:00
|
|
|
this.texture = renderer.createTextureFromSource(null, width, height, this.minFilter);
|
2020-11-06 11:42:39 +00:00
|
|
|
this.framebuffer = renderer.createFramebuffer(width, height, this.texture, false);
|
|
|
|
|
2020-11-11 17:45:58 +00:00
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
|
2020-11-06 11:42:39 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2020-11-06 12:24:46 +00:00
|
|
|
* Pushes this Render Target as the current frame buffer of the renderer.
|
2020-11-06 11:42:39 +00:00
|
|
|
*
|
2020-11-06 12:24:46 +00:00
|
|
|
* If `autoClear` is set, then clears the texture.
|
2020-11-06 11:42:39 +00:00
|
|
|
*
|
2020-11-06 12:24:46 +00:00
|
|
|
* @method Phaser.Renderer.WebGL.RenderTarget#bind
|
2020-11-06 11:42:39 +00:00
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
|
|
|
bind: function ()
|
|
|
|
{
|
|
|
|
this.renderer.pushFramebuffer(this.framebuffer);
|
|
|
|
|
|
|
|
if (this.autoClear)
|
|
|
|
{
|
|
|
|
var gl = this.pipeline.gl;
|
|
|
|
|
|
|
|
gl.clearColor(0, 0, 0, 0);
|
|
|
|
|
|
|
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-06 12:24:46 +00:00
|
|
|
/**
|
|
|
|
* Unbinds this Render Target.
|
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.RenderTarget#unbind
|
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
2020-11-06 11:42:39 +00:00
|
|
|
unbind: function ()
|
|
|
|
{
|
|
|
|
this.renderer.popFramebuffer();
|
|
|
|
},
|
|
|
|
|
2020-11-06 12:24:46 +00:00
|
|
|
/**
|
|
|
|
* Draws a quad to the pipeline, using this Render Target texture,
|
|
|
|
* sized so that it fills the renderer.
|
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.RenderTarget#draw
|
|
|
|
* @since 3.50.0
|
2020-11-11 17:45:58 +00:00
|
|
|
draw: function ()
|
|
|
|
{
|
|
|
|
var width = this.renderer.width;
|
|
|
|
var height = this.renderer.height;
|
|
|
|
|
|
|
|
this.pipeline.drawFillRect(0, 0, width, height, 0, 1, this.texture);
|
|
|
|
},
|
|
|
|
*/
|
2020-11-06 11:42:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all external references from this class and deletes the
|
|
|
|
* WebGL framebuffer and texture instances.
|
|
|
|
*
|
|
|
|
* Does not remove this Render Target from the parent pipeline.
|
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.RenderTarget#destroy
|
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
var renderer = this.pipeline.renderer;
|
|
|
|
|
|
|
|
renderer.deleteFramebuffer(this.framebuffer);
|
|
|
|
renderer.deleteTexture(this.texture);
|
|
|
|
|
|
|
|
this.pipeline = null;
|
|
|
|
this.framebuffer = null;
|
|
|
|
this.texture = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = RenderTarget;
|