gradio-app--gradio
adf0d17497
publish / version_or_publish (push) Has been cancelled
storybook-build / changes (push) Has been cancelled
storybook-build / :storybook-build (push) Has been cancelled
Sync Gradio Skills to Hugging Face / sync-skills (push) Has been cancelled
functional / changes (push) Has been cancelled
functional / build-frontend (push) Has been cancelled
functional / functional-test-SSR=false (push) Has been cancelled
functional / functional-reload (push) Has been cancelled
js / changes (push) Has been cancelled
js / js-test (push) Has been cancelled
docs-build / changes (push) Has been cancelled
docs-build / docs-build (push) Has been cancelled
docs-build / website-build (push) Has been cancelled
functional / functional-test-SSR=true (push) Has been cancelled
hygiene / hygiene-test (push) Has been cancelled
python / changes (push) Has been cancelled
python / build (push) Has been cancelled
python / test-ubuntu-latest-flaky (push) Has been cancelled
python / test-ubuntu-latest-not-flaky (push) Has been cancelled
python / test-windows-latest-flaky (push) Has been cancelled
python / test-windows-latest-not-flaky (push) Has been cancelled
111 行
2.1 KiB
Svelte
111 行
2.1 KiB
Svelte
<script lang="ts">
|
|
import { onMount } from "svelte";
|
|
import * as SPLAT from "gsplat";
|
|
import type { FileData } from "@gradio/client";
|
|
|
|
let {
|
|
value,
|
|
zoom_speed,
|
|
pan_speed
|
|
}: {
|
|
value: FileData;
|
|
zoom_speed: number;
|
|
pan_speed: number;
|
|
} = $props();
|
|
|
|
let url = $derived(value.url);
|
|
|
|
let canvas: HTMLCanvasElement;
|
|
let scene: SPLAT.Scene;
|
|
let camera: SPLAT.Camera;
|
|
let renderer: SPLAT.WebGLRenderer | null = null;
|
|
let controls: SPLAT.OrbitControls;
|
|
let mounted = $state(false);
|
|
let frameId: number | null = null;
|
|
|
|
function reset_scene(): void {
|
|
if (frameId !== null) {
|
|
cancelAnimationFrame(frameId);
|
|
frameId = null;
|
|
}
|
|
|
|
if (renderer !== null) {
|
|
renderer.dispose();
|
|
renderer = null;
|
|
}
|
|
|
|
scene = new SPLAT.Scene();
|
|
camera = new SPLAT.Camera();
|
|
renderer = new SPLAT.WebGLRenderer(canvas);
|
|
controls = new SPLAT.OrbitControls(camera, canvas);
|
|
controls.zoomSpeed = zoom_speed;
|
|
controls.panSpeed = pan_speed;
|
|
|
|
if (!value) {
|
|
return;
|
|
}
|
|
|
|
let loading = false;
|
|
const load = async (): Promise<void> => {
|
|
if (loading) {
|
|
console.error("Already loading");
|
|
return;
|
|
}
|
|
if (!url) {
|
|
throw new Error("No resolved URL");
|
|
}
|
|
loading = true;
|
|
if (url.endsWith(".ply")) {
|
|
await SPLAT.PLYLoader.LoadAsync(url, scene, undefined);
|
|
} else if (url.endsWith(".splat")) {
|
|
await SPLAT.Loader.LoadAsync(url, scene, undefined);
|
|
} else {
|
|
throw new Error("Unsupported file type");
|
|
}
|
|
loading = false;
|
|
};
|
|
|
|
const frame = (): void => {
|
|
if (!renderer) {
|
|
return;
|
|
}
|
|
|
|
if (loading) {
|
|
frameId = requestAnimationFrame(frame);
|
|
return;
|
|
}
|
|
|
|
controls.update();
|
|
renderer.render(scene, camera);
|
|
|
|
frameId = requestAnimationFrame(frame);
|
|
};
|
|
|
|
load();
|
|
frameId = requestAnimationFrame(frame);
|
|
}
|
|
|
|
onMount(() => {
|
|
if (value != null) {
|
|
reset_scene();
|
|
}
|
|
mounted = true;
|
|
|
|
return () => {
|
|
if (renderer) {
|
|
renderer.dispose();
|
|
}
|
|
};
|
|
});
|
|
|
|
let path = $derived(value?.path);
|
|
|
|
$effect(() => {
|
|
if (canvas && mounted && path) {
|
|
reset_scene();
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<canvas bind:this={canvas}></canvas>
|