Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/utils/lazy-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

export class LazyLoader {
private cache = new Map<string, any>();
private loading = new Map<string, Promise<any>>();

async load(url: string): Promise<any> {
if (this.cache.has(url)) return this.cache.get(url);
if (this.loading.has(url)) return this.loading.get(url);

const promise = fetch(url)
.then(r => r.arrayBuffer())
.then(data => {
this.cache.set(url, data);
this.loading.delete(url);
return data;
});

this.loading.set(url, promise);
return promise;
}

dispose(url: string) {
this.cache.delete(url);
}

disposeAll() {
this.cache.clear();
}
}
25 changes: 25 additions & 0 deletions src/utils/lod-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

export interface LODConfig {
distances: number[];
geometries: any[];
}

export class LODManager {
private configs = new Map<string, LODConfig>();

register(id: string, config: LODConfig) {
this.configs.set(id, config);
}

getGeometry(id: string, distance: number): any {
const config = this.configs.get(id);
if (!config) return null;

for (let i = 0; i < config.distances.length; i++) {
if (distance <= config.distances[i]) {
return config.geometries[i];
}
}
return config.geometries[config.geometries.length - 1];
}
}
Loading