From 9856200fdfde7ccccf157fc0df8ae3953588660a Mon Sep 17 00:00:00 2001 From: Felipe Alfonso Date: Tue, 15 Aug 2017 16:42:04 -0300 Subject: [PATCH] Added camera gameobject filtering --- v3/src/camera/Camera.js | 3 ++ v3/src/camera/components/Ignore.js | 16 ++++++ v3/src/checksum.js | 2 +- v3/src/gameobjects/GameObject.js | 1 + .../dynamic/DynamicBitmapTextWebGLRenderer.js | 2 +- .../static/BitmapTextWebGLRenderer.js | 2 +- .../blitter/BlitterWebGLRenderer.js | 2 +- .../effectlayer/EffectLayerWebGLRenderer.js | 2 +- .../graphics/GraphicsWebGLRenderer.js | 2 +- .../gameobjects/image/ImageWebGLRenderer.js | 2 +- .../lightlayer/DeferredRenderer.js | 8 +-- .../gameobjects/lightlayer/ForwardRenderer.js | 2 +- v3/src/gameobjects/lightlayer/LightLayer.js | 20 +++++--- v3/src/gameobjects/mesh/MeshWebGLRenderer.js | 2 +- .../renderpass/RenderPassWebGLRenderer.js | 2 +- .../gameobjects/sprite/SpriteWebGLRenderer.js | 2 +- .../text/static/TextWebGLRenderer.js | 2 +- .../tilemap/dynamic/TilemapWebGLRenderer.js | 2 +- .../static/StaticTilemapWebGLRenderer.js | 2 +- .../tilesprite/TileSpriteWebGLRenderer.js | 2 +- v3/src/plugins/CameraManager.js | 5 ++ v3/src/renderer/webgl/WebGLRenderer.js | 1 + .../renderer/webgl/resources/IndexBuffer.js | 20 ++++++-- v3/src/renderer/webgl/resources/Shader.js | 35 ++++++++----- .../renderer/webgl/resources/VertexBuffer.js | 49 ++++++++++++------- 25 files changed, 128 insertions(+), 60 deletions(-) create mode 100644 v3/src/camera/components/Ignore.js diff --git a/v3/src/camera/Camera.js b/v3/src/camera/Camera.js index 741b75d2b..90cd63af6 100644 --- a/v3/src/camera/Camera.js +++ b/v3/src/camera/Camera.js @@ -61,6 +61,8 @@ var Camera = new Class({ // Follow this._follow = null; + + this._id = 0; }, cameraToScreen: require('./components/CameraToScreen'), @@ -71,6 +73,7 @@ var Camera = new Class({ destroy: require('./components/Destroy'), fade: require('./components/Fade'), flash: require('./components/Flash'), + ignore: require('./components/Ignore'), preRender: require('./components/PreRender'), removeBounds: require('./components/RemoveBounds'), setBackgroundColor: require('./components/SetBackgroundColor'), diff --git a/v3/src/camera/components/Ignore.js b/v3/src/camera/components/Ignore.js new file mode 100644 index 000000000..537bea965 --- /dev/null +++ b/v3/src/camera/components/Ignore.js @@ -0,0 +1,16 @@ +var Ignore = function (gameObjectOrArray) +{ + if (gameObjectOrArray instanceof Array) + { + for (var index = 0; index < gameObjectOrArray.length; ++index) + { + gameObjectOrArray[index].cameraFilter |= this._id; + } + } + else + { + gameObjectOrArray.cameraFilter |= this._id; + } +}; + +module.exports = Ignore; diff --git a/v3/src/checksum.js b/v3/src/checksum.js index e8745c399..cd34953f2 100644 --- a/v3/src/checksum.js +++ b/v3/src/checksum.js @@ -1,4 +1,4 @@ var CHECKSUM = { -build: '37876e10-8162-11e7-b191-e9a855a11647' +build: '3bd13fc0-81f1-11e7-b26f-e58093bce12c' }; module.exports = CHECKSUM; \ No newline at end of file diff --git a/v3/src/gameobjects/GameObject.js b/v3/src/gameobjects/GameObject.js index de90353b3..8d3288283 100644 --- a/v3/src/gameobjects/GameObject.js +++ b/v3/src/gameobjects/GameObject.js @@ -23,6 +23,7 @@ var GameObject = new Class({ // Will Render bitmask flags for the components Visible, Alpha, Transform and Texture respectively this.renderMask = 15; this.renderFlags = 15; + this.cameraFilter = 0; this.input = null; diff --git a/v3/src/gameobjects/bitmaptext/dynamic/DynamicBitmapTextWebGLRenderer.js b/v3/src/gameobjects/bitmaptext/dynamic/DynamicBitmapTextWebGLRenderer.js index fb557cdb4..35d9d2efe 100644 --- a/v3/src/gameobjects/bitmaptext/dynamic/DynamicBitmapTextWebGLRenderer.js +++ b/v3/src/gameobjects/bitmaptext/dynamic/DynamicBitmapTextWebGLRenderer.js @@ -4,7 +4,7 @@ var tempMatrixChar = new TransformMatrix(); var DynamicBitmapTextWebGLRenderer = function (renderer, gameObject, interpolationPercentage, camera) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (gameObject.cameraFilter > 0 && (gameObject.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/bitmaptext/static/BitmapTextWebGLRenderer.js b/v3/src/gameobjects/bitmaptext/static/BitmapTextWebGLRenderer.js index 55b5899d7..783eb4f1a 100644 --- a/v3/src/gameobjects/bitmaptext/static/BitmapTextWebGLRenderer.js +++ b/v3/src/gameobjects/bitmaptext/static/BitmapTextWebGLRenderer.js @@ -3,7 +3,7 @@ var tempMatrix = new TransformMatrix(); var BitmapTextWebGLRenderer = function (renderer, gameObject, interpolationPercentage, camera) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (gameObject.cameraFilter > 0 && (gameObject.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/blitter/BlitterWebGLRenderer.js b/v3/src/gameobjects/blitter/BlitterWebGLRenderer.js index 042a677e3..206d47c1d 100644 --- a/v3/src/gameobjects/blitter/BlitterWebGLRenderer.js +++ b/v3/src/gameobjects/blitter/BlitterWebGLRenderer.js @@ -1,6 +1,6 @@ var BlitterWebGLRenderer = function (renderer, src, interpolationPercentage, camera) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (src.cameraFilter > 0 && (src.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/effectlayer/EffectLayerWebGLRenderer.js b/v3/src/gameobjects/effectlayer/EffectLayerWebGLRenderer.js index 3ed4e6aad..b233dcb9e 100644 --- a/v3/src/gameobjects/effectlayer/EffectLayerWebGLRenderer.js +++ b/v3/src/gameobjects/effectlayer/EffectLayerWebGLRenderer.js @@ -1,6 +1,6 @@ var EffectLayerWebGLRenderer = function (renderer, src, interpolationPercentage, camera) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (src.cameraFilter > 0 && (src.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/graphics/GraphicsWebGLRenderer.js b/v3/src/gameobjects/graphics/GraphicsWebGLRenderer.js index 2bc102784..cc275140c 100644 --- a/v3/src/gameobjects/graphics/GraphicsWebGLRenderer.js +++ b/v3/src/gameobjects/graphics/GraphicsWebGLRenderer.js @@ -27,7 +27,7 @@ var Path = function (x, y, width, rgb, alpha) var GraphicsWebGLRenderer = function (renderer, gameObject, interpolationPercentage, camera, forceRenderTarget) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (gameObject.cameraFilter > 0 && (gameObject.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/image/ImageWebGLRenderer.js b/v3/src/gameobjects/image/ImageWebGLRenderer.js index b61f75ca8..9549a4bb0 100644 --- a/v3/src/gameobjects/image/ImageWebGLRenderer.js +++ b/v3/src/gameobjects/image/ImageWebGLRenderer.js @@ -1,6 +1,6 @@ var ImageWebGLRenderer = function (renderer, src, interpolationPercentage, camera) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (src.cameraFilter > 0 && (src.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/lightlayer/DeferredRenderer.js b/v3/src/gameobjects/lightlayer/DeferredRenderer.js index b529b77f0..dfdcf5176 100644 --- a/v3/src/gameobjects/lightlayer/DeferredRenderer.js +++ b/v3/src/gameobjects/lightlayer/DeferredRenderer.js @@ -1,3 +1,5 @@ +var VertexBuffer = require('../../renderer/webgl/resources/VertexBuffer'); + var DeferredRenderer = function (renderer, lightLayer, interpolationPercentage, camera) { var spriteList = lightLayer.sprites; @@ -5,7 +7,7 @@ var DeferredRenderer = function (renderer, lightLayer, interpolationPercentage, var batch = renderer.spriteBatch; var gl = renderer.gl; - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || length === 0 || (lightLayer.cameraFilter > 0 && (lightLayer.cameraFilter & camera._id))) { return; } @@ -149,12 +151,12 @@ var DeferredRenderer = function (renderer, lightLayer, interpolationPercentage, renderer.setTexture({texture: lightLayer.gBufferNormalTex}, 1); gl.bindFramebuffer(gl.FRAMEBUFFER, null); - gl.useProgram(lightLayer.lightPassShader.program); + lightLayer.lightPassShader.bind(); gl.bindBuffer(gl.ARRAY_BUFFER, lightLayer.lightPassVBO); gl.enableVertexAttribArray(0); gl.vertexAttribPointer(0, 2, gl.FLOAT, false, Float32Array.BYTES_PER_ELEMENT, 0); gl.drawArrays(gl.TRIANGLES, 0, 3); - + VertexBuffer.SetDirty(); batch.bind(); }; diff --git a/v3/src/gameobjects/lightlayer/ForwardRenderer.js b/v3/src/gameobjects/lightlayer/ForwardRenderer.js index daf0b2f91..2ddffb608 100644 --- a/v3/src/gameobjects/lightlayer/ForwardRenderer.js +++ b/v3/src/gameobjects/lightlayer/ForwardRenderer.js @@ -4,7 +4,7 @@ var ForwardRenderer = function (renderer, lightLayer, interpolationPercentage, c var length = spriteList.length; var batch = renderer.spriteBatch; - if (this.renderMask !== this.renderFlags || length === 0) + if (this.renderMask !== this.renderFlags || length === 0 || (lightLayer.cameraFilter > 0 && (lightLayer.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/lightlayer/LightLayer.js b/v3/src/gameobjects/lightlayer/LightLayer.js index 575a0b542..6391c60f6 100644 --- a/v3/src/gameobjects/lightlayer/LightLayer.js +++ b/v3/src/gameobjects/lightlayer/LightLayer.js @@ -10,6 +10,7 @@ var TexturedAndNormalizedTintedShader = require('../../renderer/webgl/shaders/Te var Const = require('./Const'); var Matrix = require('../components/TransformMatrix'); var TempMatrix = new Matrix(); +var VertexBuffer = require('../../renderer/webgl/resources/VertexBuffer'); // http://cpetry.github.io/NormalMap-Online/ @@ -267,6 +268,7 @@ var LightLayer = new Class({ } gl.bindFramebuffer(gl.FRAMEBUFFER, null); + VertexBuffer.SetDirty(); } this.setOrigin(0, 0); @@ -392,18 +394,22 @@ var LightLayer = new Class({ var point = {x: 0, y: 0}; var height = renderer.height; var cameraMatrix = camera.matrix; - shader.setConstantFloat4(this.uCameraLoc, camera.x, camera.y, camera.rotation, camera.zoom); - shader.setConstantFloat2(this.uResolutionLoc, renderer.width, renderer.height); - shader.setConstantFloat3(this.ambientLightColorLoc, this.ambientLightColorR, this.ambientLightColorG, this.ambientLightColorB); + var gl = this.gl; + + shader.bind(); + + gl.uniform2f(this.uResolutionLoc, renderer.width, renderer.height); + gl.uniform3f(this.ambientLightColorLoc, this.ambientLightColorR, this.ambientLightColorG, this.ambientLightColorB); + gl.uniform4f(this.uCameraLoc, camera.x, camera.y, camera.rotation, camera.zoom); for (var index = 0; index < length; ++index) { var light = lights[index]; cameraMatrix.transformPoint(light.x, light.y, point); - shader.setConstantFloat3(locations[index].position, point.x - (camera.scrollX * light.scrollFactorX * camera.zoom), height - (point.y - (camera.scrollY * light.scrollFactorY) * camera.zoom), light.z); - shader.setConstantFloat3(locations[index].color, light.r, light.g, light.b); - shader.setConstantFloat1(locations[index].attenuation, light.attenuation); - shader.setConstantFloat1(locations[index].radius, light.radius); + gl.uniform1f(locations[index].attenuation, light.attenuation); + gl.uniform1f(locations[index].radius, light.radius); + gl.uniform3f(locations[index].position, point.x - (camera.scrollX * light.scrollFactorX * camera.zoom), height - (point.y - (camera.scrollY * light.scrollFactorY) * camera.zoom), light.z); + gl.uniform3f(locations[index].color, light.r, light.g, light.b); } } } diff --git a/v3/src/gameobjects/mesh/MeshWebGLRenderer.js b/v3/src/gameobjects/mesh/MeshWebGLRenderer.js index 20ea01297..1caba5988 100644 --- a/v3/src/gameobjects/mesh/MeshWebGLRenderer.js +++ b/v3/src/gameobjects/mesh/MeshWebGLRenderer.js @@ -1,6 +1,6 @@ var MeshWebGLRenderer = function (renderer, src, interpolationPercentage, camera) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (src.cameraFilter > 0 && (src.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/renderpass/RenderPassWebGLRenderer.js b/v3/src/gameobjects/renderpass/RenderPassWebGLRenderer.js index 53f3802d6..6c449f39c 100644 --- a/v3/src/gameobjects/renderpass/RenderPassWebGLRenderer.js +++ b/v3/src/gameobjects/renderpass/RenderPassWebGLRenderer.js @@ -1,6 +1,6 @@ var RenderPassWebGLRenderer = function (renderer, src, interpolationPercentage, camera) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (src.cameraFilter > 0 && (src.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/sprite/SpriteWebGLRenderer.js b/v3/src/gameobjects/sprite/SpriteWebGLRenderer.js index 3e66e142c..35b038470 100644 --- a/v3/src/gameobjects/sprite/SpriteWebGLRenderer.js +++ b/v3/src/gameobjects/sprite/SpriteWebGLRenderer.js @@ -1,6 +1,6 @@ var SpriteWebGLRenderer = function (renderer, src, interpolationPercentage, camera) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (src.cameraFilter > 0 && (src.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/text/static/TextWebGLRenderer.js b/v3/src/gameobjects/text/static/TextWebGLRenderer.js index f8b197abc..09ceb92f2 100644 --- a/v3/src/gameobjects/text/static/TextWebGLRenderer.js +++ b/v3/src/gameobjects/text/static/TextWebGLRenderer.js @@ -1,6 +1,6 @@ var TextWebGLRenderer = function (renderer, src, interpolationPercentage, camera) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (src.cameraFilter > 0 && (src.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/tilemap/dynamic/TilemapWebGLRenderer.js b/v3/src/gameobjects/tilemap/dynamic/TilemapWebGLRenderer.js index 9d73a9cab..d101d00da 100644 --- a/v3/src/gameobjects/tilemap/dynamic/TilemapWebGLRenderer.js +++ b/v3/src/gameobjects/tilemap/dynamic/TilemapWebGLRenderer.js @@ -1,6 +1,6 @@ var TilemapWebGLRenderer = function (renderer, gameObject, interpolationPercentage, camera) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (gameObject.cameraFilter > 0 && (gameObject.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/tilemap/static/StaticTilemapWebGLRenderer.js b/v3/src/gameobjects/tilemap/static/StaticTilemapWebGLRenderer.js index 108a5a578..7d3436301 100644 --- a/v3/src/gameobjects/tilemap/static/StaticTilemapWebGLRenderer.js +++ b/v3/src/gameobjects/tilemap/static/StaticTilemapWebGLRenderer.js @@ -1,6 +1,6 @@ var StaticTilemapWebGLRenderer = function (renderer, src, interpolationPercentage, camera) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (src.cameraFilter > 0 && (src.cameraFilter & camera._id))) { return; } diff --git a/v3/src/gameobjects/tilesprite/TileSpriteWebGLRenderer.js b/v3/src/gameobjects/tilesprite/TileSpriteWebGLRenderer.js index 2999f521e..c22402eef 100644 --- a/v3/src/gameobjects/tilesprite/TileSpriteWebGLRenderer.js +++ b/v3/src/gameobjects/tilesprite/TileSpriteWebGLRenderer.js @@ -1,6 +1,6 @@ var TileSpriteWebGLRenderer = function (renderer, src, interpolationPercentage, camera) { - if (this.renderMask !== this.renderFlags) + if (this.renderMask !== this.renderFlags || (src.cameraFilter > 0 && (src.cameraFilter & camera._id))) { return; } diff --git a/v3/src/plugins/CameraManager.js b/v3/src/plugins/CameraManager.js index 6351a744d..45d520574 100644 --- a/v3/src/plugins/CameraManager.js +++ b/v3/src/plugins/CameraManager.js @@ -12,6 +12,7 @@ var CameraManager = new Class({ function CameraManager (scene) { // The Scene that owns this plugin + this.currentCameraId = 1; this.scene = scene; this.cameras = []; @@ -30,6 +31,7 @@ var CameraManager = new Class({ // Set the default camera this.main = this.cameras[0]; + }, /* @@ -144,6 +146,9 @@ var CameraManager = new Class({ this.main = camera; } + camera._id = this.currentCameraId; + this.currentCameraId = this.currentCameraId << 1; + return camera; }, diff --git a/v3/src/renderer/webgl/WebGLRenderer.js b/v3/src/renderer/webgl/WebGLRenderer.js index d7bbf2b17..361f72678 100644 --- a/v3/src/renderer/webgl/WebGLRenderer.js +++ b/v3/src/renderer/webgl/WebGLRenderer.js @@ -130,6 +130,7 @@ var WebGLRenderer = new Class({ this.tileBatch = this.addRenderer(new TileBatch(this.game, gl, this)); this.tilemapRenderer = this.addRenderer(new TilemapRenderer(this.game, gl, this)); this.currentRenderer = this.spriteBatch; + this.currentVertexBuffer = null; this.setBlendMode(0); this.resize(this.width, this.height); }, diff --git a/v3/src/renderer/webgl/resources/IndexBuffer.js b/v3/src/renderer/webgl/resources/IndexBuffer.js index 05c307dff..d384722a0 100644 --- a/v3/src/renderer/webgl/resources/IndexBuffer.js +++ b/v3/src/renderer/webgl/resources/IndexBuffer.js @@ -1,5 +1,5 @@ var Class = require('../../../utils/Class'); - +var CurrentIndexBuffer = null; var IndexBuffer = new Class({ initialize: @@ -14,8 +14,11 @@ var IndexBuffer = new Class({ bind: function () { var gl = this.gl; - - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.bufferObject); + if (CurrentIndexBuffer !== this) + { + CurrentIndexBuffer = this; + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.bufferObject); + } return this; }, @@ -24,7 +27,11 @@ var IndexBuffer = new Class({ { var gl = this.gl; - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.bufferObject); + if (CurrentIndexBuffer !== this) + { + CurrentIndexBuffer = this; + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.bufferObject); + } gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, offset, bufferData); return this; @@ -32,4 +39,9 @@ var IndexBuffer = new Class({ }); +IndexBuffer.SetDirty = function () +{ + CurrentIndexBuffer = null; +}; + module.exports = IndexBuffer; diff --git a/v3/src/renderer/webgl/resources/Shader.js b/v3/src/renderer/webgl/resources/Shader.js index 1de40e8f9..2e3bcf42a 100644 --- a/v3/src/renderer/webgl/resources/Shader.js +++ b/v3/src/renderer/webgl/resources/Shader.js @@ -1,5 +1,5 @@ var Class = require('../../../utils/Class'); - +var CurrentShader = null; var Shader = new Class({ initialize: @@ -31,7 +31,7 @@ var Shader = new Class({ setConstantFloat1: function (location, x) { - this.gl.useProgram(this.program); + this.bind(); this.gl.uniform1f(location, x); return this; @@ -39,7 +39,7 @@ var Shader = new Class({ setConstantFloat2: function (location, x, y) { - this.gl.useProgram(this.program); + this.bind(); this.gl.uniform2f(location, x, y); return this; @@ -47,7 +47,7 @@ var Shader = new Class({ setConstantFloat3: function (location, x, y, z) { - this.gl.useProgram(this.program); + this.bind(); this.gl.uniform3f(location, x, y, z); return this; @@ -55,7 +55,7 @@ var Shader = new Class({ setConstantFloat4: function (location, x, y, z, w) { - this.gl.useProgram(this.program); + this.bind(); this.gl.uniform4f(location, x, y, z, w); return this; @@ -63,7 +63,7 @@ var Shader = new Class({ setConstantInt1: function (location, x) { - this.gl.useProgram(this.program); + this.bind(); this.gl.uniform1i(location, x); return this; @@ -71,7 +71,7 @@ var Shader = new Class({ setConstantInt2: function (location, x, y) { - this.gl.useProgram(this.program); + this.bind(); this.gl.uniform2i(location, x, y); return this; @@ -79,7 +79,7 @@ var Shader = new Class({ setConstantInt3: function (location, x, y, z) { - this.gl.useProgram(this.program); + this.bind(); this.gl.uniform3i(location, x, y, z); return this; @@ -87,7 +87,7 @@ var Shader = new Class({ setConstantInt4: function (location, x, y, z, w) { - this.gl.useProgram(this.program); + this.bind(); this.gl.uniform4i(location, x, y, z, w); return this; @@ -95,7 +95,7 @@ var Shader = new Class({ setConstantMatrix2x2: function (location, floatArray) { - this.gl.useProgram(this.program); + this.bind(); this.gl.uniformMatrix2fv(location, false, floatArray); return this; @@ -103,7 +103,7 @@ var Shader = new Class({ setConstantMatrix3x3: function (location, floatArray) { - this.gl.useProgram(this.program); + this.bind(); this.gl.uniformMatrix3fv(location, false, floatArray); return this; @@ -111,7 +111,7 @@ var Shader = new Class({ setConstantMatrix4x4: function (location, floatArray) { - this.gl.useProgram(this.program); + this.bind(); this.gl.uniformMatrix4fv(location, false, floatArray); return this; @@ -119,11 +119,20 @@ var Shader = new Class({ bind: function () { - this.gl.useProgram(this.program); + if (CurrentShader !== this) + { + CurrentShader = this; + this.gl.useProgram(this.program); + } return this; } }); +Shader.SetDirty = function () +{ + CurrentShader = null; +}; + module.exports = Shader; diff --git a/v3/src/renderer/webgl/resources/VertexBuffer.js b/v3/src/renderer/webgl/resources/VertexBuffer.js index 679bbd852..d4c4eecce 100644 --- a/v3/src/renderer/webgl/resources/VertexBuffer.js +++ b/v3/src/renderer/webgl/resources/VertexBuffer.js @@ -1,5 +1,5 @@ var Class = require('../../../utils/Class'); - +var CurrentVertexBuffer = null; var VertexBuffer = new Class({ initialize: @@ -30,7 +30,11 @@ var VertexBuffer = new Class({ { var gl = this.gl; - gl.bindBuffer(gl.ARRAY_BUFFER, this.bufferObject); + if (CurrentVertexBuffer !== this) + { + CurrentVertexBuffer = this; + gl.bindBuffer(gl.ARRAY_BUFFER, this.bufferObject); + } gl.bufferSubData(gl.ARRAY_BUFFER, offset, bufferData); return this; @@ -43,29 +47,38 @@ var VertexBuffer = new Class({ var attributes = this.attributes; var attributesLength = attributes.length; - gl.bindBuffer(gl.ARRAY_BUFFER, bufferObject); - - for (var index = 0; index < attributesLength; ++index) + if (CurrentVertexBuffer !== this) { - var element = attributes[index]; - - if (element !== undefined && element !== null) + CurrentVertexBuffer = this; + gl.bindBuffer(gl.ARRAY_BUFFER, bufferObject); + + for (var index = 0; index < attributesLength; ++index) { - gl.enableVertexAttribArray(element.index); - gl.vertexAttribPointer( - element.index, - element.size, - element.type, - element.normalized, - element.stride, - element.offset - ); + var element = attributes[index]; + + if (element !== undefined && element !== null) + { + gl.enableVertexAttribArray(element.index); + gl.vertexAttribPointer( + element.index, + element.size, + element.type, + element.normalized, + element.stride, + element.offset + ); + } } } return this; } - + }); +VertexBuffer.SetDirty = function () +{ + CurrentVertexBuffer = null; +}; + module.exports = VertexBuffer;