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

/threejs-syntax-materials

@6c190f0
Run it yourself

Use when choosing or configuring materials, loading textures, or setting up PBR workflows in Three.js. Prevents the common mistake of wrong color space on textures, forgetting material.dispose(), or not calling material.needsUpdate after changes. Covers all 15+ material types, PBR metalness/roughness, MeshPhysicalMaterial, texture maps, color space. Keywords: MeshStandardMaterial, MeshPhysicalMaterial, material, texture, PBR, metalness, roughness, normalMap, color space, SRGBColorSpace, blurry texture, pixelated, texture quality, filtering.

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

Nothing lands on disk. Nothing to clean up.

Fork this Skill

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

referencesanti-patterns.md

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

Anti-Patterns (Three.js Materials & Textures)

1. Wrong Color Space on Data Textures

// WRONG: Setting SRGBColorSpace on a normal map corrupts normal vectors
const normalMap = loader.load('normal.jpg');
normalMap.colorSpace = THREE.SRGBColorSpace; // CORRUPTED lighting!

// CORRECT: Data textures ALWAYS use NoColorSpace (the default)
const normalMap = loader.load('normal.jpg');
// colorSpace stays at NoColorSpace -- do NOT change it

// CORRECT: ONLY diffuse/emissive/color textures get SRGBColorSpace
const diffuseMap = loader.load('diffuse.jpg');
diffuseMap.colorSpace = THREE.SRGBColorSpace;

WHY: The renderer performs lighting calculations in linear space. Setting SRGBColorSpace on data textures (normal, roughness, metalness, AO, bump, displacement, alpha) applies an incorrect gamma correction, producing washed-out or oversaturated results and completely wrong surface normals.


2. Opacity Without Transparent Flag

// WRONG: opacity is silently ignored without transparent: true
const material = new THREE.MeshStandardMaterial({
  color: 0xff0000,
  opacity: 0.5    // Has NO visible effect!
});

// CORRECT: ALWAYS pair opacity with transparent
const material = new THREE.MeshStandardMaterial({
  color: 0xff0000,
  opacity: 0.5,
  transparent: true
});

WHY: Three.js skips alpha blending entirely unless transparent is explicitly true. The opacity property exists on the material but is not applied during rendering without the flag.


3. Using MeshPhysicalMaterial Everywhere

// WRONG: Using Physical for a simple colored object
const material = new THREE.MeshPhysicalMaterial({
  color: 0xff4444,
  roughness: 0.5,
  metalness: 0.0
  // No clearcoat, transmission, sheen, iridescence, or anisotropy used
});

// CORRECT: Use MeshStandardMaterial when advanced PBR features are not needed
const material = new THREE.MeshStandardMaterial({
  color: 0xff4444,
  roughness: 0.5,
  metalness: 0.0
});

WHY: MeshPhysicalMaterial compiles a significantly larger shader than MeshStandardMaterial. Every physical material instance adds shader compilation time and GPU overhead even if the advanced features are unused. ONLY use Physical when you need clearcoat, transmission, sheen, iridescence, anisotropy, or dispersion.


4. Forgetting to Dispose Materials and Textures

// WRONG: Removing mesh from scene without disposing GPU resources
scene.remove(mesh);
// GPU memory for material, textures, and geometry is NEVER freed!

// CORRECT: ALWAYS dispose before removing
mesh.geometry.dispose();
for (const key of Object.keys(mesh.material)) {
  const value = mesh.material[key];
  if (value && value.isTexture) {
    value.dispose();
  }
}
mesh.material.dispose();
scene.remove(mesh);

WHY: scene.remove() only removes the object from the scene graph. GPU-side resources (compiled shaders, texture buffers, geometry buffers) are NOT freed automatically. Without explicit dispose() calls, GPU memory leaks accumulate and eventually crash the application or cause severe performance degradation.


5. Repeating Textures Without RepeatWrapping

// WRONG: Setting repeat without changing wrapping mode
const texture = loader.load('tile.jpg');
texture.repeat.set(4, 4);
// Default ClampToEdgeWrapping: texture does NOT tile, edge pixels are stretched

// CORRECT: ALWAYS set wrapping mode when using repeat
const texture = loader.load('tile.jpg');
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(4, 4);

WHY: The default wrapping mode is ClampToEdgeWrapping, which stretches the edge pixels of the texture infinitely. repeat values greater than (1, 1) only produce visible tiling when wrapS and wrapT are set to RepeatWrapping or MirroredRepeatWrapping.


6. Setting linewidth > 1 on LineBasicMaterial

// WRONG: linewidth > 1 is silently ignored on most platforms
const material = new THREE.LineBasicMaterial({
  color: 0xff0000,
  linewidth: 5    // IGNORED on Windows, macOS, most Linux (WebGL limitation)
});

// CORRECT: Use Line2 + LineMaterial from addons for thick lines
import { Line2 } from 'three/addons/lines/Line2.js';
import { LineMaterial } from 'three/addons/lines/LineMaterial.js';
import { LineGeometry } from 'three/addons/lines/LineGeometry.js';

const geometry = new LineGeometry();
geometry.setPositions([0, 0, 0, 1, 1, 0, 2, 0, 0]);

const material = new LineMaterial({
  color: 0xff0000,
  linewidth: 5,    // Works on all platforms (screen-space pixels)
  resolution: new THREE.Vector2(window.innerWidth, window.innerHeight)
});

const line = new Line2(geometry, material);
scene.add(line);

WHY: The WebGL specification does not guarantee support for lineWidth values greater than 1. Most implementations (ANGLE on Windows/macOS, most Linux drivers) cap it at 1. The Line2/LineMaterial addon renders lines as screen-space triangles, bypassing this limitation.


7. Setting needsUpdate Every Frame

// WRONG: Forcing shader recompilation every frame
function animate() {
  material.needsUpdate = true;  // Recompiles shader EVERY frame -- massive waste
  renderer.render(scene, camera);
  requestAnimationFrame(animate);
}

// CORRECT: Only set needsUpdate once, after the change
material.flatShading = true;
material.needsUpdate = true;  // Set ONCE after the change
// Do NOT set again until the next property change

WHY: Setting needsUpdate = true triggers a full shader recompilation on the GPU. This is an expensive operation (can take 10-100ms per material). Doing it every frame causes severe stuttering and wasted GPU cycles. ONLY set it once after changing a property that affects the shader variant.


8. Forgetting uv2 for aoMap and lightMap

// WRONG: Using aoMap without uv2 attribute -- AO will not render
const material = new THREE.MeshStandardMaterial({
  aoMap: aoTexture,
  aoMapIntensity: 1.0
});
const mesh = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), material);
// aoMap is silently ignored because geometry has no uv2 attribute

// CORRECT: ALWAYS add uv2 attribute when using aoMap or lightMap
const geometry = new THREE.BoxGeometry(1, 1, 1);
geometry.setAttribute('uv2', geometry.getAttribute('uv'));
const mesh = new THREE.Mesh(geometry, material);

WHY: aoMap and lightMap sample from the second UV channel (uv2). If the geometry does not have a uv2 attribute, the maps are silently ignored with no error or warning. For simple cases, copying uv to uv2 works. For baked lighting, use a dedicated UV unwrap for uv2.


9. flipY = true on Render Target Textures

// WRONG: Default flipY on render target texture produces upside-down image
const renderTarget = new THREE.WebGLRenderTarget(512, 512);
renderer.setRenderTarget(renderTarget);
renderer.render(scene, camera);
renderer.setRenderTarget(null);

const screenMaterial = new THREE.MeshBasicMaterial({
  map: renderTarget.texture  // Upside down if flipY is not corrected
});

// CORRECT: Render target textures have flipY = false by default (correct)
// Do NOT manually set renderTarget.texture.flipY = true
// If using DataTexture, ALWAYS set flipY = false
const dataTexture = new THREE.DataTexture(data, width, height);
dataTexture.flipY = false;
dataTexture.needsUpdate = true;

WHY: Image files store pixels top-to-bottom, so flipY = true (default) corrects for this. Render targets and DataTextures store pixels bottom-to-top (OpenGL convention). Setting flipY = true on these textures flips an already-correct image upside down.


10. Not Setting Toon Gradient Filter to Nearest

// WRONG: Linear filtering blurs toon shading into smooth gradient
const gradientMap = loader.load('toon_gradient_3.png');
// Default filters: LinearFilter / LinearMipmapLinearFilter -- SMOOTH, not toon

const material = new THREE.MeshToonMaterial({
  color: 0xff4444,
  gradientMap: gradientMap   // Looks like MeshLambertMaterial, not toon!
});

// CORRECT: ALWAYS use NearestFilter for toon gradient maps
const gradientMap = loader.load('toon_gradient_3.png');
gradientMap.minFilter = THREE.NearestFilter;
gradientMap.magFilter = THREE.NearestFilter;

const material = new THREE.MeshToonMaterial({
  color: 0xff4444,
  gradientMap: gradientMap   // Sharp discrete shading steps
});

WHY: Toon shading works by quantizing light intensity into discrete steps using a gradient map (typically 3-5 pixels wide). Linear filtering interpolates between these steps, producing a smooth gradient that looks identical to Lambert shading. NearestFilter preserves the sharp step boundaries that create the cel-shaded look.

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

    The skill provides comprehensive documentation and best practices for Three.js materials and textures. No security issues were detected.

  • 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-materials