shader.js
Browserify-compatible webgl shader objects.
var shader = canvas = document gl = canvas Shader = Shader
API
require('shader.js') -> function shader(gl){}
Returns a function that accepts a WebGL context and returns the
Shader
constructor function.
shader(gl) -> Shader
Returns the Shader
constructor when provided a gl context.
Shader.fromURLs(vertexURL, fragmentURL, ready) -> undefined
Creates a Shader
instance with the vertex and fragment shaders provided
by the two URL arguments. ready
is a standard node-style callback:
function(err, shader) { }
.
Shader.fromIds(vertexID, fragmentID, ready) -> undefined
Creates a Shader
instance with the vertex and fragment shaders provided
by the two DOM elements represented by vertexID
and fragmentID
.
ready
is a standard node-style callback: function(err, shader) { }
.
new Shader(vertexSRC, fragmentSRC) -> Shader instance
Create a new WebGL shader provided the source of the vertex and fragment shaders.
Shader#handle() -> WebGL program handle
Returns a handle suitable for passing to gl.useProgram
.
// e.g.,gl
Shader#uniform(uniformName, value) -> undefined
Provides a uniform value to the GPU; relies on introspection of shader source code. Maps the following types to the following calls:
vec2
:gl.uniform2fv
vec3
:gl.uniform3fv
vec4
:gl.uniform4fv
mat4
:gl.uniformMatrix4fv
float
:gl.uniform1f
int
:gl.uniform1i
sampler2D
:gl.uniform1i
Usage with texture.js:
// sets `texture` to point at gl.TEXTURE1 with the contents of `myTexture`shader
Shader#attribute(attributeName[, type, normalized, stride, offset]) -> undefined
Describes attributes of the vertex stream to the shader.
Automatically detects desired width of incoming data based on source introspection.
Does not bind the buffer for you automatically -- you have to do that yourself ahead of time.
Roughly equivalent to:
var attribLocation = gl WIDTH = 4 // vec2 -> 2, vec3 -> 3, vec4 -> 4 gl gl gl
Shader#vertex() -> vertex shader source
Returns the program's vertex shader source.
Shader#vertex(src) -> src
Sets the program's vertex shader source.
NB:
This recompiles the shader: you will have to call
gl.useProgram(shader.handle())
again after calling this method.
Shader#fragment() -> fragment shader source
Returns the program's fragment shader source.
Shader#vertex(src) -> src
Sets the program's fragment shader source.
NB:
This recompiles the shader: you will have to call
gl.useProgram(shader.handle())
again after calling this method.