File size: 3,680 Bytes
352fb85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { Object3D } from "./Object3D";
import { SplatData } from "../splats/SplatData";
import { Splat } from "../splats/Splat";
import { EventDispatcher } from "../events/EventDispatcher";
import { ObjectAddedEvent, ObjectRemovedEvent } from "../events/Events";
import { Converter } from "../utils/Converter";

class Scene extends EventDispatcher {
    private _objects: Object3D[] = [];

    addObject: (object: Object3D) => void;
    removeObject: (object: Object3D) => void;
    findObject: (predicate: (object: Object3D) => boolean) => Object3D | undefined;
    findObjectOfType: <T extends Object3D>(type: { new (): T }) => T | undefined;
    reset: () => void;

    constructor() {
        super();

        this.addObject = (object: Object3D) => {
            this.objects.push(object);
            this.dispatchEvent(new ObjectAddedEvent(object));
        };

        this.removeObject = (object: Object3D) => {
            const index = this.objects.indexOf(object);
            if (index < 0) {
                throw new Error("Object not found in scene");
            }
            this.objects.splice(index, 1);
            this.dispatchEvent(new ObjectRemovedEvent(object));
        };

        this.findObject = (predicate: (object: Object3D) => boolean) => {
            for (const object of this.objects) {
                if (predicate(object)) {
                    return object;
                }
            }
            return undefined;
        };

        this.findObjectOfType = <T extends Object3D>(type: { new (): T }) => {
            for (const object of this.objects) {
                if (object instanceof type) {
                    return object;
                }
            }
            return undefined;
        };

        this.reset = () => {
            const objectsToRemove = this.objects.slice();
            for (const object of objectsToRemove) {
                this.removeObject(object);
            }
        };

        this.reset();
    }

    getMergedSceneDataBuffer(format: "splat" | "ply" = "splat"): ArrayBuffer {
        const buffers: Uint8Array[] = [];
        let vertexCount = 0;

        for (const object of this.objects) {
            if (object instanceof Splat) {
                const splatClone = object.clone() as Splat;

                splatClone.applyRotation();
                splatClone.applyScale();
                splatClone.applyPosition();
                const buffer = splatClone.data.serialize();

                buffers.push(buffer);
                vertexCount += splatClone.data.vertexCount;
            }
        }

        const mergedSplatData = new Uint8Array(vertexCount * SplatData.RowLength);
        let offset = 0;
        for (const buffer of buffers) {
            mergedSplatData.set(buffer, offset);
            offset += buffer.length;
        }

        if (format === "ply") {
            return Converter.SplatToPLY(mergedSplatData.buffer, vertexCount);
        }

        return mergedSplatData.buffer;
    }

    saveToFile(name: string | null = null, format: "splat" | "ply" = "splat") {
        if (!document) return;

        if (!name) {
            const now = new Date();
            name = `scene-${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}.${format}`;
        }

        const mergedData = this.getMergedSceneDataBuffer(format);

        const blob = new Blob([mergedData], { type: "application/octet-stream" });

        const link = document.createElement("a");
        link.download = name;
        link.href = URL.createObjectURL(blob);
        link.click();
    }

    get objects() {
        return this._objects;
    }
}

export { Scene };