kick.material.Shader Class
GLSL Shader object
The shader basically encapsulates a GLSL shader programs, but makes sure that the correct
WebGL settings are set when the shader is bound (such as if blending is enabled or not).
The Shader extend the default WebGL GLSL in the following way:
-
#pragma include <filename>
includes of the following KickJS file as a string:- light.glsl
- shadowmap.glsl
- Auto binds the following uniform variables:
_mvProj
(mat4) Model view projection matrix_m
(mat4) Model matrix_mv
(mat4) Model view matrix_v
(mat4) View matrix_worldCamPos
(vec4) Camera position in world coordinate_world2object
(mat4) World to Object coordinate transformation_norm
(mat3) Normal matrix (the inverse transpose of the upper 3x3 model view matrix - needed when scaling is scaling is non-uniform)_time
(float) Run time of engine_ambient
(vec3) Ambient light_viewport
(vec2) Viewport size_dLight
(mat3) Directional light matrix._dLight[0]
(vec3) Directional light direction in eye coordinates._dLight[1]
(vec3) Directional light color intensity_dLight[2]
(vec3) Directional light half vector_dLightWorldDir
(vec3) Directional light world direction_pLights[n]
(mat3) Point light matrix_pLights[n][0]
(mat3) Point light id n position_pLights[n][1]
(mat3) Point light id n color intensity_pLights[n][2]
(mat3) Point light id n attenuation vector [const, linear, quadratic]
- Defines
SHADOW
(Boolean) andLIGHTS
(Integer) based on the current configuration of the engine (cannot be modified runtime).
Constructor
kick.material.Shader
-
config
Parameters:
-
config
Object
Example:
var diffuseShader = project.load(project.ENGINE_SHADER_DIFFUSE);
var vertexShaderStr = "attribute vec3 vertex;\n"+
"uniform mat4 _mvProj;\n"+
"void main(void) {\n"+
" gl_Position = _mvProj * vec4(vertex, 1.0);\n"+
"}";
var fragmentShaderStr = "uniform highp float _time;\n"+
"void main(void) {\n"+
" highp float fraction = mod(_time/1000.0,1.0);\n"+
" gl_FragColor = vec4(fraction,fraction,fraction, 1.0);\n"+
"}";
var shader = new kick.material.Shader( {
vertexShaderSrc: vertexShaderStr,
fragmentShaderSrc: fragmentShaderStr
});
Item Index
Methods
Properties
- activeAttributes
- activeUniforms
- blend
- blendDFactor
- blendDFactorAlpha
- blendDFactorRGB
- blendSFactor
- blendSFactorAlpha
- blendSFactorRGB
- dataURI
- defaultUniforms
- depthMask
- engine
- engineUniforms
- errorLog
- faceCulling
- fragmentShaderSrc
- gl
- glState
- lookupAttribute
- lookupUniform
- materialUniforms
- name
- polygonOffsetEnabled
- polygonOffsetFactor
- polygonOffsetUnits
- renderOrder
- shaderProgramId
- shaderVersion
- uid
- vertexShaderSrc
- zTest
Methods
addListener
-
listenerFn
Registers a listener to the shader.
Parameters:
-
listenerFn
Functiona function called when shader is updated
apply
()
Boolean
Updates the shader (must be called after any shader state is changed to apply changes)
Returns:
shader created successfully
bind
()
bindMaterialUniform
-
material
-
engineUniforms
Update the material uniform
Parameters:
-
material
Object -
engineUniforms
Object
bindUniform
-
material
-
engineUniforms
-
transform
Binds the uniforms to the current shader. The uniforms is expected to be in a valid format. The method will call Shader.bindMaterialUniform if material uniforms needs to be changed.
Parameters:
-
material
kick.material.Material -
engineUniforms
Object -
transform
kick.scene.Transform
compileShader
-
str
-
isFragmentShader
Invoke shader compilation
Parameters:
-
str
String -
isFragmentShader
Boolean
contextLost
()
protected
contextRestored
()
protected
This method is public and may be called multiple times (both from materials using the shader and from the engine)
destroy
()
Deletes the shader program from memory. A destroyed shader can be used again if update shader is called
getPrecompiledSource
-
sourcecode
Parameters:
-
sourcecode
String
Returns:
sourcecode after precompiler
init
-
config
-
[resourceTracker]
Configures the object using the configuration data.
Parameters:
-
config
Objectconfiguration data in JSON format
-
[resourceTracker]
ResourceTracker optional
isValid
()
Boolean
Return true if the shader compiled successfully and is not destroyed
Returns:
is shader valid
markUniformUpdated
()
Flush the current shader bound - this force the shader to be reloaded (and its uniforms and vertex attributes are reassigned)
removeListener
-
listenerFn
Removes a listener to the shader.
Parameters:
-
listenerFn
Functiona function called when shader is updated
toJSON
()
Object
Serializes the data into a JSON object (that can be used as a config parameter in the constructor)
Note errorLog are not serialized
Returns:
config element
updateBlendKey
()
private
Updates the blend key that identifies blend+blendSFactor+blendDFactor
The key is used to fast determine if the blend settings needs to be updated
Properties
activeAttributes
Array_Object
Array of JSON data with size,type and name
activeUniforms
Array_kick.material.UniformDescriptor
Array of Object with size,type, name and index properties
blend
Boolean
Enables/disables blending (default is false).
"In RGBA mode, pixels can be drawn using a function that blends the incoming (source) RGBA values with the
RGBA values that are already in the frame buffer (the destination values)"
(From www.Opengl.org)
blendDFactor
Number
Short for blendDFactorAlpha and blendDFactorRGB
Initial value GL_SRC_ALPHA
Must be set to one of: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_DST_COLOR,
GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA,
GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, and
GL_ONE_MINUS_SRC_ALPHA.
See glBlendFunc on opengl.org
blendDFactorAlpha
Number
Specifies the blend d-factor for the alpha channel
Initial value GL_SRC_ALPHA
Must be set to one of: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_DST_COLOR,
GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA,
GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, and
GL_ONE_MINUS_SRC_ALPHA.
See glBlendFunc on opengl.org
Default: Constants.GL_ONE_MINUS_SRC_ALPHA
blendDFactorRGB
Number
Specifies the blend d-factor for the RGB channel
Initial value GL_SRC_ALPHA
Must be set to one of: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_DST_COLOR,
GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA,
GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, and
GL_ONE_MINUS_SRC_ALPHA.
See glBlendFunc on opengl.org
Default: Constants.GL_ONE_MINUS_SRC_ALPHA
blendSFactor
Number
Short for blendSFactorAlpha and blendSFactorRGB
Specifies the blend s-factor
Initial value GL_SRC_ALPHA
Must be set to one of: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_DST_COLOR,
GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA,
GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, and
GL_SRC_ALPHA_SATURATE.
See glBlendFunc on opengl.org
blendSFactorAlpha
Number
Specifies the blend source-factor for the alpha channel
Initial value GL_SRC_ALPHA
Must be set to one of: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_DST_COLOR,
GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA,
GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, and
GL_SRC_ALPHA_SATURATE.
See glBlendFunc on opengl.org
Default: Constants.GL_SRC_ALPHA
blendSFactorRGB
Number
Specifies the blend source-factor for the RGB channel
Initial value GL_SRC_ALPHA
Must be set to one of: GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_DST_COLOR,
GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA,
GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, and
GL_SRC_ALPHA_SATURATE.
See glBlendFunc on opengl.org
Default: Constants.GL_SRC_ALPHA
dataURI
String
When dataURI is specified the shader is expected to have its content from the dataURI. This means when serializing the object only dataURI and name will be saved
defaultUniforms
Object
Get default configuration of shader uniforms
depthMask
Boolean
Enable or disable writing into the depth buffer
Default: true
engineUniforms
Array Array_kick.material.UniformDescriptor
Array of Object with size,type, name and index properties
errorLog
Function
Function that will be invoked in case of error
faceCulling
Object
Must be set to kick.core.Constants.GL_FRONT, kick.core.Constants.GL_BACK (default),
kick.core.Constants.GL_FRONT_AND_BACK, kick.core.Constants.NONE
Note that in faceCulling = GL_FRONT, GL_BACK or GL_FRONT_AND_BACK with face culling enabled
faceCulling = GL_NONE means face culling disabled
Default: Constants.GL_BACK
fragmentShaderSrc
String
gl
Object
Get the gl context of the shader
glState
Object
Get the gl state
lookupAttribute
Object
Lookup of attribute location based on name.
lookupUniform
Object
Lookup of uniform based on name. for each name a kick.material.UniformDescriptor object exist
materialUniforms
Array Array_kick.material.UniformDescriptor
Array of Object with size,type, name and index properties
name
String
polygonOffsetEnabled
Boolean
(From http://www.opengl.org/)
When GL_POLYGON_OFFSET_FILL, GL_POLYGON_OFFSET_LINE, or GL_POLYGON_OFFSET_POINT is enabled, each
fragment's depth value will be offset after it is interpolated from the depth values of the appropriate
vertices. The value of the offset is factor × DZ + r × units , where DZ is a measurement of the change
in depth relative to the screen area of the polygon, and r is the smallest value that is guaranteed to
produce a resolvable offset for a given implementation. The offset is added before the depth test is
performed and before the value is written into the depth buffer.
glPolygonOffset is useful for rendering hidden-line images, for applying decals to surfaces, and for
rendering solids with highlighted edges.
Possible values:
true or false
Default false
Default: false
polygonOffsetFactor
Number
Default: 2.5
polygonOffsetUnits
Number
Default: 10.0
renderOrder
Number
Render order. Default value 1000. The following ranges are predefined:
0-999: Background. Mainly for skyboxes etc
1000-1998 Opaque geometry (default)
1999-1999 Skybox
2000-2999 Transparent. This queue is sorted in a back to front order before rendering.
3000-3999 Overlay
shaderProgramId
ShaderProgram
shaderVersion
Number
Unique shader version (this number will change whenever apply is invoked). The value may be different after serialization.
uid
Number
vertexShaderSrc
String
zTest
Object
The depth test function. Must be one of kick.core.Constants.GL_NEVER, kick.core.Constants.GL_LESS, kick.core.Constants.GL_EQUAL, kick.core.Constants.GL_LEQUAL, kick.core.Constants.GL_GREATER, kick.core.Constants.GL_NOTEQUAL, kick.core.Constants.GL_GEQUAL, kick.core.Constants.GL_ALWAYS
Default: Constants.GL_LESS
Events
fragmentShaderError
Fired when fragment shader is unable to compile
Event Payload:
-
errorMessage
String
linkerError
Fired when shader is unable to link
Event Payload:
-
errorMessage
String
vertexShaderError
Fired when vertex shader is unable to compile
Event Payload:
-
errorMessage
String