Index of every page on this site: llms.txt
All skills
impertio-studio avatar

/threejs-syntax-shaders

@6c190f0
Run it yourself

Use when writing custom GLSL shaders, creating ShaderMaterial or RawShaderMaterial, defining uniforms, or patching built-in materials with onBeforeCompile. Prevents the common mistake of wrong uniform format, missing needsUpdate on uniforms, or not including required defines. Covers ShaderMaterial, RawShaderMaterial, GLSL, uniforms, ShaderChunk, onBeforeCompile, defines, GLSL3. Keywords: ShaderMaterial, RawShaderMaterial, GLSL, vertex shader, fragment shader, uniform, varying, ShaderChunk, onBeforeCompile, custom shader, write shader, custom visual effect, GPU programming.

Use this Skill: https://skilld.dev/gh/impertio-studio/three.js-claude-skill-package/threejs-syntax-shaders

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesexamples.md

≈1.6k tokens on demand. Your agent reads this file only when SKILL.md points to it.

threejs-syntax-shaders -- Examples

Example 1: Basic Custom ShaderMaterial

A minimal ShaderMaterial with a time-based color animation.

import * as THREE from 'three';

const material = new THREE.ShaderMaterial({
  uniforms: {
    uTime: { value: 0.0 },
    uColor: { value: new THREE.Color(0x3399ff) },
  },
  vertexShader: `
    varying vec2 vUv;

    void main() {
      vUv = uv;
      gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
    }
  `,
  fragmentShader: `
    uniform float uTime;
    uniform vec3 uColor;
    varying vec2 vUv;

    void main() {
      float pulse = sin(uTime * 3.0) * 0.5 + 0.5;
      vec3 color = mix(uColor, vec3(1.0), pulse * vUv.y);
      gl_FragColor = vec4(color, 1.0);
    }
  `,
});

const mesh = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), material);

// Animation loop
const clock = new THREE.Clock();
function animate() {
  material.uniforms.uTime.value = clock.getElapsedTime();
  renderer.render(scene, camera);
  requestAnimationFrame(animate);
}
animate();

Example 2: Vertex Displacement with Texture

Displaces vertices along their normals using a height map texture.

import * as THREE from 'three';

const loader = new THREE.TextureLoader();
const heightMap = loader.load('/textures/heightmap.png');

const material = new THREE.ShaderMaterial({
  uniforms: {
    uHeightMap: { value: heightMap },
    uDisplacement: { value: 0.5 },
  },
  vertexShader: `
    uniform sampler2D uHeightMap;
    uniform float uDisplacement;
    varying vec2 vUv;
    varying float vHeight;

    void main() {
      vUv = uv;
      float height = texture2D(uHeightMap, uv).r;
      vHeight = height;
      vec3 displaced = position + normal * height * uDisplacement;
      gl_Position = projectionMatrix * modelViewMatrix * vec4(displaced, 1.0);
    }
  `,
  fragmentShader: `
    varying vec2 vUv;
    varying float vHeight;

    void main() {
      vec3 low = vec3(0.0, 0.2, 0.8);
      vec3 high = vec3(1.0, 0.9, 0.3);
      vec3 color = mix(low, high, vHeight);
      gl_FragColor = vec4(color, 1.0);
    }
  `,
});

const geometry = new THREE.PlaneGeometry(10, 10, 256, 256);
const mesh = new THREE.Mesh(geometry, material);

Example 3: onBeforeCompile -- Patching MeshStandardMaterial

Adds a wave vertex displacement to a standard PBR material while preserving all lighting and shadow behavior.

import * as THREE from 'three';

const material = new THREE.MeshStandardMaterial({
  color: 0x2194ce,
  roughness: 0.4,
  metalness: 0.1,
});

material.onBeforeCompile = (shader) => {
  // Add custom uniforms
  shader.uniforms.uTime = { value: 0.0 };
  shader.uniforms.uAmplitude = { value: 0.3 };

  // Inject uniform declaration at top of vertex shader
  shader.vertexShader = `
    uniform float uTime;
    uniform float uAmplitude;
  ` + shader.vertexShader;

  // Replace begin_vertex chunk to add displacement
  shader.vertexShader = shader.vertexShader.replace(
    '#include <begin_vertex>',
    `
    #include <begin_vertex>
    float wave = sin(transformed.x * 4.0 + uTime * 2.0) *
                 cos(transformed.z * 4.0 + uTime * 1.5);
    transformed.y += wave * uAmplitude;
    `
  );

  // Store shader reference for uniform updates
  material.userData.shader = shader;
};

// ALWAYS override customProgramCacheKey with onBeforeCompile
material.customProgramCacheKey = () => 'wavy-standard';

// Animation loop
const clock = new THREE.Clock();
function animate() {
  if (material.userData.shader) {
    material.userData.shader.uniforms.uTime.value = clock.getElapsedTime();
  }
  renderer.render(scene, camera);
  requestAnimationFrame(animate);
}
animate();

Example 4: RawShaderMaterial with GLSL3

A RawShaderMaterial using GLSL 3.0 ES syntax for full control.

import * as THREE from 'three';

const material = new THREE.RawShaderMaterial({
  glslVersion: THREE.GLSL3,
  uniforms: {
    uProjectionMatrix: { value: new THREE.Matrix4() },
    uModelViewMatrix: { value: new THREE.Matrix4() },
    uTime: { value: 0.0 },
  },
  vertexShader: `
    precision highp float;

    in vec3 position;
    in vec2 uv;

    uniform mat4 uProjectionMatrix;
    uniform mat4 uModelViewMatrix;
    uniform float uTime;

    out vec2 vUv;
    out float vWave;

    void main() {
      vUv = uv;
      vec3 pos = position;
      pos.z = sin(pos.x * 5.0 + uTime) * 0.2;
      vWave = pos.z;
      gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(pos, 1.0);
    }
  `,
  fragmentShader: `
    precision highp float;

    in vec2 vUv;
    in float vWave;

    out vec4 fragColor;

    void main() {
      vec3 color = vec3(vUv, 0.5 + vWave);
      fragColor = vec4(color, 1.0);
    }
  `,
});

// With RawShaderMaterial, you MUST manually update the matrix uniforms
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(4, 4, 64, 64), material);

function animate() {
  mesh.updateMatrixWorld();
  material.uniforms.uModelViewMatrix.value.multiplyMatrices(
    camera.matrixWorldInverse,
    mesh.matrixWorld
  );
  material.uniforms.uProjectionMatrix.value.copy(camera.projectionMatrix);
  material.uniforms.uTime.value = performance.now() / 1000;
  renderer.render(scene, camera);
  requestAnimationFrame(animate);
}

Example 5: ShaderMaterial with Fog and Lights Support

Using ShaderChunk includes to integrate with Three.js fog and lighting systems.

import * as THREE from 'three';

const material = new THREE.ShaderMaterial({
  uniforms: {
    ...THREE.UniformsLib.fog,
    uColor: { value: new THREE.Color(0xff6600) },
  },
  vertexShader: `
    #include <common>
    #include <fog_pars_vertex>

    varying vec3 vNormal;

    void main() {
      vNormal = normalize(normalMatrix * normal);
      vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
      gl_Position = projectionMatrix * mvPosition;
      #include <fog_vertex>
    }
  `,
  fragmentShader: `
    #include <common>
    #include <fog_pars_fragment>

    uniform vec3 uColor;
    varying vec3 vNormal;

    void main() {
      float light = dot(vNormal, normalize(vec3(1.0, 1.0, 1.0)));
      light = clamp(light, 0.2, 1.0);
      gl_FragColor = vec4(uColor * light, 1.0);
      #include <fog_fragment>
    }
  `,
  fog: true,  // REQUIRED to enable fog uniform injection
});

Official Sources

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts5mo3 checks · Risk SAFE
  • Gen Agent Trust Hub5mo

    This skill provides instructions and reference material for writing custom Three.js shaders. It consists entirely of markdown documentation and code examples. No security risks were identified.

  • Socket5mo

    No alerts

  • Snyk5mo

    Risk: LOW · No issues

Signed by skilld at 6c190f0. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 8 months ago
compatibility
Designed for Claude Code. Requires Three.js r160+.
metadata
{
  "author": "OpenAEC-Foundation",
  "version": "1.0"
}

README badge

README badge for impertio-studio/three.js-claude-skill-package/threejs-syntax-shaders