mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+215
-301
@@ -1,311 +1,225 @@
|
||||
---
|
||||
description: This rule provides guidelines and best practices for developing efficient, maintainable, and robust 3D web applications using Three.js. It covers aspects like code organization, performance optimization, security, testing, and common pitfalls to ensure a high-quality development experience.
|
||||
globs: *.js,*.ts,*.jsx,*.tsx,*.mjs,*.html
|
||||
description: Enforce modern, performant, and maintainable three-js development practices, focusing on WebGL error handling, efficient asset management, and render loop optimization.
|
||||
globs: **/*.{js,jsx}
|
||||
---
|
||||
# Three.js Best Practices and Coding Standards
|
||||
# three-js Best Practices
|
||||
|
||||
This document outlines the best practices and coding standards for developing with Three.js. Adhering to these guidelines will help you create efficient, maintainable, and robust 3D web applications.
|
||||
This guide establishes the definitive coding standards for three-js development, ensuring robust, performant, and maintainable 3D applications. Adhere to these rules to prevent common pitfalls and leverage three-js effectively.
|
||||
|
||||
## Library Information:
|
||||
## 1. WebGL Error Handling & Extension Support
|
||||
|
||||
- Name: three.js
|
||||
- Tags: 3d, graphics, webgl, javascript
|
||||
Always treat WebGL as a low-level layer requiring explicit error checks. Never assume universal extension support.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
### ✅ GOOD: Proactive Error Checking & Graceful Fallbacks
|
||||
|
||||
### 1.1. Directory Structure Best Practices:
|
||||
During development, actively check `gl.getError()` to catch issues beyond `OUT_OF_MEMORY` or `CONTEXT_LOST`. In production, avoid blocking calls like `getError()` and `getParameter()`. Query extension support and provide fallbacks.
|
||||
|
||||
A well-organized directory structure enhances project maintainability and scalability. Consider the following structure:
|
||||
|
||||
|
||||
project-root/
|
||||
├── src/
|
||||
│ ├── components/ # Reusable 3D components (e.g., models, scenes)
|
||||
│ │ ├── MyComponent.js
|
||||
│ │ └── ...
|
||||
│ ├── scenes/ # Specific scenes for different parts of the application
|
||||
│ │ ├── MainScene.js
|
||||
│ │ └── ...
|
||||
│ ├── models/ # 3D model files (e.g., .glb, .obj)
|
||||
│ │ ├── myModel.glb
|
||||
│ │ └── ...
|
||||
│ ├── textures/ # Texture files (e.g., .jpg, .png)
|
||||
│ │ ├── myTexture.jpg
|
||||
│ │ └── ...
|
||||
│ ├── shaders/ # Custom shader code (GLSL)
|
||||
│ │ ├── vertexShader.glsl
|
||||
│ │ └── fragmentShader.glsl
|
||||
│ ├── utils/ # Utility functions (e.g., math, helpers)
|
||||
│ │ ├── mathUtils.js
|
||||
│ │ └── ...
|
||||
│ ├── core/ # Core application logic (e.g., scene initialization, rendering loop)
|
||||
│ │ ├── renderer.js
|
||||
│ │ ├── camera.js
|
||||
│ │ ├── scene.js
|
||||
│ │ └── controls.js
|
||||
│ ├── app.js # Main application entry point
|
||||
│ └── index.html # HTML file to load the application
|
||||
├── dist/ # Distribution build
|
||||
├── node_modules/ # Node modules
|
||||
├── .gitignore
|
||||
├── package.json
|
||||
└── webpack.config.js # Or equivalent build tool configuration
|
||||
|
||||
|
||||
### 1.2. File Naming Conventions:
|
||||
|
||||
Consistent file naming improves code readability.
|
||||
|
||||
- **Components:** Use PascalCase (e.g., `MyComponent.js`).
|
||||
- **Scenes:** Use PascalCase (e.g., `MainScene.js`).
|
||||
- **Models:** Use camelCase (e.g., `myModel.glb`).
|
||||
- **Textures:** Use camelCase (e.g., `myTexture.jpg`).
|
||||
- **Shaders:** Use camelCase (e.g., `vertexShader.glsl`, `fragmentShader.glsl`).
|
||||
- **Utilities:** Use camelCase (e.g., `mathUtils.js`).
|
||||
|
||||
### 1.3. Module Organization:
|
||||
|
||||
Employ ES modules for better code organization and dependency management.
|
||||
|
||||
javascript
|
||||
// MyComponent.js
|
||||
import * as THREE from 'three';
|
||||
|
||||
export class MyComponent extends THREE.Mesh {
|
||||
constructor() {
|
||||
super();
|
||||
// ... component logic ...
|
||||
}
|
||||
```javascript
|
||||
// Development-only error checking (e.g., in a debug utility)
|
||||
function checkGLErrors(gl) {
|
||||
let error = gl.getError();
|
||||
while (error !== gl.NO_ERROR) {
|
||||
console.error(`WebGL Error: ${error}`);
|
||||
error = gl.getError();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
javascript
|
||||
// app.js
|
||||
import { MyComponent } from './components/MyComponent.js';
|
||||
import { MainScene } from './scenes/MainScene.js';
|
||||
|
||||
const scene = new MainScene();
|
||||
const myComponent = new MyComponent();
|
||||
scene.add(myComponent);
|
||||
|
||||
// ... rest of the application logic ...
|
||||
|
||||
|
||||
### 1.4. Component Architecture:
|
||||
|
||||
Use a component-based architecture to create reusable and modular 3D elements.
|
||||
|
||||
- Encapsulate Three.js objects (e.g., meshes, lights) within components.
|
||||
- Create components with well-defined interfaces (props and methods).
|
||||
- Use a scene graph to manage the hierarchy of components.
|
||||
|
||||
### 1.5. Code Splitting Strategies:
|
||||
|
||||
For large applications, use code splitting to improve initial load time.
|
||||
|
||||
- Split the application into smaller chunks based on routes or features.
|
||||
- Use dynamic imports to load components or scenes on demand.
|
||||
- Leverage Webpack or Parcel to configure code splitting.
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
### 2.1. Design Patterns:
|
||||
|
||||
- **Factory Pattern:** Use factory functions or classes to create complex Three.js objects. This can encapsulate the object creation logic, making it easier to manage and reuse. For example, a `ModelFactory` can load and process different 3D model formats.
|
||||
- **Observer Pattern:** Implement an observer pattern for handling events and interactions in the 3D scene. For instance, you can use this to notify components when a user clicks on a specific object or when the scene changes.
|
||||
- **Singleton Pattern:** For global resources like the renderer or scene manager, consider using a singleton pattern to ensure only one instance exists. However, be cautious as singletons can sometimes make testing more difficult.
|
||||
- **Command Pattern:** Decouple the execution of actions from their trigger by encapsulating them in command objects. This allows for easier undo/redo functionality and more flexible control over the 3D scene.
|
||||
|
||||
### 2.2. Recommended Approaches for Common Tasks:
|
||||
|
||||
- **Loading Models:** Use `GLTFLoader` for loading GLTF/GLB models, which are efficient and widely supported. Handle asynchronous loading using `async/await` or Promises.
|
||||
- **Texture Management:** Use `TextureLoader` to load textures. Ensure textures are properly disposed of when no longer needed to prevent memory leaks.
|
||||
- **Animation:** Use `AnimationMixer` to play animations from loaded models. Optimize animation performance by reducing the number of animated objects and keyframes.
|
||||
- **User Interaction:** Use `Raycaster` to detect intersections between the mouse cursor and 3D objects. Optimize raycasting by using bounding box checks and limiting the number of objects tested.
|
||||
|
||||
### 2.3. Anti-patterns and Code Smells:
|
||||
|
||||
- **Creating Objects Inside Render Loop:** Avoid creating new Three.js objects (e.g., `THREE.Mesh`, `THREE.Material`) within the render loop. This can lead to significant performance degradation due to constant memory allocation and garbage collection. Instead, create objects once and reuse them.
|
||||
- **Unnecessary Object Updates:** Avoid updating object properties (position, rotation, scale) unnecessarily in the render loop. Only update properties when they actually change.
|
||||
- **Ignoring Memory Management:** Failing to dispose of Three.js objects (geometries, materials, textures) when they are no longer needed will lead to memory leaks. Always call `dispose()` on these objects to release resources.
|
||||
- **Overly Complex Shaders:** Writing overly complex or inefficient shaders can negatively impact performance. Optimize shaders by reducing the number of calculations, using simpler algorithms, and minimizing texture lookups.
|
||||
- **Direct Manipulation of `__webgl` properties:** Avoid directly accessing or manipulating internal properties of Three.js objects that start with `__webgl`. These are implementation details and are subject to change, which can break your code.
|
||||
|
||||
### 2.4. State Management Best Practices:
|
||||
|
||||
- **Centralized State:** For complex applications, consider using a centralized state management library like Zustand, Redux, or Vuex to manage the application's state and synchronize changes across components. This provides a single source of truth and simplifies state updates.
|
||||
- **Immutable State:** Prefer immutable state updates to make debugging easier and improve performance by avoiding unnecessary re-renders. Libraries like Immer can help with immutable state management.
|
||||
- **Reactive Programming:** Consider using a reactive programming library like RxJS or MobX to handle asynchronous data streams and side effects in a declarative way.
|
||||
|
||||
### 2.5. Error Handling Patterns:
|
||||
|
||||
- **Try-Catch Blocks:** Use `try-catch` blocks to handle potential errors during model loading, texture loading, and other asynchronous operations.
|
||||
- **Error Boundaries:** In React or other component-based frameworks, use error boundaries to catch errors in child components and prevent the entire application from crashing.
|
||||
- **Logging:** Implement a robust logging system to track errors and debug issues in production. Use a library like Winston or Bunyan for structured logging.
|
||||
- **Fallback Mechanisms:** Provide fallback mechanisms in case of errors. For example, if a model fails to load, display a placeholder object or an error message.
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
### 3.1. Optimization Techniques:
|
||||
|
||||
- **Minimize Draw Calls:** Reduce the number of draw calls by merging geometries, using instancing, and using optimized materials.
|
||||
- **Optimize Shaders:** Simplify shader code, reduce the number of calculations, and minimize texture lookups.
|
||||
- **Use LOD (Level of Detail):** Implement LOD to render objects with lower polygon counts when they are far from the camera.
|
||||
- **Frustum Culling:** Enable frustum culling to prevent objects that are outside the camera's view from being rendered.
|
||||
- **Use Compressed Textures:** Use compressed texture formats like ETC1, PVRTC, or ASTC to reduce texture size and improve loading times.
|
||||
- **GPU Instancing:** Use `InstancedMesh` to render multiple copies of the same geometry with different transformations using a single draw call.
|
||||
- **Occlusion Culling:** Implement occlusion culling to prevent objects that are hidden behind other objects from being rendered.
|
||||
- **Use WebGL2:** If possible, use WebGL2, which offers performance improvements over WebGL1.
|
||||
|
||||
### 3.2. Memory Management:
|
||||
|
||||
- **Dispose of Objects:** Always dispose of Three.js objects (geometries, materials, textures) when they are no longer needed. Use the `dispose()` method.
|
||||
- **Reuse Objects:** Reuse Three.js objects whenever possible instead of creating new ones.
|
||||
- **Avoid Memory Leaks:** Be mindful of memory leaks, especially when dealing with event listeners and closures.
|
||||
- **Use `BufferGeometry`:** `BufferGeometry` is more memory efficient than `Geometry`. Use it whenever possible.
|
||||
|
||||
### 3.3. Rendering Optimization:
|
||||
|
||||
- **Use `requestAnimationFrame`:** Use `requestAnimationFrame` to synchronize rendering with the browser's refresh rate.
|
||||
- **Limit Frame Rate:** Limit the frame rate to prevent unnecessary rendering and reduce CPU usage.
|
||||
- **Optimize Camera Movement:** Optimize camera movement and avoid unnecessary updates to the camera's position and rotation.
|
||||
- **Use Post-processing Effects Sparingly:** Post-processing effects can be expensive. Use them sparingly and optimize their parameters.
|
||||
|
||||
### 3.4. Bundle Size Optimization:
|
||||
|
||||
- **Tree Shaking:** Use a bundler like Webpack or Parcel with tree shaking enabled to remove unused code from the Three.js library.
|
||||
- **Code Splitting:** Split the application into smaller chunks to reduce the initial load time.
|
||||
- **Minification:** Minify JavaScript and CSS code to reduce bundle size.
|
||||
- **Gzip Compression:** Enable Gzip compression on the server to reduce the size of the transferred files.
|
||||
|
||||
### 3.5. Lazy Loading:
|
||||
|
||||
- **Lazy Load Models and Textures:** Lazy load models and textures when they are needed instead of loading them all at once.
|
||||
- **Use Dynamic Imports:** Use dynamic imports to load modules on demand.
|
||||
- **Implement a Loading Screen:** Display a loading screen while assets are being loaded.
|
||||
|
||||
## 4. Security Best Practices
|
||||
|
||||
### 4.1. Common Vulnerabilities and Prevention:
|
||||
|
||||
- **Cross-Site Scripting (XSS):** Prevent XSS by sanitizing user input and avoiding the use of `innerHTML` to inject dynamic content.
|
||||
- **Cross-Site Request Forgery (CSRF):** Protect against CSRF by using anti-CSRF tokens in forms and API requests.
|
||||
- **Third-Party Dependencies:** Regularly audit and update third-party dependencies to patch security vulnerabilities.
|
||||
|
||||
### 4.2. Input Validation:
|
||||
|
||||
- **Validate User Input:** Validate user input to prevent malicious data from being processed by the application.
|
||||
- **Sanitize Data:** Sanitize data before displaying it to the user to prevent XSS attacks.
|
||||
- **Use a Validation Library:** Use a validation library like Joi or Yup to simplify input validation.
|
||||
|
||||
### 4.3. Authentication and Authorization:
|
||||
|
||||
- **Use Secure Authentication:** Use a secure authentication protocol like OAuth 2.0 or OpenID Connect.
|
||||
- **Implement Authorization:** Implement authorization to restrict access to sensitive data and functionality.
|
||||
- **Use JSON Web Tokens (JWT):** Use JWTs for secure authentication and authorization.
|
||||
|
||||
### 4.4. Data Protection:
|
||||
|
||||
- **Encrypt Sensitive Data:** Encrypt sensitive data at rest and in transit.
|
||||
- **Use HTTPS:** Use HTTPS to encrypt communication between the client and the server.
|
||||
- **Protect API Keys:** Protect API keys and other sensitive credentials.
|
||||
|
||||
### 4.5. Secure API Communication:
|
||||
|
||||
- **Use HTTPS:** Always use HTTPS for API communication to protect data in transit.
|
||||
- **Validate API Responses:** Validate API responses to ensure that the data is valid and not malicious.
|
||||
- **Implement Rate Limiting:** Implement rate limiting to prevent abuse of the API.
|
||||
|
||||
## 5. Testing Approaches
|
||||
|
||||
### 5.1. Unit Testing:
|
||||
|
||||
- **Test Individual Components:** Unit test individual Three.js components to ensure that they are functioning correctly.
|
||||
- **Use a Testing Framework:** Use a testing framework like Jest or Mocha to write and run unit tests.
|
||||
- **Mock Dependencies:** Mock dependencies to isolate the component being tested.
|
||||
|
||||
### 5.2. Integration Testing:
|
||||
|
||||
- **Test Interactions Between Components:** Integration test the interactions between different Three.js components to ensure that they are working together correctly.
|
||||
- **Test Scene Rendering:** Test the rendering of the scene to ensure that the objects are being displayed correctly.
|
||||
|
||||
### 5.3. End-to-End Testing:
|
||||
|
||||
- **Test the Entire Application:** End-to-end test the entire application to ensure that all components are working together correctly.
|
||||
- **Use a Testing Tool:** Use a testing tool like Cypress or Puppeteer to automate end-to-end tests.
|
||||
|
||||
### 5.4. Test Organization:
|
||||
|
||||
- **Organize Tests by Component:** Organize tests by component to make it easier to find and maintain tests.
|
||||
- **Use Descriptive Test Names:** Use descriptive test names to make it clear what each test is testing.
|
||||
- **Keep Tests Concise:** Keep tests concise and focused on a single aspect of the component being tested.
|
||||
|
||||
### 5.5. Mocking and Stubbing:
|
||||
|
||||
- **Mock Three.js Objects:** Mock Three.js objects to isolate the component being tested.
|
||||
- **Stub API Calls:** Stub API calls to prevent the tests from making real API requests.
|
||||
- **Use a Mocking Library:** Use a mocking library like Jest or Sinon to simplify mocking and stubbing.
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
### 6.1. Frequent Mistakes:
|
||||
|
||||
- **Not Disposing of Objects:** Forgetting to dispose of Three.js objects, leading to memory leaks.
|
||||
- **Creating Objects in Render Loop:** Creating new objects within the render loop, causing performance issues.
|
||||
- **Ignoring Performance:** Not paying attention to performance and creating inefficient scenes.
|
||||
|
||||
### 6.2. Edge Cases:
|
||||
|
||||
- **Mobile Devices:** Mobile devices have limited resources, so optimize performance for mobile.
|
||||
- **Older Browsers:** Older browsers may not support WebGL2, so ensure compatibility with WebGL1.
|
||||
- **Different Screen Sizes:** Different screen sizes require responsive design to ensure the application looks good on all devices.
|
||||
|
||||
### 6.3. Version-Specific Issues:
|
||||
|
||||
- **Breaking Changes:** Be aware of breaking changes in new versions of Three.js and update code accordingly.
|
||||
- **Deprecations:** Pay attention to deprecation warnings and update code to use the new APIs.
|
||||
|
||||
### 6.4. Compatibility Concerns:
|
||||
|
||||
- **WebGL Support:** Ensure that the user's browser supports WebGL.
|
||||
- **Device Capabilities:** Check the user's device capabilities and adjust the rendering settings accordingly.
|
||||
|
||||
### 6.5. Debugging Strategies:
|
||||
|
||||
- **Use the Browser Developer Tools:** Use the browser developer tools to inspect the scene, debug JavaScript code, and profile performance.
|
||||
- **Use the Three.js Inspector:** Use the Three.js inspector to inspect the scene graph, object properties, and shader code.
|
||||
- **Console Logging:** Use console logging to track the execution flow and debug issues.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
### 7.1. Recommended Development Tools:
|
||||
|
||||
- **IDE:** Visual Studio Code, WebStorm
|
||||
- **Browser:** Chrome, Firefox, Safari
|
||||
- **Three.js Inspector:** A browser extension for inspecting Three.js scenes.
|
||||
|
||||
### 7.2. Build Configuration:
|
||||
|
||||
- **Use a Bundler:** Use a bundler like Webpack or Parcel to bundle the application's code and dependencies.
|
||||
- **Configure Tree Shaking:** Configure tree shaking to remove unused code from the Three.js library.
|
||||
- **Minify Code:** Minify JavaScript and CSS code to reduce bundle size.
|
||||
|
||||
### 7.3. Linting and Formatting:
|
||||
|
||||
- **Use ESLint:** Use ESLint to enforce coding style and identify potential errors.
|
||||
- **Use Prettier:** Use Prettier to format code consistently.
|
||||
- **Configure a Code Editor:** Configure a code editor to automatically format code on save.
|
||||
|
||||
### 7.4. Deployment:
|
||||
|
||||
- **Use a CDN:** Use a CDN to host static assets like models and textures.
|
||||
- **Enable Gzip Compression:** Enable Gzip compression on the server to reduce the size of the transferred files.
|
||||
- **Optimize Images:** Optimize images to reduce their size and improve loading times.
|
||||
|
||||
### 7.5. CI/CD Integration:
|
||||
|
||||
- **Use a CI/CD Pipeline:** Use a CI/CD pipeline to automate the build, test, and deployment process.
|
||||
- **Run Tests Automatically:** Run tests automatically on every commit.
|
||||
- **Deploy to a Staging Environment:** Deploy to a staging environment before deploying to production.
|
||||
|
||||
By following these best practices, you can create high-quality Three.js applications that are efficient, maintainable, and robust. Remember to stay updated with the latest Three.js documentation and community resources to keep your skills sharp and your projects cutting-edge.
|
||||
// Extension support check
|
||||
const renderer = new THREE.WebGLRenderer();
|
||||
const gl = renderer.getContext();
|
||||
|
||||
if (gl.getExtension('ANGLE_instanced_arrays')) {
|
||||
// Use instanced rendering
|
||||
} else {
|
||||
console.warn('ANGLE_instanced_arrays not supported, falling back to non-instanced rendering.');
|
||||
// Implement fallback or disable feature
|
||||
}
|
||||
```
|
||||
|
||||
### ❌ BAD: Ignoring WebGL Errors & Blindly Using Extensions
|
||||
|
||||
```javascript
|
||||
// No error checking, leading to silent failures
|
||||
const geometry = new THREE.BufferGeometry();
|
||||
// ... potentially invalid buffer setup ...
|
||||
renderer.render(scene, camera); // Errors might occur but go unnoticed
|
||||
|
||||
// Using advanced features without checking support
|
||||
const instancedMesh = new THREE.InstancedMesh(geometry, material, count); // Fails on unsupported hardware
|
||||
```
|
||||
|
||||
## 2. Asset Management & Lazy Loading
|
||||
|
||||
Optimize initial load times and prevent frame stalls by loading large assets lazily and using the correct loaders.
|
||||
|
||||
### ✅ GOOD: Lazy Loading with Dedicated Loaders & Modular Structure
|
||||
|
||||
Load assets only when needed. Use `GLTFLoader`, `TextureLoader`, etc., and organize assets in a `public/` directory.
|
||||
|
||||
```javascript
|
||||
// main.js
|
||||
import * as THREE from 'three';
|
||||
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
|
||||
|
||||
export function loadModel(url, scene) {
|
||||
const loader = new GLTFLoader();
|
||||
return new Promise((resolve, reject) => {
|
||||
loader.load(
|
||||
url,
|
||||
(gltf) => {
|
||||
scene.add(gltf.scene);
|
||||
resolve(gltf.scene);
|
||||
},
|
||||
(xhr) => {
|
||||
console.log(`${(xhr.loaded / xhr.total) * 100}% loaded`);
|
||||
},
|
||||
(error) => {
|
||||
console.error('An error happened loading the model:', error);
|
||||
reject(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// In your scene setup, call loadModel when the asset is about to be visible
|
||||
// await loadModel('/public/models/my-asset.glb', scene);
|
||||
```
|
||||
|
||||
### ❌ BAD: Synchronous Loading & Unorganized Assets
|
||||
|
||||
```javascript
|
||||
// main.js - synchronous loading blocks the main thread
|
||||
// This is a simplified example, actual synchronous loading is less common with modern loaders
|
||||
// but conceptually, loading all assets at once without a loading screen is bad.
|
||||
const texture = new THREE.TextureLoader().load('large_texture.jpg'); // Blocks until loaded
|
||||
const model = new GLTFLoader().load('heavy_model.glb'); // Blocks until loaded
|
||||
scene.add(model.scene);
|
||||
// ... UI freezes ...
|
||||
```
|
||||
|
||||
## 3. Performance Optimization
|
||||
|
||||
Minimize draw calls, reuse resources, and keep shaders efficient.
|
||||
|
||||
### ✅ GOOD: Instancing, Geometry Batching, Material Reuse, & Texture Atlases
|
||||
|
||||
For repetitive objects, use `InstancedMesh`. Combine static geometries where possible. Reuse materials across objects. Consolidate textures into atlases.
|
||||
|
||||
```javascript
|
||||
// InstancedMesh for many identical objects
|
||||
const geometry = new THREE.BoxGeometry(1, 1, 1);
|
||||
const material = new THREE.MeshNormalMaterial();
|
||||
const count = 1000;
|
||||
const mesh = new THREE.InstancedMesh(geometry, material, count);
|
||||
|
||||
const matrix = new THREE.Matrix4();
|
||||
for (let i = 0; i < count; i++) {
|
||||
matrix.setPosition(Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50);
|
||||
mesh.setMatrixAt(i, matrix);
|
||||
}
|
||||
mesh.instanceMatrix.needsUpdate = true;
|
||||
scene.add(mesh);
|
||||
|
||||
// Material Reuse
|
||||
const sharedMaterial = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
|
||||
const obj1 = new THREE.Mesh(new THREE.BoxGeometry(), sharedMaterial);
|
||||
const obj2 = new THREE.Mesh(new THREE.SphereGeometry(), sharedMaterial);
|
||||
```
|
||||
|
||||
### ❌ BAD: Excessive Draw Calls & Unique Materials
|
||||
|
||||
```javascript
|
||||
// Creating individual meshes for many identical objects
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
const geometry = new THREE.BoxGeometry(1, 1, 1);
|
||||
const material = new THREE.MeshNormalMaterial(); // New material for each
|
||||
const mesh = new THREE.Mesh(geometry, material);
|
||||
mesh.position.set(Math.random() * 100 - 50, Math.random() * 100 - 50, Math.random() * 100 - 50);
|
||||
scene.add(mesh); // 1000 draw calls, 1000 materials
|
||||
}
|
||||
```
|
||||
|
||||
## 4. Resource Disposal
|
||||
|
||||
Prevent memory leaks by explicitly disposing of three-js objects when they are no longer needed.
|
||||
|
||||
### ✅ GOOD: Explicit Disposal
|
||||
|
||||
Always call `dispose()` on geometries, materials, textures, and render targets when removing them from the scene.
|
||||
|
||||
```javascript
|
||||
function disposeObject(obj) {
|
||||
if (obj.geometry) {
|
||||
obj.geometry.dispose();
|
||||
}
|
||||
if (obj.material) {
|
||||
if (Array.isArray(obj.material)) {
|
||||
obj.material.forEach(material => material.dispose());
|
||||
} else {
|
||||
obj.material.dispose();
|
||||
}
|
||||
}
|
||||
if (obj.texture) { // For textures directly attached to objects
|
||||
obj.texture.dispose();
|
||||
}
|
||||
if (obj.parent) {
|
||||
obj.parent.remove(obj);
|
||||
}
|
||||
}
|
||||
|
||||
// Example usage:
|
||||
const meshToRemove = scene.getObjectByName('myDynamicMesh');
|
||||
if (meshToRemove) {
|
||||
disposeObject(meshToRemove);
|
||||
}
|
||||
```
|
||||
|
||||
### ❌ BAD: Relying on Garbage Collection
|
||||
|
||||
```javascript
|
||||
const mesh = new THREE.Mesh(new THREE.BoxGeometry(), new THREE.MeshBasicMaterial());
|
||||
scene.add(mesh);
|
||||
// Later, just remove from scene without disposing resources
|
||||
scene.remove(mesh); // Geometry and material still consume GPU memory
|
||||
mesh = null; // Only JS reference is cleared, not GPU resources
|
||||
```
|
||||
|
||||
## 5. Project Structure & Development Workflow
|
||||
|
||||
Adopt a modern development setup for efficient development and deployment.
|
||||
|
||||
### ✅ GOOD: npm + Vite & Modular Imports
|
||||
|
||||
Use npm for dependency management and Vite for local development and production builds. Structure your project with a clear entry point (`main.js`) and a `public/` folder for static assets.
|
||||
|
||||
```javascript
|
||||
// index.html
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>My three.js App</title>
|
||||
</head>
|
||||
<body>
|
||||
<script type="module" src="/main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
// main.js
|
||||
import * as THREE from 'three';
|
||||
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
||||
// ... rest of your scene setup
|
||||
```
|
||||
|
||||
### ❌ BAD: CDN-only & Global Scope Pollution
|
||||
|
||||
```html
|
||||
<!-- index.html -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/three@0.149.0/build/three.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/three@0.149.0/examples/jsm/controls/OrbitControls.js"></script>
|
||||
<script>
|
||||
// All code in global scope, no module benefits
|
||||
const scene = new THREE.Scene();
|
||||
// ...
|
||||
</script>
|
||||
```
|
||||
Reference in New Issue
Block a user