ShaderMaterial
Quick answer
ShaderMaterial runs your own GLSL vertex and fragment code while three.js handles compilation, uniforms, and built-in matrices. Provide vertexShader, fragmentShader, and a uniforms object whose entries use a value field, and enable lights, fog, or clipping flags when the shader needs them.
new THREE.ShaderMaterial ( parameters )
Key points
- uniforms entries must be objects like a value wrapper, not raw numbers.
- Built-ins such as modelViewMatrix, projectionMatrix, position, and uv are injected automatically.
- Set glslVersion to THREE.GLSL3 for GLSL ES 3.0 syntax.
Example
const uniforms = {
uTime: { value: 0 },
uColor: { value: new THREE.Color('#4aa3df') },
uAmplitude: { value: 1.4 }
};
const material = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: `
uniform float uTime;
uniform float uAmplitude;
varying float vHeight;
void main() {
vec3 p = position;
float h = sin(p.x * 0.8 + uTime) * cos(p.z * 0.8 + uTime) * uAmplitude;
p.y += h;
vHeight = h;
gl_Position = projectionMatrix * modelViewMatrix * vec4(p, 1.0);
}
`,
fragmentShader: `
uniform vec3 uColor;
varying float vHeight;
void main() {
vec3 c = mix(uColor, vec3(1.0), clamp(vHeight * 0.4 + 0.4, 0.0, 1.0));
gl_FragColor = vec4(c, 1.0);
}
`,
side: THREE.DoubleSide
});
function animate(t) {
uniforms.uTime.value = t * 0.001;
renderer.render(scene, camera);
}
Notes
- Shared uniforms objects affect every material referencing them; clone with UniformsUtils.clone().
- Changing defines requires recompilation, so plan macro combinations rather than toggling often.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). ShaderMaterial. https://www.jingweizhi.com/en/three/api/shader-material.html (accessed 2026-11-03).