Skip to content

refactor to ImportManager to significantly improve performance,#934

Open
lavjeetrai wants to merge 1 commit into
HSF:mainfrom
lavjeetrai:import-manager
Open

refactor to ImportManager to significantly improve performance,#934
lavjeetrai wants to merge 1 commit into
HSF:mainfrom
lavjeetrai:import-manager

Conversation

@lavjeetrai

Copy link
Copy Markdown
Contributor

Description

This PR introduces a major refactor to ImportManager to significantly improve performance, memory management, and overall code architecture for importing 3D assets. It addresses several bottlenecks and enforces stricter TypeScript type safety.

Key Improvements

  • Lazy Instantiation & Caching: Loaders (GLTFLoader, OBJLoader, DRACOLoader) are now lazily instantiated and cached to prevent upfront CPU/Memory spikes when the module is loaded.
  • GPU Memory Leak Prevention: Added a disposeMaterial helper that reliably disposes of intermediate materials and their deeply nested textures.
  • Geometry Merging for Draw Call Optimization: GLTF meshes sharing the same material are now merged using BufferGeometryUtils.mergeGeometries. This drastically reduces WebGL draw calls, with a safe fallback to individual meshes if attribute mismatches occur.
  • Unified Asynchronous Flow: Removed legacy callback anti-patterns (e.g., inside parsePhnxScene) in favor of modern async/await and Promises, making the codebase much easier to trace and maintain.
  • Dynamic JSZip Imports: The jszip library is now loaded dynamically (await import('jszip')) only when a .zip file is detected, reducing the initial JavaScript bundle size.
  • Resilient ZIP Extraction: Upgraded from Promise.all to Promise.allSettled when extracting multiple files from a .zip, ensuring that a single corrupted file won't reject the entire valid payload.
  • Improved Type Safety: Eliminated arbitrary any assertions across material property checks and geometry arrays, leveraging strict type guards (e.g., MeshStandardMaterial | MeshBasicMaterial | MeshPhongMaterial) to prevent runtime errors.

Testing Instructions

  1. Load an .obj file and ensure it renders with the correct flat/double-sided properties.
  2. Load a .gltf or .glb scene (with and without DRACO compression) and confirm textures/geometries are correctly preserved.
  3. Import a .zip file containing multiple geometries and verify it unpacks and renders properly.
  4. Load a .phnx event scene and verify that both the event data and geometries are initialized without callback errors.

@lavjeetrai

Copy link
Copy Markdown
Contributor Author

@sponce @EdwardMoyse hey guys, whenever you have a minute, could you take a look at this refactor? Cleaned up some of the GLTF loading logic.

@sponce

sponce commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Just a tiny question : shall we squash the 3 commits, for the history to be cleaner ? Or feel free to rewrite the history yourself and push the new version

@lavjeetrai

Copy link
Copy Markdown
Contributor Author

I've squashed the commits into a single commit. Let me know if everything else looks good to go.

Also, PR #931 has been waiting for more than a week. Since the @EdwardMoyse seems to be offline, could you advise on whether there are any remaining blockers to getting it merged?

child.material.dispose();
child.material = material2;

if (child.material) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be a stupid question : why do we not check anymore the type ? disposeMaterial needs a Material

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Three.js, a mesh's material can be either a single Material or an array (Material[]).

The previous instanceof Material check evaluated to false for arrays, skipping disposal and causing memory leaks. By removing the strict type check, we safely pass both types to the new disposeMaterial helper, which is designed to iterate through and dispose of them correctly.
Does this approach make sense to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants