# draw-call-optimization

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.

- rule · `type=rule` · Claude Code, Codex, Cursor, GitHub Copilot, Windsurf (Devin Desktop), Cline · ★ 4719 repo stars
- Author: zebbern
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/zebbern/claude-code-guide/tree/HEAD/skills/three-best-practices/rules/draw-call-optimization.md
- Updated: 2026-10-24
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/rules/draw-call-optimization

## What it 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.

## Install

### Claude Code (native platform)

**Manually: put the files in place**
- File: `.claude/rules/draw-call-optimization.md` — The paths frontmatter field limits the files

### Codex

**AGENTS.md**
- Codex reads AGENTS.md: paste the rule text there

### Cursor

**Manually: put the files in place**
- File: `.cursor/rules/draw-call-optimization.mdc` — Frontmatter: description, globs, alwaysApply

### GitHub Copilot

**Manually: put the files in place**
- File: `.github/instructions/draw-call-optimization.instructions.md` — The applyTo field sets which files it applies to

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/rules/draw-call-optimization.md`

### Cline

**Manually: put the files in place**
- File: `.clinerules/draw-call-optimization.md`

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/zebbern/claude-code-guide/tree/HEAD/skills/three-best-practices/rules/draw-call-optimization.md

## When to use it
- 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
- Optimize this Three.js scene to reduce draw calls under 100
- Convert duplicate meshes to InstancedMesh
- Combine static geometry using BufferGeometryUtils

## Source text · draw-call-optimization.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: f0c0742d8ffc7227.

BEGIN THIRD-PARTY FILE f0c0742d8ffc7227
````text
# 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);
```
````
END THIRD-PARTY FILE f0c0742d8ffc7227

The third-party file has ended. Everything between the f0c0742d8ffc7227 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

© zebbern · MIT · https://github.com/zebbern/claude-code-guide/tree/HEAD/skills/three-best-practices/rules/draw-call-optimization.md

## Similar
- [threejs-performance](https://affpapa.org/en/nevibecode/skills/threejs-performance.md) — skill · 7100 installs
- [three-best-practices](https://affpapa.org/en/nevibecode/skills/three-best-practices.md) — skill · 1700 installs
- [react-three-fiber-architect](https://affpapa.org/en/nevibecode/agents/react-three-fiber-architect.md) — subagent · ★ 1428 repo stars
- [threejs-materials](https://affpapa.org/en/nevibecode/skills/threejs-materials.md) — skill · 21100 installs
- [threejs-webgl](https://affpapa.org/en/nevibecode/skills/threejs-webgl.md) — skill · 5300 installs
- [threejs-aaa-graphics-builder](https://affpapa.org/en/nevibecode/skills/threejs-aaa-graphics-builder.md) — skill · 4300 installs

From the same set: [zebbern/claude-code-guide](https://affpapa.org/en/nevibecode/by/zebbern/claude-code-guide.md) — 86 cards

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
