new Shader(source, options)
Represents a GLSL shader. The shader class takes the source GLSL code, compiles it and extracts all the attributes and uniforms. It also exposes methods to set the uniform values of this shader.
Please read the basic-setup tutorial to see how to use a shader
and the understanding-shaders tutorial for an in depth discussion on shaders.
Parameters:
Name | Type | Description |
---|---|---|
source |
string | the GLSL source code formatted in a way to include both vertex and fragment shaders. |
options |
object | additional options, not required. Supported options in the code sample below. |
- Source:
Example
{
// Do not compile
// (yes, there is such option, but 99.99% of the time this is not necessary)
doNotCompile: true,
// Preprocesor directives.
// This object will create
// the following directives, attached to both
// vertex and fragment shaders:
// #define COLOR_ONLY
// #define COLOR 1.0 0.0 0.0
directives: [
{ name: 'COLOR_ONLY' },
{ name: 'COLOR', value: '1.0, 0.0, 0.0' }
]
}
Methods
hasUniform() → {Object}
- Source:
Returns:
true if the shader has a uniform that has this name, null otherwise. The object returned has 3 properties: name, location, type.
- Type
- Object
setUniform(uniform, value)
using setUniform is recommended for uniforms that do not change much or unifors that have the same
value for all the objects rendered with this shader. If you need to as uniforms that are different per object
(ex. a 100 balls rendered with the same shader, but each with a different color) then it is better to use the
uniforms
object attached to each instance od SQR.Transform
.
Please refer to the understanding-shaders for more info.
Parameters:
Name | Type | Description |
---|---|---|
uniform |
string | The name of the uniform.
By convection all uniforms in SQR start with a lowercas u and the a capitalized/camelcase name follows.
Example of good uniform names: |
value |
the value of the uniform to set. It will expect a different object depending on the type of the uniform, but there are a few rules as shown in the example below. |
- Source:
Example
var sh = SQR.Shader(glslCodeString);
// glslCodeString = the code loaded from a file or wherever you get it from
// ALWAYS DO THIS FIRST!
sh.use();
// for floats/ints just a number is ok
sh.setUniform('uSpeed', 2);
// ... but a one element array will do too
sh.setUniform('uIntensity', [0.2]);
// for vectors, regular Array or Float32Array is ok
sh.setUniform('uDirection', [0.2, 0.5, 0.3]);
// for matrices, pass in the data property of any Matrix class
sh.setUniform('uBoneMatrix', boneMatrix.data);
// textures expect an instance of SQR.Texture or SQR.Cubemap
sh.setUniform('uNormalMap', SQR.Texture('assets/normalMap.jpg'));
// in all the above cases any object that has a method called 'toUniform' works too
// SQR.V2, SQR.V3 and SQR.Color have that, so:
sh.setUniform('uCenter', new SQR.V3(12, 45, 33));
sh.setUniform('uColor', SQR.Color().fromHex('#ff8000'));
// or, assuming that light is an SQR.Transform:
sh.setUniform('uLighPosition', light.position)
use()
Sets this shader as the current program in GL. This function needs to be called before any uniforms are set.
- Source: