[page:Material] →

[name]

Material rendered with custom shaders. A shader is a small program written in [link:https://www.opengl.org/documentation/glsl/ GLSL] to run on the GPU. You may want to use a custom shader if you need to: There are the following notes to bear in mind when using a *ShaderMaterial*:

Example

var material = new THREE.ShaderMaterial( { uniforms: { time: { value: 1.0 }, resolution: { value: new THREE.Vector2() } }, attributes: { vertexOpacity: { value: [] } }, vertexShader: document.getElementById( 'vertexShader' ).textContent, fragmentShader: document.getElementById( 'fragmentShader' ).textContent } );

Vertex shaders and fragment shaders

You can specify two different types of shaders for each material:

There are three types of variables in shaders: uniforms, attributes, and varyings:

Note that within the shader itself, uniforms and attributes act like constants; you can only modify their values by passing different values to the buffers from your JavaScript code.

Built-in attributes and uniforms

[page:WebGLRenderer] provides many attributes and uniforms to shaders by default; definitions of these variables are prepended to your *fragmentShader* and *vertexShader* code by [page:WebGLProgram] when the shader is compiled; you don't need to declare them yourself. These variables are described in [page:WebGLProgram].

Some of these uniforms or attributes (e.g. those pertaining lighting, fog, etc.) require properties to be set on the material in order for [page:WebGLRenderer] to copy the appropriate values to the GPU---make sure to set these flags if you want to use these features in your own shader.

If you don't want [page:WebGLProgram] to add anything to your shader code, you can use [page:RawShaderMaterial] instead of this class.

Custom attributes and uniforms

Both custom attributes and uniforms must be declared in your GLSL shader code (within *vertexShader* and/or *fragmentShader*). Custom uniforms must be defined in both the *uniforms* property of your *ShaderMaterial*, whereas any custom attributes must be defined via [page:BufferAttribute] instances. Note that *varying*s only need to be declared within the shader code (not within the material).

To declare a custom attribute, please reference the [page:BufferGeometry] page for an overview, and the [page:BufferAttribute] page for a detailed look at the *BufferAttribute* API.

When creating your attributes, each typed array that you create to hold your attribute's data must be a multiple of your data type's size. For example, if your attribute is a [page:Vector3 THREE.Vector3] type, and you have 3000 vertices in your [page:BufferGeometry], your typed array value must be created with a length of 3000 * 3, or 9000 (one value per-component). A table of each data type's size is shown below for reference:

Attribute sizes
GLSL type JavaScript type Size
float [page:Number] 1
vec2 [page:Vector2 THREE.Vector2] 2
vec3 [page:Vector3 THREE.Vector3] 3
vec3 [page:Color THREE.Color] 3
vec4 [page:Vector4 THREE.Vector4] 4
Note that attribute buffers are not refreshed automatically when their values change. To update custom attributes, set the *needsUpdate* flag to true on the [page:BufferAttribute] of the geometry (see [page:BufferGeometry] for further details).

To declare a custom uniform, use the *uniforms* property: uniforms: { time: { value: 1.0 }, resolution: { value: new THREE.Vector2() } } 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 *THREE.Vector2* objects or ten *number*s.

Uniform types
GLSL type JavaScript type
int [page:Number]
float [page:Number]
bool [page:Boolean]
bool [page:Number]
vec2 [page:Vector2 THREE.Vector2]
vec2 [page:Float32Array Float32Array] (*)
vec2 [page:Array Array] (*)
vec3 [page:Vector3 THREE.Vector3]
vec3 [page:Color THREE.Color]
vec3 [page:Float32Array Float32Array] (*)
vec3 [page:Array Array] (*)
vec4 [page:Vector4 THREE.Vector4]
vec4 [page:Quaternion THREE.Quaternion]
vec4 [page:Float32Array Float32Array] (*)
vec4 [page:Array Array] (*)
mat2 [page:Float32Array Float32Array] (*)
mat2 [page:Array Array] (*)
mat3 [page:Matrix3 THREE.Matrix3]
mat3 [page:Float32Array Float32Array] (*)
mat3 [page:Array Array] (*)
mat4 [page:Matrix3 THREE.Matrix4]
mat4 [page:Float32Array Float32Array] (*)
mat4 [page:Array Array] (*)
ivec2, bvec2 [page:Float32Array Float32Array] (*)
ivec2, bvec2 [page:Array Array] (*)
ivec3, bvec3 [page:Int32Array Int32Array] (*)
ivec3, bvec3 [page:Array Array] (*)
ivec4, bvec4 [page:Int32Array Int32Array] (*)
ivec4, bvec4 [page:Array Array] (*)
sampler2D [page:Texture THREE.Texture]
samplerCube [page:CubeTexture THREE.CubeTexture]

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

Constructor

[name]( [page:Object parameters] )

parameters -- An object containing various parameters setting up shaders and their uniforms.
shading — Define shading type. Default is THREE.SmoothShading.
fog — Define whether the material color is affected by global fog settings. Default is true.
wireframe — render geometry as wireframe. Default is false.
wireframeLinewidth — Line thickness. Default is 1.
vertexColors — Define how the vertices gets colored. Default is THREE.NoColors.
skinning — Define whether the material uses skinning. Default is false.
morphTargets — Define whether the material uses morphTargets. Default is false.

Properties

See the base [page:Material] class for common properties.

[property:Object uniforms]

Object specifying the uniforms to be passed to the shader code; keys are uniform names, values are definitions of the form { value: 1.0 } where *value* is the value of the uniform. Names must match the name of the uniform, as defined in the GLSL code. Note that uniforms are refreshed on every frame, so updating the value of the uniform will immediately update the value available to the GLSL code.

[property:Object defines]

Defines custom constants using *#define* directives within the GLSL code for both the vertex shader and the fragment shader; each key/value pair yields another directive: defines: { FOO: 15, BAR: true } yields the lines #define FOO 15 #define BAR true in the GLSL code.

[property:String vertexShader]

Vertex shader GLSL code. This is the actual code for the shader. In the example above, the *vertexShader* and *fragmentShader* code is extracted from the DOM; it could be passed as a string directly or loaded via AJAX instead.

[property:String fragmentShader]

Fragment shader GLSL code. This is the actual code for the shader. In the example above, the *vertexShader* and *fragmentShader* code is extracted from the DOM; it could be passed as a string directly or loaded via AJAX instead.

[property:Number shading]

Define shading type, which determines whether normals are smoothed between vertices; possible values are [page:Materials THREE.SmoothShading] or [page:Materials THREE.FlatShading]. Default is THREE.SmoothShading.

[property:Number linewidth]

Controls line thickness; only effective if the material is attached to a [page:Line]. Default is 1.
Due to limitations in the ANGLE layer, on Windows platforms linewidth will always be 1 regardless of the set value.

[property:Boolean wireframe]

Render geometry as wireframe (using GL_LINES instead of GL_TRIANGLES). Default is false (i.e. render as flat polygons).

[property:Number wireframeLinewidth]

Controls wireframe thickness; only effective if the material is attached to a [page:Mesh] and *wireframe* is true. Default is 1.
Due to limitations in the ANGLE layer, on Windows platforms linewidth will always be 1 regardless of the set value.

[property:Boolean fog]

Define whether the material color is affected by global fog settings; true to pass fog uniforms to the shader. Default is false.

[property:Boolean lights]

Defines whether this material uses lighting; true to pass uniform data related to lighting to this shader. Default is false.

[property:Boolean clipping]

Defines whether this material supports clipping; true to let the renderer pass the clippingPlanes uniform. Default is false.

[property:Number vertexColors]

Define how the vertices are colored, by defining how the *colors* attribute gets populated. Possible values are [page:Materials THREE.NoColors], [page:Materials THREE.FaceColors] and [page:Materials THREE.VertexColors]. Default is THREE.NoColors.

[property:Boolean skinning]

Define whether the material uses skinning; true to pass skinning attributes to the shader. Default is false.

[property:Boolean morphTargets]

Defines whether the material uses morphTargets; true morphTarget attributes to this shader

[property:boolean morphNormals]

Defines whether the material uses morphNormals. Set as true to pass morphNormal attributes from the [page:Geometry] to the shader. Default is *false*.

[property:WebGLProgram program]

The compiled shader program associated with this material, generated by [page:WebGLRenderer]. You should not need to access this property.

Methods

[method:ShaderMaterial clone]() [page:ShaderMaterial this]

Generates a shallow copy of this material. Note that the vertexShader and fragmentShader are copied by reference, as are the definitions of the *attributes*; this means that clones of the material will share the same compiled [page:WebGLProgram]. However, the *uniforms* are copied by value, which allows you to have different sets of uniforms for different copies of the material.

Source

[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]