-
Optimize TreeBatcher (src/foliage/tree-batcher.ts):
- Import
THREEat the top ofsrc/foliage/tree-batcher.ts(it already does, but we need_scratchMatrixor write directly). - Actually wait,
addInstancetakesmatrix(which is aTHREE.Matrix4). The issue is thatmesh.setMatrixAt(index, matrix)is called, which calls.toArray()and sets needsUpdate. - Wait,
mesh.setMatrixAt(index, matrix)uses.toArray()internally. Wait, the memory guideline says:To eliminate CPU overhead and garbage collection spikes from Matrix4 composition (Object3D.updateMatrix and mesh.setMatrixAt) in high-frequency batcher loops, write position, rotation, and scale data directly into the flat Float32Array of the InstancedMesh's instanceMatrix.array... - So inside
TreeBatcher.addInstance, we can change:// ⚡ OPTIMIZATION: Write directly to instanceMatrix.array to bypass setMatrixAt overhead matrix.toArray(mesh.instanceMatrix.array, index * 16);
- Oh,
tree-batcher.tslines 481-555 usemesh.matrixWorld. The methodaddInstancereceivesmatrix.
- Import
-
Optimize Proximity Enter/Leave Math (src/systems/interaction.ts):
- Line 143:
try { obj.userData.onProximityEnter(playerPosition.distanceTo(obj.position)); } catch(e) ... distanceTousesMath.sqrt(). The proximity loop already calculateddistSq.- Change
playerPosition.distanceTo(obj.position)to useMath.sqrt(distSq)sincedistSqis already computed? Wait, the check for Enters is in a separate loop that doesn't havedistSq. - Wait,
nextNearbyis populated based ondistSq < radiusSq. When an object is found innextNearbybut notprevNearby, it's an Enter. We could either re-calculatedistSqandMath.sqrtor just usedistanceTobut we shouldn't usedistanceTo(). - Better:
Math.sqrt(playerPosition.distanceToSquared(obj.position))to avoidVector3allocation/overhead? Wait,distanceToSquareddoesn't allocate.distanceTo()is justMath.sqrt(this.distanceToSquared(v)). DoesdistanceToallocate? The memory guideline says: "avoiddistanceTo()which uses the expensiveMath.sqrt()function. Instead, usedistanceToSquared()and compare it against pre-squared thresholds, or delay the square root calculation until after an early bounds check." - Ah, so since
onProximityEnterexpects a distance, we should passMath.sqrt(playerPosition.distanceToSquared(obj.position)). Wait, if we usedistanceToSquaredwe can just pass that? DoesonProximityEnterneed the exact distance, or can we pass the square root ofdistanceToSquared?
- Line 143:
-
Optimize Harpoon Line Math (src/gameplay/harpoon-line.ts):
- Line 94:
const distance = playerPos.distanceTo(anchor); - We can replace this with
Math.sqrt(playerPos.distanceToSquared(anchor));
- Line 94:
-
Fix VRAM Leaks in Scene Removal:
src/rendering/shader-warmup.ts: Line 270scene.remove(mesh);should also dispose the geometry and material. Wait,this.warmupGeometry.dispose()is called later, and the material is warmed up.src/core/main.ts: Line 276scene.remove(previewMushroom);doesn't dispose of the mushroom's geometry and material. ThepreviewMushroomis removed from the scene and discarded. It should traverse and dispose of its geometries/materials.src/systems/weather/weather-effects.ts:scene.remove(rainMesh);,scene.remove(mistMesh);but they seem to callpercussionRain.dispose()andmelodicMist.dispose()first. What aboutlightningLight?src/core/deferred-init.ts: Line 208scene.remove(dummyGroup);dummyGroup contains dummyMesh which has dummyGeo and dummyMat. They should be disposed!dummyGeo.dispose(); dummyMat.dispose();
-
Pre commit instructions:
- "Complete pre-commit steps to ensure proper testing, verification, review, and reflection are done."