From 581fdb50ee0924abe90a633289d2d093d403e7a4 Mon Sep 17 00:00:00 2001 From: Felipe Alfonso Date: Tue, 11 Apr 2017 10:15:38 -0300 Subject: [PATCH] RenderTarget added to renderable game objects --- .../bitmaptext/dynamic/DynamicBitmapText.js | 1 + .../bitmaptext/static/BitmapText.js | 1 + v3/src/gameobjects/blitter/Blitter.js | 1 + v3/src/gameobjects/graphics/Graphics.js | 1 + v3/src/gameobjects/renderpass/RenderPass.js | 196 ++++++++++++++++++ .../renderpass/RenderPassCanvasRenderer.js | 7 + .../renderpass/RenderPassFactory.js | 20 ++ .../renderpass/RenderPassRender.js | 6 + .../renderpass/RenderPassWebGLRenderer.js | 7 + v3/src/gameobjects/text/static/Text.js | 1 + v3/src/renderer/webgl/WebGLRenderer.js | 2 +- 11 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 v3/src/gameobjects/renderpass/RenderPass.js create mode 100644 v3/src/gameobjects/renderpass/RenderPassCanvasRenderer.js create mode 100644 v3/src/gameobjects/renderpass/RenderPassFactory.js create mode 100644 v3/src/gameobjects/renderpass/RenderPassRender.js create mode 100644 v3/src/gameobjects/renderpass/RenderPassWebGLRenderer.js diff --git a/v3/src/gameobjects/bitmaptext/dynamic/DynamicBitmapText.js b/v3/src/gameobjects/bitmaptext/dynamic/DynamicBitmapText.js index b31c107a9..f38499ccc 100644 --- a/v3/src/gameobjects/bitmaptext/dynamic/DynamicBitmapText.js +++ b/v3/src/gameobjects/bitmaptext/dynamic/DynamicBitmapText.js @@ -12,6 +12,7 @@ var DynamicBitmapText = new Class({ Components.Alpha, Components.BlendMode, Components.Origin, + Components.RenderTarget, Components.Size, Components.Texture, Components.Transform, diff --git a/v3/src/gameobjects/bitmaptext/static/BitmapText.js b/v3/src/gameobjects/bitmaptext/static/BitmapText.js index c79c14dd8..dba76a3c3 100644 --- a/v3/src/gameobjects/bitmaptext/static/BitmapText.js +++ b/v3/src/gameobjects/bitmaptext/static/BitmapText.js @@ -12,6 +12,7 @@ var BitmapText = new Class({ Components.Alpha, Components.BlendMode, Components.Origin, + Components.RenderTarget, Components.Size, Components.Texture, Components.Transform, diff --git a/v3/src/gameobjects/blitter/Blitter.js b/v3/src/gameobjects/blitter/Blitter.js index 06c8e428d..11af6e0f5 100644 --- a/v3/src/gameobjects/blitter/Blitter.js +++ b/v3/src/gameobjects/blitter/Blitter.js @@ -30,6 +30,7 @@ var Blitter = new Class({ Mixins: [ Components.Alpha, Components.BlendMode, + Components.RenderTarget, Components.ScaleMode, Components.Size, Components.Texture, diff --git a/v3/src/gameobjects/graphics/Graphics.js b/v3/src/gameobjects/graphics/Graphics.js index 79260e683..aacc6afe2 100644 --- a/v3/src/gameobjects/graphics/Graphics.js +++ b/v3/src/gameobjects/graphics/Graphics.js @@ -14,6 +14,7 @@ var Graphics = new Class({ Components.Alpha, Components.BlendMode, Components.Transform, + Components.RenderTarget, Components.Visible, Render ], diff --git a/v3/src/gameobjects/renderpass/RenderPass.js b/v3/src/gameobjects/renderpass/RenderPass.js new file mode 100644 index 000000000..8ad4262a8 --- /dev/null +++ b/v3/src/gameobjects/renderpass/RenderPass.js @@ -0,0 +1,196 @@ +var Class = require('../../utils/Class'); +var GameObject = require('../GameObject'); +var Components = require('../../components'); +var Render = require('./RenderPassRender'); + +var RenderPass = new Class({ + + Extends: GameObject, + + Mixins: [ + Components.Alpha, + Components.BlendMode, + Components.Flip, + Components.GetBounds, + Components.Origin, + Components.RenderTarget, + Components.ScaleMode, + Components.Size, + Components.Transform, + Components.Visible, + Render + ], + + initialize: + + function RenderPass(state, x, y, width, height, shaderName, fragmentShader) + { + GameObject.call(this, state); + + var resourceManager = state.game.renderer.resourceManager; + var gl; + + this.dstRenderTarget = null + this.dstRenderTexture = null; + this.dstShader = null; + this.uniforms = {}; + + if (resourceManager !== undefined) + { + /*gl = state.game.renderer.gl; + this.dstShader = resourceManager.createShader(shaderName, { + vert: TexturedAndNormalizedTintedShader.vert, + frag: fragmentShader + }); + this.dstRenderTexture = resourceManager.createTexture( + 0, + gl.LINEAR, gl.LINEAR, + gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE, + gl.RGBA, + null, width, height + ); + this.dstRenderTarget = resourceManager.createRenderTarget(width, height, this.dstRenderTexture, null); */ + state.game.renderer.currentTexture = null; // force rebinding of prev texture + } + this.flipY = true; + this.setPosition(x, y); + this.setSize(width, height); + this.setOrigin(0, 0); + }, + + render: function (gameObject) + { + }, + + getUniformLocation: function (uniformName) + { + var dstShader = this.dstShader; + var uniforms = this.uniforms; + var location; + + if (uniformName in uniforms) + { + location = uniforms[uniformName]; + } + else + { + location = dstShader.getUniformLocation(uniformName); + uniforms[uniformName] = location; + } + + return location; + }, + + setFloat: function (uniformName, x) + { + var dstShader = this.dstShader; + + if (dstShader === null) + return; + + dstShader.setConstantFloat1(this.getUniformLocation(uniformName), x); + }, + + setFloat2: function (uniformName, x, y) + { + var dstShader = this.dstShader; + + if (dstShader === null) + return; + + dstShader.setConstantFloat2(this.getUniformLocation(uniformName), x, y); + }, + + setFloat3: function (uniformName, x, y, z) + { + var dstShader = this.dstShader; + + if (dstShader === null) + return; + + dstShader.setConstantFloat3(this.getUniformLocation(uniformName), x, y, z); + }, + + setFloat4: function (uniformName, x, y, z, w) + { + var dstShader = this.dstShader; + + if (dstShader === null) + return; + + dstShader.setConstantFloat4(this.getUniformLocation(uniformName), x, y, z, w); + }, + + setInt: function (uniformName, x) + { + var dstShader = this.dstShader; + + if (dstShader === null) + return; + + dstShader.setConstantInt1(this.getUniformLocation(uniformName), x); + }, + + setInt2: function (uniformName, x, y) + { + var dstShader = this.dstShader; + + if (dstShader === null) + return; + + dstShader.setConstantInt2(this.getUniformLocation(uniformName), x, y); + }, + + setInt3: function (uniformName, x, y, z) + { + var dstShader = this.dstShader; + + if (dstShader === null) + return; + + dstShader.setConstantInt3(this.getUniformLocation(uniformName), x, y, z); + }, + + setInt4: function (uniformName, x, y, z, w) + { + var dstShader = this.dstShader; + + if (dstShader === null) + return; + + dstShader.setConstantInt4(this.getUniformLocation(uniformName), x, y, z, w); + }, + + setMatrix2x2: function (uniformName, matrix) + { + var dstShader = this.dstShader; + + if (dstShader === null) + return; + + dstShader.setConstantMatrix2x2(this.getUniformLocation(uniformName), matrix); + }, + + setMatrix3x3: function (uniformName, matrix) + { + var dstShader = this.dstShader; + + if (dstShader === null) + return; + + dstShader.setConstantMatrix3x3(this.getUniformLocation(uniformName), matrix); + }, + + setMatrix4x4: function (uniformName, matrix) + { + var dstShader = this.dstShader; + + if (dstShader === null) + return; + + dstShader.setConstantMatrix4x4(this.getUniformLocation(uniformName), matrix); + } + +}); + +module.exports = RenderPass; diff --git a/v3/src/gameobjects/renderpass/RenderPassCanvasRenderer.js b/v3/src/gameobjects/renderpass/RenderPassCanvasRenderer.js new file mode 100644 index 000000000..9eaeecbe3 --- /dev/null +++ b/v3/src/gameobjects/renderpass/RenderPassCanvasRenderer.js @@ -0,0 +1,7 @@ +var RenderPassCanvasRenderer = function (renderer, src, interpolationPercentage, camera) +{ + +}; + +module.exports = RenderPassCanvasRenderer; + \ No newline at end of file diff --git a/v3/src/gameobjects/renderpass/RenderPassFactory.js b/v3/src/gameobjects/renderpass/RenderPassFactory.js new file mode 100644 index 000000000..9636beaa7 --- /dev/null +++ b/v3/src/gameobjects/renderpass/RenderPassFactory.js @@ -0,0 +1,20 @@ +var RenderPass = require('./RenderPass'); +var FactoryContainer = require('../../gameobjects/FactoryContainer'); + +var RenderPassFactory = { + + KEY: 'renderPass', + + add: function (x, y, width, height, shaderName, fragmentShader) + { + return this.children.add(new RenderPass(this.state, x, y, width, height, shaderName, fragmentShader)); + }, + + make: function (x, y, width, height, shaderName, fragmentShader) + { + return new RenderPass(this.state, x, y, width, height, shaderName, fragmentShader); + } + +}; + +module.exports = FactoryContainer.register(RenderPassFactory); diff --git a/v3/src/gameobjects/renderpass/RenderPassRender.js b/v3/src/gameobjects/renderpass/RenderPassRender.js new file mode 100644 index 000000000..5fe348693 --- /dev/null +++ b/v3/src/gameobjects/renderpass/RenderPassRender.js @@ -0,0 +1,6 @@ +module.exports = { + + renderCanvas: require('./RenderPassCanvasRenderer'), + renderWebGL: require('./RenderPassWebGLRenderer') + +}; diff --git a/v3/src/gameobjects/renderpass/RenderPassWebGLRenderer.js b/v3/src/gameobjects/renderpass/RenderPassWebGLRenderer.js new file mode 100644 index 000000000..719289000 --- /dev/null +++ b/v3/src/gameobjects/renderpass/RenderPassWebGLRenderer.js @@ -0,0 +1,7 @@ +var RenderPassWebGLRenderer = function (renderer, src, interpolationPercentage, camera) +{ + +}; + +module.exports = RenderPassWebGLRenderer; + \ No newline at end of file diff --git a/v3/src/gameobjects/text/static/Text.js b/v3/src/gameobjects/text/static/Text.js index 4030a5cf9..b623fc42a 100644 --- a/v3/src/gameobjects/text/static/Text.js +++ b/v3/src/gameobjects/text/static/Text.js @@ -16,6 +16,7 @@ var Text = new Class({ Components.BlendMode, Components.GetBounds, Components.Origin, + Components.RenderTarget, Components.ScaleMode, Components.Transform, Components.Visible, diff --git a/v3/src/renderer/webgl/WebGLRenderer.js b/v3/src/renderer/webgl/WebGLRenderer.js index b108c778d..29e8fef3f 100644 --- a/v3/src/renderer/webgl/WebGLRenderer.js +++ b/v3/src/renderer/webgl/WebGLRenderer.js @@ -446,7 +446,7 @@ WebGLRenderer.prototype = { dstTexture.height = srcCanvas.height; /* we must rebind old texture */ - gl.bindTexture(gl.TEXTURE_2D, this.currentTexture.texture); + this.currentTexture = null; return dstTexture; }