PDA

View Full Version : getUniformLocation: is uniform exists or not.



sphere
01-07-2010, 03:32 AM
getUniformLocation returns WebGLUniformLocation object even if there is no uniform in program (for example, getAttribLocation returns -1 in this case).
How to check if an uniform really exists?

easywebgl
01-11-2010, 01:34 AM
An easy way to test if it would exist would be by pulling the value of it and see what it returns.
tested with lesson1 of learningwebgl.

var uniformlocation = gl.getUniformLocation(shaderProgram, "uMVMatrix");
var uniform = gl.getUniform(shaderProgram, uniformlocation);
uniform === WebGLFloatArray;

var uniformlocation = gl.getUniformLocation(shaderProgram, "ThisMatrixdoesn'texist");
var uniform = gl.getUniform(shaderProgram, uniformlocation);
uniform === null;

Btw this is only tested in chromium but I believe this would be an easy way to find it.

easywebgl
01-11-2010, 11:12 AM
var uniformlocation,uniform;
uniformlocation = gl.getUniformLocation(shaderProgram, NameOfUniformVariable);
if (uniformlocation !== -1){
uniform = gl.getUniform(shaderProgram, uniformlocation);
} else {
uniform = null;
}

if uniform now returns null then it means it doesn't exist in that shader

This is tested in both minefield and chromium
hopefully the other browser work with this code too

sphere
01-21-2010, 05:15 AM
Thanks, easywebgl.