GL.Shader Class
Shader class to upload programs to the GPU
Constructor
GL.Shader
-
vertexSource
-
fragmentSource
-
macros
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
Item Index
Methods
- bind
- draw
- drawBuffers
- drawRange
- extractShaderInfo
- getLocation
- hasAttribute
- hasUniform
- setUniform
- Shader.compileSource
- Shader.createFX
- Shader.expandImports
- Shader.fromURL
- Shader.getBlendShader
- Shader.getBlurShader
- Shader.getColoredScreenShader
- Shader.getFlatShader
- Shader.getFXAAShader
- Shader.getPartialQuadShader
- Shader.getQuadShader
- Shader.getScreenShader
- Shader.getUniformFunc
- toViewport
- uniforms
- updateShader
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
Numbercould be gl.LINES, gl.POINTS, gl.TRIANGLES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN
-
index_buffer_name
Stringthe 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
Objectan object containing all the buffers
-
indexBuffer
IndexBuffer -
mode
Numbercould be gl.LINES, gl.POINTS, gl.TRIANGLES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN
-
range_start
Numberfirst primitive to render
-
range_length
Numbernumber 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
Numbercould be gl.LINES, gl.POINTS, gl.TRIANGLES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN
-
start
Numberfirst primitive to render
-
length
Numbernumber of primitives to render
-
index_buffer_name
Stringthe 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
Returns the location of a uniform or attribute
Parameters:
-
name
String
Returns:
location
hasAttribute
-
name
Returns if this shader has an attribute with the given name
Parameters:
-
name
Stringname of the attribute
Returns:
hasUniform
-
name
Returns if this shader has a uniform with the given name
Parameters:
-
name
Stringname of the uniform
Returns:
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
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
Numbercould be gl.VERTEX_SHADER or gl.FRAGMENT_SHADER
-
source
Stringthe source file to compile
Returns:
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
Stringstring containg code, like "color = color * 2.0;"
-
[uniforms=null]
String optionalstring containg extra uniforms, like "uniform vec3 u_pos;"
Shader.expandImports
-
code
-
files
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
Stringthe source code
-
files
Object[Optional] object with files to import from (otherwise Shader.files is used)
Returns:
the code with the lines #import removed and replaced by the code
Shader.fromURL
-
vs_path
-
fs_path
-
on_complete
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
Stringthe url to the vertex shader
-
fs_path
Stringthe url to the fragment shader
-
on_complete
Function[Optional] a callback to call once the shader is ready.
Returns:
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
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
Objectinfo about the uniform
Returns:
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]