Skip to main content
rule CX Codex

draw-call-optimization — Claude Code rule: what it does

The draw-call-optimization rule for Claude Code guides Three.js optimization, reducing draw calls to below 100 per frame through instancing, batching, and geometry merging.

MIT ★ 4,719 updated 2026-10-24

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/draw-call-optimization.md
Open on GitHub

What draw-call-optimization does

This rule guides AI assistants in cutting Three.js draw call counts to sustain 60fps across standard devices. It outlines a decision flow for geometry handling: using InstancedMesh for identical meshes, BatchedMesh for diverse geometries with identical materials, and BufferGeometryUtils.mergeGeometries for static meshes.

The guide also covers material sharing, layered textures via DataArrayTexture, and render loop tracking using renderer.info.render.calls. Because separate material instances break batching, the instructions flag per-mesh material creation inside loops.

What it helps with

  • reduce Three.js draw calls below 100 per frame
  • convert repeated objects to InstancedMesh
  • combine varied geometries using BatchedMesh
  • enforce shared material instances across meshes

Example prompts for the agent

  • Optimize this Three.js scene to reduce draw calls under 100
  • Convert duplicate meshes to InstancedMesh
  • Combine static geometry using BufferGeometryUtils

How to install draw-call-optimization

Manually: put the files in place

.claude/rules/draw-call-optimization.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about draw-call-optimization

How to install draw-call-optimization in Claude Code?

Place the rule markdown file at .claude/rules/draw-call-optimization.md.

What does draw-call-optimization do?

It teaches the agent techniques for Three.js optimization, targeting under 100 draw calls per frame through instancing and material sharing.

When does the rule recommend BatchedMesh over InstancedMesh?

It recommends BatchedMesh when rendering distinct geometries that share the same material, reserving InstancedMesh for identical objects.

Source text

draw-call-optimization.md · MIT
# Draw Call Optimization

> Source: [100 Three.js Tips - Utsubo](https://www.utsubo.com/blog/threejs-best-practices-100-tips)

Draw calls are the primary performance bottleneck in most Three.js applications.

## Target: Under 100 Draw Calls Per Frame

Most devices maintain 60fps below 100 draw calls. Check progress via:

```javascript
console.log('Draw calls:', renderer.info.render.calls);
console.log('Triangles:', renderer.info.render.triangles);
```

## Optimization Techniques

### 1. InstancedMesh (Identical Objects)

Reduces N draw calls to 1 for identical geometry.

```javascript
const mesh = new THREE.InstancedMesh(geometry, material, 1000);
const matrix = new THREE.Matrix4();

for (let i = 0; i < 1000; i++) {
  matrix.setPosition(positions[i]);
  mesh.setMatrixAt(i, matrix);
}
mesh.instanceMatrix.needsUpdate = true;
```

### 2. BatchedMesh (Varied Geometries)

Combines multiple geometries sharing materials into single draw call. Allows per-instance geometry variation.

```javascript
const batchedMesh = new THREE.BatchedMesh(
  maxGeometryCount,
  maxVertexCount,
  maxIndexCount,
  material
);

const geoId1 = batchedMesh.addGeometry(geometry1);
const geoId2 = batchedMesh.addGeometry(geometry2);

batchedMesh.addInstance(geoId1);
batchedMesh.addInstance(geoId2);
```

### 3. Merge Static Geometry

```javascript
import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js';

const merged = mergeGeometries([geo1, geo2, geo3]);
const mesh = new THREE.Mesh(merged, sharedMaterial);
```

### 4. Share Materials

```javascript
// BAD: Separate materials per mesh
meshes.forEach(m => {
  m.material = new MeshStandardMaterial({ color: 'red' });
});

// GOOD: Shared material
const sharedMaterial = new MeshStandardMaterial({ color: 'red' });
meshes.forEach(m => {
  m.material = sharedMaterial;
});
```

### 5. Array Textures (Modern Browsers)

Combine multiple textures into layers, accessed by index in shaders:

```javascript
const textureArray = new THREE.DataArrayTexture(data, width, height, depth);
```

### 6. Frustum Culling

Enabled by default. Understand how it works:

```javascript
const frustum = new THREE.Frustum();
const matrix = new THREE.Matrix4().multiplyMatrices(
  camera.projectionMatrix,
  camera.matrixWorldInverse
);
frustum.setFromProjectionMatrix(matrix);

if (frustum.intersectsObject(mesh)) {
  // Object is visible
}
```

## Decision Tree

```
Need to render many objects?
├── All identical geometry?
│   └── Use InstancedMesh
├── Different geometries, same material?
│   └── Use BatchedMesh
├── Static objects?
│   └── Merge with BufferGeometryUtils
└── Dynamic objects?
    └── Consider object pooling + visibility toggling
```

## Monitoring

```javascript
setInterval(() => {
  const info = renderer.info.render;
  console.log(`Calls: ${info.calls}, Tris: ${info.triangles}`);
}, 1000);
```

© zebbern · MIT · zebbern/claude-code-guide

Similar IDE rules

All IDE rules for Game dev and 3D: Unity, Unreal, Godot, Blender →
threejs-performance CloudAI-X threejs-performance — a skill for Claude Code, Codex, Cursor and other platforms measures three.js rendering speed and optimizes scenes. Runs when a scene lags or hits memory limits. CX Codex +1 7,100installs three-best-practices three-agent-skills three-best-practices — a Claude Code skill carrying Three.js performance and best-practice rules. It kicks in when the agent writes, reviews or optimizes 3D scenes, WebGL/WebGPU rendering, shaders and TSL. CX Codex +1 1,700installs react-three-fiber-architect Claude Design Skillstack react-three-fiber-architect — a subagent that plans React Three Fiber scenes: component layout, materials and lights, asset loading, and where rendering gets expensive. CX Codex ★ 1,428repo stars threejs-materials CloudAI-X Skill for Claude Code, Codex, Cursor and other platforms: choosing and configuring three.js materials — from basic to PBR, toon shading and point clouds. Runs when you need to style a mesh, tune blending or switch to WebGPU. CX Codex +1 21,100installs threejs-webgl freshtechbro threejs-webgl configures coding agents to build Three.js scenes, set up WebGL or WebGPU renderers, adjust lighting, and load glTF models for web 3D apps. CX Codex +1 5,300installs threejs-aaa-graphics-builder majidmanzarpour threejs-aaa-graphics-builder — a skill for upgrading Three.js games from rough prototypes to production visuals: builds materials, lighting, VFX, shaders, and evaluates scenes against a scorecard. CX Codex +1 4,300installs

From the same set86

The whole set →
tsl-why-use tsl-why-use — a rule that pushes the coding agent to build custom Three.js materials from TSL nodes rather than patching shaders through onBeforeCompile. CX Codex +1 ★ 4,719repo stars asset-compression Guides your AI coding agent through 3D asset compression for Three.js. Covers Draco, Meshopt, KTX2 textures, and LOD implementation. CX Codex +1 ★ 4,719repo stars perf-never-set-state-in-useframe perf-never-set-state-in-useframe — a react-three-fiber rule that bans setState inside useFrame and pushes you to mutate a ref instead, so animation stops triggering a React render every frame. CX Codex +1 ★ 4,719repo stars frame-delta-time frame-delta-time — a rule that makes every animated movement multiply by delta, so speed stops depending on the display's refresh rate. CX Codex +1 ★ 4,719repo stars object-pooling object-pooling — a rule that pushes the agent to reuse bullets, particles and NPCs from a pool instead of building and discarding meshes every frame. CX Codex +1 ★ 4,719repo stars shader-mobile The shader-mobile rule for Claude Code provides guidelines for optimizing GLSL and Three.js shaders for mobile GPUs to prevent framerate drops on mobile devices. CX Codex +1 ★ 4,719repo stars drei-use-gltf The drei-use-gltf rule guides GLTF loading through useGLTF, including caching and preloading. It fits React Three Fiber scenes that need model assets. CX Codex +1 ★ 4,719repo stars perf-zustand-selectors perf-zustand-selectors — a rule file that pushes the agent to subscribe components to individual Zustand slices instead of the whole store, so a component repaints only when the data it reads changes. CX Codex +1 ★ 4,719repo stars

Project sponsors

Companies that keep this analytics open