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
.claude/rules/draw-call-optimization.mdWhat 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.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/draw-call-optimization.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/draw-call-optimization.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/draw-call-optimization.mdManually: put the files in place
.clinerules/draw-call-optimization.mdStep 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