File size: 11,039 Bytes
1c7ed2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee7fb9f
 
 
 
22ce44c
ee7fb9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adc456b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee7fb9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c7ed2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee7fb9f
1c7ed2a
 
 
 
 
 
7cc027c
ee7fb9f
 
 
 
 
 
 
 
 
1ad0366
7cc027c
 
 
 
ee7fb9f
7cc027c
 
 
 
 
 
1c7ed2a
 
 
 
 
ee7fb9f
 
 
 
 
 
 
 
 
1ad0366
ee7fb9f
 
1c7ed2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee7fb9f
1c7ed2a
 
 
 
 
 
 
 
7cc027c
 
1c7ed2a
1ad0366
1c7ed2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee7fb9f
1c7ed2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Copyright (c) 2025 Very 360 VR. DBA Hyper Interactive

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

function createUI() {
    var advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");

    var initInstructions = new BABYLON.GUI.TextBlock();
    initInstructions.text = "Gaussian Splattings Object Compressor\n\nDrag and drop\n.ply or .splat or .hyp to start\n\nOR";
    initInstructions.color = "black";
    initInstructions.fontSize = 40;
    advancedTexture.addControl(initInstructions);   

    // create a background for navigation instructions as a button
    var navInstructionBt = BABYLON.GUI.Button.CreateSimpleButton("nav", "");
    navInstructionBt.color = "black";
    navInstructionBt.background = "white";
    navInstructionBt.textBlock.fontSize = 40;
    navInstructionBt.alpha = 0.5;
    navInstructionBt.isVisible = false;
    advancedTexture.addControl(navInstructionBt);

    var navInstructionsTxt = new BABYLON.GUI.TextBlock();
    navInstructionsTxt.text = "Drag to rotate\nScroll to zoom\nRight click to pan";
    navInstructionsTxt.color = "black";
    navInstructionsTxt.background = "white";
    navInstructionsTxt.fontSize = 40;
    navInstructionsTxt.isVisible = false;
    advancedTexture.addControl(navInstructionsTxt);   

    
    var homeBt = BABYLON.GUI.Button.CreateSimpleButton("home", "hyperinteractivity.com");
    homeBt.width = 0.25;    
    homeBt.height = 0.05;
    homeBt.color = "white";
    homeBt.background = "black";
    homeBt.isVisible = true;
    homeBt.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_RIGHT;
    homeBt.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
    // setup the button callbnack
    homeBt.onPointerDownObservable.add(function() {
        window.location.href = "https://hyperinteractivity.com";
    });
    advancedTexture.addControl(homeBt);

    var loadExampleBt = BABYLON.GUI.Button.CreateSimpleButton("loadExample", "load our example");
    loadExampleBt.width = 0.2;
    loadExampleBt.height = 0.2;
    loadExampleBt.color = "white";
    loadExampleBt.background = "black";
    loadExampleBt.textBlock.fontSize = 40;
    // make the button's corner round
    loadExampleBt.cornerRadius = 30;
    loadExampleBt.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
    loadExampleBt.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
    advancedTexture.addControl(loadExampleBt);

    var convertBt = BABYLON.GUI.Button.CreateSimpleButton("convert", "Convert");
    convertBt.width = 0.2;
    convertBt.height = 0.2;
    convertBt.textBlock.fontSize = 40;
    convertBt.color = "white";
    convertBt.background = "black";
    // make the button's corner round
    convertBt.cornerRadius = 30;
    convertBt.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
    convertBt.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
    convertBt.isVisible = false;
    advancedTexture.addControl(convertBt);

    var downloadBt = BABYLON.GUI.Button.CreateSimpleButton("download", "Download");
    downloadBt.width = 0.2;
    downloadBt.height = 0.2;
    downloadBt.color = "white";
    downloadBt.background = "green";
    downloadBt.cornerRadius = 30;
    downloadBt.textBlock.fontSize = 40;
    downloadBt.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
    downloadBt.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
    downloadBt.isVisible = false;
    advancedTexture.addControl(downloadBt);

    var progressBar = new BABYLON.GUI.Rectangle();
    progressBar.height = 0.1;
    progressBar.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
    progressBar.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
    progressBar.background = "black";
    progressBar.isVisible = false;
    advancedTexture.addControl( progressBar );

    var progressBarInner = new BABYLON.GUI.Rectangle();
    progressBarInner.width = 0;
    progressBarInner.height = 0.1; 
    progressBarInner.thickness = 0;
    progressBarInner.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
    progressBarInner.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
    progressBarInner.background = "green";
    progressBarInner.isVisible = false;
    advancedTexture.addControl( progressBarInner );
    
    return [initInstructions, [navInstructionBt, navInstructionsTxt], loadExampleBt, convertBt, downloadBt, progressBar, progressBarInner];
}

function downloadHyp(hyp, filename) {
    const blob = new Blob([hyp.buffer], { type: 'application/octet-stream' });
    const url = URL.createObjectURL(blob);
    const anchor = document.createElement('a');
  
    anchor.href = url;
    anchor.download = filename;
    anchor.click();
  
    setTimeout(() => {
      URL.revokeObjectURL(url);
    }, 100);    
}



var createScene = function () {

    var hypData = null;
    var gaussianSplattingsMesh = null;
    var gaussianSplattingsFile = null;

    // create a basic BJS Scene object
    var scene = new BABYLON.Scene(engine);
    scene.clearColor = new BABYLON.Color3(1.0, 1.0, 1.0);   

    // create camera
    let radius = 3;
    var camera = new BABYLON.ArcRotateCamera("camera1", -Math.PI / 2, Math.PI / 2, radius,new BABYLON.Vector3(0, 0, 0), scene);
    // This attaches the camera to the canvas
    camera.attachControl(canvas, true);
    camera.fov = 0.6;

    let [initInstructions, navInstruciton, loadExampleBt, convertBt, downloadBt, progressBar, progressBarInner]  = createUI();

    // setup drag and drop
    let filesInput = new BABYLON.FilesInput(engine, null, scene, null, null, null, function () { BABYLON.Tools.ClearLogCache() }, null, null);
    filesInput.onProcessFileCallback = (file, name, extension) => {dragAndDropCB(file, name, extension); };
    filesInput.monitorElementForDragNDrop(canvas);

    function loadExampleCB() {
        // show the navigation instruction
        navInstruciton[0].isVisible = true;
        navInstruciton[1].isVisible = true;
        setTimeout(function() {
            navInstruciton[0].isVisible = false;
            navInstruciton[1].isVisible = false;    
        }, 1500);
        
        initInstructions.isVisible = false;         
        loadExampleBt.isVisible = false;
        let url = "https://hyperinteractive.ai/data/clown.splat"
        gaussianSplattingsMesh = new BABYLON.GaussianSplattingMesh("gs", url, scene, true);
        gaussianSplattingsFile = "clown";
        gaussianSplattingsMesh.onMeshReadyObservable.add(() => {
            initInstructions.isVisible = false;
            convertBt.isVisible = true;
            downloadBt.isVisible = false;
        });
    }


    function dragAndDropCB(file, name, extension) {
        if(extension !== "splat" && extension !== "ply" && extension !== "hyp") {
            alert("Only .splat, .ply, and .hyp files are supported");
            return;
        }

        // show the navigation instruction 
        initInstructions.isVisible = false;         
        navInstruciton[0].isVisible = true;
        navInstruciton[1].isVisible = true;
        setTimeout(function() {
            navInstruciton[0].isVisible = false;
            navInstruciton[1].isVisible = false;    
        }, 1500);
        
        loadExampleBt.isVisible = false;

        // remove the extention from the filename
        gaussianSplattingsFile = name.replace(/\.[^/.]+$/, "");

        if(gaussianSplattingsMesh) {
            gaussianSplattingsMesh.dispose();        
        }
        
        const blob = new Blob([file]);
        let url = URL.createObjectURL(blob); 
        if(extension === "hyp") {
            const fileReader = new FileReader();
            fileReader.onload = function() {
                const arrayBuffer = this.result;
                const hypData = new Uint8Array(arrayBuffer);
                
                let newGS = fromHyp(hypData, scene)
                if(gaussianSplattingsMesh)
                    gaussianSplattingsMesh.dispose();
                gaussianSplattingsMesh = newGS;         
            };
            fileReader.readAsArrayBuffer(blob);
        }
        else {
            gaussianSplattingsMesh = new BABYLON.GaussianSplattingMesh("gs", url, scene, true);
    
            gaussianSplattingsMesh.onMeshReadyObservable.add(() => {
                initInstructions.isVisible = false;
                convertBt.isVisible = true;
                downloadBt.isVisible = false;
            });     
        }
    }

    // setup callbacks for the buttons

    loadExampleBt.onPointerDownObservable.add(loadExampleCB);

    convertBt.onPointerDownObservable.add(function() {
        convertBt.isVisible = false;        

        progressBarInner.isVisible = true;
        progressBar.isVisible = true;
        updateProgressBar(0.0);
        
        // convert Gaussian Splatting to hyp
        babylonGsToHyp(gaussianSplattingsMesh, { updateCB: updateProgressBar}).then(
            (hyp) => {

                // hide the progress bar
                progressBarInner.isVisible = false;
                progressBar.isVisible = false;
                        
                hypData = hyp;
                if(hypData){
                    downloadBt.isVisible = true;

                    let newGS = fromHyp(hypData, scene)
                    gaussianSplattingsMesh.dispose();
                    gaussianSplattingsMesh = newGS;
                }
                else {
                    alert("Failed to convert to hyp");
                    
                    gaussianSplattingsMesh.dispose();
                    gaussianSplattingsMesh = null;

                    initInstructions.isVisible = true;
                }
            }
        );
    });

    downloadBt.onPointerDownObservable.add(function() {
        downloadHyp(hypData, gaussianSplattingsFile + ".hyp");
    }  );

    function updateProgressBar(progress) {
        progressBarInner.width = progress;
    }

    return scene;
};