Skip to content

Commit

Permalink
better propagation of loading errors
Browse files Browse the repository at this point in the history
  • Loading branch information
slimbuck committed Oct 17, 2024
1 parent 4a352a0 commit ac2317f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/asset-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class AssetLoader {
})
.catch((err) => {
console.error(err);
reject(`Failed to load splat data (${err})`);
reject('Failed to load splat data');
});
}).finally(() => {
stopSpinner();
Expand Down
31 changes: 23 additions & 8 deletions src/file-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { convertPly, convertPlyCompressed, convertSplat } from './splat-convert'
import { startSpinner, stopSpinner } from './ui/spinner';
import { ElementType } from './element';
import { Splat } from './splat';
import { localize } from './ui/localization';

interface RemoteStorageDetails {
method: string;
Expand Down Expand Up @@ -121,13 +122,27 @@ const loadCameraPoses = async (url: string, filename: string, events: Events) =>
// initialize file handler events
const initFileHandler = async (scene: Scene, events: Events, dropTarget: HTMLElement, remoteStorageDetails: RemoteStorageDetails) => {

const handleLoad = (url: string, filename: string) => {
if (filename.toLowerCase().endsWith('.json')) {
return loadCameraPoses(url, filename, events);
} else if (filename.toLowerCase().endsWith('.ply') || filename.toLowerCase().endsWith('.splat')) {
return scene.loadModel(url, filename);
} else {
return null;
const handleLoad = async (url: string, filename: string) => {
try {
const lowerFilename = (filename || url).toLowerCase();
if (lowerFilename.endsWith('.json')) {
await loadCameraPoses(url, filename, events);
} else if (lowerFilename.endsWith('.ply') || lowerFilename.endsWith('.splat')) {
await scene.assetLoader.loadModel({ url, filename })
.then((model) => {
scene.add(model);
scene.camera.focus();
events.fire('loaded', filename);
});
} else {
throw new Error(`Unsupported file type`);
}
} catch (err) {
events.invoke('showPopup', {
type: 'error',
header: localize('popup.error-loading'),
message: `${err.message ?? err} while loading '${filename}'`
});
}
};

Expand All @@ -141,7 +156,7 @@ const initFileHandler = async (scene: Scene, events: Events, dropTarget: HTMLEle
fileSelector = document.createElement('input');
fileSelector.setAttribute('id', 'file-selector');
fileSelector.setAttribute('type', 'file');
fileSelector.setAttribute('accept', '.ply');
fileSelector.setAttribute('accept', '.ply,.splat');
fileSelector.setAttribute('multiple', 'true');

fileSelector.onchange = async () => {
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ const main = async () => {
window.launchQueue.setConsumer(async (launchParams: LaunchParams) => {
for (const file of launchParams.files) {
const blob = await file.getFile();
scene.loadModel(URL.createObjectURL(blob), file.name);
const url = URL.createObjectURL(blob);
await events.invoke('load', url, file.name);
URL.revokeObjectURL(url);
}
});
}
Expand Down
15 changes: 0 additions & 15 deletions src/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,6 @@ class Scene {
this.app.start();
}

async loadModel(url: string, filename: string) {
try {
const model = await this.assetLoader.loadModel({ url, filename });
this.add(model);
this.camera.focus();
this.events.fire('loaded', filename);
} catch (err) {
this.events.invoke('showPopup', {
type: 'error',
header: localize('popup.error-loading'),
message: `${err.message ?? err} while loading '${filename}'`
});
}
}

clear() {
const models = this.getElementsByType(ElementType.model);
models.forEach((model) => {
Expand Down

0 comments on commit ac2317f

Please sign in to comment.