Shader

cjcliffe edited this page Oct 15, 2011 · 4 revisions

CubicVR.Shader

The Shader class provides a simple wrapper to interface with GLSL shaders, uniforms and attributes. All attributes and uniforms bound (including array and structured) will also be added to the Shader object as a property with the same name and can be manipulated through gl.uniform*() calls (bypassing the wrapper functions) for added performance.

Constructor:

Shader( vertex_source, fragment_source )

Parameters:

  • vertex_source : Vertex Shader source,
  • fragment_source : Fragment Shader source.

Both Vertex and Fragment shader source parameters can be one of the following:

  • ID of a script currently in the DOM containing the shader source or url attribute.
  • A direct path or URL to an external file containing the shader source.
  • A string containing the shader source itself.

Methods:

use()

Binds the shader program to WebGL and prepares it for use. This must be done to bring the program into context before any of the operations below can be performed successfully.

Returns:

none.

addMatrix( uniform_name, default_value )

Add a 2x2, 3x3 or 4x4 matrix uniform.

Parameters:

  • uniform_name : Name of the uniform to bind.
  • default_value (optional) : A default matrix to set, float array sizes 4 (2x2), 9 (3x3) and 16 (4x4) are accepted.

Returns:

WebGL uniform pointer.

addVector( uniform_name, default_value )

Add a vec2, vec3 or vec4 vector uniform.

Parameters:

  • uniform_name : Name of the uniform to bind.
  • default_value (optional) : A default vector array to set, float array length 2 (vec2), 3 (vec3) and 4 (vec4) are accepted.

Returns:

WebGL uniform pointer.

addFloat( uniform_name, default_value )

Add a float uniform.

Parameters:

  • uniform_name : Name of the uniform to bind.
  • default_value (optional) : A default float value to set.

Returns:

WebGL uniform pointer.

addInt( uniform_name, default_value )

Add an int uniform.

Parameters:

  • uniform_name : Name of the uniform to bind.
  • default_value (optional) : A default int value to set.

Returns:

WebGL uniform pointer.

setMatrix( uniform_name, value )

Set a 2x2, 3x3 or 4x4 matrix uniform.

Parameters:

  • uniform_name : Name of the uniform to set.
  • value : A matrix to set, float array sizes 4 (2x2), 9 (3x3) and 16 (4x4) are accepted.

Returns:

none.

setVector( uniform_name, value )

Set a vec2, vec3 or vec4 vector uniform.

Parameters:

  • uniform_name : Name of the uniform to set.
  • value : A vector array to set, float array length 2 (vec2), 3 (vec3) and 4 (vec4) are accepted.

Returns:

none

setFloat( uniform_name, value )

Set a float uniform.

Parameters:

  • uniform_name : Name of the uniform to set.
  • value : A float value to set.

Returns:

none

setInt( uniform_name, value )

Set an int uniform.

Parameters:

  • uniform_name : Name of the uniform to set.
  • value : An int value to set.

Returns:

none

addVertexArray( attribute_id )

Add a vertex (vec3) attribute array, represents a WebGL buffer with 3 floats per element.

Parameters:

attribute_id - Name of the attribute to bind.

Returns:

WebGL attribute pointer.

addUVArray( attribute_id )

Add a uv (vec2) attribute array, represents a WebGL buffer with 2 floats per element.

Parameters:

attribute_id - Name of the attribute to bind.

Returns:

WebGL attribute pointer.

addFloatArray( attribute_id )

Add a float attribute array, represents a WebGL buffer with one float per element.

Parameters:

attribute_id - Name of the attribute to bind.

Returns:

WebGL attribute pointer.

bindArray( attribute_id, buffer )

Bind an attribute array, will bind a WebGL array buffer to the given attribute.

Parameters:

  • attribute_id : Name of the attribute to bind.
  • buffer : A WebGL Buffer matching the parameters this attribute was bound with (vec3, vec2, float).

Returns:

none

clearArray( attribute_id )

Clear an attribute array, will unbind a WebGL array buffer from the given attribute.

Parameters:

attribute_id - Name of the attribute to unbind.

Returns:

none