|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2020 Photon Storm Ltd. |
| 4 | + * @license {@link https://opensource.org/licenses/MIT|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Class = require('../../utils/Class'); |
| 8 | + |
| 9 | +/** |
| 10 | + * @classdesc |
| 11 | + * A Render Target encapsulates a WebGL framebuffer and the WebGL Texture that displays it. |
| 12 | + * |
| 13 | + * Instances of this class belong WebGL Pipelines. |
| 14 | + * |
| 15 | + * @class RenderTarget |
| 16 | + * @memberof Phaser.Renderer.WebGL |
| 17 | + * @constructor |
| 18 | + * @since 3.50.0 |
| 19 | + * |
| 20 | + * @param {Phaser.Renderer.WebGL.WebGLPipeline} pipeline - The WebGLPipeline to which this Render Target belongs. |
| 21 | + * @param {number} width - The width of the WebGL Pipeline. |
| 22 | + * @param {number} height - The height of the WebGL Pipeline. |
| 23 | + * @param {number} scale - The scale of the Render Target. This is a scale factor applied to the pipeline size. |
| 24 | + * @param {boolean} autoClear - Automatically gl clear this framebuffer during render? |
| 25 | + */ |
| 26 | +var RenderTarget = new Class({ |
| 27 | + |
| 28 | + initialize: |
| 29 | + |
| 30 | + function RenderTarget (pipeline, width, height, scale, autoClear) |
| 31 | + { |
| 32 | + /** |
| 33 | + * A reference to the WebGLPipeline that owns this Render Target. |
| 34 | + * |
| 35 | + * A Render Target class can only belong to a single pipeline. |
| 36 | + * |
| 37 | + * @name Phaser.Renderer.WebGL.RenderTarget#pipeline |
| 38 | + * @type {Phaser.Renderer.WebGL.WebGLPipeline} |
| 39 | + * @since 3.50.0 |
| 40 | + */ |
| 41 | + this.pipeline = pipeline; |
| 42 | + |
| 43 | + /** |
| 44 | + * A reference to the WebGLRenderer instance. |
| 45 | + * |
| 46 | + * @name Phaser.Renderer.WebGL.RenderTarget#renderer |
| 47 | + * @type {Phaser.Renderer.WebGL.WebGLRenderer} |
| 48 | + * @since 3.50.0 |
| 49 | + */ |
| 50 | + this.renderer = pipeline.renderer; |
| 51 | + |
| 52 | + /** |
| 53 | + * The WebGLFramebuffer this pipeline is targeting, if any. |
| 54 | + * |
| 55 | + * @name Phaser.Renderer.WebGL.RenderTarget#framebuffer |
| 56 | + * @type {WebGLFramebuffer} |
| 57 | + * @since 3.50.0 |
| 58 | + */ |
| 59 | + this.framebuffer = null; |
| 60 | + |
| 61 | + /** |
| 62 | + * The WebGLTexture this pipeline is targeting, if any. |
| 63 | + * |
| 64 | + * @name Phaser.Renderer.WebGL.RenderTarget#texture |
| 65 | + * @type {WebGLTexture} |
| 66 | + * @since 3.50.0 |
| 67 | + */ |
| 68 | + this.texture = null; |
| 69 | + |
| 70 | + /** |
| 71 | + * The dimensions of this Render Target are based on the scale of the WebGLRenderer. |
| 72 | + * |
| 73 | + * This value controls how much those dimensions are scaled. |
| 74 | + * |
| 75 | + * @name Phaser.Renderer.WebGL.RenderTarget#scale |
| 76 | + * @type {number} |
| 77 | + * @since 3.50.0 |
| 78 | + */ |
| 79 | + this.scale = scale; |
| 80 | + |
| 81 | + /** |
| 82 | + * Controls if this Render Target is automatically cleared (via `gl.COLOR_BUFFER_BIT`) |
| 83 | + * during the `WebGLPipeline.postBind` method. |
| 84 | + * |
| 85 | + * If you need more control over how, or if, the target is cleared, you can disable |
| 86 | + * this via the config, or even directly at runtime. |
| 87 | + * |
| 88 | + * @name Phaser.Renderer.WebGL.RenderTarget#autoClear |
| 89 | + * @type {boolean} |
| 90 | + * @since 3.50.0 |
| 91 | + */ |
| 92 | + this.autoClear = autoClear; |
| 93 | + |
| 94 | + this.resize(width, height); |
| 95 | + }, |
| 96 | + |
| 97 | + /** |
| 98 | + * Resizes this Render Target. |
| 99 | + * |
| 100 | + * Deletes both the frame buffer and texture, if they exist and then re-creates |
| 101 | + * them using the new sizes. |
| 102 | + * |
| 103 | + * This method is called automatically by the pipeline during its resize handler. |
| 104 | + * |
| 105 | + * @method Phaser.Renderer.WebGL.RenderTarget#resize |
| 106 | + * @since 3.50.0 |
| 107 | + * |
| 108 | + * @param {number} width - The new width of the WebGL Pipeline. |
| 109 | + * @param {number} height - The new height of the WebGL Pipeline. |
| 110 | + * |
| 111 | + * @return {this} This RenderTarget instance. |
| 112 | + */ |
| 113 | + resize: function (width, height) |
| 114 | + { |
| 115 | + var renderer = this.renderer; |
| 116 | + |
| 117 | + renderer.deleteFramebuffer(this.framebuffer); |
| 118 | + |
| 119 | + renderer.deleteTexture(this.texture); |
| 120 | + |
| 121 | + width *= this.scale; |
| 122 | + height *= this.scale; |
| 123 | + |
| 124 | + this.texture = renderer.createTextureFromSource(null, width, height, 0); |
| 125 | + |
| 126 | + this.framebuffer = renderer.createFramebuffer(width, height, this.texture, false); |
| 127 | + |
| 128 | + return this; |
| 129 | + }, |
| 130 | + |
| 131 | + /** |
| 132 | + * Sets the program this shader uses as being the active shader in the WebGL Renderer. |
| 133 | + * |
| 134 | + * This method is called every time the parent pipeline is made the current active pipeline. |
| 135 | + * |
| 136 | + * @method Phaser.Renderer.WebGL.WebGLShader#bind |
| 137 | + * @since 3.50.0 |
| 138 | + * |
| 139 | + * @param {boolean} [setAttributes=false] - Should the vertex attribute pointers be set? |
| 140 | + * @param {boolean} [flush=false] - Flush the pipeline before binding this shader? |
| 141 | + * |
| 142 | + * @return {this} This WebGLShader instance. |
| 143 | + */ |
| 144 | + bind: function () |
| 145 | + { |
| 146 | + this.renderer.pushFramebuffer(this.framebuffer); |
| 147 | + |
| 148 | + if (this.autoClear) |
| 149 | + { |
| 150 | + var gl = this.pipeline.gl; |
| 151 | + |
| 152 | + gl.clearColor(0, 0, 0, 0); |
| 153 | + |
| 154 | + gl.clear(gl.COLOR_BUFFER_BIT); |
| 155 | + } |
| 156 | + }, |
| 157 | + |
| 158 | + unbind: function () |
| 159 | + { |
| 160 | + this.renderer.popFramebuffer(); |
| 161 | + }, |
| 162 | + |
| 163 | + draw: function () |
| 164 | + { |
| 165 | + var texture = this.texture; |
| 166 | + |
| 167 | + var width = texture.width; |
| 168 | + var height = texture.height; |
| 169 | + |
| 170 | + this.pipeline.drawFillRect(0, 0, width, height, 0x0, 1, texture, true); |
| 171 | + }, |
| 172 | + |
| 173 | + /** |
| 174 | + * Removes all external references from this class and deletes the |
| 175 | + * WebGL framebuffer and texture instances. |
| 176 | + * |
| 177 | + * Does not remove this Render Target from the parent pipeline. |
| 178 | + * |
| 179 | + * @name Phaser.Renderer.WebGL.RenderTarget#destroy |
| 180 | + * @since 3.50.0 |
| 181 | + */ |
| 182 | + destroy: function () |
| 183 | + { |
| 184 | + var renderer = this.pipeline.renderer; |
| 185 | + |
| 186 | + renderer.deleteFramebuffer(this.framebuffer); |
| 187 | + renderer.deleteTexture(this.texture); |
| 188 | + |
| 189 | + this.pipeline = null; |
| 190 | + this.framebuffer = null; |
| 191 | + this.texture = null; |
| 192 | + } |
| 193 | + |
| 194 | +}); |
| 195 | + |
| 196 | +module.exports = RenderTarget; |
0 commit comments