Three best practices

From ngophatneknha/ai-engineering-hub · View interactive version

Three best practices is an AI agent skill from the GitHub repository ngophatneknha/ai-engineering-hub. Three.js performance optimization and best practices guidelines. Use when writing, reviewing, or optimizing Three.js code. Triggers on tasks involving 3D scenes, WebGL/WebGPU rendering, geometries, materials, textures, lighting, shaders, or TSL.

How to use this skill

Skill file

Three.js Best Practices

Comprehensive performance optimization guide for Three.js applications. Contains 120+ rules across 18 categories, prioritized by impact.

Sources & Credits

This skill compiles best practices from multiple authoritative sources:

- Official guidelines from Three.js llms branch maintained by mrdoob

- 100 Three.js Tips by Utsubo - Excellent comprehensive guide covering WebGPU, asset optimization, and performance tips

When to Apply

Reference these guidelines when:

Rule Categories by Priority

| Priority | Category | Impact | Prefix |

|----------|----------|--------|--------|

| 0 | Modern Setup & Imports | FUNDAMENTAL | setup- |

| 1 | Memory Management & Dispose | CRITICAL | memory- |

| 2 | Render Loop Optimization | CRITICAL | render- |

| 3 | Draw Call Optimization | CRITICAL | drawcall- |

| 4 | Geometry & Buffer Management | HIGH | geometry- |

| 5 | Material & Texture Optimization | HIGH | material- |

| 6 | Asset Compression | HIGH | asset- |

| 7 | Lighting & Shadows | MEDIUM-HIGH | lighting- |

| 8 | Scene Graph Organization | MEDIUM | scene- |

| 9 | Shader Best Practices (GLSL) | MEDIUM | shader- |

| 10 | TSL (Three.js Shading Language) | MEDIUM | tsl- |

| 11 | WebGPU Renderer | MEDIUM | webgpu- |

| 12 | Loading & Assets | MEDIUM | loading- |

| 13 | Core Web Vitals | MEDIUM-HIGH | vitals- |

| 14 | Camera & Controls | LOW-MEDIUM | camera- |

| 15 | Animation System | MEDIUM | animation- |

| 16 | Physics Integration | MEDIUM | physics- |

| 17 | WebXR / VR / AR | MEDIUM | webxr- |

| 18 | Audio | LOW-MEDIUM | audio- |

| 19 | Post-Processing | MEDIUM | postpro- |

| 20 | Mobile Optimization | HIGH | mobile- |

| 21 | Production | HIGH | error-, migration- |

| 22 | Debug & DevTools | LOW | debug- |

Quick Reference

0. Modern Setup (FUNDAMENTAL)

1. Memory Management (CRITICAL)

2. Render Loop (CRITICAL)

3. Draw Call Optimization (CRITICAL)

4. Geometry (HIGH)

5. Materials & Textures (HIGH)

6. Asset Compression (HIGH)

7. Lighting & Shadows (MEDIUM-HIGH)

8. Scene Graph (MEDIUM)

9. Shaders GLSL (MEDIUM)

10. TSL - Three.js Shading Language (MEDIUM)

11. WebGPU Renderer (MEDIUM)

12. Loading & Assets (MEDIUM)

13. Core Web Vitals (MEDIUM-HIGH)

14. Camera & Controls (LOW-MEDIUM)

15. Animation (MEDIUM)

16. Physics (MEDIUM)

17. WebXR (MEDIUM)

18. Audio (LOW-MEDIUM)

19. Post-Processing (MEDIUM)

20. Optimization (HIGH)

21. Production (HIGH)

22. Debug & DevTools (LOW)

How to Use

Read individual rule files for detailed explanations and code examples:

rules/setup-use-import-maps.md
rules/memory-dispose-geometry.md
rules/tsl-complete-reference.md
rules/mobile-optimization.md

Each rule file contains:

Key Patterns

Modern Import Maps

<script type="importmap">
{
  "imports": {
    "three": "https://cdn.jsdelivr.net/npm/three@0.182.0/build/three.module.js",
    "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.182.0/examples/jsm/",
    "three/tsl": "https://cdn.jsdelivr.net/npm/three@0.182.0/build/three.tsl.js"
  }
}
</script>

Proper Disposal

function disposeObject(obj) {
  if (obj.geometry) obj.geometry.dispose();
  if (obj.material) {
    if (Array.isArray(obj.material)) {
      obj.material.forEach(m => m.dispose());
    } else {
      obj.material.dispose();
    }
  }
}

TSL Basic Usage

import { texture, uv, color, time, sin } from 'three/tsl';

const material = new THREE.MeshStandardNodeMaterial();
material.colorNode = texture(map).mul(color(0xff0000));
material.colorNode = color(0x00ff00).mul(sin(time).mul(0.5).add(0.5));

Mobile Detection

const isMobile = /Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, isMobile ? 1.5 : 2));
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.