Phaser.RenderTexture = function (game, width, height, key, scaleMode, resolution, renderer, textureUnit){ if (width === undefined) { width = 100; } if (height === undefined) { height = 100; } if (key === undefined) { key = ''; } if (scaleMode === undefined) { scaleMode = Phaser.scaleModes.DEFAULT; } if (resolution === undefined) { resolution = 1; } if (renderer === undefined) { renderer = PIXI.defaultRenderer; } if (textureUnit === undefined) { textureUnit = 0; } this.game = game; this.key = key; this.type = Phaser.RENDERTEXTURE; this._tempMatrix = new Phaser.Matrix(); this.width = width; this.height = height; this.resolution = resolution; this.frame = new Phaser.Rectangle(0, 0, this.width * this.resolution, this.height * this.resolution); this.crop = this.frame.clone(); this.baseTexture = new PIXI.BaseTexture(); this.baseTexture.width = this.width * this.resolution; this.baseTexture.height = this.height * this.resolution; this.baseTexture._glTextures = null ; this.baseTexture.resolution = this.resolution; this.baseTexture.scaleMode = scaleMode; this.baseTexture.hasLoaded = true ; PIXI.Texture.call(this, this.baseTexture, this.frame.clone()); this.renderer = renderer; if (this.renderer.type === Phaser.WEBGL) { var gl = this.renderer.gl; this.baseTexture.textureIndex = textureUnit; this.baseTexture._dirty[gl.id] = false ; this.textureBuffer = new PIXI.FilterTexture(gl, this.width, this.height, this.baseTexture.scaleMode, textureUnit); this.baseTexture._glTextures = this.textureBuffer.texture; this.projection = new Phaser.Point(this.width * 0.5, - this.height * 0.5); } else { this.textureBuffer = new PIXI.CanvasBuffer(this.width * this.resolution, this.height * this.resolution); this.baseTexture.source = this.textureBuffer.canvas; } this.valid = true ; this.tempMatrix = new Phaser.Matrix(); this._updateUvs(); } ; Phaser.RenderTexture.prototype = Object.create(PIXI.Texture.prototype); Phaser.RenderTexture.prototype.constructor = Phaser.RenderTexture; Phaser.RenderTexture.prototype.renderXY = function (displayObject, x, y, clear){ displayObject.updateTransform(); this._tempMatrix.copyFrom(displayObject.worldTransform); this._tempMatrix.tx = x; this._tempMatrix.ty = y; if (this.renderer.type === Phaser.WEBGL) { this._renderWebGL(displayObject, this._tempMatrix, clear); } else { this._renderCanvas(displayObject, this._tempMatrix, clear); } } ; Phaser.RenderTexture.prototype.renderRawXY = function (displayObject, x, y, clear){ this._tempMatrix.identity().translate(x, y); if (this.renderer.type === Phaser.WEBGL) { this._renderWebGL(displayObject, this._tempMatrix, clear); } else { this._renderCanvas(displayObject, this._tempMatrix, clear); } } ; Phaser.RenderTexture.prototype.render = function (displayObject, matrix, clear){ if (matrix === undefined || matrix === null ) { this._tempMatrix.copyFrom(displayObject.worldTransform); } else { this._tempMatrix.copyFrom(matrix); } if (this.renderer.type === Phaser.WEBGL) { this._renderWebGL(displayObject, this._tempMatrix, clear); } else { this._renderCanvas(displayObject, this._tempMatrix, clear); } } ; Phaser.RenderTexture.prototype.resize = function (width, height, updateBase){ if (width === this.width && height === this.height) { return ; } this.valid = (width > 0 && height > 0); this.width = width; this.height = height; this.frame.width = this.crop.width = width * this.resolution; this.frame.height = this.crop.height = height * this.resolution; if (updateBase) { this.baseTexture.width = this.width * this.resolution; this.baseTexture.height = this.height * this.resolution; } if (this.renderer.type === Phaser.WEBGL) { this.projection.x = this.width / 2; this.projection.y = - this.height / 2; } if (!this.valid) { return ; } this.textureBuffer.resize(this.width, this.height); } ; Phaser.RenderTexture.prototype.clear = function (){ if (!this.valid) { return ; } if (this.renderer.type === Phaser.WEBGL) { this.renderer.gl.bindFramebuffer(this.renderer.gl.FRAMEBUFFER, this.textureBuffer.frameBuffer); } _AN_Call_clear('clear', this.textureBuffer); } ; Phaser.RenderTexture.prototype._renderWebGL = function (displayObject, matrix, clear){ if (!this.valid || displayObject.alpha === 0) { return ; } var wt = displayObject.worldTransform; wt.identity(); wt.translate(0, this.projection.y * 2); if (matrix) { wt.append(matrix); } wt.scale(1, -1); for (var i = 0; i < _AN_Read_length('length', displayObject.children); i++ ){ displayObject.children[i].updateTransform(); } var gl = this.renderer.gl; gl.viewport(0, 0, this.width * this.resolution, this.height * this.resolution); gl.bindFramebuffer(gl.FRAMEBUFFER, this.textureBuffer.frameBuffer); if (clear) { _AN_Call_clear('clear', this.textureBuffer); } this.renderer.spriteBatch.dirty = true ; this.renderer.renderDisplayObject(displayObject, this.projection, this.textureBuffer.frameBuffer, matrix); this.renderer.spriteBatch.dirty = true ; gl.bindFramebuffer(gl.FRAMEBUFFER, null ); } ; Phaser.RenderTexture.prototype._renderCanvas = function (displayObject, matrix, clear){ if (!this.valid || displayObject.alpha === 0) { return ; } var wt = displayObject.worldTransform; wt.identity(); if (matrix) { wt.append(matrix); } for (var i = 0; i < _AN_Read_length('length', displayObject.children); i++ ){ displayObject.children[i].updateTransform(); } if (clear) { _AN_Call_clear('clear', this.textureBuffer); } var realResolution = this.renderer.resolution; this.renderer.resolution = this.resolution; this.renderer.renderDisplayObject(displayObject, this.textureBuffer.context, matrix); this.renderer.resolution = realResolution; } ; Phaser.RenderTexture.prototype.getImage = function (){ var image = new Image(); _AN_Write_src('src', image, false , this.getBase64()); return image; } ; Phaser.RenderTexture.prototype.getBase64 = function (){ return this.getCanvas().toDataURL(); } ; Phaser.RenderTexture.prototype.getCanvas = function (){ if (this.renderer.type === Phaser.WEBGL) { var gl = this.renderer.gl; var width = this.textureBuffer.width; var height = this.textureBuffer.height; var webGLPixels = new Uint8Array(4 * width * height); gl.bindFramebuffer(gl.FRAMEBUFFER, this.textureBuffer.frameBuffer); gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, webGLPixels); gl.bindFramebuffer(gl.FRAMEBUFFER, null ); var tempCanvas = new PIXI.CanvasBuffer(width, height); var canvasData = tempCanvas.context.getImageData(0, 0, width, height); canvasData.data.set(webGLPixels); tempCanvas.context.putImageData(canvasData, 0, 0); return tempCanvas.canvas; } else { return this.textureBuffer.canvas; } } ;