diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 58e4db8..e826b78 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -6,126 +6,111 @@ export default class State { - constructor(data) - { - this.data = 0; - - this.blendMode = 0; - this.polygonOffset = 0; - } - - set blend( value ) - { - if( !!(this.data & (1 << BLEND)) !== value ) - { - this.data ^= (1 << BLEND); - } - } - - get blend() - { - return !!(this.data & (1 << BLEND)) - } - - //////////// - - set offsets( value ) - { - if( !!(this.data & (1 << OFFSET)) !== value ) - { - this.data ^= (1< b.name) ? 1 : -1; - }); + attributesArray.sort((a, b) => (a.name > b.name) ? 1 : -1); // eslint-disable-line no-confusing-arrow const attributes = {}; @@ -65,6 +62,7 @@ for (let i = 0; i < attributesArray.length; i++) { const attrib = attributesArray[i]; + attrib.location = i; attributes[attrib.name] = attrib; } diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 58e4db8..e826b78 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -6,126 +6,111 @@ export default class State { - constructor(data) - { - this.data = 0; - - this.blendMode = 0; - this.polygonOffset = 0; - } - - set blend( value ) - { - if( !!(this.data & (1 << BLEND)) !== value ) - { - this.data ^= (1 << BLEND); - } - } - - get blend() - { - return !!(this.data & (1 << BLEND)) - } - - //////////// - - set offsets( value ) - { - if( !!(this.data & (1 << OFFSET)) !== value ) - { - this.data ^= (1< b.name) ? 1 : -1; - }); + attributesArray.sort((a, b) => (a.name > b.name) ? 1 : -1); // eslint-disable-line no-confusing-arrow const attributes = {}; @@ -65,6 +62,7 @@ for (let i = 0; i < attributesArray.length; i++) { const attrib = attributesArray[i]; + attrib.location = i; attributes[attrib.name] = attrib; } diff --git a/src/core/shader/generateUniformsSync.js b/src/core/shader/generateUniformsSync.js index e2b719a..e9995d9 100644 --- a/src/core/shader/generateUniformsSync.js +++ b/src/core/shader/generateUniformsSync.js @@ -1,83 +1,79 @@ +const GLSL_TO_SINGLE_SETTERS_CACHED = { - -var generateUniformsSync = function(uniformData) -{ - - var textureCount = 1; - var func = 'var value = null; var cacheValue = null' - - for (var i in uniformData) - { - let data = uniformData[i]; - - // TODO && uniformData[i].value !== 0 <-- do we still need this? - if(data.type === 'sampler2D') - { - func += '\ngl.uniform1iv(uniformData.'+i+'.location, renderer.bindTexture(uniformValues.' + i + ', ' + (textureCount++) + ', true) );\n' - } - else if (data.type === 'mat3') - { - func += '\nvalue = uniformValues.' + i + ';'; - func += '\ngl.uniformMatrix3fv(uniformData.'+i+'.location, false, (value.a !== undefined) ? value : value.toArray(true));\n' - } - else if (data.type === 'vec2') - { - func += '\nvalue = uniformValues.' + i + ';\n'; - func += '\nif(value.x !== undefined)gl.uniform2f(uniformData.'+i+'.location, value.x, value.y);' - func += '\nelse gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n' - - } - else - { - var template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', 'uniformData.'+i+'.location'); - func += '\ncacheValue = uniformData.' + i + '.value;\n'; - func += 'value = uniformValues.' + i + ';\n'; - func += template +'\n'; - } - - }; - - return new Function('uniformData', 'uniformValues', 'gl', func); -} - -var GLSL_TO_SINGLE_SETTERS_CACHED = { - - 'float': `if(cacheValue !== value) + float: `if(cacheValue !== value) { cacheValue = value; gl.uniform1f(location, value) }`, - 'vec2': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) + vec2: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; gl.uniform2f(location, value[0], value[1]) }`, - 'vec3': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) + vec3: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; cacheValue[2] = value[2]; gl.uniform3f(location, value[0], value[1], value[2]) }`, - 'vec4': 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', + vec4: 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', - 'int': 'gl.uniform1i(location, value)', - 'ivec2': 'gl.uniform2i(location, value[0], value[1])', - 'ivec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'ivec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + int: 'gl.uniform1i(location, value)', + ivec2: 'gl.uniform2i(location, value[0], value[1])', + ivec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + ivec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'bool': 'gl.uniform1i(location, value)', - 'bvec2': 'gl.uniform2i(location, value[0], value[1])', - 'bvec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'bvec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + bool: 'gl.uniform1i(location, value)', + bvec2: 'gl.uniform2i(location, value[0], value[1])', + bvec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + bvec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'mat2': 'gl.uniformMatrix2fv(location, false, value)', - 'mat3': 'gl.uniformMatrix3fv(location, false, value)', - 'mat4': 'gl.uniformMatrix4fv(location, false, value)', + mat2: 'gl.uniformMatrix2fv(location, false, value)', + mat3: 'gl.uniformMatrix3fv(location, false, value)', + mat4: 'gl.uniformMatrix4fv(location, false, value)', - 'sampler2D':'uniform1i(location, value)' + sampler2D: 'uniform1i(location, value)', }; -module.exports = generateUniformsSync; +export default function generateUniformsSync(uniformData) +{ + let textureCount = 1; + let func = 'var value = null; var cacheValue = null'; + + for (const i in uniformData) + { + const data = uniformData[i]; + + // TODO && uniformData[i].value !== 0 <-- do we still need this? + if (data.type === 'sampler2D') + { + func += `\ngl.uniform1iv(uniformData.${i}.location, renderer.bindTexture(uniformValues.${i}, ${textureCount++}, true) );\n`; // eslint-disable-line max-len + } + else if (data.type === 'mat3') + { + func += `\nvalue = uniformValues.${i}; +gl.uniformMatrix3fv(uniformData.${i}.location, false, (value.a !== undefined) ? value : value.toArray(true));\n`; + } + else if (data.type === 'vec2') + { + func += `\nvalue = uniformValues.${i}; +if(value.x !== undefined)gl.uniform2f(uniformData.${i}.location, value.x, value.y); +else gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n`; + } + else + { + const template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', `uniformData.${i}.location`); + + func += `\ncacheValue = uniformData.${i}.value; +value = uniformValues.${i}; +${template};\n`; + } + } + + // console.log(func); + + return new Function('uniformData', 'uniformValues', 'gl', func); // eslint-disable-line no-new-func +} diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 58e4db8..e826b78 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -6,126 +6,111 @@ export default class State { - constructor(data) - { - this.data = 0; - - this.blendMode = 0; - this.polygonOffset = 0; - } - - set blend( value ) - { - if( !!(this.data & (1 << BLEND)) !== value ) - { - this.data ^= (1 << BLEND); - } - } - - get blend() - { - return !!(this.data & (1 << BLEND)) - } - - //////////// - - set offsets( value ) - { - if( !!(this.data & (1 << OFFSET)) !== value ) - { - this.data ^= (1< b.name) ? 1 : -1; - }); + attributesArray.sort((a, b) => (a.name > b.name) ? 1 : -1); // eslint-disable-line no-confusing-arrow const attributes = {}; @@ -65,6 +62,7 @@ for (let i = 0; i < attributesArray.length; i++) { const attrib = attributesArray[i]; + attrib.location = i; attributes[attrib.name] = attrib; } diff --git a/src/core/shader/generateUniformsSync.js b/src/core/shader/generateUniformsSync.js index e2b719a..e9995d9 100644 --- a/src/core/shader/generateUniformsSync.js +++ b/src/core/shader/generateUniformsSync.js @@ -1,83 +1,79 @@ +const GLSL_TO_SINGLE_SETTERS_CACHED = { - -var generateUniformsSync = function(uniformData) -{ - - var textureCount = 1; - var func = 'var value = null; var cacheValue = null' - - for (var i in uniformData) - { - let data = uniformData[i]; - - // TODO && uniformData[i].value !== 0 <-- do we still need this? - if(data.type === 'sampler2D') - { - func += '\ngl.uniform1iv(uniformData.'+i+'.location, renderer.bindTexture(uniformValues.' + i + ', ' + (textureCount++) + ', true) );\n' - } - else if (data.type === 'mat3') - { - func += '\nvalue = uniformValues.' + i + ';'; - func += '\ngl.uniformMatrix3fv(uniformData.'+i+'.location, false, (value.a !== undefined) ? value : value.toArray(true));\n' - } - else if (data.type === 'vec2') - { - func += '\nvalue = uniformValues.' + i + ';\n'; - func += '\nif(value.x !== undefined)gl.uniform2f(uniformData.'+i+'.location, value.x, value.y);' - func += '\nelse gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n' - - } - else - { - var template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', 'uniformData.'+i+'.location'); - func += '\ncacheValue = uniformData.' + i + '.value;\n'; - func += 'value = uniformValues.' + i + ';\n'; - func += template +'\n'; - } - - }; - - return new Function('uniformData', 'uniformValues', 'gl', func); -} - -var GLSL_TO_SINGLE_SETTERS_CACHED = { - - 'float': `if(cacheValue !== value) + float: `if(cacheValue !== value) { cacheValue = value; gl.uniform1f(location, value) }`, - 'vec2': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) + vec2: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; gl.uniform2f(location, value[0], value[1]) }`, - 'vec3': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) + vec3: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; cacheValue[2] = value[2]; gl.uniform3f(location, value[0], value[1], value[2]) }`, - 'vec4': 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', + vec4: 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', - 'int': 'gl.uniform1i(location, value)', - 'ivec2': 'gl.uniform2i(location, value[0], value[1])', - 'ivec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'ivec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + int: 'gl.uniform1i(location, value)', + ivec2: 'gl.uniform2i(location, value[0], value[1])', + ivec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + ivec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'bool': 'gl.uniform1i(location, value)', - 'bvec2': 'gl.uniform2i(location, value[0], value[1])', - 'bvec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'bvec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + bool: 'gl.uniform1i(location, value)', + bvec2: 'gl.uniform2i(location, value[0], value[1])', + bvec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + bvec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'mat2': 'gl.uniformMatrix2fv(location, false, value)', - 'mat3': 'gl.uniformMatrix3fv(location, false, value)', - 'mat4': 'gl.uniformMatrix4fv(location, false, value)', + mat2: 'gl.uniformMatrix2fv(location, false, value)', + mat3: 'gl.uniformMatrix3fv(location, false, value)', + mat4: 'gl.uniformMatrix4fv(location, false, value)', - 'sampler2D':'uniform1i(location, value)' + sampler2D: 'uniform1i(location, value)', }; -module.exports = generateUniformsSync; +export default function generateUniformsSync(uniformData) +{ + let textureCount = 1; + let func = 'var value = null; var cacheValue = null'; + + for (const i in uniformData) + { + const data = uniformData[i]; + + // TODO && uniformData[i].value !== 0 <-- do we still need this? + if (data.type === 'sampler2D') + { + func += `\ngl.uniform1iv(uniformData.${i}.location, renderer.bindTexture(uniformValues.${i}, ${textureCount++}, true) );\n`; // eslint-disable-line max-len + } + else if (data.type === 'mat3') + { + func += `\nvalue = uniformValues.${i}; +gl.uniformMatrix3fv(uniformData.${i}.location, false, (value.a !== undefined) ? value : value.toArray(true));\n`; + } + else if (data.type === 'vec2') + { + func += `\nvalue = uniformValues.${i}; +if(value.x !== undefined)gl.uniform2f(uniformData.${i}.location, value.x, value.y); +else gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n`; + } + else + { + const template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', `uniformData.${i}.location`); + + func += `\ncacheValue = uniformData.${i}.value; +value = uniformValues.${i}; +${template};\n`; + } + } + + // console.log(func); + + return new Function('uniformData', 'uniformValues', 'gl', func); // eslint-disable-line no-new-func +} diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 4495081..1c7ad70 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -108,10 +108,8 @@ // step 2: check the maximum number of if statements the shader can have too.. this.MAX_TEXTURES = checkMaxIfStatmentsInShader(this.MAX_TEXTURES, gl); - console.log(this.MAX_TEXTURE) const shader = this.shader = generateMultiTextureShader(gl, this.MAX_TEXTURES); - console.log(shader) // create a couple of buffers this.indexBuffer = glCore.GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); @@ -123,8 +121,6 @@ for (let i = 0; i < this.vaoMax; i++) { this.vertexBuffers[i] = glCore.GLBuffer.createVertexBuffer(gl, null, gl.STREAM_DRAW); - console.log(this.vertexBuffers) - console.log(shader.attributes) /* eslint-disable max-len */ // build the vao object that will render.. @@ -132,11 +128,13 @@ .addIndex(this.indexBuffer) .addAttribute(this.vertexBuffers[i], shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) .addAttribute(this.vertexBuffers[i], shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) - if(shader.attributes.aTextureId) - { - this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); - } + .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); + + if (shader.attributes.aTextureId) + { + this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); + } + /* eslint-disable max-len */ } diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 58e4db8..e826b78 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -6,126 +6,111 @@ export default class State { - constructor(data) - { - this.data = 0; - - this.blendMode = 0; - this.polygonOffset = 0; - } - - set blend( value ) - { - if( !!(this.data & (1 << BLEND)) !== value ) - { - this.data ^= (1 << BLEND); - } - } - - get blend() - { - return !!(this.data & (1 << BLEND)) - } - - //////////// - - set offsets( value ) - { - if( !!(this.data & (1 << OFFSET)) !== value ) - { - this.data ^= (1< b.name) ? 1 : -1; - }); + attributesArray.sort((a, b) => (a.name > b.name) ? 1 : -1); // eslint-disable-line no-confusing-arrow const attributes = {}; @@ -65,6 +62,7 @@ for (let i = 0; i < attributesArray.length; i++) { const attrib = attributesArray[i]; + attrib.location = i; attributes[attrib.name] = attrib; } diff --git a/src/core/shader/generateUniformsSync.js b/src/core/shader/generateUniformsSync.js index e2b719a..e9995d9 100644 --- a/src/core/shader/generateUniformsSync.js +++ b/src/core/shader/generateUniformsSync.js @@ -1,83 +1,79 @@ +const GLSL_TO_SINGLE_SETTERS_CACHED = { - -var generateUniformsSync = function(uniformData) -{ - - var textureCount = 1; - var func = 'var value = null; var cacheValue = null' - - for (var i in uniformData) - { - let data = uniformData[i]; - - // TODO && uniformData[i].value !== 0 <-- do we still need this? - if(data.type === 'sampler2D') - { - func += '\ngl.uniform1iv(uniformData.'+i+'.location, renderer.bindTexture(uniformValues.' + i + ', ' + (textureCount++) + ', true) );\n' - } - else if (data.type === 'mat3') - { - func += '\nvalue = uniformValues.' + i + ';'; - func += '\ngl.uniformMatrix3fv(uniformData.'+i+'.location, false, (value.a !== undefined) ? value : value.toArray(true));\n' - } - else if (data.type === 'vec2') - { - func += '\nvalue = uniformValues.' + i + ';\n'; - func += '\nif(value.x !== undefined)gl.uniform2f(uniformData.'+i+'.location, value.x, value.y);' - func += '\nelse gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n' - - } - else - { - var template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', 'uniformData.'+i+'.location'); - func += '\ncacheValue = uniformData.' + i + '.value;\n'; - func += 'value = uniformValues.' + i + ';\n'; - func += template +'\n'; - } - - }; - - return new Function('uniformData', 'uniformValues', 'gl', func); -} - -var GLSL_TO_SINGLE_SETTERS_CACHED = { - - 'float': `if(cacheValue !== value) + float: `if(cacheValue !== value) { cacheValue = value; gl.uniform1f(location, value) }`, - 'vec2': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) + vec2: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; gl.uniform2f(location, value[0], value[1]) }`, - 'vec3': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) + vec3: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; cacheValue[2] = value[2]; gl.uniform3f(location, value[0], value[1], value[2]) }`, - 'vec4': 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', + vec4: 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', - 'int': 'gl.uniform1i(location, value)', - 'ivec2': 'gl.uniform2i(location, value[0], value[1])', - 'ivec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'ivec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + int: 'gl.uniform1i(location, value)', + ivec2: 'gl.uniform2i(location, value[0], value[1])', + ivec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + ivec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'bool': 'gl.uniform1i(location, value)', - 'bvec2': 'gl.uniform2i(location, value[0], value[1])', - 'bvec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'bvec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + bool: 'gl.uniform1i(location, value)', + bvec2: 'gl.uniform2i(location, value[0], value[1])', + bvec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + bvec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'mat2': 'gl.uniformMatrix2fv(location, false, value)', - 'mat3': 'gl.uniformMatrix3fv(location, false, value)', - 'mat4': 'gl.uniformMatrix4fv(location, false, value)', + mat2: 'gl.uniformMatrix2fv(location, false, value)', + mat3: 'gl.uniformMatrix3fv(location, false, value)', + mat4: 'gl.uniformMatrix4fv(location, false, value)', - 'sampler2D':'uniform1i(location, value)' + sampler2D: 'uniform1i(location, value)', }; -module.exports = generateUniformsSync; +export default function generateUniformsSync(uniformData) +{ + let textureCount = 1; + let func = 'var value = null; var cacheValue = null'; + + for (const i in uniformData) + { + const data = uniformData[i]; + + // TODO && uniformData[i].value !== 0 <-- do we still need this? + if (data.type === 'sampler2D') + { + func += `\ngl.uniform1iv(uniformData.${i}.location, renderer.bindTexture(uniformValues.${i}, ${textureCount++}, true) );\n`; // eslint-disable-line max-len + } + else if (data.type === 'mat3') + { + func += `\nvalue = uniformValues.${i}; +gl.uniformMatrix3fv(uniformData.${i}.location, false, (value.a !== undefined) ? value : value.toArray(true));\n`; + } + else if (data.type === 'vec2') + { + func += `\nvalue = uniformValues.${i}; +if(value.x !== undefined)gl.uniform2f(uniformData.${i}.location, value.x, value.y); +else gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n`; + } + else + { + const template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', `uniformData.${i}.location`); + + func += `\ncacheValue = uniformData.${i}.value; +value = uniformValues.${i}; +${template};\n`; + } + } + + // console.log(func); + + return new Function('uniformData', 'uniformValues', 'gl', func); // eslint-disable-line no-new-func +} diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 4495081..1c7ad70 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -108,10 +108,8 @@ // step 2: check the maximum number of if statements the shader can have too.. this.MAX_TEXTURES = checkMaxIfStatmentsInShader(this.MAX_TEXTURES, gl); - console.log(this.MAX_TEXTURE) const shader = this.shader = generateMultiTextureShader(gl, this.MAX_TEXTURES); - console.log(shader) // create a couple of buffers this.indexBuffer = glCore.GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); @@ -123,8 +121,6 @@ for (let i = 0; i < this.vaoMax; i++) { this.vertexBuffers[i] = glCore.GLBuffer.createVertexBuffer(gl, null, gl.STREAM_DRAW); - console.log(this.vertexBuffers) - console.log(shader.attributes) /* eslint-disable max-len */ // build the vao object that will render.. @@ -132,11 +128,13 @@ .addIndex(this.indexBuffer) .addAttribute(this.vertexBuffers[i], shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) .addAttribute(this.vertexBuffers[i], shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) - if(shader.attributes.aTextureId) - { - this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); - } + .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); + + if (shader.attributes.aTextureId) + { + this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); + } + /* eslint-disable max-len */ } diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index 8d8c31c..0b07d77 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -15,7 +15,6 @@ */ constructor(geometry, shader, uniforms, state, drawMode = core.DRAW_MODES.TRIANGLES) { - //drawMode = core.DRAW_MODES.TRIANGLES, uniforms = {}) super(); /** @@ -43,15 +42,12 @@ * @member {number} * @see PIXI.mesh.Mesh.DRAW_MODES */ - this.drawMode = 4//drawMode; + this.drawMode = drawMode; + // TODO uniform auto generation? + // make sure to add required feilds this.uniforms = uniforms; -// geometry// -// shader// -// uniforms// -// state// - /** * The tint applied to the mesh. This is a [r,g,b] value. A value of [1,1,1] will remove any * tint effect. diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 58e4db8..e826b78 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -6,126 +6,111 @@ export default class State { - constructor(data) - { - this.data = 0; - - this.blendMode = 0; - this.polygonOffset = 0; - } - - set blend( value ) - { - if( !!(this.data & (1 << BLEND)) !== value ) - { - this.data ^= (1 << BLEND); - } - } - - get blend() - { - return !!(this.data & (1 << BLEND)) - } - - //////////// - - set offsets( value ) - { - if( !!(this.data & (1 << OFFSET)) !== value ) - { - this.data ^= (1< b.name) ? 1 : -1; - }); + attributesArray.sort((a, b) => (a.name > b.name) ? 1 : -1); // eslint-disable-line no-confusing-arrow const attributes = {}; @@ -65,6 +62,7 @@ for (let i = 0; i < attributesArray.length; i++) { const attrib = attributesArray[i]; + attrib.location = i; attributes[attrib.name] = attrib; } diff --git a/src/core/shader/generateUniformsSync.js b/src/core/shader/generateUniformsSync.js index e2b719a..e9995d9 100644 --- a/src/core/shader/generateUniformsSync.js +++ b/src/core/shader/generateUniformsSync.js @@ -1,83 +1,79 @@ +const GLSL_TO_SINGLE_SETTERS_CACHED = { - -var generateUniformsSync = function(uniformData) -{ - - var textureCount = 1; - var func = 'var value = null; var cacheValue = null' - - for (var i in uniformData) - { - let data = uniformData[i]; - - // TODO && uniformData[i].value !== 0 <-- do we still need this? - if(data.type === 'sampler2D') - { - func += '\ngl.uniform1iv(uniformData.'+i+'.location, renderer.bindTexture(uniformValues.' + i + ', ' + (textureCount++) + ', true) );\n' - } - else if (data.type === 'mat3') - { - func += '\nvalue = uniformValues.' + i + ';'; - func += '\ngl.uniformMatrix3fv(uniformData.'+i+'.location, false, (value.a !== undefined) ? value : value.toArray(true));\n' - } - else if (data.type === 'vec2') - { - func += '\nvalue = uniformValues.' + i + ';\n'; - func += '\nif(value.x !== undefined)gl.uniform2f(uniformData.'+i+'.location, value.x, value.y);' - func += '\nelse gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n' - - } - else - { - var template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', 'uniformData.'+i+'.location'); - func += '\ncacheValue = uniformData.' + i + '.value;\n'; - func += 'value = uniformValues.' + i + ';\n'; - func += template +'\n'; - } - - }; - - return new Function('uniformData', 'uniformValues', 'gl', func); -} - -var GLSL_TO_SINGLE_SETTERS_CACHED = { - - 'float': `if(cacheValue !== value) + float: `if(cacheValue !== value) { cacheValue = value; gl.uniform1f(location, value) }`, - 'vec2': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) + vec2: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; gl.uniform2f(location, value[0], value[1]) }`, - 'vec3': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) + vec3: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; cacheValue[2] = value[2]; gl.uniform3f(location, value[0], value[1], value[2]) }`, - 'vec4': 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', + vec4: 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', - 'int': 'gl.uniform1i(location, value)', - 'ivec2': 'gl.uniform2i(location, value[0], value[1])', - 'ivec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'ivec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + int: 'gl.uniform1i(location, value)', + ivec2: 'gl.uniform2i(location, value[0], value[1])', + ivec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + ivec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'bool': 'gl.uniform1i(location, value)', - 'bvec2': 'gl.uniform2i(location, value[0], value[1])', - 'bvec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'bvec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + bool: 'gl.uniform1i(location, value)', + bvec2: 'gl.uniform2i(location, value[0], value[1])', + bvec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + bvec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'mat2': 'gl.uniformMatrix2fv(location, false, value)', - 'mat3': 'gl.uniformMatrix3fv(location, false, value)', - 'mat4': 'gl.uniformMatrix4fv(location, false, value)', + mat2: 'gl.uniformMatrix2fv(location, false, value)', + mat3: 'gl.uniformMatrix3fv(location, false, value)', + mat4: 'gl.uniformMatrix4fv(location, false, value)', - 'sampler2D':'uniform1i(location, value)' + sampler2D: 'uniform1i(location, value)', }; -module.exports = generateUniformsSync; +export default function generateUniformsSync(uniformData) +{ + let textureCount = 1; + let func = 'var value = null; var cacheValue = null'; + + for (const i in uniformData) + { + const data = uniformData[i]; + + // TODO && uniformData[i].value !== 0 <-- do we still need this? + if (data.type === 'sampler2D') + { + func += `\ngl.uniform1iv(uniformData.${i}.location, renderer.bindTexture(uniformValues.${i}, ${textureCount++}, true) );\n`; // eslint-disable-line max-len + } + else if (data.type === 'mat3') + { + func += `\nvalue = uniformValues.${i}; +gl.uniformMatrix3fv(uniformData.${i}.location, false, (value.a !== undefined) ? value : value.toArray(true));\n`; + } + else if (data.type === 'vec2') + { + func += `\nvalue = uniformValues.${i}; +if(value.x !== undefined)gl.uniform2f(uniformData.${i}.location, value.x, value.y); +else gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n`; + } + else + { + const template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', `uniformData.${i}.location`); + + func += `\ncacheValue = uniformData.${i}.value; +value = uniformValues.${i}; +${template};\n`; + } + } + + // console.log(func); + + return new Function('uniformData', 'uniformValues', 'gl', func); // eslint-disable-line no-new-func +} diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 4495081..1c7ad70 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -108,10 +108,8 @@ // step 2: check the maximum number of if statements the shader can have too.. this.MAX_TEXTURES = checkMaxIfStatmentsInShader(this.MAX_TEXTURES, gl); - console.log(this.MAX_TEXTURE) const shader = this.shader = generateMultiTextureShader(gl, this.MAX_TEXTURES); - console.log(shader) // create a couple of buffers this.indexBuffer = glCore.GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); @@ -123,8 +121,6 @@ for (let i = 0; i < this.vaoMax; i++) { this.vertexBuffers[i] = glCore.GLBuffer.createVertexBuffer(gl, null, gl.STREAM_DRAW); - console.log(this.vertexBuffers) - console.log(shader.attributes) /* eslint-disable max-len */ // build the vao object that will render.. @@ -132,11 +128,13 @@ .addIndex(this.indexBuffer) .addAttribute(this.vertexBuffers[i], shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) .addAttribute(this.vertexBuffers[i], shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) - if(shader.attributes.aTextureId) - { - this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); - } + .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); + + if (shader.attributes.aTextureId) + { + this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); + } + /* eslint-disable max-len */ } diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index 8d8c31c..0b07d77 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -15,7 +15,6 @@ */ constructor(geometry, shader, uniforms, state, drawMode = core.DRAW_MODES.TRIANGLES) { - //drawMode = core.DRAW_MODES.TRIANGLES, uniforms = {}) super(); /** @@ -43,15 +42,12 @@ * @member {number} * @see PIXI.mesh.Mesh.DRAW_MODES */ - this.drawMode = 4//drawMode; + this.drawMode = drawMode; + // TODO uniform auto generation? + // make sure to add required feilds this.uniforms = uniforms; -// geometry// -// shader// -// uniforms// -// state// - /** * The tint applied to the mesh. This is a [r,g,b] value. A value of [1,1,1] will remove any * tint effect. diff --git a/src/mesh/Plane.js b/src/mesh/Plane.js index 9d2aa67..4ad7ef5 100644 --- a/src/mesh/Plane.js +++ b/src/mesh/Plane.js @@ -5,7 +5,7 @@ import { join } from 'path'; let meshShader; -const temp = [0,0,0]; +const temp = [0, 0, 0]; /** * The Plane allows you to draw a texture across several points and them manipulate these points @@ -33,11 +33,15 @@ { const geometry = new Geometry(); - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } geometry.addAttribute('aVertexPosition', new Float32Array(2), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(2), 2) - geometry.addIndex(new Uint16Array(2)) + .addAttribute('aTextureCoord', new Float32Array(2), 2) + .addIndex(new Uint16Array(2)); super(geometry, meshShader, 4); @@ -69,7 +73,7 @@ this.refresh(); - this.tint; + this.tint = 0xFFFFFF; } /** @@ -80,7 +84,6 @@ { const total = this.verticesX * this.verticesY; const verts = []; - const colors = []; const uvs = []; const indices = []; const texture = this.texture; @@ -113,8 +116,6 @@ } } - // cons - const totalSub = segmentsX * segmentsY; for (let i = 0; i < totalSub; i++) @@ -131,24 +132,21 @@ indices.push(value2, value4, value3); } - // console.log(indices) this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// - + this.shader.uniforms.uSampler2 = this.texture; this.vertices = new Float32Array(verts); this.uvs = new Float32Array(uvs); this.indices = new Uint16Array(indices); - this.geometry.getAttribute('aVertexPosition').data = this.vertices; - this.geometry.getAttribute('aTextureCoord').data = this.uvs; - this.geometry.data.indexBuffer.data = this.indices; + this.geometry.getAttribute('aVertexPosition').data = this.vertices; + this.geometry.getAttribute('aTextureCoord').data = this.uvs; + this.geometry.data.indexBuffer.data = this.indices; - // ensure that the changes are uploaded + // ensure that the changes are uploaded this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); - this.geometry.data.indexBuffer.update();// - + this.geometry.data.indexBuffer.update(); } /** @@ -165,10 +163,10 @@ } } - _renderWebGL(renderer) + _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); @@ -176,11 +174,7 @@ updateTransform() { - this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); } - - - } diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 58e4db8..e826b78 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -6,126 +6,111 @@ export default class State { - constructor(data) - { - this.data = 0; - - this.blendMode = 0; - this.polygonOffset = 0; - } - - set blend( value ) - { - if( !!(this.data & (1 << BLEND)) !== value ) - { - this.data ^= (1 << BLEND); - } - } - - get blend() - { - return !!(this.data & (1 << BLEND)) - } - - //////////// - - set offsets( value ) - { - if( !!(this.data & (1 << OFFSET)) !== value ) - { - this.data ^= (1< b.name) ? 1 : -1; - }); + attributesArray.sort((a, b) => (a.name > b.name) ? 1 : -1); // eslint-disable-line no-confusing-arrow const attributes = {}; @@ -65,6 +62,7 @@ for (let i = 0; i < attributesArray.length; i++) { const attrib = attributesArray[i]; + attrib.location = i; attributes[attrib.name] = attrib; } diff --git a/src/core/shader/generateUniformsSync.js b/src/core/shader/generateUniformsSync.js index e2b719a..e9995d9 100644 --- a/src/core/shader/generateUniformsSync.js +++ b/src/core/shader/generateUniformsSync.js @@ -1,83 +1,79 @@ +const GLSL_TO_SINGLE_SETTERS_CACHED = { - -var generateUniformsSync = function(uniformData) -{ - - var textureCount = 1; - var func = 'var value = null; var cacheValue = null' - - for (var i in uniformData) - { - let data = uniformData[i]; - - // TODO && uniformData[i].value !== 0 <-- do we still need this? - if(data.type === 'sampler2D') - { - func += '\ngl.uniform1iv(uniformData.'+i+'.location, renderer.bindTexture(uniformValues.' + i + ', ' + (textureCount++) + ', true) );\n' - } - else if (data.type === 'mat3') - { - func += '\nvalue = uniformValues.' + i + ';'; - func += '\ngl.uniformMatrix3fv(uniformData.'+i+'.location, false, (value.a !== undefined) ? value : value.toArray(true));\n' - } - else if (data.type === 'vec2') - { - func += '\nvalue = uniformValues.' + i + ';\n'; - func += '\nif(value.x !== undefined)gl.uniform2f(uniformData.'+i+'.location, value.x, value.y);' - func += '\nelse gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n' - - } - else - { - var template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', 'uniformData.'+i+'.location'); - func += '\ncacheValue = uniformData.' + i + '.value;\n'; - func += 'value = uniformValues.' + i + ';\n'; - func += template +'\n'; - } - - }; - - return new Function('uniformData', 'uniformValues', 'gl', func); -} - -var GLSL_TO_SINGLE_SETTERS_CACHED = { - - 'float': `if(cacheValue !== value) + float: `if(cacheValue !== value) { cacheValue = value; gl.uniform1f(location, value) }`, - 'vec2': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) + vec2: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; gl.uniform2f(location, value[0], value[1]) }`, - 'vec3': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) + vec3: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; cacheValue[2] = value[2]; gl.uniform3f(location, value[0], value[1], value[2]) }`, - 'vec4': 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', + vec4: 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', - 'int': 'gl.uniform1i(location, value)', - 'ivec2': 'gl.uniform2i(location, value[0], value[1])', - 'ivec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'ivec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + int: 'gl.uniform1i(location, value)', + ivec2: 'gl.uniform2i(location, value[0], value[1])', + ivec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + ivec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'bool': 'gl.uniform1i(location, value)', - 'bvec2': 'gl.uniform2i(location, value[0], value[1])', - 'bvec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'bvec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + bool: 'gl.uniform1i(location, value)', + bvec2: 'gl.uniform2i(location, value[0], value[1])', + bvec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + bvec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'mat2': 'gl.uniformMatrix2fv(location, false, value)', - 'mat3': 'gl.uniformMatrix3fv(location, false, value)', - 'mat4': 'gl.uniformMatrix4fv(location, false, value)', + mat2: 'gl.uniformMatrix2fv(location, false, value)', + mat3: 'gl.uniformMatrix3fv(location, false, value)', + mat4: 'gl.uniformMatrix4fv(location, false, value)', - 'sampler2D':'uniform1i(location, value)' + sampler2D: 'uniform1i(location, value)', }; -module.exports = generateUniformsSync; +export default function generateUniformsSync(uniformData) +{ + let textureCount = 1; + let func = 'var value = null; var cacheValue = null'; + + for (const i in uniformData) + { + const data = uniformData[i]; + + // TODO && uniformData[i].value !== 0 <-- do we still need this? + if (data.type === 'sampler2D') + { + func += `\ngl.uniform1iv(uniformData.${i}.location, renderer.bindTexture(uniformValues.${i}, ${textureCount++}, true) );\n`; // eslint-disable-line max-len + } + else if (data.type === 'mat3') + { + func += `\nvalue = uniformValues.${i}; +gl.uniformMatrix3fv(uniformData.${i}.location, false, (value.a !== undefined) ? value : value.toArray(true));\n`; + } + else if (data.type === 'vec2') + { + func += `\nvalue = uniformValues.${i}; +if(value.x !== undefined)gl.uniform2f(uniformData.${i}.location, value.x, value.y); +else gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n`; + } + else + { + const template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', `uniformData.${i}.location`); + + func += `\ncacheValue = uniformData.${i}.value; +value = uniformValues.${i}; +${template};\n`; + } + } + + // console.log(func); + + return new Function('uniformData', 'uniformValues', 'gl', func); // eslint-disable-line no-new-func +} diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 4495081..1c7ad70 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -108,10 +108,8 @@ // step 2: check the maximum number of if statements the shader can have too.. this.MAX_TEXTURES = checkMaxIfStatmentsInShader(this.MAX_TEXTURES, gl); - console.log(this.MAX_TEXTURE) const shader = this.shader = generateMultiTextureShader(gl, this.MAX_TEXTURES); - console.log(shader) // create a couple of buffers this.indexBuffer = glCore.GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); @@ -123,8 +121,6 @@ for (let i = 0; i < this.vaoMax; i++) { this.vertexBuffers[i] = glCore.GLBuffer.createVertexBuffer(gl, null, gl.STREAM_DRAW); - console.log(this.vertexBuffers) - console.log(shader.attributes) /* eslint-disable max-len */ // build the vao object that will render.. @@ -132,11 +128,13 @@ .addIndex(this.indexBuffer) .addAttribute(this.vertexBuffers[i], shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) .addAttribute(this.vertexBuffers[i], shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) - if(shader.attributes.aTextureId) - { - this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); - } + .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); + + if (shader.attributes.aTextureId) + { + this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); + } + /* eslint-disable max-len */ } diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index 8d8c31c..0b07d77 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -15,7 +15,6 @@ */ constructor(geometry, shader, uniforms, state, drawMode = core.DRAW_MODES.TRIANGLES) { - //drawMode = core.DRAW_MODES.TRIANGLES, uniforms = {}) super(); /** @@ -43,15 +42,12 @@ * @member {number} * @see PIXI.mesh.Mesh.DRAW_MODES */ - this.drawMode = 4//drawMode; + this.drawMode = drawMode; + // TODO uniform auto generation? + // make sure to add required feilds this.uniforms = uniforms; -// geometry// -// shader// -// uniforms// -// state// - /** * The tint applied to the mesh. This is a [r,g,b] value. A value of [1,1,1] will remove any * tint effect. diff --git a/src/mesh/Plane.js b/src/mesh/Plane.js index 9d2aa67..4ad7ef5 100644 --- a/src/mesh/Plane.js +++ b/src/mesh/Plane.js @@ -5,7 +5,7 @@ import { join } from 'path'; let meshShader; -const temp = [0,0,0]; +const temp = [0, 0, 0]; /** * The Plane allows you to draw a texture across several points and them manipulate these points @@ -33,11 +33,15 @@ { const geometry = new Geometry(); - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } geometry.addAttribute('aVertexPosition', new Float32Array(2), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(2), 2) - geometry.addIndex(new Uint16Array(2)) + .addAttribute('aTextureCoord', new Float32Array(2), 2) + .addIndex(new Uint16Array(2)); super(geometry, meshShader, 4); @@ -69,7 +73,7 @@ this.refresh(); - this.tint; + this.tint = 0xFFFFFF; } /** @@ -80,7 +84,6 @@ { const total = this.verticesX * this.verticesY; const verts = []; - const colors = []; const uvs = []; const indices = []; const texture = this.texture; @@ -113,8 +116,6 @@ } } - // cons - const totalSub = segmentsX * segmentsY; for (let i = 0; i < totalSub; i++) @@ -131,24 +132,21 @@ indices.push(value2, value4, value3); } - // console.log(indices) this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// - + this.shader.uniforms.uSampler2 = this.texture; this.vertices = new Float32Array(verts); this.uvs = new Float32Array(uvs); this.indices = new Uint16Array(indices); - this.geometry.getAttribute('aVertexPosition').data = this.vertices; - this.geometry.getAttribute('aTextureCoord').data = this.uvs; - this.geometry.data.indexBuffer.data = this.indices; + this.geometry.getAttribute('aVertexPosition').data = this.vertices; + this.geometry.getAttribute('aTextureCoord').data = this.uvs; + this.geometry.data.indexBuffer.data = this.indices; - // ensure that the changes are uploaded + // ensure that the changes are uploaded this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); - this.geometry.data.indexBuffer.update();// - + this.geometry.data.indexBuffer.update(); } /** @@ -165,10 +163,10 @@ } } - _renderWebGL(renderer) + _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); @@ -176,11 +174,7 @@ updateTransform() { - this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); } - - - } diff --git a/src/mesh/Rope.js b/src/mesh/Rope.js index 1b167ee..0589890 100644 --- a/src/mesh/Rope.js +++ b/src/mesh/Rope.js @@ -5,6 +5,7 @@ import { join } from 'path'; let meshShader; +const temp = new Float32Array([0, 0, 0]); /** * The rope allows you to draw a texture across several points and them manipulate these points @@ -29,15 +30,19 @@ */ constructor(texture, points) { - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } const geometry = new Geometry(); geometry.addAttribute('aVertexPosition', new Float32Array(points.length * 4), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) - geometry.addIndex(new Uint16Array(points.length * 2)) + .addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) + .addIndex(new Uint16Array(points.length * 2)); - super(geometry, meshShader, 5); + super(geometry, meshShader, {}, 5); this.texture = texture; @@ -77,7 +82,7 @@ const vertices = this.geometry.getAttribute('aVertexPosition').data; const uvs = this.geometry.getAttribute('aTextureCoord').data; - //TODO - lets make this more accessable... maybe a getIndx()? + // TODO - lets make this more accessable... maybe a getIndx()? const indices = this.geometry.data.indexBuffer.data; // if too little points, or texture hasn't got UVs set yet just move on. @@ -132,8 +137,6 @@ this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); this.geometry.data.indexBuffer.update(); - - // console.log(this.geometry.data) } /** @@ -143,8 +146,6 @@ */ _onTextureUpdate() { - //super._onTextureUpdate(); - // wait for the Rope ctor to finish before calling refresh if (this._ready) { @@ -218,7 +219,7 @@ } this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.uSampler2 = this.texture; this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); @@ -226,8 +227,8 @@ _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 58e4db8..e826b78 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -6,126 +6,111 @@ export default class State { - constructor(data) - { - this.data = 0; - - this.blendMode = 0; - this.polygonOffset = 0; - } - - set blend( value ) - { - if( !!(this.data & (1 << BLEND)) !== value ) - { - this.data ^= (1 << BLEND); - } - } - - get blend() - { - return !!(this.data & (1 << BLEND)) - } - - //////////// - - set offsets( value ) - { - if( !!(this.data & (1 << OFFSET)) !== value ) - { - this.data ^= (1< b.name) ? 1 : -1; - }); + attributesArray.sort((a, b) => (a.name > b.name) ? 1 : -1); // eslint-disable-line no-confusing-arrow const attributes = {}; @@ -65,6 +62,7 @@ for (let i = 0; i < attributesArray.length; i++) { const attrib = attributesArray[i]; + attrib.location = i; attributes[attrib.name] = attrib; } diff --git a/src/core/shader/generateUniformsSync.js b/src/core/shader/generateUniformsSync.js index e2b719a..e9995d9 100644 --- a/src/core/shader/generateUniformsSync.js +++ b/src/core/shader/generateUniformsSync.js @@ -1,83 +1,79 @@ +const GLSL_TO_SINGLE_SETTERS_CACHED = { - -var generateUniformsSync = function(uniformData) -{ - - var textureCount = 1; - var func = 'var value = null; var cacheValue = null' - - for (var i in uniformData) - { - let data = uniformData[i]; - - // TODO && uniformData[i].value !== 0 <-- do we still need this? - if(data.type === 'sampler2D') - { - func += '\ngl.uniform1iv(uniformData.'+i+'.location, renderer.bindTexture(uniformValues.' + i + ', ' + (textureCount++) + ', true) );\n' - } - else if (data.type === 'mat3') - { - func += '\nvalue = uniformValues.' + i + ';'; - func += '\ngl.uniformMatrix3fv(uniformData.'+i+'.location, false, (value.a !== undefined) ? value : value.toArray(true));\n' - } - else if (data.type === 'vec2') - { - func += '\nvalue = uniformValues.' + i + ';\n'; - func += '\nif(value.x !== undefined)gl.uniform2f(uniformData.'+i+'.location, value.x, value.y);' - func += '\nelse gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n' - - } - else - { - var template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', 'uniformData.'+i+'.location'); - func += '\ncacheValue = uniformData.' + i + '.value;\n'; - func += 'value = uniformValues.' + i + ';\n'; - func += template +'\n'; - } - - }; - - return new Function('uniformData', 'uniformValues', 'gl', func); -} - -var GLSL_TO_SINGLE_SETTERS_CACHED = { - - 'float': `if(cacheValue !== value) + float: `if(cacheValue !== value) { cacheValue = value; gl.uniform1f(location, value) }`, - 'vec2': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) + vec2: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; gl.uniform2f(location, value[0], value[1]) }`, - 'vec3': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) + vec3: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; cacheValue[2] = value[2]; gl.uniform3f(location, value[0], value[1], value[2]) }`, - 'vec4': 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', + vec4: 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', - 'int': 'gl.uniform1i(location, value)', - 'ivec2': 'gl.uniform2i(location, value[0], value[1])', - 'ivec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'ivec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + int: 'gl.uniform1i(location, value)', + ivec2: 'gl.uniform2i(location, value[0], value[1])', + ivec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + ivec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'bool': 'gl.uniform1i(location, value)', - 'bvec2': 'gl.uniform2i(location, value[0], value[1])', - 'bvec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'bvec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + bool: 'gl.uniform1i(location, value)', + bvec2: 'gl.uniform2i(location, value[0], value[1])', + bvec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + bvec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'mat2': 'gl.uniformMatrix2fv(location, false, value)', - 'mat3': 'gl.uniformMatrix3fv(location, false, value)', - 'mat4': 'gl.uniformMatrix4fv(location, false, value)', + mat2: 'gl.uniformMatrix2fv(location, false, value)', + mat3: 'gl.uniformMatrix3fv(location, false, value)', + mat4: 'gl.uniformMatrix4fv(location, false, value)', - 'sampler2D':'uniform1i(location, value)' + sampler2D: 'uniform1i(location, value)', }; -module.exports = generateUniformsSync; +export default function generateUniformsSync(uniformData) +{ + let textureCount = 1; + let func = 'var value = null; var cacheValue = null'; + + for (const i in uniformData) + { + const data = uniformData[i]; + + // TODO && uniformData[i].value !== 0 <-- do we still need this? + if (data.type === 'sampler2D') + { + func += `\ngl.uniform1iv(uniformData.${i}.location, renderer.bindTexture(uniformValues.${i}, ${textureCount++}, true) );\n`; // eslint-disable-line max-len + } + else if (data.type === 'mat3') + { + func += `\nvalue = uniformValues.${i}; +gl.uniformMatrix3fv(uniformData.${i}.location, false, (value.a !== undefined) ? value : value.toArray(true));\n`; + } + else if (data.type === 'vec2') + { + func += `\nvalue = uniformValues.${i}; +if(value.x !== undefined)gl.uniform2f(uniformData.${i}.location, value.x, value.y); +else gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n`; + } + else + { + const template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', `uniformData.${i}.location`); + + func += `\ncacheValue = uniformData.${i}.value; +value = uniformValues.${i}; +${template};\n`; + } + } + + // console.log(func); + + return new Function('uniformData', 'uniformValues', 'gl', func); // eslint-disable-line no-new-func +} diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 4495081..1c7ad70 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -108,10 +108,8 @@ // step 2: check the maximum number of if statements the shader can have too.. this.MAX_TEXTURES = checkMaxIfStatmentsInShader(this.MAX_TEXTURES, gl); - console.log(this.MAX_TEXTURE) const shader = this.shader = generateMultiTextureShader(gl, this.MAX_TEXTURES); - console.log(shader) // create a couple of buffers this.indexBuffer = glCore.GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); @@ -123,8 +121,6 @@ for (let i = 0; i < this.vaoMax; i++) { this.vertexBuffers[i] = glCore.GLBuffer.createVertexBuffer(gl, null, gl.STREAM_DRAW); - console.log(this.vertexBuffers) - console.log(shader.attributes) /* eslint-disable max-len */ // build the vao object that will render.. @@ -132,11 +128,13 @@ .addIndex(this.indexBuffer) .addAttribute(this.vertexBuffers[i], shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) .addAttribute(this.vertexBuffers[i], shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) - if(shader.attributes.aTextureId) - { - this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); - } + .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); + + if (shader.attributes.aTextureId) + { + this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); + } + /* eslint-disable max-len */ } diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index 8d8c31c..0b07d77 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -15,7 +15,6 @@ */ constructor(geometry, shader, uniforms, state, drawMode = core.DRAW_MODES.TRIANGLES) { - //drawMode = core.DRAW_MODES.TRIANGLES, uniforms = {}) super(); /** @@ -43,15 +42,12 @@ * @member {number} * @see PIXI.mesh.Mesh.DRAW_MODES */ - this.drawMode = 4//drawMode; + this.drawMode = drawMode; + // TODO uniform auto generation? + // make sure to add required feilds this.uniforms = uniforms; -// geometry// -// shader// -// uniforms// -// state// - /** * The tint applied to the mesh. This is a [r,g,b] value. A value of [1,1,1] will remove any * tint effect. diff --git a/src/mesh/Plane.js b/src/mesh/Plane.js index 9d2aa67..4ad7ef5 100644 --- a/src/mesh/Plane.js +++ b/src/mesh/Plane.js @@ -5,7 +5,7 @@ import { join } from 'path'; let meshShader; -const temp = [0,0,0]; +const temp = [0, 0, 0]; /** * The Plane allows you to draw a texture across several points and them manipulate these points @@ -33,11 +33,15 @@ { const geometry = new Geometry(); - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } geometry.addAttribute('aVertexPosition', new Float32Array(2), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(2), 2) - geometry.addIndex(new Uint16Array(2)) + .addAttribute('aTextureCoord', new Float32Array(2), 2) + .addIndex(new Uint16Array(2)); super(geometry, meshShader, 4); @@ -69,7 +73,7 @@ this.refresh(); - this.tint; + this.tint = 0xFFFFFF; } /** @@ -80,7 +84,6 @@ { const total = this.verticesX * this.verticesY; const verts = []; - const colors = []; const uvs = []; const indices = []; const texture = this.texture; @@ -113,8 +116,6 @@ } } - // cons - const totalSub = segmentsX * segmentsY; for (let i = 0; i < totalSub; i++) @@ -131,24 +132,21 @@ indices.push(value2, value4, value3); } - // console.log(indices) this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// - + this.shader.uniforms.uSampler2 = this.texture; this.vertices = new Float32Array(verts); this.uvs = new Float32Array(uvs); this.indices = new Uint16Array(indices); - this.geometry.getAttribute('aVertexPosition').data = this.vertices; - this.geometry.getAttribute('aTextureCoord').data = this.uvs; - this.geometry.data.indexBuffer.data = this.indices; + this.geometry.getAttribute('aVertexPosition').data = this.vertices; + this.geometry.getAttribute('aTextureCoord').data = this.uvs; + this.geometry.data.indexBuffer.data = this.indices; - // ensure that the changes are uploaded + // ensure that the changes are uploaded this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); - this.geometry.data.indexBuffer.update();// - + this.geometry.data.indexBuffer.update(); } /** @@ -165,10 +163,10 @@ } } - _renderWebGL(renderer) + _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); @@ -176,11 +174,7 @@ updateTransform() { - this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); } - - - } diff --git a/src/mesh/Rope.js b/src/mesh/Rope.js index 1b167ee..0589890 100644 --- a/src/mesh/Rope.js +++ b/src/mesh/Rope.js @@ -5,6 +5,7 @@ import { join } from 'path'; let meshShader; +const temp = new Float32Array([0, 0, 0]); /** * The rope allows you to draw a texture across several points and them manipulate these points @@ -29,15 +30,19 @@ */ constructor(texture, points) { - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } const geometry = new Geometry(); geometry.addAttribute('aVertexPosition', new Float32Array(points.length * 4), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) - geometry.addIndex(new Uint16Array(points.length * 2)) + .addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) + .addIndex(new Uint16Array(points.length * 2)); - super(geometry, meshShader, 5); + super(geometry, meshShader, {}, 5); this.texture = texture; @@ -77,7 +82,7 @@ const vertices = this.geometry.getAttribute('aVertexPosition').data; const uvs = this.geometry.getAttribute('aTextureCoord').data; - //TODO - lets make this more accessable... maybe a getIndx()? + // TODO - lets make this more accessable... maybe a getIndx()? const indices = this.geometry.data.indexBuffer.data; // if too little points, or texture hasn't got UVs set yet just move on. @@ -132,8 +137,6 @@ this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); this.geometry.data.indexBuffer.update(); - - // console.log(this.geometry.data) } /** @@ -143,8 +146,6 @@ */ _onTextureUpdate() { - //super._onTextureUpdate(); - // wait for the Rope ctor to finish before calling refresh if (this._ready) { @@ -218,7 +219,7 @@ } this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.uSampler2 = this.texture; this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); @@ -226,8 +227,8 @@ _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); diff --git a/src/mesh/geometry/Attribute.js b/src/mesh/geometry/Attribute.js index 2c6516c..d07085f 100644 --- a/src/mesh/geometry/Attribute.js +++ b/src/mesh/geometry/Attribute.js @@ -1,26 +1,24 @@ class Attribute { + constructor(buffer, size = 2, stride = 0, start = 0, normalised = false) + { + this.buffer = buffer; + this.normalized = normalised; + this.size = size; + this.stride = stride; + this.start = start; + this.type = null; + } - constructor(buffer, size = 2, stride = 0, start = 0, normalised = false) - { - this.buffer = buffer; - this.normalized = normalised; - this.size = size; - this.stride = stride; - this.start = start; - this.type = null; - } + destroy() + { + this.buffer = null; + } - destroy() - { - this.buffer = null; - } - + static from(buffer, stride, start, normalised) + { + return new Attribute(buffer, stride, start, normalised); + } } -Attribute.from = function (buffer, stride, start, normalised) -{ - return new Attribute(buffer, stride, start, normalised); -}; - module.exports = Attribute; diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 58e4db8..e826b78 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -6,126 +6,111 @@ export default class State { - constructor(data) - { - this.data = 0; - - this.blendMode = 0; - this.polygonOffset = 0; - } - - set blend( value ) - { - if( !!(this.data & (1 << BLEND)) !== value ) - { - this.data ^= (1 << BLEND); - } - } - - get blend() - { - return !!(this.data & (1 << BLEND)) - } - - //////////// - - set offsets( value ) - { - if( !!(this.data & (1 << OFFSET)) !== value ) - { - this.data ^= (1< b.name) ? 1 : -1; - }); + attributesArray.sort((a, b) => (a.name > b.name) ? 1 : -1); // eslint-disable-line no-confusing-arrow const attributes = {}; @@ -65,6 +62,7 @@ for (let i = 0; i < attributesArray.length; i++) { const attrib = attributesArray[i]; + attrib.location = i; attributes[attrib.name] = attrib; } diff --git a/src/core/shader/generateUniformsSync.js b/src/core/shader/generateUniformsSync.js index e2b719a..e9995d9 100644 --- a/src/core/shader/generateUniformsSync.js +++ b/src/core/shader/generateUniformsSync.js @@ -1,83 +1,79 @@ +const GLSL_TO_SINGLE_SETTERS_CACHED = { - -var generateUniformsSync = function(uniformData) -{ - - var textureCount = 1; - var func = 'var value = null; var cacheValue = null' - - for (var i in uniformData) - { - let data = uniformData[i]; - - // TODO && uniformData[i].value !== 0 <-- do we still need this? - if(data.type === 'sampler2D') - { - func += '\ngl.uniform1iv(uniformData.'+i+'.location, renderer.bindTexture(uniformValues.' + i + ', ' + (textureCount++) + ', true) );\n' - } - else if (data.type === 'mat3') - { - func += '\nvalue = uniformValues.' + i + ';'; - func += '\ngl.uniformMatrix3fv(uniformData.'+i+'.location, false, (value.a !== undefined) ? value : value.toArray(true));\n' - } - else if (data.type === 'vec2') - { - func += '\nvalue = uniformValues.' + i + ';\n'; - func += '\nif(value.x !== undefined)gl.uniform2f(uniformData.'+i+'.location, value.x, value.y);' - func += '\nelse gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n' - - } - else - { - var template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', 'uniformData.'+i+'.location'); - func += '\ncacheValue = uniformData.' + i + '.value;\n'; - func += 'value = uniformValues.' + i + ';\n'; - func += template +'\n'; - } - - }; - - return new Function('uniformData', 'uniformValues', 'gl', func); -} - -var GLSL_TO_SINGLE_SETTERS_CACHED = { - - 'float': `if(cacheValue !== value) + float: `if(cacheValue !== value) { cacheValue = value; gl.uniform1f(location, value) }`, - 'vec2': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) + vec2: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; gl.uniform2f(location, value[0], value[1]) }`, - 'vec3': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) + vec3: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; cacheValue[2] = value[2]; gl.uniform3f(location, value[0], value[1], value[2]) }`, - 'vec4': 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', + vec4: 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', - 'int': 'gl.uniform1i(location, value)', - 'ivec2': 'gl.uniform2i(location, value[0], value[1])', - 'ivec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'ivec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + int: 'gl.uniform1i(location, value)', + ivec2: 'gl.uniform2i(location, value[0], value[1])', + ivec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + ivec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'bool': 'gl.uniform1i(location, value)', - 'bvec2': 'gl.uniform2i(location, value[0], value[1])', - 'bvec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'bvec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + bool: 'gl.uniform1i(location, value)', + bvec2: 'gl.uniform2i(location, value[0], value[1])', + bvec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + bvec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'mat2': 'gl.uniformMatrix2fv(location, false, value)', - 'mat3': 'gl.uniformMatrix3fv(location, false, value)', - 'mat4': 'gl.uniformMatrix4fv(location, false, value)', + mat2: 'gl.uniformMatrix2fv(location, false, value)', + mat3: 'gl.uniformMatrix3fv(location, false, value)', + mat4: 'gl.uniformMatrix4fv(location, false, value)', - 'sampler2D':'uniform1i(location, value)' + sampler2D: 'uniform1i(location, value)', }; -module.exports = generateUniformsSync; +export default function generateUniformsSync(uniformData) +{ + let textureCount = 1; + let func = 'var value = null; var cacheValue = null'; + + for (const i in uniformData) + { + const data = uniformData[i]; + + // TODO && uniformData[i].value !== 0 <-- do we still need this? + if (data.type === 'sampler2D') + { + func += `\ngl.uniform1iv(uniformData.${i}.location, renderer.bindTexture(uniformValues.${i}, ${textureCount++}, true) );\n`; // eslint-disable-line max-len + } + else if (data.type === 'mat3') + { + func += `\nvalue = uniformValues.${i}; +gl.uniformMatrix3fv(uniformData.${i}.location, false, (value.a !== undefined) ? value : value.toArray(true));\n`; + } + else if (data.type === 'vec2') + { + func += `\nvalue = uniformValues.${i}; +if(value.x !== undefined)gl.uniform2f(uniformData.${i}.location, value.x, value.y); +else gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n`; + } + else + { + const template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', `uniformData.${i}.location`); + + func += `\ncacheValue = uniformData.${i}.value; +value = uniformValues.${i}; +${template};\n`; + } + } + + // console.log(func); + + return new Function('uniformData', 'uniformValues', 'gl', func); // eslint-disable-line no-new-func +} diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 4495081..1c7ad70 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -108,10 +108,8 @@ // step 2: check the maximum number of if statements the shader can have too.. this.MAX_TEXTURES = checkMaxIfStatmentsInShader(this.MAX_TEXTURES, gl); - console.log(this.MAX_TEXTURE) const shader = this.shader = generateMultiTextureShader(gl, this.MAX_TEXTURES); - console.log(shader) // create a couple of buffers this.indexBuffer = glCore.GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); @@ -123,8 +121,6 @@ for (let i = 0; i < this.vaoMax; i++) { this.vertexBuffers[i] = glCore.GLBuffer.createVertexBuffer(gl, null, gl.STREAM_DRAW); - console.log(this.vertexBuffers) - console.log(shader.attributes) /* eslint-disable max-len */ // build the vao object that will render.. @@ -132,11 +128,13 @@ .addIndex(this.indexBuffer) .addAttribute(this.vertexBuffers[i], shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) .addAttribute(this.vertexBuffers[i], shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) - if(shader.attributes.aTextureId) - { - this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); - } + .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); + + if (shader.attributes.aTextureId) + { + this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); + } + /* eslint-disable max-len */ } diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index 8d8c31c..0b07d77 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -15,7 +15,6 @@ */ constructor(geometry, shader, uniforms, state, drawMode = core.DRAW_MODES.TRIANGLES) { - //drawMode = core.DRAW_MODES.TRIANGLES, uniforms = {}) super(); /** @@ -43,15 +42,12 @@ * @member {number} * @see PIXI.mesh.Mesh.DRAW_MODES */ - this.drawMode = 4//drawMode; + this.drawMode = drawMode; + // TODO uniform auto generation? + // make sure to add required feilds this.uniforms = uniforms; -// geometry// -// shader// -// uniforms// -// state// - /** * The tint applied to the mesh. This is a [r,g,b] value. A value of [1,1,1] will remove any * tint effect. diff --git a/src/mesh/Plane.js b/src/mesh/Plane.js index 9d2aa67..4ad7ef5 100644 --- a/src/mesh/Plane.js +++ b/src/mesh/Plane.js @@ -5,7 +5,7 @@ import { join } from 'path'; let meshShader; -const temp = [0,0,0]; +const temp = [0, 0, 0]; /** * The Plane allows you to draw a texture across several points and them manipulate these points @@ -33,11 +33,15 @@ { const geometry = new Geometry(); - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } geometry.addAttribute('aVertexPosition', new Float32Array(2), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(2), 2) - geometry.addIndex(new Uint16Array(2)) + .addAttribute('aTextureCoord', new Float32Array(2), 2) + .addIndex(new Uint16Array(2)); super(geometry, meshShader, 4); @@ -69,7 +73,7 @@ this.refresh(); - this.tint; + this.tint = 0xFFFFFF; } /** @@ -80,7 +84,6 @@ { const total = this.verticesX * this.verticesY; const verts = []; - const colors = []; const uvs = []; const indices = []; const texture = this.texture; @@ -113,8 +116,6 @@ } } - // cons - const totalSub = segmentsX * segmentsY; for (let i = 0; i < totalSub; i++) @@ -131,24 +132,21 @@ indices.push(value2, value4, value3); } - // console.log(indices) this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// - + this.shader.uniforms.uSampler2 = this.texture; this.vertices = new Float32Array(verts); this.uvs = new Float32Array(uvs); this.indices = new Uint16Array(indices); - this.geometry.getAttribute('aVertexPosition').data = this.vertices; - this.geometry.getAttribute('aTextureCoord').data = this.uvs; - this.geometry.data.indexBuffer.data = this.indices; + this.geometry.getAttribute('aVertexPosition').data = this.vertices; + this.geometry.getAttribute('aTextureCoord').data = this.uvs; + this.geometry.data.indexBuffer.data = this.indices; - // ensure that the changes are uploaded + // ensure that the changes are uploaded this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); - this.geometry.data.indexBuffer.update();// - + this.geometry.data.indexBuffer.update(); } /** @@ -165,10 +163,10 @@ } } - _renderWebGL(renderer) + _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); @@ -176,11 +174,7 @@ updateTransform() { - this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); } - - - } diff --git a/src/mesh/Rope.js b/src/mesh/Rope.js index 1b167ee..0589890 100644 --- a/src/mesh/Rope.js +++ b/src/mesh/Rope.js @@ -5,6 +5,7 @@ import { join } from 'path'; let meshShader; +const temp = new Float32Array([0, 0, 0]); /** * The rope allows you to draw a texture across several points and them manipulate these points @@ -29,15 +30,19 @@ */ constructor(texture, points) { - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } const geometry = new Geometry(); geometry.addAttribute('aVertexPosition', new Float32Array(points.length * 4), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) - geometry.addIndex(new Uint16Array(points.length * 2)) + .addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) + .addIndex(new Uint16Array(points.length * 2)); - super(geometry, meshShader, 5); + super(geometry, meshShader, {}, 5); this.texture = texture; @@ -77,7 +82,7 @@ const vertices = this.geometry.getAttribute('aVertexPosition').data; const uvs = this.geometry.getAttribute('aTextureCoord').data; - //TODO - lets make this more accessable... maybe a getIndx()? + // TODO - lets make this more accessable... maybe a getIndx()? const indices = this.geometry.data.indexBuffer.data; // if too little points, or texture hasn't got UVs set yet just move on. @@ -132,8 +137,6 @@ this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); this.geometry.data.indexBuffer.update(); - - // console.log(this.geometry.data) } /** @@ -143,8 +146,6 @@ */ _onTextureUpdate() { - //super._onTextureUpdate(); - // wait for the Rope ctor to finish before calling refresh if (this._ready) { @@ -218,7 +219,7 @@ } this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.uSampler2 = this.texture; this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); @@ -226,8 +227,8 @@ _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); diff --git a/src/mesh/geometry/Attribute.js b/src/mesh/geometry/Attribute.js index 2c6516c..d07085f 100644 --- a/src/mesh/geometry/Attribute.js +++ b/src/mesh/geometry/Attribute.js @@ -1,26 +1,24 @@ class Attribute { + constructor(buffer, size = 2, stride = 0, start = 0, normalised = false) + { + this.buffer = buffer; + this.normalized = normalised; + this.size = size; + this.stride = stride; + this.start = start; + this.type = null; + } - constructor(buffer, size = 2, stride = 0, start = 0, normalised = false) - { - this.buffer = buffer; - this.normalized = normalised; - this.size = size; - this.stride = stride; - this.start = start; - this.type = null; - } + destroy() + { + this.buffer = null; + } - destroy() - { - this.buffer = null; - } - + static from(buffer, stride, start, normalised) + { + return new Attribute(buffer, stride, start, normalised); + } } -Attribute.from = function (buffer, stride, start, normalised) -{ - return new Attribute(buffer, stride, start, normalised); -}; - module.exports = Attribute; diff --git a/src/mesh/geometry/Buffer.js b/src/mesh/geometry/Buffer.js index 9ff6889..72c361b 100644 --- a/src/mesh/geometry/Buffer.js +++ b/src/mesh/geometry/Buffer.js @@ -11,27 +11,27 @@ */ export default class Buffer { - constructor(data) - { - /** - * The type of the buffer - * - * @member {gl.ARRAY_BUFFER|gl.ELEMENT_ARRAY_BUFFER} - */ - // this.type = type || gl.ARRAY_BUFFER; + constructor(data) + { + /** + * The type of the buffer + * + * @member {gl.ARRAY_BUFFER|gl.ELEMENT_ARRAY_BUFFER} + */ + // this.type = type || gl.ARRAY_BUFFER; - /** - * The draw type of the buffer - * - * @member {gl.STATIC_DRAW|gl.DYNAMIC_DRAW|gl.STREAM_DRAW} - */ - // this.drawType = drawType || gl.STATIC_DRAW; + /** + * The draw type of the buffer + * + * @member {gl.STATIC_DRAW|gl.DYNAMIC_DRAW|gl.STREAM_DRAW} + */ + // this.drawType = drawType || gl.STATIC_DRAW; - /** - * The data in the buffer, as a typed array - * - * @member {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} - */ + /** + * The data in the buffer, as a typed array + * + * @member {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} + */ this.data = data; this._glBuffers = []; @@ -39,35 +39,35 @@ this._updateID = 0; this.id = UID++; - } + } - /** - * Uploads the buffer to the GPU - * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data to upload - * @param offset {Number} if only a subset of the data should be uploaded, this is the amount of data to subtract - */ - update() - { - this._updateID++; - } + /** + * Uploads the buffer to the GPU + * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data to upload + * @param offset {Number} if only a subset of the data should be uploaded, this is the amount of data to subtract + */ + update() + { + this._updateID++; + } - /** - * Destroys the buffer - * - */ - destroy() - { - for (let i = 0; i < this._glBuffers.length; i++) - { - this._glBuffers[i].destroy(); - } + /** + * Destroys the buffer + * + */ + destroy() + { + for (let i = 0; i < this._glBuffers.length; i++) + { + this._glBuffers[i].destroy(); + } - this.data = null; - } + this.data = null; + } - static from(data) - { - return new Buffer(data); - } + static from(data) + { + return new Buffer(data); + } } diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 58e4db8..e826b78 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -6,126 +6,111 @@ export default class State { - constructor(data) - { - this.data = 0; - - this.blendMode = 0; - this.polygonOffset = 0; - } - - set blend( value ) - { - if( !!(this.data & (1 << BLEND)) !== value ) - { - this.data ^= (1 << BLEND); - } - } - - get blend() - { - return !!(this.data & (1 << BLEND)) - } - - //////////// - - set offsets( value ) - { - if( !!(this.data & (1 << OFFSET)) !== value ) - { - this.data ^= (1< b.name) ? 1 : -1; - }); + attributesArray.sort((a, b) => (a.name > b.name) ? 1 : -1); // eslint-disable-line no-confusing-arrow const attributes = {}; @@ -65,6 +62,7 @@ for (let i = 0; i < attributesArray.length; i++) { const attrib = attributesArray[i]; + attrib.location = i; attributes[attrib.name] = attrib; } diff --git a/src/core/shader/generateUniformsSync.js b/src/core/shader/generateUniformsSync.js index e2b719a..e9995d9 100644 --- a/src/core/shader/generateUniformsSync.js +++ b/src/core/shader/generateUniformsSync.js @@ -1,83 +1,79 @@ +const GLSL_TO_SINGLE_SETTERS_CACHED = { - -var generateUniformsSync = function(uniformData) -{ - - var textureCount = 1; - var func = 'var value = null; var cacheValue = null' - - for (var i in uniformData) - { - let data = uniformData[i]; - - // TODO && uniformData[i].value !== 0 <-- do we still need this? - if(data.type === 'sampler2D') - { - func += '\ngl.uniform1iv(uniformData.'+i+'.location, renderer.bindTexture(uniformValues.' + i + ', ' + (textureCount++) + ', true) );\n' - } - else if (data.type === 'mat3') - { - func += '\nvalue = uniformValues.' + i + ';'; - func += '\ngl.uniformMatrix3fv(uniformData.'+i+'.location, false, (value.a !== undefined) ? value : value.toArray(true));\n' - } - else if (data.type === 'vec2') - { - func += '\nvalue = uniformValues.' + i + ';\n'; - func += '\nif(value.x !== undefined)gl.uniform2f(uniformData.'+i+'.location, value.x, value.y);' - func += '\nelse gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n' - - } - else - { - var template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', 'uniformData.'+i+'.location'); - func += '\ncacheValue = uniformData.' + i + '.value;\n'; - func += 'value = uniformValues.' + i + ';\n'; - func += template +'\n'; - } - - }; - - return new Function('uniformData', 'uniformValues', 'gl', func); -} - -var GLSL_TO_SINGLE_SETTERS_CACHED = { - - 'float': `if(cacheValue !== value) + float: `if(cacheValue !== value) { cacheValue = value; gl.uniform1f(location, value) }`, - 'vec2': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) + vec2: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; gl.uniform2f(location, value[0], value[1]) }`, - 'vec3': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) + vec3: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; cacheValue[2] = value[2]; gl.uniform3f(location, value[0], value[1], value[2]) }`, - 'vec4': 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', + vec4: 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', - 'int': 'gl.uniform1i(location, value)', - 'ivec2': 'gl.uniform2i(location, value[0], value[1])', - 'ivec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'ivec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + int: 'gl.uniform1i(location, value)', + ivec2: 'gl.uniform2i(location, value[0], value[1])', + ivec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + ivec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'bool': 'gl.uniform1i(location, value)', - 'bvec2': 'gl.uniform2i(location, value[0], value[1])', - 'bvec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'bvec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + bool: 'gl.uniform1i(location, value)', + bvec2: 'gl.uniform2i(location, value[0], value[1])', + bvec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + bvec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'mat2': 'gl.uniformMatrix2fv(location, false, value)', - 'mat3': 'gl.uniformMatrix3fv(location, false, value)', - 'mat4': 'gl.uniformMatrix4fv(location, false, value)', + mat2: 'gl.uniformMatrix2fv(location, false, value)', + mat3: 'gl.uniformMatrix3fv(location, false, value)', + mat4: 'gl.uniformMatrix4fv(location, false, value)', - 'sampler2D':'uniform1i(location, value)' + sampler2D: 'uniform1i(location, value)', }; -module.exports = generateUniformsSync; +export default function generateUniformsSync(uniformData) +{ + let textureCount = 1; + let func = 'var value = null; var cacheValue = null'; + + for (const i in uniformData) + { + const data = uniformData[i]; + + // TODO && uniformData[i].value !== 0 <-- do we still need this? + if (data.type === 'sampler2D') + { + func += `\ngl.uniform1iv(uniformData.${i}.location, renderer.bindTexture(uniformValues.${i}, ${textureCount++}, true) );\n`; // eslint-disable-line max-len + } + else if (data.type === 'mat3') + { + func += `\nvalue = uniformValues.${i}; +gl.uniformMatrix3fv(uniformData.${i}.location, false, (value.a !== undefined) ? value : value.toArray(true));\n`; + } + else if (data.type === 'vec2') + { + func += `\nvalue = uniformValues.${i}; +if(value.x !== undefined)gl.uniform2f(uniformData.${i}.location, value.x, value.y); +else gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n`; + } + else + { + const template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', `uniformData.${i}.location`); + + func += `\ncacheValue = uniformData.${i}.value; +value = uniformValues.${i}; +${template};\n`; + } + } + + // console.log(func); + + return new Function('uniformData', 'uniformValues', 'gl', func); // eslint-disable-line no-new-func +} diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 4495081..1c7ad70 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -108,10 +108,8 @@ // step 2: check the maximum number of if statements the shader can have too.. this.MAX_TEXTURES = checkMaxIfStatmentsInShader(this.MAX_TEXTURES, gl); - console.log(this.MAX_TEXTURE) const shader = this.shader = generateMultiTextureShader(gl, this.MAX_TEXTURES); - console.log(shader) // create a couple of buffers this.indexBuffer = glCore.GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); @@ -123,8 +121,6 @@ for (let i = 0; i < this.vaoMax; i++) { this.vertexBuffers[i] = glCore.GLBuffer.createVertexBuffer(gl, null, gl.STREAM_DRAW); - console.log(this.vertexBuffers) - console.log(shader.attributes) /* eslint-disable max-len */ // build the vao object that will render.. @@ -132,11 +128,13 @@ .addIndex(this.indexBuffer) .addAttribute(this.vertexBuffers[i], shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) .addAttribute(this.vertexBuffers[i], shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) - if(shader.attributes.aTextureId) - { - this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); - } + .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); + + if (shader.attributes.aTextureId) + { + this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); + } + /* eslint-disable max-len */ } diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index 8d8c31c..0b07d77 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -15,7 +15,6 @@ */ constructor(geometry, shader, uniforms, state, drawMode = core.DRAW_MODES.TRIANGLES) { - //drawMode = core.DRAW_MODES.TRIANGLES, uniforms = {}) super(); /** @@ -43,15 +42,12 @@ * @member {number} * @see PIXI.mesh.Mesh.DRAW_MODES */ - this.drawMode = 4//drawMode; + this.drawMode = drawMode; + // TODO uniform auto generation? + // make sure to add required feilds this.uniforms = uniforms; -// geometry// -// shader// -// uniforms// -// state// - /** * The tint applied to the mesh. This is a [r,g,b] value. A value of [1,1,1] will remove any * tint effect. diff --git a/src/mesh/Plane.js b/src/mesh/Plane.js index 9d2aa67..4ad7ef5 100644 --- a/src/mesh/Plane.js +++ b/src/mesh/Plane.js @@ -5,7 +5,7 @@ import { join } from 'path'; let meshShader; -const temp = [0,0,0]; +const temp = [0, 0, 0]; /** * The Plane allows you to draw a texture across several points and them manipulate these points @@ -33,11 +33,15 @@ { const geometry = new Geometry(); - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } geometry.addAttribute('aVertexPosition', new Float32Array(2), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(2), 2) - geometry.addIndex(new Uint16Array(2)) + .addAttribute('aTextureCoord', new Float32Array(2), 2) + .addIndex(new Uint16Array(2)); super(geometry, meshShader, 4); @@ -69,7 +73,7 @@ this.refresh(); - this.tint; + this.tint = 0xFFFFFF; } /** @@ -80,7 +84,6 @@ { const total = this.verticesX * this.verticesY; const verts = []; - const colors = []; const uvs = []; const indices = []; const texture = this.texture; @@ -113,8 +116,6 @@ } } - // cons - const totalSub = segmentsX * segmentsY; for (let i = 0; i < totalSub; i++) @@ -131,24 +132,21 @@ indices.push(value2, value4, value3); } - // console.log(indices) this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// - + this.shader.uniforms.uSampler2 = this.texture; this.vertices = new Float32Array(verts); this.uvs = new Float32Array(uvs); this.indices = new Uint16Array(indices); - this.geometry.getAttribute('aVertexPosition').data = this.vertices; - this.geometry.getAttribute('aTextureCoord').data = this.uvs; - this.geometry.data.indexBuffer.data = this.indices; + this.geometry.getAttribute('aVertexPosition').data = this.vertices; + this.geometry.getAttribute('aTextureCoord').data = this.uvs; + this.geometry.data.indexBuffer.data = this.indices; - // ensure that the changes are uploaded + // ensure that the changes are uploaded this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); - this.geometry.data.indexBuffer.update();// - + this.geometry.data.indexBuffer.update(); } /** @@ -165,10 +163,10 @@ } } - _renderWebGL(renderer) + _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); @@ -176,11 +174,7 @@ updateTransform() { - this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); } - - - } diff --git a/src/mesh/Rope.js b/src/mesh/Rope.js index 1b167ee..0589890 100644 --- a/src/mesh/Rope.js +++ b/src/mesh/Rope.js @@ -5,6 +5,7 @@ import { join } from 'path'; let meshShader; +const temp = new Float32Array([0, 0, 0]); /** * The rope allows you to draw a texture across several points and them manipulate these points @@ -29,15 +30,19 @@ */ constructor(texture, points) { - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } const geometry = new Geometry(); geometry.addAttribute('aVertexPosition', new Float32Array(points.length * 4), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) - geometry.addIndex(new Uint16Array(points.length * 2)) + .addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) + .addIndex(new Uint16Array(points.length * 2)); - super(geometry, meshShader, 5); + super(geometry, meshShader, {}, 5); this.texture = texture; @@ -77,7 +82,7 @@ const vertices = this.geometry.getAttribute('aVertexPosition').data; const uvs = this.geometry.getAttribute('aTextureCoord').data; - //TODO - lets make this more accessable... maybe a getIndx()? + // TODO - lets make this more accessable... maybe a getIndx()? const indices = this.geometry.data.indexBuffer.data; // if too little points, or texture hasn't got UVs set yet just move on. @@ -132,8 +137,6 @@ this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); this.geometry.data.indexBuffer.update(); - - // console.log(this.geometry.data) } /** @@ -143,8 +146,6 @@ */ _onTextureUpdate() { - //super._onTextureUpdate(); - // wait for the Rope ctor to finish before calling refresh if (this._ready) { @@ -218,7 +219,7 @@ } this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.uSampler2 = this.texture; this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); @@ -226,8 +227,8 @@ _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); diff --git a/src/mesh/geometry/Attribute.js b/src/mesh/geometry/Attribute.js index 2c6516c..d07085f 100644 --- a/src/mesh/geometry/Attribute.js +++ b/src/mesh/geometry/Attribute.js @@ -1,26 +1,24 @@ class Attribute { + constructor(buffer, size = 2, stride = 0, start = 0, normalised = false) + { + this.buffer = buffer; + this.normalized = normalised; + this.size = size; + this.stride = stride; + this.start = start; + this.type = null; + } - constructor(buffer, size = 2, stride = 0, start = 0, normalised = false) - { - this.buffer = buffer; - this.normalized = normalised; - this.size = size; - this.stride = stride; - this.start = start; - this.type = null; - } + destroy() + { + this.buffer = null; + } - destroy() - { - this.buffer = null; - } - + static from(buffer, stride, start, normalised) + { + return new Attribute(buffer, stride, start, normalised); + } } -Attribute.from = function (buffer, stride, start, normalised) -{ - return new Attribute(buffer, stride, start, normalised); -}; - module.exports = Attribute; diff --git a/src/mesh/geometry/Buffer.js b/src/mesh/geometry/Buffer.js index 9ff6889..72c361b 100644 --- a/src/mesh/geometry/Buffer.js +++ b/src/mesh/geometry/Buffer.js @@ -11,27 +11,27 @@ */ export default class Buffer { - constructor(data) - { - /** - * The type of the buffer - * - * @member {gl.ARRAY_BUFFER|gl.ELEMENT_ARRAY_BUFFER} - */ - // this.type = type || gl.ARRAY_BUFFER; + constructor(data) + { + /** + * The type of the buffer + * + * @member {gl.ARRAY_BUFFER|gl.ELEMENT_ARRAY_BUFFER} + */ + // this.type = type || gl.ARRAY_BUFFER; - /** - * The draw type of the buffer - * - * @member {gl.STATIC_DRAW|gl.DYNAMIC_DRAW|gl.STREAM_DRAW} - */ - // this.drawType = drawType || gl.STATIC_DRAW; + /** + * The draw type of the buffer + * + * @member {gl.STATIC_DRAW|gl.DYNAMIC_DRAW|gl.STREAM_DRAW} + */ + // this.drawType = drawType || gl.STATIC_DRAW; - /** - * The data in the buffer, as a typed array - * - * @member {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} - */ + /** + * The data in the buffer, as a typed array + * + * @member {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} + */ this.data = data; this._glBuffers = []; @@ -39,35 +39,35 @@ this._updateID = 0; this.id = UID++; - } + } - /** - * Uploads the buffer to the GPU - * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data to upload - * @param offset {Number} if only a subset of the data should be uploaded, this is the amount of data to subtract - */ - update() - { - this._updateID++; - } + /** + * Uploads the buffer to the GPU + * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data to upload + * @param offset {Number} if only a subset of the data should be uploaded, this is the amount of data to subtract + */ + update() + { + this._updateID++; + } - /** - * Destroys the buffer - * - */ - destroy() - { - for (let i = 0; i < this._glBuffers.length; i++) - { - this._glBuffers[i].destroy(); - } + /** + * Destroys the buffer + * + */ + destroy() + { + for (let i = 0; i < this._glBuffers.length; i++) + { + this._glBuffers[i].destroy(); + } - this.data = null; - } + this.data = null; + } - static from(data) - { - return new Buffer(data); - } + static from(data) + { + return new Buffer(data); + } } diff --git a/src/mesh/geometry/Geometry.js b/src/mesh/geometry/Geometry.js index be4ab48..321949c 100644 --- a/src/mesh/geometry/Geometry.js +++ b/src/mesh/geometry/Geometry.js @@ -4,73 +4,74 @@ import GeometryData from './GeometryData'; var UID = 0; + class Geometry { - constructor(data, style) - { - this.style = style || new GeometryStyle(); - this.data = data || new GeometryData(); + constructor(data, style) + { + this.style = style || new GeometryStyle(); + this.data = data || new GeometryData(); - this.glVertexArrayObjects = []; + this.glVertexArrayObjects = []; - this.id = UID++; - } + this.id = UID++; + } - addAttribute(id, buffer, size = 2, stride = 0, start = 0, normalised = false) - { - // check if this is a buffer! - if(!buffer.data) - { - // its an array! - buffer = new Buffer(buffer); - } + addAttribute(id, buffer, size = 2, stride = 0, start = 0, normalised = false) + { + // check if this is a buffer! + if (!buffer.data) + { + // its an array! + buffer = new Buffer(buffer); + } - this.style.addAttribute(id, new Attribute(buffer.id, size, stride, start, normalised)); - this.data.add(buffer.id, buffer); + this.style.addAttribute(id, new Attribute(buffer.id, size, stride, start, normalised)); + this.data.add(buffer.id, buffer); - return this; - } + return this; + } - getAttribute(id) - { - return this.data[ this.style.attributes[id].buffer ]; - } + getAttribute(id) + { + return this.data[this.style.attributes[id].buffer]; + } - addIndex(buffer) - { - if(!buffer.data) - { - // its an array! - buffer = new Buffer(buffer); - } + addIndex(buffer) + { + if (!buffer.data) + { + // its an array! + buffer = new Buffer(buffer); + } - this.data.addIndex(buffer); + this.data.addIndex(buffer); - return this; - } + return this; + } - destroy() - { - //TODO - this is wrong! - for (let i = 0; i < this.buffers.length; i++) - { - this.buffers[i].destroy(); - } + destroy() + { + //TODO - this is wrong! + for (let i = 0; i < this.buffers.length; i++) + { + this.buffers[i].destroy(); + } - this.buffers = null; - this.attributes = null; + this.buffers = null; + this.attributes = null; - for (let i = 0; i < this.glVertexArrayObjects.length; i++) - { - this.glVertexArrayObjects[i].destroy(); - } + for (let i = 0; i < this.glVertexArrayObjects.length; i++) + { + this.glVertexArrayObjects[i].destroy(); + } - this.glVertexArrayObjects = null; + this.glVertexArrayObjects = null; - this.indexBuffer.destroy(); - this.indexBuffer = null; - } + this.indexBuffer.destroy(); + this.indexBuffer = null; + } } export default Geometry; diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 58e4db8..e826b78 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -6,126 +6,111 @@ export default class State { - constructor(data) - { - this.data = 0; - - this.blendMode = 0; - this.polygonOffset = 0; - } - - set blend( value ) - { - if( !!(this.data & (1 << BLEND)) !== value ) - { - this.data ^= (1 << BLEND); - } - } - - get blend() - { - return !!(this.data & (1 << BLEND)) - } - - //////////// - - set offsets( value ) - { - if( !!(this.data & (1 << OFFSET)) !== value ) - { - this.data ^= (1< b.name) ? 1 : -1; - }); + attributesArray.sort((a, b) => (a.name > b.name) ? 1 : -1); // eslint-disable-line no-confusing-arrow const attributes = {}; @@ -65,6 +62,7 @@ for (let i = 0; i < attributesArray.length; i++) { const attrib = attributesArray[i]; + attrib.location = i; attributes[attrib.name] = attrib; } diff --git a/src/core/shader/generateUniformsSync.js b/src/core/shader/generateUniformsSync.js index e2b719a..e9995d9 100644 --- a/src/core/shader/generateUniformsSync.js +++ b/src/core/shader/generateUniformsSync.js @@ -1,83 +1,79 @@ +const GLSL_TO_SINGLE_SETTERS_CACHED = { - -var generateUniformsSync = function(uniformData) -{ - - var textureCount = 1; - var func = 'var value = null; var cacheValue = null' - - for (var i in uniformData) - { - let data = uniformData[i]; - - // TODO && uniformData[i].value !== 0 <-- do we still need this? - if(data.type === 'sampler2D') - { - func += '\ngl.uniform1iv(uniformData.'+i+'.location, renderer.bindTexture(uniformValues.' + i + ', ' + (textureCount++) + ', true) );\n' - } - else if (data.type === 'mat3') - { - func += '\nvalue = uniformValues.' + i + ';'; - func += '\ngl.uniformMatrix3fv(uniformData.'+i+'.location, false, (value.a !== undefined) ? value : value.toArray(true));\n' - } - else if (data.type === 'vec2') - { - func += '\nvalue = uniformValues.' + i + ';\n'; - func += '\nif(value.x !== undefined)gl.uniform2f(uniformData.'+i+'.location, value.x, value.y);' - func += '\nelse gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n' - - } - else - { - var template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', 'uniformData.'+i+'.location'); - func += '\ncacheValue = uniformData.' + i + '.value;\n'; - func += 'value = uniformValues.' + i + ';\n'; - func += template +'\n'; - } - - }; - - return new Function('uniformData', 'uniformValues', 'gl', func); -} - -var GLSL_TO_SINGLE_SETTERS_CACHED = { - - 'float': `if(cacheValue !== value) + float: `if(cacheValue !== value) { cacheValue = value; gl.uniform1f(location, value) }`, - 'vec2': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) + vec2: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; gl.uniform2f(location, value[0], value[1]) }`, - 'vec3': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) + vec3: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; cacheValue[2] = value[2]; gl.uniform3f(location, value[0], value[1], value[2]) }`, - 'vec4': 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', + vec4: 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', - 'int': 'gl.uniform1i(location, value)', - 'ivec2': 'gl.uniform2i(location, value[0], value[1])', - 'ivec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'ivec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + int: 'gl.uniform1i(location, value)', + ivec2: 'gl.uniform2i(location, value[0], value[1])', + ivec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + ivec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'bool': 'gl.uniform1i(location, value)', - 'bvec2': 'gl.uniform2i(location, value[0], value[1])', - 'bvec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'bvec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + bool: 'gl.uniform1i(location, value)', + bvec2: 'gl.uniform2i(location, value[0], value[1])', + bvec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + bvec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'mat2': 'gl.uniformMatrix2fv(location, false, value)', - 'mat3': 'gl.uniformMatrix3fv(location, false, value)', - 'mat4': 'gl.uniformMatrix4fv(location, false, value)', + mat2: 'gl.uniformMatrix2fv(location, false, value)', + mat3: 'gl.uniformMatrix3fv(location, false, value)', + mat4: 'gl.uniformMatrix4fv(location, false, value)', - 'sampler2D':'uniform1i(location, value)' + sampler2D: 'uniform1i(location, value)', }; -module.exports = generateUniformsSync; +export default function generateUniformsSync(uniformData) +{ + let textureCount = 1; + let func = 'var value = null; var cacheValue = null'; + + for (const i in uniformData) + { + const data = uniformData[i]; + + // TODO && uniformData[i].value !== 0 <-- do we still need this? + if (data.type === 'sampler2D') + { + func += `\ngl.uniform1iv(uniformData.${i}.location, renderer.bindTexture(uniformValues.${i}, ${textureCount++}, true) );\n`; // eslint-disable-line max-len + } + else if (data.type === 'mat3') + { + func += `\nvalue = uniformValues.${i}; +gl.uniformMatrix3fv(uniformData.${i}.location, false, (value.a !== undefined) ? value : value.toArray(true));\n`; + } + else if (data.type === 'vec2') + { + func += `\nvalue = uniformValues.${i}; +if(value.x !== undefined)gl.uniform2f(uniformData.${i}.location, value.x, value.y); +else gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n`; + } + else + { + const template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', `uniformData.${i}.location`); + + func += `\ncacheValue = uniformData.${i}.value; +value = uniformValues.${i}; +${template};\n`; + } + } + + // console.log(func); + + return new Function('uniformData', 'uniformValues', 'gl', func); // eslint-disable-line no-new-func +} diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 4495081..1c7ad70 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -108,10 +108,8 @@ // step 2: check the maximum number of if statements the shader can have too.. this.MAX_TEXTURES = checkMaxIfStatmentsInShader(this.MAX_TEXTURES, gl); - console.log(this.MAX_TEXTURE) const shader = this.shader = generateMultiTextureShader(gl, this.MAX_TEXTURES); - console.log(shader) // create a couple of buffers this.indexBuffer = glCore.GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); @@ -123,8 +121,6 @@ for (let i = 0; i < this.vaoMax; i++) { this.vertexBuffers[i] = glCore.GLBuffer.createVertexBuffer(gl, null, gl.STREAM_DRAW); - console.log(this.vertexBuffers) - console.log(shader.attributes) /* eslint-disable max-len */ // build the vao object that will render.. @@ -132,11 +128,13 @@ .addIndex(this.indexBuffer) .addAttribute(this.vertexBuffers[i], shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) .addAttribute(this.vertexBuffers[i], shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) - if(shader.attributes.aTextureId) - { - this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); - } + .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); + + if (shader.attributes.aTextureId) + { + this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); + } + /* eslint-disable max-len */ } diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index 8d8c31c..0b07d77 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -15,7 +15,6 @@ */ constructor(geometry, shader, uniforms, state, drawMode = core.DRAW_MODES.TRIANGLES) { - //drawMode = core.DRAW_MODES.TRIANGLES, uniforms = {}) super(); /** @@ -43,15 +42,12 @@ * @member {number} * @see PIXI.mesh.Mesh.DRAW_MODES */ - this.drawMode = 4//drawMode; + this.drawMode = drawMode; + // TODO uniform auto generation? + // make sure to add required feilds this.uniforms = uniforms; -// geometry// -// shader// -// uniforms// -// state// - /** * The tint applied to the mesh. This is a [r,g,b] value. A value of [1,1,1] will remove any * tint effect. diff --git a/src/mesh/Plane.js b/src/mesh/Plane.js index 9d2aa67..4ad7ef5 100644 --- a/src/mesh/Plane.js +++ b/src/mesh/Plane.js @@ -5,7 +5,7 @@ import { join } from 'path'; let meshShader; -const temp = [0,0,0]; +const temp = [0, 0, 0]; /** * The Plane allows you to draw a texture across several points and them manipulate these points @@ -33,11 +33,15 @@ { const geometry = new Geometry(); - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } geometry.addAttribute('aVertexPosition', new Float32Array(2), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(2), 2) - geometry.addIndex(new Uint16Array(2)) + .addAttribute('aTextureCoord', new Float32Array(2), 2) + .addIndex(new Uint16Array(2)); super(geometry, meshShader, 4); @@ -69,7 +73,7 @@ this.refresh(); - this.tint; + this.tint = 0xFFFFFF; } /** @@ -80,7 +84,6 @@ { const total = this.verticesX * this.verticesY; const verts = []; - const colors = []; const uvs = []; const indices = []; const texture = this.texture; @@ -113,8 +116,6 @@ } } - // cons - const totalSub = segmentsX * segmentsY; for (let i = 0; i < totalSub; i++) @@ -131,24 +132,21 @@ indices.push(value2, value4, value3); } - // console.log(indices) this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// - + this.shader.uniforms.uSampler2 = this.texture; this.vertices = new Float32Array(verts); this.uvs = new Float32Array(uvs); this.indices = new Uint16Array(indices); - this.geometry.getAttribute('aVertexPosition').data = this.vertices; - this.geometry.getAttribute('aTextureCoord').data = this.uvs; - this.geometry.data.indexBuffer.data = this.indices; + this.geometry.getAttribute('aVertexPosition').data = this.vertices; + this.geometry.getAttribute('aTextureCoord').data = this.uvs; + this.geometry.data.indexBuffer.data = this.indices; - // ensure that the changes are uploaded + // ensure that the changes are uploaded this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); - this.geometry.data.indexBuffer.update();// - + this.geometry.data.indexBuffer.update(); } /** @@ -165,10 +163,10 @@ } } - _renderWebGL(renderer) + _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); @@ -176,11 +174,7 @@ updateTransform() { - this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); } - - - } diff --git a/src/mesh/Rope.js b/src/mesh/Rope.js index 1b167ee..0589890 100644 --- a/src/mesh/Rope.js +++ b/src/mesh/Rope.js @@ -5,6 +5,7 @@ import { join } from 'path'; let meshShader; +const temp = new Float32Array([0, 0, 0]); /** * The rope allows you to draw a texture across several points and them manipulate these points @@ -29,15 +30,19 @@ */ constructor(texture, points) { - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } const geometry = new Geometry(); geometry.addAttribute('aVertexPosition', new Float32Array(points.length * 4), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) - geometry.addIndex(new Uint16Array(points.length * 2)) + .addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) + .addIndex(new Uint16Array(points.length * 2)); - super(geometry, meshShader, 5); + super(geometry, meshShader, {}, 5); this.texture = texture; @@ -77,7 +82,7 @@ const vertices = this.geometry.getAttribute('aVertexPosition').data; const uvs = this.geometry.getAttribute('aTextureCoord').data; - //TODO - lets make this more accessable... maybe a getIndx()? + // TODO - lets make this more accessable... maybe a getIndx()? const indices = this.geometry.data.indexBuffer.data; // if too little points, or texture hasn't got UVs set yet just move on. @@ -132,8 +137,6 @@ this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); this.geometry.data.indexBuffer.update(); - - // console.log(this.geometry.data) } /** @@ -143,8 +146,6 @@ */ _onTextureUpdate() { - //super._onTextureUpdate(); - // wait for the Rope ctor to finish before calling refresh if (this._ready) { @@ -218,7 +219,7 @@ } this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.uSampler2 = this.texture; this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); @@ -226,8 +227,8 @@ _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); diff --git a/src/mesh/geometry/Attribute.js b/src/mesh/geometry/Attribute.js index 2c6516c..d07085f 100644 --- a/src/mesh/geometry/Attribute.js +++ b/src/mesh/geometry/Attribute.js @@ -1,26 +1,24 @@ class Attribute { + constructor(buffer, size = 2, stride = 0, start = 0, normalised = false) + { + this.buffer = buffer; + this.normalized = normalised; + this.size = size; + this.stride = stride; + this.start = start; + this.type = null; + } - constructor(buffer, size = 2, stride = 0, start = 0, normalised = false) - { - this.buffer = buffer; - this.normalized = normalised; - this.size = size; - this.stride = stride; - this.start = start; - this.type = null; - } + destroy() + { + this.buffer = null; + } - destroy() - { - this.buffer = null; - } - + static from(buffer, stride, start, normalised) + { + return new Attribute(buffer, stride, start, normalised); + } } -Attribute.from = function (buffer, stride, start, normalised) -{ - return new Attribute(buffer, stride, start, normalised); -}; - module.exports = Attribute; diff --git a/src/mesh/geometry/Buffer.js b/src/mesh/geometry/Buffer.js index 9ff6889..72c361b 100644 --- a/src/mesh/geometry/Buffer.js +++ b/src/mesh/geometry/Buffer.js @@ -11,27 +11,27 @@ */ export default class Buffer { - constructor(data) - { - /** - * The type of the buffer - * - * @member {gl.ARRAY_BUFFER|gl.ELEMENT_ARRAY_BUFFER} - */ - // this.type = type || gl.ARRAY_BUFFER; + constructor(data) + { + /** + * The type of the buffer + * + * @member {gl.ARRAY_BUFFER|gl.ELEMENT_ARRAY_BUFFER} + */ + // this.type = type || gl.ARRAY_BUFFER; - /** - * The draw type of the buffer - * - * @member {gl.STATIC_DRAW|gl.DYNAMIC_DRAW|gl.STREAM_DRAW} - */ - // this.drawType = drawType || gl.STATIC_DRAW; + /** + * The draw type of the buffer + * + * @member {gl.STATIC_DRAW|gl.DYNAMIC_DRAW|gl.STREAM_DRAW} + */ + // this.drawType = drawType || gl.STATIC_DRAW; - /** - * The data in the buffer, as a typed array - * - * @member {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} - */ + /** + * The data in the buffer, as a typed array + * + * @member {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} + */ this.data = data; this._glBuffers = []; @@ -39,35 +39,35 @@ this._updateID = 0; this.id = UID++; - } + } - /** - * Uploads the buffer to the GPU - * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data to upload - * @param offset {Number} if only a subset of the data should be uploaded, this is the amount of data to subtract - */ - update() - { - this._updateID++; - } + /** + * Uploads the buffer to the GPU + * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data to upload + * @param offset {Number} if only a subset of the data should be uploaded, this is the amount of data to subtract + */ + update() + { + this._updateID++; + } - /** - * Destroys the buffer - * - */ - destroy() - { - for (let i = 0; i < this._glBuffers.length; i++) - { - this._glBuffers[i].destroy(); - } + /** + * Destroys the buffer + * + */ + destroy() + { + for (let i = 0; i < this._glBuffers.length; i++) + { + this._glBuffers[i].destroy(); + } - this.data = null; - } + this.data = null; + } - static from(data) - { - return new Buffer(data); - } + static from(data) + { + return new Buffer(data); + } } diff --git a/src/mesh/geometry/Geometry.js b/src/mesh/geometry/Geometry.js index be4ab48..321949c 100644 --- a/src/mesh/geometry/Geometry.js +++ b/src/mesh/geometry/Geometry.js @@ -4,73 +4,74 @@ import GeometryData from './GeometryData'; var UID = 0; + class Geometry { - constructor(data, style) - { - this.style = style || new GeometryStyle(); - this.data = data || new GeometryData(); + constructor(data, style) + { + this.style = style || new GeometryStyle(); + this.data = data || new GeometryData(); - this.glVertexArrayObjects = []; + this.glVertexArrayObjects = []; - this.id = UID++; - } + this.id = UID++; + } - addAttribute(id, buffer, size = 2, stride = 0, start = 0, normalised = false) - { - // check if this is a buffer! - if(!buffer.data) - { - // its an array! - buffer = new Buffer(buffer); - } + addAttribute(id, buffer, size = 2, stride = 0, start = 0, normalised = false) + { + // check if this is a buffer! + if (!buffer.data) + { + // its an array! + buffer = new Buffer(buffer); + } - this.style.addAttribute(id, new Attribute(buffer.id, size, stride, start, normalised)); - this.data.add(buffer.id, buffer); + this.style.addAttribute(id, new Attribute(buffer.id, size, stride, start, normalised)); + this.data.add(buffer.id, buffer); - return this; - } + return this; + } - getAttribute(id) - { - return this.data[ this.style.attributes[id].buffer ]; - } + getAttribute(id) + { + return this.data[this.style.attributes[id].buffer]; + } - addIndex(buffer) - { - if(!buffer.data) - { - // its an array! - buffer = new Buffer(buffer); - } + addIndex(buffer) + { + if (!buffer.data) + { + // its an array! + buffer = new Buffer(buffer); + } - this.data.addIndex(buffer); + this.data.addIndex(buffer); - return this; - } + return this; + } - destroy() - { - //TODO - this is wrong! - for (let i = 0; i < this.buffers.length; i++) - { - this.buffers[i].destroy(); - } + destroy() + { + //TODO - this is wrong! + for (let i = 0; i < this.buffers.length; i++) + { + this.buffers[i].destroy(); + } - this.buffers = null; - this.attributes = null; + this.buffers = null; + this.attributes = null; - for (let i = 0; i < this.glVertexArrayObjects.length; i++) - { - this.glVertexArrayObjects[i].destroy(); - } + for (let i = 0; i < this.glVertexArrayObjects.length; i++) + { + this.glVertexArrayObjects[i].destroy(); + } - this.glVertexArrayObjects = null; + this.glVertexArrayObjects = null; - this.indexBuffer.destroy(); - this.indexBuffer = null; - } + this.indexBuffer.destroy(); + this.indexBuffer = null; + } } export default Geometry; diff --git a/src/mesh/geometry/GeometryData.js b/src/mesh/geometry/GeometryData.js index 28dd0ca..4d988a5 100644 --- a/src/mesh/geometry/GeometryData.js +++ b/src/mesh/geometry/GeometryData.js @@ -1,35 +1,33 @@ -class GeometryData +export default class GeometryData { - constructor() - { - this.buffers = []; - this.indexBuffer = null; - } + constructor() + { + this.buffers = []; + this.indexBuffer = null; + } - add(id, buffer) - { - // only one! - if (this.buffers.indexOf(buffer) === -1) - { - this.buffers.push(buffer); - this[id] = buffer; - } + add(id, buffer) + { + // only one! + if (this.buffers.indexOf(buffer) === -1) + { + this.buffers.push(buffer); + this[id] = buffer; + } - return this; - } + return this; + } - addIndex(buffer) - { - buffer.index = true; - this.indexBuffer = buffer; + addIndex(buffer) + { + buffer.index = true; + this.indexBuffer = buffer; - if (this.buffers.indexOf(buffer) === -1) - { - this.buffers.push(buffer); - } + if (this.buffers.indexOf(buffer) === -1) + { + this.buffers.push(buffer); + } - return this; - } -} - -export default GeometryData; + return this; + } +} \ No newline at end of file diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 58e4db8..e826b78 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -6,126 +6,111 @@ export default class State { - constructor(data) - { - this.data = 0; - - this.blendMode = 0; - this.polygonOffset = 0; - } - - set blend( value ) - { - if( !!(this.data & (1 << BLEND)) !== value ) - { - this.data ^= (1 << BLEND); - } - } - - get blend() - { - return !!(this.data & (1 << BLEND)) - } - - //////////// - - set offsets( value ) - { - if( !!(this.data & (1 << OFFSET)) !== value ) - { - this.data ^= (1< b.name) ? 1 : -1; - }); + attributesArray.sort((a, b) => (a.name > b.name) ? 1 : -1); // eslint-disable-line no-confusing-arrow const attributes = {}; @@ -65,6 +62,7 @@ for (let i = 0; i < attributesArray.length; i++) { const attrib = attributesArray[i]; + attrib.location = i; attributes[attrib.name] = attrib; } diff --git a/src/core/shader/generateUniformsSync.js b/src/core/shader/generateUniformsSync.js index e2b719a..e9995d9 100644 --- a/src/core/shader/generateUniformsSync.js +++ b/src/core/shader/generateUniformsSync.js @@ -1,83 +1,79 @@ +const GLSL_TO_SINGLE_SETTERS_CACHED = { - -var generateUniformsSync = function(uniformData) -{ - - var textureCount = 1; - var func = 'var value = null; var cacheValue = null' - - for (var i in uniformData) - { - let data = uniformData[i]; - - // TODO && uniformData[i].value !== 0 <-- do we still need this? - if(data.type === 'sampler2D') - { - func += '\ngl.uniform1iv(uniformData.'+i+'.location, renderer.bindTexture(uniformValues.' + i + ', ' + (textureCount++) + ', true) );\n' - } - else if (data.type === 'mat3') - { - func += '\nvalue = uniformValues.' + i + ';'; - func += '\ngl.uniformMatrix3fv(uniformData.'+i+'.location, false, (value.a !== undefined) ? value : value.toArray(true));\n' - } - else if (data.type === 'vec2') - { - func += '\nvalue = uniformValues.' + i + ';\n'; - func += '\nif(value.x !== undefined)gl.uniform2f(uniformData.'+i+'.location, value.x, value.y);' - func += '\nelse gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n' - - } - else - { - var template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', 'uniformData.'+i+'.location'); - func += '\ncacheValue = uniformData.' + i + '.value;\n'; - func += 'value = uniformValues.' + i + ';\n'; - func += template +'\n'; - } - - }; - - return new Function('uniformData', 'uniformValues', 'gl', func); -} - -var GLSL_TO_SINGLE_SETTERS_CACHED = { - - 'float': `if(cacheValue !== value) + float: `if(cacheValue !== value) { cacheValue = value; gl.uniform1f(location, value) }`, - 'vec2': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) + vec2: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; gl.uniform2f(location, value[0], value[1]) }`, - 'vec3': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) + vec3: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; cacheValue[2] = value[2]; gl.uniform3f(location, value[0], value[1], value[2]) }`, - 'vec4': 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', + vec4: 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', - 'int': 'gl.uniform1i(location, value)', - 'ivec2': 'gl.uniform2i(location, value[0], value[1])', - 'ivec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'ivec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + int: 'gl.uniform1i(location, value)', + ivec2: 'gl.uniform2i(location, value[0], value[1])', + ivec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + ivec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'bool': 'gl.uniform1i(location, value)', - 'bvec2': 'gl.uniform2i(location, value[0], value[1])', - 'bvec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'bvec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + bool: 'gl.uniform1i(location, value)', + bvec2: 'gl.uniform2i(location, value[0], value[1])', + bvec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + bvec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'mat2': 'gl.uniformMatrix2fv(location, false, value)', - 'mat3': 'gl.uniformMatrix3fv(location, false, value)', - 'mat4': 'gl.uniformMatrix4fv(location, false, value)', + mat2: 'gl.uniformMatrix2fv(location, false, value)', + mat3: 'gl.uniformMatrix3fv(location, false, value)', + mat4: 'gl.uniformMatrix4fv(location, false, value)', - 'sampler2D':'uniform1i(location, value)' + sampler2D: 'uniform1i(location, value)', }; -module.exports = generateUniformsSync; +export default function generateUniformsSync(uniformData) +{ + let textureCount = 1; + let func = 'var value = null; var cacheValue = null'; + + for (const i in uniformData) + { + const data = uniformData[i]; + + // TODO && uniformData[i].value !== 0 <-- do we still need this? + if (data.type === 'sampler2D') + { + func += `\ngl.uniform1iv(uniformData.${i}.location, renderer.bindTexture(uniformValues.${i}, ${textureCount++}, true) );\n`; // eslint-disable-line max-len + } + else if (data.type === 'mat3') + { + func += `\nvalue = uniformValues.${i}; +gl.uniformMatrix3fv(uniformData.${i}.location, false, (value.a !== undefined) ? value : value.toArray(true));\n`; + } + else if (data.type === 'vec2') + { + func += `\nvalue = uniformValues.${i}; +if(value.x !== undefined)gl.uniform2f(uniformData.${i}.location, value.x, value.y); +else gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n`; + } + else + { + const template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', `uniformData.${i}.location`); + + func += `\ncacheValue = uniformData.${i}.value; +value = uniformValues.${i}; +${template};\n`; + } + } + + // console.log(func); + + return new Function('uniformData', 'uniformValues', 'gl', func); // eslint-disable-line no-new-func +} diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 4495081..1c7ad70 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -108,10 +108,8 @@ // step 2: check the maximum number of if statements the shader can have too.. this.MAX_TEXTURES = checkMaxIfStatmentsInShader(this.MAX_TEXTURES, gl); - console.log(this.MAX_TEXTURE) const shader = this.shader = generateMultiTextureShader(gl, this.MAX_TEXTURES); - console.log(shader) // create a couple of buffers this.indexBuffer = glCore.GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); @@ -123,8 +121,6 @@ for (let i = 0; i < this.vaoMax; i++) { this.vertexBuffers[i] = glCore.GLBuffer.createVertexBuffer(gl, null, gl.STREAM_DRAW); - console.log(this.vertexBuffers) - console.log(shader.attributes) /* eslint-disable max-len */ // build the vao object that will render.. @@ -132,11 +128,13 @@ .addIndex(this.indexBuffer) .addAttribute(this.vertexBuffers[i], shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) .addAttribute(this.vertexBuffers[i], shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) - if(shader.attributes.aTextureId) - { - this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); - } + .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); + + if (shader.attributes.aTextureId) + { + this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); + } + /* eslint-disable max-len */ } diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index 8d8c31c..0b07d77 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -15,7 +15,6 @@ */ constructor(geometry, shader, uniforms, state, drawMode = core.DRAW_MODES.TRIANGLES) { - //drawMode = core.DRAW_MODES.TRIANGLES, uniforms = {}) super(); /** @@ -43,15 +42,12 @@ * @member {number} * @see PIXI.mesh.Mesh.DRAW_MODES */ - this.drawMode = 4//drawMode; + this.drawMode = drawMode; + // TODO uniform auto generation? + // make sure to add required feilds this.uniforms = uniforms; -// geometry// -// shader// -// uniforms// -// state// - /** * The tint applied to the mesh. This is a [r,g,b] value. A value of [1,1,1] will remove any * tint effect. diff --git a/src/mesh/Plane.js b/src/mesh/Plane.js index 9d2aa67..4ad7ef5 100644 --- a/src/mesh/Plane.js +++ b/src/mesh/Plane.js @@ -5,7 +5,7 @@ import { join } from 'path'; let meshShader; -const temp = [0,0,0]; +const temp = [0, 0, 0]; /** * The Plane allows you to draw a texture across several points and them manipulate these points @@ -33,11 +33,15 @@ { const geometry = new Geometry(); - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } geometry.addAttribute('aVertexPosition', new Float32Array(2), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(2), 2) - geometry.addIndex(new Uint16Array(2)) + .addAttribute('aTextureCoord', new Float32Array(2), 2) + .addIndex(new Uint16Array(2)); super(geometry, meshShader, 4); @@ -69,7 +73,7 @@ this.refresh(); - this.tint; + this.tint = 0xFFFFFF; } /** @@ -80,7 +84,6 @@ { const total = this.verticesX * this.verticesY; const verts = []; - const colors = []; const uvs = []; const indices = []; const texture = this.texture; @@ -113,8 +116,6 @@ } } - // cons - const totalSub = segmentsX * segmentsY; for (let i = 0; i < totalSub; i++) @@ -131,24 +132,21 @@ indices.push(value2, value4, value3); } - // console.log(indices) this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// - + this.shader.uniforms.uSampler2 = this.texture; this.vertices = new Float32Array(verts); this.uvs = new Float32Array(uvs); this.indices = new Uint16Array(indices); - this.geometry.getAttribute('aVertexPosition').data = this.vertices; - this.geometry.getAttribute('aTextureCoord').data = this.uvs; - this.geometry.data.indexBuffer.data = this.indices; + this.geometry.getAttribute('aVertexPosition').data = this.vertices; + this.geometry.getAttribute('aTextureCoord').data = this.uvs; + this.geometry.data.indexBuffer.data = this.indices; - // ensure that the changes are uploaded + // ensure that the changes are uploaded this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); - this.geometry.data.indexBuffer.update();// - + this.geometry.data.indexBuffer.update(); } /** @@ -165,10 +163,10 @@ } } - _renderWebGL(renderer) + _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); @@ -176,11 +174,7 @@ updateTransform() { - this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); } - - - } diff --git a/src/mesh/Rope.js b/src/mesh/Rope.js index 1b167ee..0589890 100644 --- a/src/mesh/Rope.js +++ b/src/mesh/Rope.js @@ -5,6 +5,7 @@ import { join } from 'path'; let meshShader; +const temp = new Float32Array([0, 0, 0]); /** * The rope allows you to draw a texture across several points and them manipulate these points @@ -29,15 +30,19 @@ */ constructor(texture, points) { - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } const geometry = new Geometry(); geometry.addAttribute('aVertexPosition', new Float32Array(points.length * 4), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) - geometry.addIndex(new Uint16Array(points.length * 2)) + .addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) + .addIndex(new Uint16Array(points.length * 2)); - super(geometry, meshShader, 5); + super(geometry, meshShader, {}, 5); this.texture = texture; @@ -77,7 +82,7 @@ const vertices = this.geometry.getAttribute('aVertexPosition').data; const uvs = this.geometry.getAttribute('aTextureCoord').data; - //TODO - lets make this more accessable... maybe a getIndx()? + // TODO - lets make this more accessable... maybe a getIndx()? const indices = this.geometry.data.indexBuffer.data; // if too little points, or texture hasn't got UVs set yet just move on. @@ -132,8 +137,6 @@ this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); this.geometry.data.indexBuffer.update(); - - // console.log(this.geometry.data) } /** @@ -143,8 +146,6 @@ */ _onTextureUpdate() { - //super._onTextureUpdate(); - // wait for the Rope ctor to finish before calling refresh if (this._ready) { @@ -218,7 +219,7 @@ } this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.uSampler2 = this.texture; this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); @@ -226,8 +227,8 @@ _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); diff --git a/src/mesh/geometry/Attribute.js b/src/mesh/geometry/Attribute.js index 2c6516c..d07085f 100644 --- a/src/mesh/geometry/Attribute.js +++ b/src/mesh/geometry/Attribute.js @@ -1,26 +1,24 @@ class Attribute { + constructor(buffer, size = 2, stride = 0, start = 0, normalised = false) + { + this.buffer = buffer; + this.normalized = normalised; + this.size = size; + this.stride = stride; + this.start = start; + this.type = null; + } - constructor(buffer, size = 2, stride = 0, start = 0, normalised = false) - { - this.buffer = buffer; - this.normalized = normalised; - this.size = size; - this.stride = stride; - this.start = start; - this.type = null; - } + destroy() + { + this.buffer = null; + } - destroy() - { - this.buffer = null; - } - + static from(buffer, stride, start, normalised) + { + return new Attribute(buffer, stride, start, normalised); + } } -Attribute.from = function (buffer, stride, start, normalised) -{ - return new Attribute(buffer, stride, start, normalised); -}; - module.exports = Attribute; diff --git a/src/mesh/geometry/Buffer.js b/src/mesh/geometry/Buffer.js index 9ff6889..72c361b 100644 --- a/src/mesh/geometry/Buffer.js +++ b/src/mesh/geometry/Buffer.js @@ -11,27 +11,27 @@ */ export default class Buffer { - constructor(data) - { - /** - * The type of the buffer - * - * @member {gl.ARRAY_BUFFER|gl.ELEMENT_ARRAY_BUFFER} - */ - // this.type = type || gl.ARRAY_BUFFER; + constructor(data) + { + /** + * The type of the buffer + * + * @member {gl.ARRAY_BUFFER|gl.ELEMENT_ARRAY_BUFFER} + */ + // this.type = type || gl.ARRAY_BUFFER; - /** - * The draw type of the buffer - * - * @member {gl.STATIC_DRAW|gl.DYNAMIC_DRAW|gl.STREAM_DRAW} - */ - // this.drawType = drawType || gl.STATIC_DRAW; + /** + * The draw type of the buffer + * + * @member {gl.STATIC_DRAW|gl.DYNAMIC_DRAW|gl.STREAM_DRAW} + */ + // this.drawType = drawType || gl.STATIC_DRAW; - /** - * The data in the buffer, as a typed array - * - * @member {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} - */ + /** + * The data in the buffer, as a typed array + * + * @member {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} + */ this.data = data; this._glBuffers = []; @@ -39,35 +39,35 @@ this._updateID = 0; this.id = UID++; - } + } - /** - * Uploads the buffer to the GPU - * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data to upload - * @param offset {Number} if only a subset of the data should be uploaded, this is the amount of data to subtract - */ - update() - { - this._updateID++; - } + /** + * Uploads the buffer to the GPU + * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data to upload + * @param offset {Number} if only a subset of the data should be uploaded, this is the amount of data to subtract + */ + update() + { + this._updateID++; + } - /** - * Destroys the buffer - * - */ - destroy() - { - for (let i = 0; i < this._glBuffers.length; i++) - { - this._glBuffers[i].destroy(); - } + /** + * Destroys the buffer + * + */ + destroy() + { + for (let i = 0; i < this._glBuffers.length; i++) + { + this._glBuffers[i].destroy(); + } - this.data = null; - } + this.data = null; + } - static from(data) - { - return new Buffer(data); - } + static from(data) + { + return new Buffer(data); + } } diff --git a/src/mesh/geometry/Geometry.js b/src/mesh/geometry/Geometry.js index be4ab48..321949c 100644 --- a/src/mesh/geometry/Geometry.js +++ b/src/mesh/geometry/Geometry.js @@ -4,73 +4,74 @@ import GeometryData from './GeometryData'; var UID = 0; + class Geometry { - constructor(data, style) - { - this.style = style || new GeometryStyle(); - this.data = data || new GeometryData(); + constructor(data, style) + { + this.style = style || new GeometryStyle(); + this.data = data || new GeometryData(); - this.glVertexArrayObjects = []; + this.glVertexArrayObjects = []; - this.id = UID++; - } + this.id = UID++; + } - addAttribute(id, buffer, size = 2, stride = 0, start = 0, normalised = false) - { - // check if this is a buffer! - if(!buffer.data) - { - // its an array! - buffer = new Buffer(buffer); - } + addAttribute(id, buffer, size = 2, stride = 0, start = 0, normalised = false) + { + // check if this is a buffer! + if (!buffer.data) + { + // its an array! + buffer = new Buffer(buffer); + } - this.style.addAttribute(id, new Attribute(buffer.id, size, stride, start, normalised)); - this.data.add(buffer.id, buffer); + this.style.addAttribute(id, new Attribute(buffer.id, size, stride, start, normalised)); + this.data.add(buffer.id, buffer); - return this; - } + return this; + } - getAttribute(id) - { - return this.data[ this.style.attributes[id].buffer ]; - } + getAttribute(id) + { + return this.data[this.style.attributes[id].buffer]; + } - addIndex(buffer) - { - if(!buffer.data) - { - // its an array! - buffer = new Buffer(buffer); - } + addIndex(buffer) + { + if (!buffer.data) + { + // its an array! + buffer = new Buffer(buffer); + } - this.data.addIndex(buffer); + this.data.addIndex(buffer); - return this; - } + return this; + } - destroy() - { - //TODO - this is wrong! - for (let i = 0; i < this.buffers.length; i++) - { - this.buffers[i].destroy(); - } + destroy() + { + //TODO - this is wrong! + for (let i = 0; i < this.buffers.length; i++) + { + this.buffers[i].destroy(); + } - this.buffers = null; - this.attributes = null; + this.buffers = null; + this.attributes = null; - for (let i = 0; i < this.glVertexArrayObjects.length; i++) - { - this.glVertexArrayObjects[i].destroy(); - } + for (let i = 0; i < this.glVertexArrayObjects.length; i++) + { + this.glVertexArrayObjects[i].destroy(); + } - this.glVertexArrayObjects = null; + this.glVertexArrayObjects = null; - this.indexBuffer.destroy(); - this.indexBuffer = null; - } + this.indexBuffer.destroy(); + this.indexBuffer = null; + } } export default Geometry; diff --git a/src/mesh/geometry/GeometryData.js b/src/mesh/geometry/GeometryData.js index 28dd0ca..4d988a5 100644 --- a/src/mesh/geometry/GeometryData.js +++ b/src/mesh/geometry/GeometryData.js @@ -1,35 +1,33 @@ -class GeometryData +export default class GeometryData { - constructor() - { - this.buffers = []; - this.indexBuffer = null; - } + constructor() + { + this.buffers = []; + this.indexBuffer = null; + } - add(id, buffer) - { - // only one! - if (this.buffers.indexOf(buffer) === -1) - { - this.buffers.push(buffer); - this[id] = buffer; - } + add(id, buffer) + { + // only one! + if (this.buffers.indexOf(buffer) === -1) + { + this.buffers.push(buffer); + this[id] = buffer; + } - return this; - } + return this; + } - addIndex(buffer) - { - buffer.index = true; - this.indexBuffer = buffer; + addIndex(buffer) + { + buffer.index = true; + this.indexBuffer = buffer; - if (this.buffers.indexOf(buffer) === -1) - { - this.buffers.push(buffer); - } + if (this.buffers.indexOf(buffer) === -1) + { + this.buffers.push(buffer); + } - return this; - } -} - -export default GeometryData; + return this; + } +} \ No newline at end of file diff --git a/src/mesh/geometry/GeometryStyle.js b/src/mesh/geometry/GeometryStyle.js index 27d4a86..e626483 100644 --- a/src/mesh/geometry/GeometryStyle.js +++ b/src/mesh/geometry/GeometryStyle.js @@ -1,6 +1,5 @@ -class GeometryStyle +export default class GeometryStyle { - constructor() { this.attributes = {}; @@ -52,6 +51,4 @@ this.indexBuffer = null; } -} - -export default GeometryStyle; +} \ No newline at end of file diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 58e4db8..e826b78 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -6,126 +6,111 @@ export default class State { - constructor(data) - { - this.data = 0; - - this.blendMode = 0; - this.polygonOffset = 0; - } - - set blend( value ) - { - if( !!(this.data & (1 << BLEND)) !== value ) - { - this.data ^= (1 << BLEND); - } - } - - get blend() - { - return !!(this.data & (1 << BLEND)) - } - - //////////// - - set offsets( value ) - { - if( !!(this.data & (1 << OFFSET)) !== value ) - { - this.data ^= (1< b.name) ? 1 : -1; - }); + attributesArray.sort((a, b) => (a.name > b.name) ? 1 : -1); // eslint-disable-line no-confusing-arrow const attributes = {}; @@ -65,6 +62,7 @@ for (let i = 0; i < attributesArray.length; i++) { const attrib = attributesArray[i]; + attrib.location = i; attributes[attrib.name] = attrib; } diff --git a/src/core/shader/generateUniformsSync.js b/src/core/shader/generateUniformsSync.js index e2b719a..e9995d9 100644 --- a/src/core/shader/generateUniformsSync.js +++ b/src/core/shader/generateUniformsSync.js @@ -1,83 +1,79 @@ +const GLSL_TO_SINGLE_SETTERS_CACHED = { - -var generateUniformsSync = function(uniformData) -{ - - var textureCount = 1; - var func = 'var value = null; var cacheValue = null' - - for (var i in uniformData) - { - let data = uniformData[i]; - - // TODO && uniformData[i].value !== 0 <-- do we still need this? - if(data.type === 'sampler2D') - { - func += '\ngl.uniform1iv(uniformData.'+i+'.location, renderer.bindTexture(uniformValues.' + i + ', ' + (textureCount++) + ', true) );\n' - } - else if (data.type === 'mat3') - { - func += '\nvalue = uniformValues.' + i + ';'; - func += '\ngl.uniformMatrix3fv(uniformData.'+i+'.location, false, (value.a !== undefined) ? value : value.toArray(true));\n' - } - else if (data.type === 'vec2') - { - func += '\nvalue = uniformValues.' + i + ';\n'; - func += '\nif(value.x !== undefined)gl.uniform2f(uniformData.'+i+'.location, value.x, value.y);' - func += '\nelse gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n' - - } - else - { - var template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', 'uniformData.'+i+'.location'); - func += '\ncacheValue = uniformData.' + i + '.value;\n'; - func += 'value = uniformValues.' + i + ';\n'; - func += template +'\n'; - } - - }; - - return new Function('uniformData', 'uniformValues', 'gl', func); -} - -var GLSL_TO_SINGLE_SETTERS_CACHED = { - - 'float': `if(cacheValue !== value) + float: `if(cacheValue !== value) { cacheValue = value; gl.uniform1f(location, value) }`, - 'vec2': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) + vec2: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; gl.uniform2f(location, value[0], value[1]) }`, - 'vec3': `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) + vec3: `if(cacheValue[0] !== value[0] || cacheValue[1] !== value[1] || cacheValue[2] !== value[2]) { cacheValue[0] = value[0]; cacheValue[1] = value[1]; cacheValue[2] = value[2]; gl.uniform3f(location, value[0], value[1], value[2]) }`, - 'vec4': 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', + vec4: 'gl.uniform4f(location, value[0], value[1], value[2], value[3])', - 'int': 'gl.uniform1i(location, value)', - 'ivec2': 'gl.uniform2i(location, value[0], value[1])', - 'ivec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'ivec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + int: 'gl.uniform1i(location, value)', + ivec2: 'gl.uniform2i(location, value[0], value[1])', + ivec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + ivec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'bool': 'gl.uniform1i(location, value)', - 'bvec2': 'gl.uniform2i(location, value[0], value[1])', - 'bvec3': 'gl.uniform3i(location, value[0], value[1], value[2])', - 'bvec4': 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', + bool: 'gl.uniform1i(location, value)', + bvec2: 'gl.uniform2i(location, value[0], value[1])', + bvec3: 'gl.uniform3i(location, value[0], value[1], value[2])', + bvec4: 'gl.uniform4i(location, value[0], value[1], value[2], value[3])', - 'mat2': 'gl.uniformMatrix2fv(location, false, value)', - 'mat3': 'gl.uniformMatrix3fv(location, false, value)', - 'mat4': 'gl.uniformMatrix4fv(location, false, value)', + mat2: 'gl.uniformMatrix2fv(location, false, value)', + mat3: 'gl.uniformMatrix3fv(location, false, value)', + mat4: 'gl.uniformMatrix4fv(location, false, value)', - 'sampler2D':'uniform1i(location, value)' + sampler2D: 'uniform1i(location, value)', }; -module.exports = generateUniformsSync; +export default function generateUniformsSync(uniformData) +{ + let textureCount = 1; + let func = 'var value = null; var cacheValue = null'; + + for (const i in uniformData) + { + const data = uniformData[i]; + + // TODO && uniformData[i].value !== 0 <-- do we still need this? + if (data.type === 'sampler2D') + { + func += `\ngl.uniform1iv(uniformData.${i}.location, renderer.bindTexture(uniformValues.${i}, ${textureCount++}, true) );\n`; // eslint-disable-line max-len + } + else if (data.type === 'mat3') + { + func += `\nvalue = uniformValues.${i}; +gl.uniformMatrix3fv(uniformData.${i}.location, false, (value.a !== undefined) ? value : value.toArray(true));\n`; + } + else if (data.type === 'vec2') + { + func += `\nvalue = uniformValues.${i}; +if(value.x !== undefined)gl.uniform2f(uniformData.${i}.location, value.x, value.y); +else gl.uniform2f(uniformData.'+i+'.location, value[0], value[1]);\n`; + } + else + { + const template = GLSL_TO_SINGLE_SETTERS_CACHED[data.type].replace('location', `uniformData.${i}.location`); + + func += `\ncacheValue = uniformData.${i}.value; +value = uniformValues.${i}; +${template};\n`; + } + } + + // console.log(func); + + return new Function('uniformData', 'uniformValues', 'gl', func); // eslint-disable-line no-new-func +} diff --git a/src/core/sprites/webgl/SpriteRenderer.js b/src/core/sprites/webgl/SpriteRenderer.js index 4495081..1c7ad70 100644 --- a/src/core/sprites/webgl/SpriteRenderer.js +++ b/src/core/sprites/webgl/SpriteRenderer.js @@ -108,10 +108,8 @@ // step 2: check the maximum number of if statements the shader can have too.. this.MAX_TEXTURES = checkMaxIfStatmentsInShader(this.MAX_TEXTURES, gl); - console.log(this.MAX_TEXTURE) const shader = this.shader = generateMultiTextureShader(gl, this.MAX_TEXTURES); - console.log(shader) // create a couple of buffers this.indexBuffer = glCore.GLBuffer.createIndexBuffer(gl, this.indices, gl.STATIC_DRAW); @@ -123,8 +121,6 @@ for (let i = 0; i < this.vaoMax; i++) { this.vertexBuffers[i] = glCore.GLBuffer.createVertexBuffer(gl, null, gl.STREAM_DRAW); - console.log(this.vertexBuffers) - console.log(shader.attributes) /* eslint-disable max-len */ // build the vao object that will render.. @@ -132,11 +128,13 @@ .addIndex(this.indexBuffer) .addAttribute(this.vertexBuffers[i], shader.attributes.aVertexPosition, gl.FLOAT, false, this.vertByteSize, 0) .addAttribute(this.vertexBuffers[i], shader.attributes.aTextureCoord, gl.UNSIGNED_SHORT, true, this.vertByteSize, 2 * 4) - .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4) - if(shader.attributes.aTextureId) - { - this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); - } + .addAttribute(this.vertexBuffers[i], shader.attributes.aColor, gl.UNSIGNED_BYTE, true, this.vertByteSize, 3 * 4); + + if (shader.attributes.aTextureId) + { + this.vaos[i].addAttribute(this.vertexBuffers[i], shader.attributes.aTextureId, gl.FLOAT, false, this.vertByteSize, 4 * 4); + } + /* eslint-disable max-len */ } diff --git a/src/mesh/Mesh.js b/src/mesh/Mesh.js index 8d8c31c..0b07d77 100644 --- a/src/mesh/Mesh.js +++ b/src/mesh/Mesh.js @@ -15,7 +15,6 @@ */ constructor(geometry, shader, uniforms, state, drawMode = core.DRAW_MODES.TRIANGLES) { - //drawMode = core.DRAW_MODES.TRIANGLES, uniforms = {}) super(); /** @@ -43,15 +42,12 @@ * @member {number} * @see PIXI.mesh.Mesh.DRAW_MODES */ - this.drawMode = 4//drawMode; + this.drawMode = drawMode; + // TODO uniform auto generation? + // make sure to add required feilds this.uniforms = uniforms; -// geometry// -// shader// -// uniforms// -// state// - /** * The tint applied to the mesh. This is a [r,g,b] value. A value of [1,1,1] will remove any * tint effect. diff --git a/src/mesh/Plane.js b/src/mesh/Plane.js index 9d2aa67..4ad7ef5 100644 --- a/src/mesh/Plane.js +++ b/src/mesh/Plane.js @@ -5,7 +5,7 @@ import { join } from 'path'; let meshShader; -const temp = [0,0,0]; +const temp = [0, 0, 0]; /** * The Plane allows you to draw a texture across several points and them manipulate these points @@ -33,11 +33,15 @@ { const geometry = new Geometry(); - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } geometry.addAttribute('aVertexPosition', new Float32Array(2), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(2), 2) - geometry.addIndex(new Uint16Array(2)) + .addAttribute('aTextureCoord', new Float32Array(2), 2) + .addIndex(new Uint16Array(2)); super(geometry, meshShader, 4); @@ -69,7 +73,7 @@ this.refresh(); - this.tint; + this.tint = 0xFFFFFF; } /** @@ -80,7 +84,6 @@ { const total = this.verticesX * this.verticesY; const verts = []; - const colors = []; const uvs = []; const indices = []; const texture = this.texture; @@ -113,8 +116,6 @@ } } - // cons - const totalSub = segmentsX * segmentsY; for (let i = 0; i < totalSub; i++) @@ -131,24 +132,21 @@ indices.push(value2, value4, value3); } - // console.log(indices) this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// - + this.shader.uniforms.uSampler2 = this.texture; this.vertices = new Float32Array(verts); this.uvs = new Float32Array(uvs); this.indices = new Uint16Array(indices); - this.geometry.getAttribute('aVertexPosition').data = this.vertices; - this.geometry.getAttribute('aTextureCoord').data = this.uvs; - this.geometry.data.indexBuffer.data = this.indices; + this.geometry.getAttribute('aVertexPosition').data = this.vertices; + this.geometry.getAttribute('aTextureCoord').data = this.uvs; + this.geometry.data.indexBuffer.data = this.indices; - // ensure that the changes are uploaded + // ensure that the changes are uploaded this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); - this.geometry.data.indexBuffer.update();// - + this.geometry.data.indexBuffer.update(); } /** @@ -165,10 +163,10 @@ } } - _renderWebGL(renderer) + _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); @@ -176,11 +174,7 @@ updateTransform() { - this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); } - - - } diff --git a/src/mesh/Rope.js b/src/mesh/Rope.js index 1b167ee..0589890 100644 --- a/src/mesh/Rope.js +++ b/src/mesh/Rope.js @@ -5,6 +5,7 @@ import { join } from 'path'; let meshShader; +const temp = new Float32Array([0, 0, 0]); /** * The rope allows you to draw a texture across several points and them manipulate these points @@ -29,15 +30,19 @@ */ constructor(texture, points) { - if(!meshShader)meshShader = new core.Shader( readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + if (!meshShader) + { + meshShader = new core.Shader(readFileSync(join(__dirname, './webgl/mesh.vert'), 'utf8'), + readFileSync(join(__dirname, './webgl/mesh.frag'), 'utf8')); + } const geometry = new Geometry(); geometry.addAttribute('aVertexPosition', new Float32Array(points.length * 4), 2) - geometry.addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) - geometry.addIndex(new Uint16Array(points.length * 2)) + .addAttribute('aTextureCoord', new Float32Array(points.length * 4), 2) + .addIndex(new Uint16Array(points.length * 2)); - super(geometry, meshShader, 5); + super(geometry, meshShader, {}, 5); this.texture = texture; @@ -77,7 +82,7 @@ const vertices = this.geometry.getAttribute('aVertexPosition').data; const uvs = this.geometry.getAttribute('aTextureCoord').data; - //TODO - lets make this more accessable... maybe a getIndx()? + // TODO - lets make this more accessable... maybe a getIndx()? const indices = this.geometry.data.indexBuffer.data; // if too little points, or texture hasn't got UVs set yet just move on. @@ -132,8 +137,6 @@ this.geometry.getAttribute('aVertexPosition').update(); this.geometry.getAttribute('aTextureCoord').update(); this.geometry.data.indexBuffer.update(); - - // console.log(this.geometry.data) } /** @@ -143,8 +146,6 @@ */ _onTextureUpdate() { - //super._onTextureUpdate(); - // wait for the Rope ctor to finish before calling refresh if (this._ready) { @@ -218,7 +219,7 @@ } this.shader.uniforms.alpha = 1; - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.uSampler2 = this.texture; this.geometry.getAttribute('aVertexPosition').update(); this.containerUpdateTransform(); @@ -226,8 +227,8 @@ _renderWebGL(renderer) { - this.shader.uniforms.tint = core.utils.hex2rgb(this.tint,temp) - this.shader.uniforms.uSampler2 = this.texture;// + this.shader.uniforms.tint = core.utils.hex2rgb(this.tint, temp); + this.shader.uniforms.uSampler2 = this.texture; renderer.setObjectRenderer(renderer.plugins.mesh); renderer.plugins.mesh.render(this); diff --git a/src/mesh/geometry/Attribute.js b/src/mesh/geometry/Attribute.js index 2c6516c..d07085f 100644 --- a/src/mesh/geometry/Attribute.js +++ b/src/mesh/geometry/Attribute.js @@ -1,26 +1,24 @@ class Attribute { + constructor(buffer, size = 2, stride = 0, start = 0, normalised = false) + { + this.buffer = buffer; + this.normalized = normalised; + this.size = size; + this.stride = stride; + this.start = start; + this.type = null; + } - constructor(buffer, size = 2, stride = 0, start = 0, normalised = false) - { - this.buffer = buffer; - this.normalized = normalised; - this.size = size; - this.stride = stride; - this.start = start; - this.type = null; - } + destroy() + { + this.buffer = null; + } - destroy() - { - this.buffer = null; - } - + static from(buffer, stride, start, normalised) + { + return new Attribute(buffer, stride, start, normalised); + } } -Attribute.from = function (buffer, stride, start, normalised) -{ - return new Attribute(buffer, stride, start, normalised); -}; - module.exports = Attribute; diff --git a/src/mesh/geometry/Buffer.js b/src/mesh/geometry/Buffer.js index 9ff6889..72c361b 100644 --- a/src/mesh/geometry/Buffer.js +++ b/src/mesh/geometry/Buffer.js @@ -11,27 +11,27 @@ */ export default class Buffer { - constructor(data) - { - /** - * The type of the buffer - * - * @member {gl.ARRAY_BUFFER|gl.ELEMENT_ARRAY_BUFFER} - */ - // this.type = type || gl.ARRAY_BUFFER; + constructor(data) + { + /** + * The type of the buffer + * + * @member {gl.ARRAY_BUFFER|gl.ELEMENT_ARRAY_BUFFER} + */ + // this.type = type || gl.ARRAY_BUFFER; - /** - * The draw type of the buffer - * - * @member {gl.STATIC_DRAW|gl.DYNAMIC_DRAW|gl.STREAM_DRAW} - */ - // this.drawType = drawType || gl.STATIC_DRAW; + /** + * The draw type of the buffer + * + * @member {gl.STATIC_DRAW|gl.DYNAMIC_DRAW|gl.STREAM_DRAW} + */ + // this.drawType = drawType || gl.STATIC_DRAW; - /** - * The data in the buffer, as a typed array - * - * @member {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} - */ + /** + * The data in the buffer, as a typed array + * + * @member {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} + */ this.data = data; this._glBuffers = []; @@ -39,35 +39,35 @@ this._updateID = 0; this.id = UID++; - } + } - /** - * Uploads the buffer to the GPU - * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data to upload - * @param offset {Number} if only a subset of the data should be uploaded, this is the amount of data to subtract - */ - update() - { - this._updateID++; - } + /** + * Uploads the buffer to the GPU + * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data to upload + * @param offset {Number} if only a subset of the data should be uploaded, this is the amount of data to subtract + */ + update() + { + this._updateID++; + } - /** - * Destroys the buffer - * - */ - destroy() - { - for (let i = 0; i < this._glBuffers.length; i++) - { - this._glBuffers[i].destroy(); - } + /** + * Destroys the buffer + * + */ + destroy() + { + for (let i = 0; i < this._glBuffers.length; i++) + { + this._glBuffers[i].destroy(); + } - this.data = null; - } + this.data = null; + } - static from(data) - { - return new Buffer(data); - } + static from(data) + { + return new Buffer(data); + } } diff --git a/src/mesh/geometry/Geometry.js b/src/mesh/geometry/Geometry.js index be4ab48..321949c 100644 --- a/src/mesh/geometry/Geometry.js +++ b/src/mesh/geometry/Geometry.js @@ -4,73 +4,74 @@ import GeometryData from './GeometryData'; var UID = 0; + class Geometry { - constructor(data, style) - { - this.style = style || new GeometryStyle(); - this.data = data || new GeometryData(); + constructor(data, style) + { + this.style = style || new GeometryStyle(); + this.data = data || new GeometryData(); - this.glVertexArrayObjects = []; + this.glVertexArrayObjects = []; - this.id = UID++; - } + this.id = UID++; + } - addAttribute(id, buffer, size = 2, stride = 0, start = 0, normalised = false) - { - // check if this is a buffer! - if(!buffer.data) - { - // its an array! - buffer = new Buffer(buffer); - } + addAttribute(id, buffer, size = 2, stride = 0, start = 0, normalised = false) + { + // check if this is a buffer! + if (!buffer.data) + { + // its an array! + buffer = new Buffer(buffer); + } - this.style.addAttribute(id, new Attribute(buffer.id, size, stride, start, normalised)); - this.data.add(buffer.id, buffer); + this.style.addAttribute(id, new Attribute(buffer.id, size, stride, start, normalised)); + this.data.add(buffer.id, buffer); - return this; - } + return this; + } - getAttribute(id) - { - return this.data[ this.style.attributes[id].buffer ]; - } + getAttribute(id) + { + return this.data[this.style.attributes[id].buffer]; + } - addIndex(buffer) - { - if(!buffer.data) - { - // its an array! - buffer = new Buffer(buffer); - } + addIndex(buffer) + { + if (!buffer.data) + { + // its an array! + buffer = new Buffer(buffer); + } - this.data.addIndex(buffer); + this.data.addIndex(buffer); - return this; - } + return this; + } - destroy() - { - //TODO - this is wrong! - for (let i = 0; i < this.buffers.length; i++) - { - this.buffers[i].destroy(); - } + destroy() + { + //TODO - this is wrong! + for (let i = 0; i < this.buffers.length; i++) + { + this.buffers[i].destroy(); + } - this.buffers = null; - this.attributes = null; + this.buffers = null; + this.attributes = null; - for (let i = 0; i < this.glVertexArrayObjects.length; i++) - { - this.glVertexArrayObjects[i].destroy(); - } + for (let i = 0; i < this.glVertexArrayObjects.length; i++) + { + this.glVertexArrayObjects[i].destroy(); + } - this.glVertexArrayObjects = null; + this.glVertexArrayObjects = null; - this.indexBuffer.destroy(); - this.indexBuffer = null; - } + this.indexBuffer.destroy(); + this.indexBuffer = null; + } } export default Geometry; diff --git a/src/mesh/geometry/GeometryData.js b/src/mesh/geometry/GeometryData.js index 28dd0ca..4d988a5 100644 --- a/src/mesh/geometry/GeometryData.js +++ b/src/mesh/geometry/GeometryData.js @@ -1,35 +1,33 @@ -class GeometryData +export default class GeometryData { - constructor() - { - this.buffers = []; - this.indexBuffer = null; - } + constructor() + { + this.buffers = []; + this.indexBuffer = null; + } - add(id, buffer) - { - // only one! - if (this.buffers.indexOf(buffer) === -1) - { - this.buffers.push(buffer); - this[id] = buffer; - } + add(id, buffer) + { + // only one! + if (this.buffers.indexOf(buffer) === -1) + { + this.buffers.push(buffer); + this[id] = buffer; + } - return this; - } + return this; + } - addIndex(buffer) - { - buffer.index = true; - this.indexBuffer = buffer; + addIndex(buffer) + { + buffer.index = true; + this.indexBuffer = buffer; - if (this.buffers.indexOf(buffer) === -1) - { - this.buffers.push(buffer); - } + if (this.buffers.indexOf(buffer) === -1) + { + this.buffers.push(buffer); + } - return this; - } -} - -export default GeometryData; + return this; + } +} \ No newline at end of file diff --git a/src/mesh/geometry/GeometryStyle.js b/src/mesh/geometry/GeometryStyle.js index 27d4a86..e626483 100644 --- a/src/mesh/geometry/GeometryStyle.js +++ b/src/mesh/geometry/GeometryStyle.js @@ -1,6 +1,5 @@ -class GeometryStyle +export default class GeometryStyle { - constructor() { this.attributes = {}; @@ -52,6 +51,4 @@ this.indexBuffer = null; } -} - -export default GeometryStyle; +} \ No newline at end of file diff --git a/src/mesh/webgl/MeshRenderer.js b/src/mesh/webgl/MeshRenderer.js index 64ae550..d6158fb 100644 --- a/src/mesh/webgl/MeshRenderer.js +++ b/src/mesh/webgl/MeshRenderer.js @@ -26,8 +26,7 @@ onContextChange() { this.gl = this.renderer.gl; - this.CONTEXT_UID = this.renderer.CONTEXT_UID - // nothing to see here! + this.CONTEXT_UID = this.renderer.CONTEXT_UID; } /** @@ -37,10 +36,6 @@ */ render(mesh) { - // always use shaders - rather than GLShadr - - // generate geometry structure from a shader :) - // set the shader props.. if (mesh.shader.uniforms.translationMatrix) { @@ -48,9 +43,6 @@ mesh.shader.uniforms.translationMatrix = mesh.transform.worldTransform.toArray(true); } - // set the correct blend mode -// this.renderer.state.setBlendMode(mesh.blendMode); - // bind the shader.. this.renderer.shaderManager.bindShader(mesh.shader, true); @@ -60,8 +52,6 @@ // sync uniforms.. this.renderer.state.setState(mesh.state); - // now time for geometry.. - // bind the geometry... this.bindGeometry(mesh.geometry); @@ -81,22 +71,21 @@ this.renderer.bindVao(vao); const data = geometry.data; - // if (geometry.autoUpdate) + // TODO - optimise later! + // don't need to loop through if nothing changed! + // maybe look to add an 'autoupdate' to geometry? + for (let i = 0; i < data.buffers.length; i++) { - // TODO - optimise later! - for (let i = 0; i < data.buffers.length; i++) + const buffer = data.buffers[i]; + + const glBuffer = buffer._glBuffers[this.CONTEXT_UID]; + + if (buffer._updateID !== glBuffer._updateID) { - const buffer = data.buffers[i]; + glBuffer._updateID = buffer._updateID; - const glBuffer = buffer._glBuffers[this.CONTEXT_UID]; - - if (buffer._updateID !== glBuffer._updateID) - { - glBuffer._updateID = buffer._updateID; - - // TODO - partial upload?? - glBuffer.upload(buffer.data, 0); - } + // TODO - partial upload?? + glBuffer.upload(buffer.data, 0); } } }