H3DU.ShaderProgram
H3DU.ShaderProgram(context, [vertexShader], [fragmentShader])
Deprecated: This class is likely to become a private class. Use the H3DU.ShaderInfo class instead, which is not coupled to WebGL contexts.
Represents a WebGL shader program. A shader program in WebGL consists of a vertex shader (which processes vertices), and a fragment shader (which processes pixels). Shader programs are specially designed for running on a graphics processing unit, or GPU.
When the H3DU.ShaderProgram constructor is called, it will compile and link a shader program from the source text passed to it, but it won't use that program until the use() method is called. If the program is compiled and linked successfully, the constructor will also gather a list of the program's attributes (vertex-specific variables in vertex buffer objects) and uniforms (variables not specific to a vertex).
If compiling or linking the shader program fails, a diagnostic log is output to the JavaScript console.
Parameters
context
(Type: WebGLRenderingContext | WebGL2RenderingContext | Object)
A WebGL context to associate with this scene, or an object, such as H3DU.Scene3D, that implements a no-argumentgetContext
method that returns a WebGL context.vertexShader
(Type: string) (optional)
Source text of a vertex shader, in OpenGL ES Shading Language (GLSL). If null, a default vertex shader is used instead.fragmentShader
(Type: string) (optional)
Source text of a fragment shader in GLSL. If null, a default fragment shader is used instead.
Methods
- dispose
Disposes resources from this shader program. - get
Gets the location of the given uniform or attribute's name in this program. - getContext
Gets the WebGL context associated with this shader program object. - getDefaultFragment
Deprecated: Use H3DU.ShaderInfo.getDefaultFragment instead. - getDefaultVertex
Deprecated: Use H3DU.ShaderInfo.getDefaultVertex instead. - getEdgeDetectEffect
Deprecated: Use H3DU.ShaderInfo.makeEdgeDetectEffect instead. - getInvertEffect
Deprecated: Use H3DU.ShaderInfo.makeInvertEffect instead. - getUniform
Gets the value of the given uniform in this program. - makeCopyEffect
Deprecated: Use H3DU.ShaderInfo.makeCopyEffect instead. - makeEffect
Deprecated: Use H3DU.ShaderInfo.makeEffect instead. - makeEffectFragment
Deprecated: Use H3DU.ShaderInfo.makeEffectFragment instead. - setUniforms
Sets the values of one or more uniforms in this program. - use
Makes this program the active program in the WebGL context associated with it.
H3DU.ShaderProgram#dispose()
Disposes resources from this shader program.
Return Value
This method doesn't return a value. (Type: void)
H3DU.ShaderProgram#get(name)
Gets the location of the given uniform or attribute's name in this program. (Although the location may change each time the shader program is linked, that normally only happens upon construction in the case of H3DU.ShaderInfo.)
Parameters
name
(Type: string)
The name of an attribute or uniform defined in either the vertex or fragment shader of this shader program. If the uniform or attribute is an array, each element in the array is named as in these examples: "unif[0]", "unif[1]". If it's a struct, each member in the struct is named as in these examples: "unif.member1", "unif.member2". If it's an array of struct, each member is named as in these examples: "unif[0].member1", "unif[0].member2".
Return Value
The location of the uniform or attribute name, or null if it doesn't exist. (Type: number | WebGLUniformLocation | null)
H3DU.ShaderProgram#getContext()
Gets the WebGL context associated with this shader program object.
Return Value
Return value. (Type: WebGLRenderingContext | WebGL2RenderingContext)
(static) H3DU.ShaderProgram.getDefaultFragment()
Deprecated: Use H3DU.ShaderInfo.getDefaultFragment instead.
Gets the text of the default fragment shader. Putting "#define SHADING\n" at the start of the return value enables the lighting model. Putting "#define SPECULAR\n" at the start of the return value enables specular highlights (as long as SHADING is also enabled).
Return Value
The resulting shader text. (Type: string)
(static) H3DU.ShaderProgram.getDefaultVertex()
Deprecated: Use H3DU.ShaderInfo.getDefaultVertex instead.
Gets the text of the default vertex shader. Putting "#define SHADING\n" at the start of the return value enables the lighting model.
Return Value
The resulting shader text. (Type: string)
(static) H3DU.ShaderProgram.getEdgeDetectEffect([context])
Deprecated: Use H3DU.ShaderInfo.makeEdgeDetectEffect instead.
Generates a shader program that generates a two-color texture showing the source texture's edges.
Parameters
context
(Type: Object) (optional)
No longer used; ignored.
Return Value
The resulting shader program. (Type: H3DU.ShaderInfo)
(static) H3DU.ShaderProgram.getInvertEffect([context])
Deprecated: Use H3DU.ShaderInfo.makeInvertEffect instead.
Generates a shader program that inverts the colors of a texture.
Parameters
context
(Type: Object) (optional)
No longer used; ignored.
Return Value
The resulting shader program. (Type: H3DU.ShaderInfo)
H3DU.ShaderProgram#getUniform(name)
Gets the value of the given uniform in this program. This method may be called at any time, even if this program is not currently the active program in the WebGL context.
Parameters
name
(Type: string)
The name of a uniform defined in either the vertex or fragment shader of this shader program. See get().
Return Value
The uniform's value, or null if it doesn't exist or if an attribute is named, not a uniform. (Type: Number | Array.<number>)
(static) H3DU.ShaderProgram.makeCopyEffect()
Deprecated: Use H3DU.ShaderInfo.makeCopyEffect instead.
Generates a shader program that copies the colors of a texture.
Return Value
The resulting shader program. (Type: H3DU.ShaderInfo)
(static) H3DU.ShaderProgram.makeEffect(context, functionCode)
Deprecated: Use H3DU.ShaderInfo.makeEffect instead.
Generates a shader program for applying a raster effect (postprocessing effect) to a texture.
Parameters
context
(Type: Object)
No longer used; ignored.functionCode
(Type: string)
A string giving shader code in OpenGL ES Shading Language (GLSL) that must contain a function with the following signature:vec4 textureEffect(sampler2D sampler, vec2 uvCoord, vec2 textureSize)
where
sampler
is the texture sampler,uvCoord
is the texture coordinates ranging from 0 to 1 in each component,textureSize
is the dimensions of the texture in pixels, and the return value is the new color at the given texture coordinates. Besides this requirement, the shader code is also free to define additional uniforms, constants, functions, and so on (but not another "main" function).
Return Value
The resulting shader program. (Type: H3DU.ShaderInfo)
(static) H3DU.ShaderProgram.makeEffectFragment(functionCode)
Deprecated: Use H3DU.ShaderInfo.makeEffectFragment instead.
Generates source code for a fragment shader for applying a raster effect to a texture.
Parameters
functionCode
(Type: string)
See H3DU.ShaderProgram.makeEffect().
Return Value
The source text of the resulting fragment shader. (Type: string)
H3DU.ShaderProgram#setUniforms(uniforms)
Sets the values of one or more uniforms in this program. If this program is not the active program in the WebGL context, saves their values until the next time this object's "use" method is called.
Parameters
uniforms
(Type: Object)
An object whose keys are the names of uniforms defined in either the vertex or fragment shader of this shader program. If the uniform is an array, each element in the array is named as in these examples: "unif[0]", "unif[1]". If it's a struct, each member in the struct is named as in these examples: "unif.member1", "unif.member2". If it's an array of struct, each member is named as in these examples: "unif[0].member1", "unif[0].member2". The value of each key depends on the data type expected for the uniform named by that key. The value can be a 3-, 4-, 9-, or 16-element array if the uniform is a "vec3", "vec4", "mat3", or "mat4", respectively, or a Number if the uniform is a "float" or "int".
Return Value
This object. (Type: H3DU.ShaderProgram)
H3DU.ShaderProgram#use()
Makes this program the active program in the WebGL context associated with it. If any uniforms were saved to be written later (because this program wasn't active in the WebGL context when the "setUniforms" method was called), sets their values now.
Return Value
This object. (Type: H3DU.ShaderProgram)