Uniform

Uniform

new

A shader program's uniform, including the uniform's value. This is most commonly used to change the value of a uniform, but can also be used retrieve a uniform's name and datatype, which is useful for creating user interfaces for tweaking shaders.

Do not create a uniform object with the new keyword; a shader program's uniforms are available via ShaderProgram#getAllUniforms.

Changing a uniform's value will affect future calls to Context#draw that use the corresponding shader program.

The datatype of the value property depends on the datatype used in the GLSL declaration as shown in the examples in the table below.

GLSL JavaScript
uniform float u_float; sp.getAllUniforms().u_float.value = 1.0;
uniform vec2 u_vec2; sp.getAllUniforms().u_vec2.value = new Cartesian2(1.0, 2.0);
uniform vec3 u_vec3; sp.getAllUniforms().u_vec3.value = new Cartesian3(1.0, 2.0, 3.0);
uniform vec4 u_vec4; sp.getAllUniforms().u_vec4.value = new Cartesian4(1.0, 2.0, 3.0, 4.0);
uniform int u_int; sp.getAllUniforms().u_int.value = 1;
uniform ivec2 u_ivec2; sp.getAllUniforms().u_ivec2.value = new Cartesian2(1, 2);
uniform ivec3 u_ivec3; sp.getAllUniforms().u_ivec3.value = new Cartesian3(1, 2, 3);
uniform ivec4 u_ivec4; sp.getAllUniforms().u_ivec4.value = new Cartesian4(1, 2, 3, 4);
uniform bool u_bool; sp.getAllUniforms().u_bool.value = true;
uniform bvec2 u_bvec2; sp.getAllUniforms().u_bvec2.value = new Cartesian2(true, true);
uniform bvec3 u_bvec3; sp.getAllUniforms().u_bvec3.value = new Cartesian3(true, true, true);
uniform bvec4 u_bvec4; sp.getAllUniforms().u_bvec4.value = new Cartesian4(true, true, true, true);
uniform mat2 u_mat2; sp.getAllUniforms().u_mat2.value = new Matrix2(1.0, 2.0, 3.0, 4.0);
uniform mat3 u_mat3; sp.getAllUniforms().u_mat3.value = new Matrix3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
uniform mat4 u_mat4; sp.getAllUniforms().u_mat4.value = new Matrix4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0);
uniform sampler2D u_texture; sp.getAllUniforms().u_texture.value = context.createTexture2D(...);
uniform samplerCube u_cubeMap; sp.getAllUniforms().u_cubeMap.value = context.createCubeMap(...);

When the GLSL uniform is declared as an array, value is also an array as shown in Example 2. Individual members of a struct uniform can be accessed as done in Example 3.

Uniforms whose names starting with czm_, such as czm_viewProjection, are called automatic uniforms; they are implicitly declared and automatically assigned to in Context.draw based on the UniformState.

Example
// Example 1. Create a shader program and set its
// one uniform, a 4x4 matrix, to the identity matrix
var vs =
  'attribute vec4 position; ' +
  'uniform mat4 u_mvp; ' +
  'void main() { gl_Position = u_mvp * position; }';
var fs = // ...
var sp = context.createShaderProgram(vs, fs);

var mvp = sp.getAllUniforms().u_mvp;
console.log(mvp.getName());           // 'u_mvp'
console.log(mvp.getDatatype().name);  // 'FLOAT_MAT4'
mvp.value = Cesium.Matrix4.IDENTITY;

//////////////////////////////////////////////////////////////////////

// Example 2. Setting values for a GLSL array uniform
// GLSL:  uniform float u_float[2];
sp.getAllUniforms().u_float.value = new Cesium.Cartesian2(1.0, 2.0);

// GLSL:  uniform vec4 u_vec4[2];
sp.getAllUniforms().u_vec4.value = [
  Cesium.Cartesian4.UNIT_X,
  Cesium.Cartesian4.UNIT_Y
];

//////////////////////////////////////////////////////////////////////

// Example 3. Setting values for members of a GLSL struct
// GLSL:  uniform struct { float f; vec4 v; } u_struct;
sp.getAllUniforms()['u_struct.f'].value = 1.0;
sp.getAllUniforms()['u_struct.v'].value = new Cartesian4(1.0, 2.0, 3.0, 4.0);
See:
Source:

Members

The value of the uniform. The datatype depends on the datatype used in the GLSL declaration as explained in the Uniform help and shown in the examples below.
Example
// GLSL:  uniform float u_float;
sp.getAllUniforms().u_float.value = 1.0;

// GLSL:  uniform vec4 u_vec4;
sp.getAllUniforms().u_vec4.value = Cesium.Cartesian4.ZERO;

// GLSL:  uniform bvec4 u_bvec4;
sp.getAllUniforms().u_bvec4.value = new Cesium.Cartesian4(true, true, true, true);

// GLSL:  uniform mat4 u_mat4;
sp.getAllUniforms().u_mat4.value = Cesium.Matrix4.IDENTITY;

// GLSL:  uniform sampler2D u_texture;
sp.getAllUniforms().u_texture.value = context.createTexture2D(...);

// GLSL:  uniform vec2 u_vec2[2];
sp.getAllUniforms().u_vec2.value = [
  new Cesium.Cartesian2(1.0, 2.0),
  new Cesium.Cartesian2(3.0, 4.0)
];

// GLSL:  uniform struct { float f; vec4 v; } u_struct;
sp.getAllUniforms()['u_struct.f'].value = 1.0;
sp.getAllUniforms()['u_struct.v'].value = new Cesium.Cartesian4(1.0, 2.0, 3.0, 4.0);
See:

Methods

Returns the datatype of the uniform. This is useful when dynamically creating a user interface to tweak shader uniform values.

Returns:
UniformDatatype The datatype of the uniform.
Example
// GLSL: uniform mat4 u_mvp;
console.log(sp.getAllUniforms().u_mvp.getDatatype().name);  // 'FLOAT_MAT4'
See:

Returns the case-sensitive name of the GLSL uniform.

Returns:
String The name of the uniform.
Example
// GLSL: uniform mat4 u_mvp;
console.log(sp.getAllUniforms().u_mvp.getName());  // 'u_mvp'