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
68 changes: 65 additions & 3 deletions src/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { RenderMode } from './RenderMode.js';
import { LogLevel } from './LogLevel.js';
import { SceneRevealMode } from './SceneRevealMode.js';
import { SplatRenderMode } from './SplatRenderMode.js';
import { SplatBuffer } from './loaders/SplatBuffer.js';
import { openDB } from '../util/idb.js';

const THREE_CAMERA_FOV = 50;
const MINIMUM_DISTANCE_TO_NEW_FOCAL_POINT = .75;
Expand Down Expand Up @@ -283,6 +285,9 @@ export class Viewer {
this.disposing = false;
this.disposed = false;
this.disposePromise = null;
this.dbPromise = null;
// Whether to use indexedDB for caching splat buffers
this.cacheEnabled = options.cacheEnabled === true;
if (!this.dropInMode) this.init();
}

Expand Down Expand Up @@ -1009,9 +1014,11 @@ export class Viewer {
for (let i = 0; i < sceneOptions.length; i++) {
const options = sceneOptions[i];
const format = (options.format !== undefined && options.format !== null) ? options.format : sceneFormatFromPath(options.path);
const baseDownloadPromise = this.downloadSplatSceneToSplatBuffer(options.path, options.splatAlphaRemovalThreshold,
onLoadProgress.bind(this, i), false, undefined,
format, options.headers);
// add indexedDB when caching is enabled
const baseDownloadPromise = {
promise: this.downloadOrCacheSplatBuffer(options.path, options.splatAlphaRemovalThreshold,
onLoadProgress.bind(this, i), false, undefined, format, options.headers)
};
baseDownloadPromises.push(baseDownloadPromise);
nativeDownloadPromises.push(baseDownloadPromise.promise);
}
Expand Down Expand Up @@ -2096,4 +2103,59 @@ export class Viewer {
isMobile() {
return navigator.userAgent.includes('Mobi');
}

async getDb() {
if (!this.dbPromise) {
this.dbPromise = openDB('SplatBufferCacheDB', 1, {
upgrade(db) {
if (!db.objectStoreNames.contains('buffers')) {
db.createObjectStore('buffers');
}
}
});
}
return this.dbPromise;
}

async getCachedSplatBuffer(key) {
if (!this.cacheEnabled) return null;
const db = await this.getDb();
const bufferData = await db.get('buffers', key);
if (!bufferData) return null;
return new SplatBuffer(bufferData);
}

async setCachedSplatBuffer(key, buffer) {
if (!this.cacheEnabled) return;
const db = await this.getDb();
await db.put('buffers', buffer.bufferData, key);
}

async downloadOrCacheSplatBuffer(
path,
splatAlphaRemovalThreshold = 1,
onProgress = undefined,
progressiveBuild = false,
onSectionBuilt = undefined,
format,
headers
) {
const key = `${path}_${format}_${splatAlphaRemovalThreshold}`;
let cached = null;
if (this.cacheEnabled) {
cached = await this.getCachedSplatBuffer(key);
if (cached) return cached;
}
const buffer = await this.downloadSplatSceneToSplatBuffer(
path,
splatAlphaRemovalThreshold,
onProgress,
progressiveBuild,
onSectionBuilt,
format,
headers
);
if (this.cacheEnabled) await this.setCachedSplatBuffer(key, buffer);
return buffer;
}
}
8 changes: 8 additions & 0 deletions util/idb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.