threejs-syntax-shaders

Write custom GLSL shaders and ShaderMaterial configurations for Three.js applications.

1|Updated Aug 20, 2023
One-click install
npx skills add https://github.com/imanlangaran/mini-projects --skill threejs-syntax-shaders-imanlangaran
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: threejs-syntax-shaders
Source: https://github.com/imanlangaran/mini-projects/tree/main/react/three-gsap/.claude/skills/threejs-syntax-shaders
Command: npx skills add https://github.com/imanlangaran/mini-projects --skill threejs-syntax-shaders-imanlangaran

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing custom shaders in Three.js is error-prone: bare uniform values silently fail, redeclaring built-in uniforms causes GLSL compilation errors, and missing customProgramCacheKey makes onBeforeCompile patches disappear. This Skill provides the correct syntax patterns, uniform formats, and anti-patterns to avoid these failures. ## Core Features & Use Cases - ShaderMaterial and RawShaderMaterial guidance: Covers constructor parameters, built-in uniforms and attributes, precision requirements, and when to use each material type. - Uniform and define management: Documents the { value: ... } wrapper format, GLSL-to-JavaScript type mapping, mutation-based updates, and needsUpdate rules for defines. - onBeforeCompile patching: Shows how to modify built-in materials like MeshStandardMaterial while preserving PBR lighting, including customProgramCacheKey requirements. - Use Case: You want to add a wave vertex displacement to a MeshStandardMaterial. The Skill provides the exact onBeforeCompile pattern, cache key override, and null-checked uniform update loop to make it work on the first try. ## Quick Start Write a Three.js ShaderMaterial with a time uniform and a fragment shader that animates color based on UV coordinates.

Frequently Asked Questions about threejs-syntax-shaders

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create a custom ShaderMaterial in Three.js?

Create a THREE.ShaderMaterial with uniforms wrapped in { value: ... } objects, plus vertexShader and fragmentShader GLSL strings. Built-in matrices like projectionMatrix and modelViewMatrix are injected automatically, so use them directly without declaring them.

What is the difference between ShaderMaterial and RawShaderMaterial?

ShaderMaterial automatically injects built-in uniforms, attributes, and precision declarations, and supports #include chunks. RawShaderMaterial injects nothing, so you must declare all uniforms, attributes, and precision qualifiers yourself, and #include directives are not processed.

Why is my Three.js uniform not updating in the shader?

Uniforms fail when passed as bare values instead of { value: ... } objects, or when the uniform object is replaced instead of mutating its .value property. Always update material.uniforms.uTime.value directly in the animation loop.

How do I modify a MeshStandardMaterial shader with onBeforeCompile?

Assign material.onBeforeCompile a callback that edits shader.vertexShader and shader.fragmentShader via string replacement on #include chunks. Always override material.customProgramCacheKey and null-check material.userData.shader before updating uniforms.

Does Three.js ShaderMaterial support GLSL3 syntax?

Yes, set glslVersion: THREE.GLSL3 on the material. In GLSL3 you must replace attribute/varying with in/out, declare an out vec4 instead of using gl_FragColor, and use texture() instead of texture2D().

When should I set material.needsUpdate in Three.js?

Set needsUpdate = true only when changing defines, vertexShader, fragmentShader, lights, fog, or clipping, since these require shader recompilation. Never set it for uniform value changes, which are sent to the GPU automatically each frame.