Line data Source code
1 : import { SplatMesh } from '@sparkjsdev/spark'
2 : import * as THREE from 'three'
3 :
4 : import { isGaussianSplatPLY } from './loader/ply'
5 :
6 : import type {
7 : ModelAdapter,
8 : ModelAdapterCapabilities,
9 : ModelLoadContext,
10 : ModelLoadResult
11 : } from './ModelAdapter'
12 :
13 24 : export class SplatModelAdapter implements ModelAdapter {
14 : readonly kind = 'splat' as const
15 : readonly extensions = ['spz', 'splat', 'ksplat', 'ply'] as const
16 : readonly capabilities: ModelAdapterCapabilities = {
17 : fitToViewer: true,
18 : requiresMaterialRebuild: false,
19 : gizmoTransform: true,
20 : lighting: false,
21 : exportable: true,
22 : materialModes: [],
23 : fitTargetSize: 20
24 : }
25 :
26 : async matches(
27 : extension: string,
28 : fetchBytes: () => Promise<ArrayBuffer>
29 : ): Promise<boolean> {
30 0 : if (extension !== 'ply') return true
31 0 : return isGaussianSplatPLY(await fetchBytes())
32 : }
33 :
34 : async load(
35 : ctx: ModelLoadContext,
36 : _path: string,
37 : filename: string,
38 : fetchBytes: () => Promise<ArrayBuffer>
39 : ): Promise<ModelLoadResult> {
40 0 : const arrayBuffer = await fetchBytes()
41 :
42 0 : const splatMesh = new SplatMesh({
43 : fileBytes: arrayBuffer,
44 : fileName: filename
45 : })
46 0 : await splatMesh.initialized
47 0 : splatMesh.quaternion.set(1, 0, 0, 0)
48 0 : ctx.setOriginalModel(splatMesh)
49 :
50 0 : const splatGroup = new THREE.Group()
51 0 : splatGroup.add(splatMesh)
52 0 : return { object: splatGroup, capabilities: this.capabilities }
53 : }
54 :
55 : computeBounds(model: THREE.Object3D): THREE.Box3 | null {
56 0 : const splat = model.children[0]
57 0 : if (!(splat instanceof SplatMesh)) return null
58 0 : splat.updateWorldMatrix(true, false)
59 0 : return splat.getBoundingBox(false).clone().applyMatrix4(splat.matrixWorld)
60 : }
61 :
62 : disposeModel(model: THREE.Object3D): void {
63 0 : model.traverse((child) => {
64 0 : if (child instanceof SplatMesh) {
65 0 : child.dispose()
66 : }
67 : })
68 : }
69 :
70 : defaultCameraPose(): { size: THREE.Vector3; center: THREE.Vector3 } {
71 0 : return {
72 : size: new THREE.Vector3(5, 5, 5),
73 : center: new THREE.Vector3(0, 2.5, 0)
74 : }
75 : }
76 : }
|