ShaderProgram Class
Creates a new ShaderProgram from the given source, and an optional map of attribute
locations as
Note: Chrome version 31 was giving me issues with attribute locations -- you may want to omit this to let the browser pick the locations for you.
Constructor
ShaderProgram
-
context
-
vertSource
-
fragSource
-
attributeLocations
Parameters:
-
context
WebGLContextthe context to manage this object
-
vertSource
Stringthe vertex shader source
-
fragSource
Stringthe fragment shader source
-
attributeLocations
Objectthe attribute locations
Item Index
Methods
bind
()
Called to bind this shader. Note that there is no "unbind" since technically such a thing is not possible in the programmable pipeline.
You must bind a shader before settings its uniforms.
create
()
This is called during the ShaderProgram constructor, and may need to be called again after context loss and restore.
destroy
()
Destroys this shader and its resources. You should not try to use this after destroying it.
getAttributeInfo
-
name
Returns the cached attribute info (size, type, location). If the attribute is not found in the cache, it is assumed to not exist, and this method returns null.
This may return null even if the attribute is defined in GLSL: if it is inactive (i.e. not used in the program or disabled) then it may be optimized out.
Parameters:
-
name
Stringthe attribute name as defined in GLSL
Returns:
an object containing location, size and type
getAttributeLocation
-
name
Returns the cached uniform location object. If the uniform is not found, this method returns null.
Parameters:
-
name
Stringthe uniform name as defined in GLSL
Returns:
the location object
getUniform
-
name
Returns the uniform value by name.
Parameters:
-
name
Stringthe uniform name as defined in GLSL
Returns:
The value of the WebGL uniform
getUniformAt
-
location
Returns the uniform value at the specified WebGLUniformLocation.
Parameters:
-
location
WebGLUniformLocationthe location object
Returns:
The value of the WebGL uniform
getUniformInfo
-
name
Returns the cached uniform info (size, type, location). If the uniform is not found in the cache, it is assumed to not exist, and this method returns null.
This may return null even if the uniform is defined in GLSL: if it is inactive (i.e. not used in the program) then it may be optimized out.
Parameters:
-
name
Stringthe uniform name as defined in GLSL
Returns:
an object containing location, size, and type
getUniformLocation
-
name
Returns the cached uniform location object, assuming it exists and is active. Note that uniforms may be inactive if the GLSL compiler deemed them unused.
Parameters:
-
name
Stringthe uniform name as defined in GLSL
Returns:
the location object
hasAttribute
-
name
Returns true if the attribute is active and found in this compiled program.
Parameters:
-
name
Stringthe attribute name
Returns:
true if the attribute is found and active
hasUniform
-
name
Returns true if the uniform is active and found in this compiled program. Note that uniforms may be inactive if the GLSL compiler deemed them unused.
Parameters:
-
name
Stringthe uniform name
Returns:
true if the uniform is found and active
setUniformf
-
name
-
x
-
y
-
z
-
w
A convenience method to set uniformf from the given arguments.
We determine which GL call to make based on the number of arguments
passed. For example, setUniformf("var", 0, 1)
maps to gl.uniform2f
.
Parameters:
-
name
Stringthe name of the uniform
-
x
GLfloatthe x component for floats
-
y
GLfloatthe y component for vec2
-
z
GLfloatthe z component for vec3
-
w
GLfloatthe w component for vec4
setUniformfv
-
name
-
arrayBuffer
-
count
A convenience method to set uniformNfv from the given ArrayBuffer. We determine which GL call to make based on the length of the array buffer (for 1-4 component vectors stored in a Float32Array). To use this method to upload data to uniform arrays, you need to specify the 'count' parameter; i.e. the data type you are using for that array. If specified, this will dictate whether to call uniform1fv, uniform2fv, etc.
Parameters:
-
name
Stringthe name of the uniform
-
arrayBuffer
ArrayBufferthe array buffer
-
count
Numberoptional, the explicit data type count, e.g. 2 for vec2
setUniformi
-
name
-
x
-
y
-
z
-
w
A convenience method to set uniformi from the given arguments.
We determine which GL call to make based on the number of arguments
passed. For example, setUniformi("var", 0, 1)
maps to gl.uniform2i
.
Parameters:
-
name
Stringthe name of the uniform
-
x
GLintthe x component for ints
-
y
GLintthe y component for ivec2
-
z
GLintthe z component for ivec3
-
w
GLintthe w component for ivec4
setUniformiv
-
name
-
arrayBuffer
-
count
A convenience method to set uniformNiv from the given ArrayBuffer. We determine which GL call to make based on the length of the array buffer (for 1-4 component vectors stored in a int array). To use this method to upload data to uniform arrays, you need to specify the 'count' parameter; i.e. the data type you are using for that array. If specified, this will dictate whether to call uniform1fv, uniform2fv, etc.
Parameters:
-
name
Stringthe name of the uniform
-
arrayBuffer
ArrayBufferthe array buffer
-
count
Numberoptional, the explicit data type count, e.g. 2 for ivec2