API Docs for:
Show:

GL.Shader Class

Defined in: ../src/shader.js:6

Shader class to upload programs to the GPU

Constructor

GL.Shader

(
  • vertexSource
  • fragmentSource
  • macros
)

Defined in ../src/shader.js:6

Parameters:

  • vertexSource String

    (it also allows to pass a compiled vertex shader)

  • fragmentSource String

    (it also allows to pass a compiled fragment shader)

  • macros Object

    (optional) precompiler macros to be applied when compiling

Methods

bind

()

enables the shader (calls useProgram)

draw

(
  • mesh
  • mode
  • index_buffer_name
)

Renders a mesh using this shader, remember to use the function uniforms before to enable the shader

Parameters:

  • mesh Mesh
  • mode Number

    could be gl.LINES, gl.POINTS, gl.TRIANGLES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN

  • index_buffer_name String

    the name of the index buffer, if not provided triangles will be assumed

drawBuffers

(
  • vertexBuffers
  • indexBuffer
  • mode
  • range_start
  • range_length
)

render several buffers with a given index buffer

Parameters:

  • vertexBuffers Object

    an object containing all the buffers

  • indexBuffer IndexBuffer
  • mode Number

    could be gl.LINES, gl.POINTS, gl.TRIANGLES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN

  • range_start Number

    first primitive to render

  • range_length Number

    number of primitives to render

drawRange

(
  • mesh
  • mode
  • start
  • length
  • index_buffer_name
)

Renders a range of a mesh using this shader

Parameters:

  • mesh Mesh
  • mode Number

    could be gl.LINES, gl.POINTS, gl.TRIANGLES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN

  • start Number

    first primitive to render

  • length Number

    number of primitives to render

  • index_buffer_name String

    the name of the index buffer, if not provided triangles will be assumed

extractShaderInfo

()

It extract all the info about the compiled shader program, all the info about uniforms and attributes. This info is stored so it works faster during rendering.

getLocation

(
  • name
)
WebGLUniformLocation

Returns the location of a uniform or attribute

Parameters:

  • name String

Returns:

WebGLUniformLocation:

location

hasAttribute

(
  • name
)
Boolean

Returns if this shader has an attribute with the given name

Parameters:

  • name String

    name of the attribute

Returns:

Boolean:

hasUniform

(
  • name
)
Boolean

Returns if this shader has a uniform with the given name

Parameters:

  • name String

    name of the uniform

Returns:

Boolean:

setUniform

(
  • name
  • value
)

Uploads a uniform to the Shader. You dont need to specify types, they are infered from the shader info. Shader must be binded!

Parameters:

  • name String
  • value

Shader.compileSource

(
  • type
  • source
)
WebGLShader

Defined in ../src/shader.js:77

Compiles one single shader source (could be gl.VERTEX_SHADER or gl.FRAGMENT_SHADER) and returns the webgl shader handler Used internaly to compile the vertex and fragment shader. It throws an exception if there is any error in the code

Parameters:

  • type Number

    could be gl.VERTEX_SHADER or gl.FRAGMENT_SHADER

  • source String

    the source file to compile

Returns:

WebGLShader:

the handler from webgl

Shader.createFX

(
  • code
  • [uniforms=null]
)

Allows to create a simple shader meant to be used to process a texture, instead of having to define the generic Vertex & Fragment Shader code

Parameters:

  • code String

    string containg code, like "color = color * 2.0;"

  • [uniforms=null] String optional

    string containg extra uniforms, like "uniform vec3 u_pos;"

Shader.expandImports

(
  • code
  • files
)
String

Given a source code with the directive #import it expands it inserting the code using Shader.files to fetch for import files. Warning: Imports are evaluated only the first inclusion, the rest are ignored to avoid double inclusion of functions Also, imports cannot have other imports inside.

Parameters:

  • code String

    the source code

  • files Object

    [Optional] object with files to import from (otherwise Shader.files is used)

Returns:

String:

the code with the lines #import removed and replaced by the code

Shader.fromURL

(
  • vs_path
  • fs_path
  • on_complete
)
Shader

Create a shader from two urls. While the system is fetching the two urls, the shader contains a dummy shader that renders black.

Parameters:

  • vs_path String

    the url to the vertex shader

  • fs_path String

    the url to the fragment shader

  • on_complete Function

    [Optional] a callback to call once the shader is ready.

Returns:

Shader:

Shader.getBlendShader

()

Returns a shader that blends two textures shader must have: u_factor, u_texture, u_texture2

Shader.getBlurShader

()

Returns a shader used to apply gaussian blur to one texture in one axis (you should use it twice to get a gaussian blur) shader params are: vec2 u_offset, float u_intensity

Shader.getColoredScreenShader

()

Returns a shader ready to render a colored textured quad in fullscreen, use with Mesh.getScreenQuad() mesh shader params vec4 u_color and sampler2D u_texture

Shader.getFlatShader

()

Returns a flat shader (useful to render lines)

Shader.getFXAAShader

()

Returns a shader to apply FXAA antialiasing params are vec2 u_viewportSize, vec2 u_iViewportSize or you can call shader.setup()

Shader.getPartialQuadShader

()

Returns a shader ready to render part of a texture into the viewport shader must have: u_position, u_size, u_viewport, u_transform, u_texture_area (vec4)

Shader.getQuadShader

()

Returns a shader ready to render a quad with transform, use with Mesh.getScreenQuad() mesh shader must have: u_position, u_size, u_viewport, u_transform (mat3)

Shader.getScreenShader

()

Returns a shader ready to render a textured quad in fullscreen, use with Mesh.getScreenQuad() mesh shader params sampler2D u_texture

Shader.getUniformFunc

(
  • data
)
Function

Tells you which function to call when uploading a uniform according to the data type in the shader Used internally from extractShaderInfo to optimize calls

Parameters:

  • data Object

    info about the uniform

Returns:

Function:

toViewport

(
  • uniforms
)

Renders a fullscreen quad with this shader applied

Parameters:

  • uniforms Object

uniforms

(
  • uniforms
)

Uploads a set of uniforms to the Shader. You dont need to specify types, they are infered from the shader info.

Parameters:

  • uniforms Object

updateShader

(
  • vertexSource
  • fragmentSource
  • macros
)

It updates the code inside one shader

Parameters:

  • vertexSource String
  • fragmentSource String
  • macros Object

    [optional]