new Uniform
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.allUniforms.u_float.value = 1.0; |
uniform vec2 u_vec2; |
sp.allUniforms.u_vec2.value = new Cartesian2(1.0, 2.0); |
uniform vec3 u_vec3; |
sp.allUniforms.u_vec3.value = new Cartesian3(1.0, 2.0, 3.0); |
uniform vec4 u_vec4; |
sp.allUniforms.u_vec4.value = new Cartesian4(1.0, 2.0, 3.0, 4.0); |
uniform int u_int; |
sp.allUniforms.u_int.value = 1; |
uniform ivec2 u_ivec2; |
sp.allUniforms.u_ivec2.value = new Cartesian2(1, 2); |
uniform ivec3 u_ivec3; |
sp.allUniforms.u_ivec3.value = new Cartesian3(1, 2, 3); |
uniform ivec4 u_ivec4; |
sp.allUniforms.u_ivec4.value = new Cartesian4(1, 2, 3, 4); |
uniform bool u_bool; |
sp.allUniforms.u_bool.value = true; |
uniform bvec2 u_bvec2; |
sp.allUniforms.u_bvec2.value = new Cartesian2(true, true); |
uniform bvec3 u_bvec3; |
sp.allUniforms.u_bvec3.value = new Cartesian3(true, true, true); |
uniform bvec4 u_bvec4; |
sp.allUniforms.u_bvec4.value = new Cartesian4(true, true, true, true); |
uniform mat2 u_mat2; |
sp.allUniforms.u_mat2.value = new Matrix2(1.0, 2.0, 3.0, 4.0); |
uniform mat3 u_mat3; |
sp.allUniforms.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.allUniforms.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.allUniforms.u_texture.value = context.createTexture2D(...); |
uniform samplerCube u_cubeMap; |
sp.allUniforms.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.allUniforms.u_mvp; console.log(mvp.name); // 'u_mvp' console.log(mvp.datatype.name); // 'FLOAT_MAT4' mvp.value = Cesium.Matrix4.IDENTITY; ////////////////////////////////////////////////////////////////////// // Example 2. Setting values for a GLSL array uniform // GLSL: uniform float u_float[2]; sp.allUniforms.u_float.value = new Cesium.Cartesian2(1.0, 2.0); // GLSL: uniform vec4 u_vec4[2]; sp.allUniforms.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.allUniforms['u_struct.f'].value = 1.0; sp.allUniforms['u_struct.v'].value = new Cartesian4(1.0, 2.0, 3.0, 4.0);
- Uniform#value
- UniformDatatype
- ShaderProgram#getAllUniforms
- UniformState
- Context#draw
- Context#createTexture2D
- Context#createCubeMap
See:
Source:
Members
-
datatype :UniformDatatype
-
The datatype of the uniform. This is useful when dynamically creating a user interface to tweak shader uniform values.
-
datatype :UniformDatatype
-
The datatype of the uniform. This is useful when dynamically creating a user interface to tweak shader uniform values.
-
name :String
-
The case-sensitive name of the GLSL uniform.
-
name :String
-
The case-sensitive name of the GLSL uniform.
-
value
-
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.allUniforms.u_float.value = 1.0; // GLSL: uniform vec4 u_vec4; sp.allUniforms.u_vec4.value = Cesium.Cartesian4.ZERO; // GLSL: uniform bvec4 u_bvec4; sp.allUniforms.u_bvec4.value = new Cesium.Cartesian4(true, true, true, true); // GLSL: uniform mat4 u_mat4; sp.allUniforms.u_mat4.value = Cesium.Matrix4.IDENTITY; // GLSL: uniform sampler2D u_texture; sp.allUniforms.u_texture.value = context.createTexture2D(...); // GLSL: uniform vec2 u_vec2[2]; sp.allUniforms.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.allUniforms['u_struct.f'].value = 1.0; sp.allUniforms['u_struct.v'].value = new Cesium.Cartesian4(1.0, 2.0, 3.0, 4.0);