feat: Optimize rendering and physics with dirty chunk tracking and adaptive update rates
This commit introduces several performance optimizations: - Implement chunk-based dirty rendering - Add adaptive physics update rates - Return modification status from element update functions - Reduce unnecessary rendering and physics calculations - Track world movement for efficient re-rendering The key changes include: 1. Adding `dirtyChunks` and `worldMoved` tracking 2. Modifying element update functions to return modification status 3. Implementing adaptive physics update rates based on FPS 4. Skipping rendering for unchanged chunks 5. Reducing computational overhead in physics and rendering loops These optimizations should significantly improve the simulation's performance, especially with large numbers of elements.
This commit is contained in:
11
js/render.js
11
js/render.js
@@ -13,6 +13,11 @@ function render() {
|
||||
|
||||
const key = getChunkKey(chunkX, chunkY);
|
||||
|
||||
// Skip rendering if the chunk hasn't changed and isn't marked as dirty
|
||||
if (!dirtyChunks.has(key) && !worldMoved) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!chunks.has(key)) continue;
|
||||
|
||||
const chunk = chunks.get(key);
|
||||
@@ -115,8 +120,14 @@ function render() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove this chunk from the dirty list after rendering
|
||||
dirtyChunks.delete(key);
|
||||
}
|
||||
|
||||
// Reset world moved flag after rendering
|
||||
worldMoved = false;
|
||||
|
||||
// Draw cursor position and update debug info
|
||||
if (currentMouseX !== undefined && currentMouseY !== undefined) {
|
||||
const worldX = Math.floor(currentMouseX / PIXEL_SIZE) + worldOffsetX;
|
||||
|
||||
Reference in New Issue
Block a user