diff --git a/src/core/shader/Shader.js b/src/core/shader/Shader.js index 3768f03..b05379a 100644 --- a/src/core/shader/Shader.js +++ b/src/core/shader/Shader.js @@ -1,4 +1,5 @@ import Program from './Program'; +import UniformGroup from './UniformGroup'; // let math = require('../../../math'); /** @@ -15,26 +16,78 @@ constructor(program, uniforms) { this.program = program; - this.uniforms = uniforms || {}; + + // lets see whats been passed in + // uniforms should be converted to a uniform group + if(uniforms) + { + if(uniforms instanceof UniformGroup) + { + this.uniformGroup = uniforms; + } + else + { + this.uniformGroup = new UniformGroup(uniforms) + } + } + else + { + this.uniformGroup = new UniformGroup({}); + } // time to build some getters and setters! // I guess down the line this could sort of generate an instruction list rather than use dirty ids? // does the trick for now though! for (const i in program.uniformData) { - const uniform = this.uniforms[i]; + const uniform = this.uniformGroup.uniforms[i]; - if (!uniform) + if (!uniform) // bad as we need check wher ethe uniforms are.. { - this.uniforms[i] = program.uniformData[i].value; + //this.uniforms.uniforms[i] = program.uniformData[i].value; } else if (uniform instanceof Array) { - this.uniforms[i] = new Float32Array(uniform); + this.uniformGroup.uniforms[i] = new Float32Array(uniform); } } } + get uniforms() + { + return this.uniformGroup.uniforms; + } +/* + +var offsetBuffer = gl.createBuffer(); +gl.bindBuffer(gl.ARRAY_BUFFER, offsetBuffer); +gl.bufferData(gl.ARRAY_BUFFER, offsets, gl.STATIC_DRAW); + +var colorBuffer = gl.createBuffer(); +gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); +gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW); + +var instanceCount = 4; +var ext = gl.getExtension("ANGLE_instanced_arrays"); // Vendor prefixes may apply! + +// Bind the rest of the vertex attributes normally +bindMeshArrays(gl); + +// Bind the instance position data +gl.bindBuffer(gl.ARRAY_BUFFER, offsetBuffer); +gl.enableVertexAttribArray(offsetLocation); +gl.vertexAttribPointer(offsetLocation, 3, gl.FLOAT, false, 12, 0); +ext.vertexAttribDivisorANGLE(offsetLocation, 1); // This makes it instanced! + +// Bind the instance color data +gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); +gl.enableVertexAttribArray(colorLocation); +gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 16, 0); +ext.vertexAttribDivisorANGLE(colorLocation, 1); // This makes it instanced! + +// Draw the instanced meshes +ext.drawElementsInstancedANGLE(gl.TRIANGLES, indexCount, gl.UNSIGNED_SHORT, 0, instanceCount); +*/ /** * A short hand function to create a shader based of a vertex and fragment shader * diff --git a/src/core/shader/Shader.js b/src/core/shader/Shader.js index 3768f03..b05379a 100644 --- a/src/core/shader/Shader.js +++ b/src/core/shader/Shader.js @@ -1,4 +1,5 @@ import Program from './Program'; +import UniformGroup from './UniformGroup'; // let math = require('../../../math'); /** @@ -15,26 +16,78 @@ constructor(program, uniforms) { this.program = program; - this.uniforms = uniforms || {}; + + // lets see whats been passed in + // uniforms should be converted to a uniform group + if(uniforms) + { + if(uniforms instanceof UniformGroup) + { + this.uniformGroup = uniforms; + } + else + { + this.uniformGroup = new UniformGroup(uniforms) + } + } + else + { + this.uniformGroup = new UniformGroup({}); + } // time to build some getters and setters! // I guess down the line this could sort of generate an instruction list rather than use dirty ids? // does the trick for now though! for (const i in program.uniformData) { - const uniform = this.uniforms[i]; + const uniform = this.uniformGroup.uniforms[i]; - if (!uniform) + if (!uniform) // bad as we need check wher ethe uniforms are.. { - this.uniforms[i] = program.uniformData[i].value; + //this.uniforms.uniforms[i] = program.uniformData[i].value; } else if (uniform instanceof Array) { - this.uniforms[i] = new Float32Array(uniform); + this.uniformGroup.uniforms[i] = new Float32Array(uniform); } } } + get uniforms() + { + return this.uniformGroup.uniforms; + } +/* + +var offsetBuffer = gl.createBuffer(); +gl.bindBuffer(gl.ARRAY_BUFFER, offsetBuffer); +gl.bufferData(gl.ARRAY_BUFFER, offsets, gl.STATIC_DRAW); + +var colorBuffer = gl.createBuffer(); +gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); +gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW); + +var instanceCount = 4; +var ext = gl.getExtension("ANGLE_instanced_arrays"); // Vendor prefixes may apply! + +// Bind the rest of the vertex attributes normally +bindMeshArrays(gl); + +// Bind the instance position data +gl.bindBuffer(gl.ARRAY_BUFFER, offsetBuffer); +gl.enableVertexAttribArray(offsetLocation); +gl.vertexAttribPointer(offsetLocation, 3, gl.FLOAT, false, 12, 0); +ext.vertexAttribDivisorANGLE(offsetLocation, 1); // This makes it instanced! + +// Bind the instance color data +gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); +gl.enableVertexAttribArray(colorLocation); +gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 16, 0); +ext.vertexAttribDivisorANGLE(colorLocation, 1); // This makes it instanced! + +// Draw the instanced meshes +ext.drawElementsInstancedANGLE(gl.TRIANGLES, indexCount, gl.UNSIGNED_SHORT, 0, instanceCount); +*/ /** * A short hand function to create a shader based of a vertex and fragment shader * diff --git a/src/core/shader/UniformGroup.js b/src/core/shader/UniformGroup.js index 89bc2ef..5de1a7a 100644 --- a/src/core/shader/UniformGroup.js +++ b/src/core/shader/UniformGroup.js @@ -25,8 +25,6 @@ this.id = UID++; this.static = !!_static; - - console.log(this.static + "<<<<<<<") } update() diff --git a/src/core/shader/Shader.js b/src/core/shader/Shader.js index 3768f03..b05379a 100644 --- a/src/core/shader/Shader.js +++ b/src/core/shader/Shader.js @@ -1,4 +1,5 @@ import Program from './Program'; +import UniformGroup from './UniformGroup'; // let math = require('../../../math'); /** @@ -15,26 +16,78 @@ constructor(program, uniforms) { this.program = program; - this.uniforms = uniforms || {}; + + // lets see whats been passed in + // uniforms should be converted to a uniform group + if(uniforms) + { + if(uniforms instanceof UniformGroup) + { + this.uniformGroup = uniforms; + } + else + { + this.uniformGroup = new UniformGroup(uniforms) + } + } + else + { + this.uniformGroup = new UniformGroup({}); + } // time to build some getters and setters! // I guess down the line this could sort of generate an instruction list rather than use dirty ids? // does the trick for now though! for (const i in program.uniformData) { - const uniform = this.uniforms[i]; + const uniform = this.uniformGroup.uniforms[i]; - if (!uniform) + if (!uniform) // bad as we need check wher ethe uniforms are.. { - this.uniforms[i] = program.uniformData[i].value; + //this.uniforms.uniforms[i] = program.uniformData[i].value; } else if (uniform instanceof Array) { - this.uniforms[i] = new Float32Array(uniform); + this.uniformGroup.uniforms[i] = new Float32Array(uniform); } } } + get uniforms() + { + return this.uniformGroup.uniforms; + } +/* + +var offsetBuffer = gl.createBuffer(); +gl.bindBuffer(gl.ARRAY_BUFFER, offsetBuffer); +gl.bufferData(gl.ARRAY_BUFFER, offsets, gl.STATIC_DRAW); + +var colorBuffer = gl.createBuffer(); +gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); +gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW); + +var instanceCount = 4; +var ext = gl.getExtension("ANGLE_instanced_arrays"); // Vendor prefixes may apply! + +// Bind the rest of the vertex attributes normally +bindMeshArrays(gl); + +// Bind the instance position data +gl.bindBuffer(gl.ARRAY_BUFFER, offsetBuffer); +gl.enableVertexAttribArray(offsetLocation); +gl.vertexAttribPointer(offsetLocation, 3, gl.FLOAT, false, 12, 0); +ext.vertexAttribDivisorANGLE(offsetLocation, 1); // This makes it instanced! + +// Bind the instance color data +gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); +gl.enableVertexAttribArray(colorLocation); +gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 16, 0); +ext.vertexAttribDivisorANGLE(colorLocation, 1); // This makes it instanced! + +// Draw the instanced meshes +ext.drawElementsInstancedANGLE(gl.TRIANGLES, indexCount, gl.UNSIGNED_SHORT, 0, instanceCount); +*/ /** * A short hand function to create a shader based of a vertex and fragment shader * diff --git a/src/core/shader/UniformGroup.js b/src/core/shader/UniformGroup.js index 89bc2ef..5de1a7a 100644 --- a/src/core/shader/UniformGroup.js +++ b/src/core/shader/UniformGroup.js @@ -25,8 +25,6 @@ this.id = UID++; this.static = !!_static; - - console.log(this.static + "<<<<<<<") } update() diff --git a/src/mesh/geometry/Attribute.js b/src/mesh/geometry/Attribute.js index 633f9bb..83f92c2 100644 --- a/src/mesh/geometry/Attribute.js +++ b/src/mesh/geometry/Attribute.js @@ -18,7 +18,7 @@ * @param {Number} [stride=0] How far apart (in floats) the start of each value is. (used for interleaving data) * @param {Number} [start=0] How far into the array to start reading values (used for interleaving data) */ - constructor(buffer, size, normalised = false, type = 5126, stride, start) + constructor(buffer, size, normalised = false, type = 5126, stride, start, instance) { this.buffer = buffer; this.size = size; @@ -26,6 +26,7 @@ this.type = type; this.stride = stride; this.start = start; + this.instance = instance; } /** diff --git a/src/core/shader/Shader.js b/src/core/shader/Shader.js index 3768f03..b05379a 100644 --- a/src/core/shader/Shader.js +++ b/src/core/shader/Shader.js @@ -1,4 +1,5 @@ import Program from './Program'; +import UniformGroup from './UniformGroup'; // let math = require('../../../math'); /** @@ -15,26 +16,78 @@ constructor(program, uniforms) { this.program = program; - this.uniforms = uniforms || {}; + + // lets see whats been passed in + // uniforms should be converted to a uniform group + if(uniforms) + { + if(uniforms instanceof UniformGroup) + { + this.uniformGroup = uniforms; + } + else + { + this.uniformGroup = new UniformGroup(uniforms) + } + } + else + { + this.uniformGroup = new UniformGroup({}); + } // time to build some getters and setters! // I guess down the line this could sort of generate an instruction list rather than use dirty ids? // does the trick for now though! for (const i in program.uniformData) { - const uniform = this.uniforms[i]; + const uniform = this.uniformGroup.uniforms[i]; - if (!uniform) + if (!uniform) // bad as we need check wher ethe uniforms are.. { - this.uniforms[i] = program.uniformData[i].value; + //this.uniforms.uniforms[i] = program.uniformData[i].value; } else if (uniform instanceof Array) { - this.uniforms[i] = new Float32Array(uniform); + this.uniformGroup.uniforms[i] = new Float32Array(uniform); } } } + get uniforms() + { + return this.uniformGroup.uniforms; + } +/* + +var offsetBuffer = gl.createBuffer(); +gl.bindBuffer(gl.ARRAY_BUFFER, offsetBuffer); +gl.bufferData(gl.ARRAY_BUFFER, offsets, gl.STATIC_DRAW); + +var colorBuffer = gl.createBuffer(); +gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); +gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW); + +var instanceCount = 4; +var ext = gl.getExtension("ANGLE_instanced_arrays"); // Vendor prefixes may apply! + +// Bind the rest of the vertex attributes normally +bindMeshArrays(gl); + +// Bind the instance position data +gl.bindBuffer(gl.ARRAY_BUFFER, offsetBuffer); +gl.enableVertexAttribArray(offsetLocation); +gl.vertexAttribPointer(offsetLocation, 3, gl.FLOAT, false, 12, 0); +ext.vertexAttribDivisorANGLE(offsetLocation, 1); // This makes it instanced! + +// Bind the instance color data +gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); +gl.enableVertexAttribArray(colorLocation); +gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 16, 0); +ext.vertexAttribDivisorANGLE(colorLocation, 1); // This makes it instanced! + +// Draw the instanced meshes +ext.drawElementsInstancedANGLE(gl.TRIANGLES, indexCount, gl.UNSIGNED_SHORT, 0, instanceCount); +*/ /** * A short hand function to create a shader based of a vertex and fragment shader * diff --git a/src/core/shader/UniformGroup.js b/src/core/shader/UniformGroup.js index 89bc2ef..5de1a7a 100644 --- a/src/core/shader/UniformGroup.js +++ b/src/core/shader/UniformGroup.js @@ -25,8 +25,6 @@ this.id = UID++; this.static = !!_static; - - console.log(this.static + "<<<<<<<") } update() diff --git a/src/mesh/geometry/Attribute.js b/src/mesh/geometry/Attribute.js index 633f9bb..83f92c2 100644 --- a/src/mesh/geometry/Attribute.js +++ b/src/mesh/geometry/Attribute.js @@ -18,7 +18,7 @@ * @param {Number} [stride=0] How far apart (in floats) the start of each value is. (used for interleaving data) * @param {Number} [start=0] How far into the array to start reading values (used for interleaving data) */ - constructor(buffer, size, normalised = false, type = 5126, stride, start) + constructor(buffer, size, normalised = false, type = 5126, stride, start, instance) { this.buffer = buffer; this.size = size; @@ -26,6 +26,7 @@ this.type = type; this.stride = stride; this.start = start; + this.instance = instance; } /** diff --git a/src/mesh/geometry/Geometry.js b/src/mesh/geometry/Geometry.js index 1bfe793..c773eac 100644 --- a/src/mesh/geometry/Geometry.js +++ b/src/mesh/geometry/Geometry.js @@ -75,7 +75,7 @@ * * @return {PIXI.mesh.Geometry} returns self, useful for chaining. */ - addAttribute(id, buffer, size, normalised = false, type, stride, start) + addAttribute(id, buffer, size, normalised = false, type, stride, start, instance = false) { if (!buffer) { @@ -114,7 +114,7 @@ bufferIndex = this.buffers.length - 1; } - this.attributes[id] = new Attribute(bufferIndex, size, normalised, type, stride, start); + this.attributes[id] = new Attribute(bufferIndex, size, normalised, type, stride, start, instance); return this; } @@ -269,7 +269,8 @@ attrib.normalized, attrib.type, attrib.stride, - attrib.start + attrib.start, + attrib.instance ); } diff --git a/src/core/shader/Shader.js b/src/core/shader/Shader.js index 3768f03..b05379a 100644 --- a/src/core/shader/Shader.js +++ b/src/core/shader/Shader.js @@ -1,4 +1,5 @@ import Program from './Program'; +import UniformGroup from './UniformGroup'; // let math = require('../../../math'); /** @@ -15,26 +16,78 @@ constructor(program, uniforms) { this.program = program; - this.uniforms = uniforms || {}; + + // lets see whats been passed in + // uniforms should be converted to a uniform group + if(uniforms) + { + if(uniforms instanceof UniformGroup) + { + this.uniformGroup = uniforms; + } + else + { + this.uniformGroup = new UniformGroup(uniforms) + } + } + else + { + this.uniformGroup = new UniformGroup({}); + } // time to build some getters and setters! // I guess down the line this could sort of generate an instruction list rather than use dirty ids? // does the trick for now though! for (const i in program.uniformData) { - const uniform = this.uniforms[i]; + const uniform = this.uniformGroup.uniforms[i]; - if (!uniform) + if (!uniform) // bad as we need check wher ethe uniforms are.. { - this.uniforms[i] = program.uniformData[i].value; + //this.uniforms.uniforms[i] = program.uniformData[i].value; } else if (uniform instanceof Array) { - this.uniforms[i] = new Float32Array(uniform); + this.uniformGroup.uniforms[i] = new Float32Array(uniform); } } } + get uniforms() + { + return this.uniformGroup.uniforms; + } +/* + +var offsetBuffer = gl.createBuffer(); +gl.bindBuffer(gl.ARRAY_BUFFER, offsetBuffer); +gl.bufferData(gl.ARRAY_BUFFER, offsets, gl.STATIC_DRAW); + +var colorBuffer = gl.createBuffer(); +gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); +gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW); + +var instanceCount = 4; +var ext = gl.getExtension("ANGLE_instanced_arrays"); // Vendor prefixes may apply! + +// Bind the rest of the vertex attributes normally +bindMeshArrays(gl); + +// Bind the instance position data +gl.bindBuffer(gl.ARRAY_BUFFER, offsetBuffer); +gl.enableVertexAttribArray(offsetLocation); +gl.vertexAttribPointer(offsetLocation, 3, gl.FLOAT, false, 12, 0); +ext.vertexAttribDivisorANGLE(offsetLocation, 1); // This makes it instanced! + +// Bind the instance color data +gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); +gl.enableVertexAttribArray(colorLocation); +gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 16, 0); +ext.vertexAttribDivisorANGLE(colorLocation, 1); // This makes it instanced! + +// Draw the instanced meshes +ext.drawElementsInstancedANGLE(gl.TRIANGLES, indexCount, gl.UNSIGNED_SHORT, 0, instanceCount); +*/ /** * A short hand function to create a shader based of a vertex and fragment shader * diff --git a/src/core/shader/UniformGroup.js b/src/core/shader/UniformGroup.js index 89bc2ef..5de1a7a 100644 --- a/src/core/shader/UniformGroup.js +++ b/src/core/shader/UniformGroup.js @@ -25,8 +25,6 @@ this.id = UID++; this.static = !!_static; - - console.log(this.static + "<<<<<<<") } update() diff --git a/src/mesh/geometry/Attribute.js b/src/mesh/geometry/Attribute.js index 633f9bb..83f92c2 100644 --- a/src/mesh/geometry/Attribute.js +++ b/src/mesh/geometry/Attribute.js @@ -18,7 +18,7 @@ * @param {Number} [stride=0] How far apart (in floats) the start of each value is. (used for interleaving data) * @param {Number} [start=0] How far into the array to start reading values (used for interleaving data) */ - constructor(buffer, size, normalised = false, type = 5126, stride, start) + constructor(buffer, size, normalised = false, type = 5126, stride, start, instance) { this.buffer = buffer; this.size = size; @@ -26,6 +26,7 @@ this.type = type; this.stride = stride; this.start = start; + this.instance = instance; } /** diff --git a/src/mesh/geometry/Geometry.js b/src/mesh/geometry/Geometry.js index 1bfe793..c773eac 100644 --- a/src/mesh/geometry/Geometry.js +++ b/src/mesh/geometry/Geometry.js @@ -75,7 +75,7 @@ * * @return {PIXI.mesh.Geometry} returns self, useful for chaining. */ - addAttribute(id, buffer, size, normalised = false, type, stride, start) + addAttribute(id, buffer, size, normalised = false, type, stride, start, instance = false) { if (!buffer) { @@ -114,7 +114,7 @@ bufferIndex = this.buffers.length - 1; } - this.attributes[id] = new Attribute(bufferIndex, size, normalised, type, stride, start); + this.attributes[id] = new Attribute(bufferIndex, size, normalised, type, stride, start, instance); return this; } @@ -269,7 +269,8 @@ attrib.normalized, attrib.type, attrib.stride, - attrib.start + attrib.start, + attrib.instance ); } diff --git a/src/mesh/webgl/MeshRenderer.js b/src/mesh/webgl/MeshRenderer.js index 0ba2687..84966a4 100644 --- a/src/mesh/webgl/MeshRenderer.js +++ b/src/mesh/webgl/MeshRenderer.js @@ -54,7 +54,7 @@ } // set unifomrs.. - this.renderer.shaderManager.syncUniformGroup(mesh.shader.uniforms); + this.renderer.shaderManager.syncUniformGroup(mesh.shader.uniformGroup); // sync uniforms.. this.renderer.state.setState(mesh.state); @@ -63,7 +63,7 @@ this.bindGeometry(mesh.geometry, glShader); // then render it - mesh.geometry.glVertexArrayObjects[this.CONTEXT_UID].draw(mesh.drawMode, mesh.size, mesh.start); + mesh.geometry.glVertexArrayObjects[this.CONTEXT_UID].draw(mesh.drawMode, mesh.size, mesh.start, mesh.instanceCount); } /** @@ -72,7 +72,7 @@ */ draw(mesh) { - mesh.geometry.glVertexArrayObjects[this.CONTEXT_UID].draw(mesh.drawMode, mesh.size, mesh.start); + mesh.geometry.glVertexArrayObjects[this.CONTEXT_UID].draw(mesh.drawMode, mesh.size, mesh.start, mesh.instanceCount); } /** @@ -203,7 +203,8 @@ attribute.type || 5126, // (5126 = FLOAT) attribute.normalized, attribute.stride, - attribute.start); + attribute.start, + attribute.instance); } geometry.glVertexArrayObjects[this.CONTEXT_UID] = vao;