Spaces:
Running
Running
Create index_sans_gif.js
Browse files- index_sans_gif.js +181 -0
index_sans_gif.js
ADDED
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
(function() {
|
2 |
+
let cameraInstance = null;
|
3 |
+
let controlsInstance = null;
|
4 |
+
let initialCameraPosition = null;
|
5 |
+
let initialCameraRotation = null;
|
6 |
+
|
7 |
+
function getScriptQueryParam(param) {
|
8 |
+
var params = new URLSearchParams("");
|
9 |
+
if (document.currentScript && document.currentScript.src.indexOf('?') !== -1) {
|
10 |
+
var queryString = document.currentScript.src.split('?')[1];
|
11 |
+
params = new URLSearchParams(queryString);
|
12 |
+
} else {
|
13 |
+
params = new URLSearchParams(window.location.search);
|
14 |
+
}
|
15 |
+
return params.get(param);
|
16 |
+
}
|
17 |
+
|
18 |
+
var plyUrl = getScriptQueryParam("ply_url");
|
19 |
+
var minZoom = parseFloat(getScriptQueryParam("minZoom") || "0");
|
20 |
+
var maxZoom = parseFloat(getScriptQueryParam("maxZoom") || "20");
|
21 |
+
var minAngle = parseFloat(getScriptQueryParam("minAngle") || "0");
|
22 |
+
var maxAngle = parseFloat(getScriptQueryParam("maxAngle") || "360");
|
23 |
+
var isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
24 |
+
|
25 |
+
var styleEl = document.createElement('style');
|
26 |
+
styleEl.textContent = `
|
27 |
+
#viewer-container {
|
28 |
+
position: relative;
|
29 |
+
width: 100%;
|
30 |
+
height: 100vh;
|
31 |
+
background: #FEFEFD;
|
32 |
+
border: 1px solid #474558;
|
33 |
+
border-radius: 10px;
|
34 |
+
}
|
35 |
+
#canvas {
|
36 |
+
width: 100%;
|
37 |
+
height: 100%;
|
38 |
+
display: block;
|
39 |
+
}
|
40 |
+
#progress-dialog {
|
41 |
+
position: absolute;
|
42 |
+
top: 50%;
|
43 |
+
left: 50%;
|
44 |
+
transform: translate(-50%, -50%);
|
45 |
+
background: rgba(255,255,255,0.9);
|
46 |
+
padding: 20px;
|
47 |
+
border-radius: 5px;
|
48 |
+
z-index: 1000;
|
49 |
+
display: none;
|
50 |
+
}
|
51 |
+
#menu-content {
|
52 |
+
display: none;
|
53 |
+
position: absolute;
|
54 |
+
top: 62px;
|
55 |
+
right: 70px;
|
56 |
+
background: #FFFEF4;
|
57 |
+
border: 1px solid #474558;
|
58 |
+
border-radius: 5px;
|
59 |
+
padding: 10px;
|
60 |
+
font-size: 15px;
|
61 |
+
line-height: 1.4;
|
62 |
+
color: #474558;
|
63 |
+
}
|
64 |
+
.widget-button {
|
65 |
+
position: absolute;
|
66 |
+
width: 45px;
|
67 |
+
height: 45px;
|
68 |
+
background-color: #FFFEF4;
|
69 |
+
border: 1px solid #474558;
|
70 |
+
border-radius: 50%;
|
71 |
+
cursor: pointer;
|
72 |
+
font-size: 14px;
|
73 |
+
color: #474558;
|
74 |
+
display: flex;
|
75 |
+
align-items: center;
|
76 |
+
justify-content: center;
|
77 |
+
}
|
78 |
+
#fullscreen-toggle { top: 17px; right: 15px; }
|
79 |
+
#help-toggle { top: 72px; right: 15px; }
|
80 |
+
#reset-camera-btn { top: 127px; right: 15px; }
|
81 |
+
`;
|
82 |
+
document.head.appendChild(styleEl);
|
83 |
+
|
84 |
+
var viewerContainer = document.createElement('div');
|
85 |
+
viewerContainer.id = 'viewer-container';
|
86 |
+
viewerContainer.innerHTML = `
|
87 |
+
<canvas id="canvas"></canvas>
|
88 |
+
<div id="progress-dialog">
|
89 |
+
<progress id="progress-indicator" max="100" value="0"></progress>
|
90 |
+
</div>
|
91 |
+
<button id="fullscreen-toggle" class="widget-button">⇱</button>
|
92 |
+
<button id="help-toggle" class="widget-button">?</button>
|
93 |
+
<button id="reset-camera-btn" class="widget-button">🗘</button>
|
94 |
+
<div id="menu-content">
|
95 |
+
- Rotate with right click<br>
|
96 |
+
- Zoom in/out with middle click<br>
|
97 |
+
- Translate with left click
|
98 |
+
</div>
|
99 |
+
`;
|
100 |
+
document.currentScript.parentNode.appendChild(viewerContainer);
|
101 |
+
|
102 |
+
var fullscreenToggle = document.getElementById('fullscreen-toggle');
|
103 |
+
var helpToggle = document.getElementById('help-toggle');
|
104 |
+
var resetCameraBtn = document.getElementById('reset-camera-btn');
|
105 |
+
var menuContent = document.getElementById('menu-content');
|
106 |
+
var canvas = document.getElementById('canvas');
|
107 |
+
var progressDialog = document.getElementById('progress-dialog');
|
108 |
+
var progressIndicator = document.getElementById('progress-indicator');
|
109 |
+
|
110 |
+
fullscreenToggle.addEventListener('click', function() {
|
111 |
+
if (isIOS) {
|
112 |
+
viewerContainer.classList.toggle('fake-fullscreen');
|
113 |
+
fullscreenToggle.textContent = viewerContainer.classList.contains('fake-fullscreen') ? '⇲' : '⇱';
|
114 |
+
} else {
|
115 |
+
if (!document.fullscreenElement) {
|
116 |
+
viewerContainer.requestFullscreen();
|
117 |
+
} else {
|
118 |
+
document.exitFullscreen();
|
119 |
+
}
|
120 |
+
}
|
121 |
+
});
|
122 |
+
|
123 |
+
document.addEventListener('fullscreenchange', function() {
|
124 |
+
fullscreenToggle.textContent = document.fullscreenElement ? '⇲' : '⇱';
|
125 |
+
});
|
126 |
+
|
127 |
+
helpToggle.addEventListener('click', function(e) {
|
128 |
+
e.stopPropagation();
|
129 |
+
menuContent.style.display = menuContent.style.display === 'block' ? 'none' : 'block';
|
130 |
+
});
|
131 |
+
|
132 |
+
resetCameraBtn.addEventListener('click', function() {
|
133 |
+
if (cameraInstance && initialCameraPosition && initialCameraRotation) {
|
134 |
+
cameraInstance.position.copy(initialCameraPosition);
|
135 |
+
cameraInstance.rotation.copy(initialCameraRotation);
|
136 |
+
if (typeof cameraInstance.update === 'function') cameraInstance.update();
|
137 |
+
if (controlsInstance && typeof controlsInstance.update === 'function') controlsInstance.update();
|
138 |
+
}
|
139 |
+
});
|
140 |
+
|
141 |
+
async function initializeViewer() {
|
142 |
+
const SPLAT = await import("https://cdn.jsdelivr.net/npm/gsplat@latest");
|
143 |
+
progressDialog.style.display = 'block';
|
144 |
+
const renderer = new SPLAT.WebGLRenderer(canvas);
|
145 |
+
const scene = new SPLAT.Scene();
|
146 |
+
const camera = new SPLAT.Camera();
|
147 |
+
const controls = new SPLAT.OrbitControls(camera, canvas);
|
148 |
+
|
149 |
+
cameraInstance = camera;
|
150 |
+
controlsInstance = controls;
|
151 |
+
initialCameraPosition = camera.position.clone();
|
152 |
+
initialCameraRotation = camera.rotation.clone();
|
153 |
+
|
154 |
+
canvas.style.background = "#FEFEFD";
|
155 |
+
controls.maxZoom = maxZoom;
|
156 |
+
controls.minZoom = minZoom;
|
157 |
+
controls.minAngle = minAngle;
|
158 |
+
controls.maxAngle = maxAngle;
|
159 |
+
controls.update();
|
160 |
+
|
161 |
+
try {
|
162 |
+
await SPLAT.PLYLoader.LoadAsync(plyUrl, scene, (progress) => {
|
163 |
+
progressIndicator.value = progress * 100;
|
164 |
+
});
|
165 |
+
progressDialog.style.display = 'none';
|
166 |
+
} catch (error) {
|
167 |
+
console.error("Error loading PLY file:", error);
|
168 |
+
progressDialog.innerHTML = `<p style="color: red">Error loading model: ${error.message}</p>`;
|
169 |
+
}
|
170 |
+
|
171 |
+
const frame = () => {
|
172 |
+
controls.update();
|
173 |
+
renderer.render(scene, camera);
|
174 |
+
requestAnimationFrame(frame);
|
175 |
+
};
|
176 |
+
|
177 |
+
window.addEventListener("resize", () => renderer.setSize(canvas.clientWidth, canvas.clientHeight));
|
178 |
+
requestAnimationFrame(frame);
|
179 |
+
}
|
180 |
+
initializeViewer();
|
181 |
+
})();
|