Skip to main content

Uniform

Uniforms are global GLSL variables. They are passed to shader programs.

Code Example

When declaring a uniform of a ShaderMaterial, it is declared by value or by object.

uniforms: { time: { value: 1.0 }, resolution: new Uniform( new Vector2() ) };  

Uniform types

Each uniform must have a value property. The type of the value must correspond to the type of the uniform variable in the GLSL code as specified for the primitive GLSL types in the table below. Uniform structures and arrays are also supported. GLSL arrays of primitive type must either be specified as an array of the corresponding THREE objects or as a flat array containing the data of all the objects. In other words; GLSL primitives in arrays must not be represented by arrays. This rule does not apply transitively. An array of vec2 arrays, each with a length of five vectors, must be an array of arrays, of either five Vector2 objects or ten numbers.

Uniform typesGLSL typeJavaScript type
intNumber
uint (WebGL 2)Number
floatNumber
boolBoolean
boolNumber
vec2THREE.Vector2
vec2Float32Array (*)
vec2Array (*)
vec3THREE.Vector3
vec3THREE.Color
vec3Float32Array (*)
vec3Array (*)
vec4THREE.Vector4
vec4THREE.Quaternion
vec4Float32Array (*)
vec4Array (*)
mat2Float32Array (*)
mat2Array (*)
mat3THREE.Matrix3
mat3Float32Array (*)
mat3Array (*)
mat4THREE.Matrix4
mat4Float32Array (*)
mat4Array (*)
ivec2, bvec2Float32Array (*)
ivec2, bvec2Array (*)
ivec3, bvec3Int32Array (*)
ivec3, bvec3Array (*)
ivec4, bvec4Int32Array (*)
ivec4, bvec4Array (*)
sampler2DTHREE.Texture
samplerCubeTHREE.CubeTexture

(*) Same for an (innermost) array (dimension) of the same GLSL type, containing the components of all vectors or matrices in the array.

Structured Uniforms

Sometimes you want to organize uniforms as structs in your shader code. The following style must be used so three.js is able to process structured uniform data.

uniforms = { data: { value: { position: new Vector3(), direction: new Vector3(
0, 0, 1 ) } } };

This definition can be mapped on the following GLSL code:

struct Data { vec3 position; vec3 direction; }; uniform Data data;  

Structured Uniforms with Arrays

It's also possible to manage structs in arrays. The syntax for this use case looks like so:

const entry1 = { position: new Vector3(), direction: new Vector3( 0, 0, 1 ) };
const entry2 = { position: new Vector3( 1, 1, 1 ), direction: new Vector3( 0,
1, 0 ) }; uniforms = { data: { value: [ entry1, entry2 ] } };

This definition can be mapped on the following GLSL code:

struct Data { vec3 position; vec3 direction; }; uniform Data data[ 2 ];  

Constructor

Uniform

function Uniform( value: Object ): void;  

value -- An object containing the value to set up the uniform. It's type must be one of the Uniform Types described above.

Properties

value

value: Object;  

Current value of the uniform.

Methods

clone

function clone( ): Uniform;  

Returns a clone of this uniform.
If the uniform's value property is an Object with a clone() method, this is used, otherwise the value is copied by assignment. Array values are shared between cloned Uniforms.

Source

src/core/Uniform.js