-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-website
More file actions
executable file
·215 lines (187 loc) · 6.46 KB
/
generate-website
File metadata and controls
executable file
·215 lines (187 loc) · 6.46 KB
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
#!/bin/bash
if [ -z "$( ls -A './output' )" ]; then
echo "No files found in the ./output folder, exiting quietly"
exit 0
fi
rm -rf ./website || exit 1
mkdir -p ./website || exit 1
# Copy all reduced PLY files and build JavaScript array
SPLAT_ARRAY=""
FIRST_FILE=""
for file in ./output/*-reduced.ply; do
[ -e "$file" ] || continue
BASE=$(basename "$file")
cp "$file" ./website/
if [ -z "$FIRST_FILE" ]; then
FIRST_FILE="$BASE"
SPLAT_ARRAY="\"$BASE\""
else
SPLAT_ARRAY="$SPLAT_ARRAY, \"$BASE\""
fi
done
# Create index page
cat <<EOM > "./website/index.html" || exit 1
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>ML — Sharp VR Album</title>
<style>
body {
margin: auto;
}
main {
margin: auto;
max-width: 768px;
}
</style>
</head>
<body>
<main>
<h1>ML — Sharp VR Album</h1>
<p>View your photo album in VR! Use the PREV/NEXT buttons in VR to navigate between scenes.</p>
<p><a href="viewer.html">Enter VR Viewer</a></p>
<h2>Available Scenes:</h2>
<ul>
$(for file in ./output/*-reduced.ply; do
[ -e "$file" ] || continue
BASE=$(basename "$file" .ply)
BASE=${BASE%-reduced}
echo "<li>$BASE</li>"
done)
</ul>
<p>Made with <a href="https://github.com/boutell/ml-sharp-ez">ml-sharp-ez</a> and <a href="https://github.com/apple/ml-sharp">ml-sharp</a></p>
</main>
</body>
EOM
# Create single VR viewer page
cat <<EOM > "./website/viewer.html" || exit 1
<!DOCTYPE html>
<style> body {margin: 0;} </style>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.178.0/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.178.0/examples/jsm/",
"@sparkjsdev/spark": "https://sparkjs.dev/releases/spark/0.1.10/spark.module.js"
}
}
</script>
<script type="module">
import * as THREE from "three";
import { VRButton } from 'three/addons/webxr/VRButton.js';
import { SplatMesh } from "@sparkjsdev/spark";
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 1.5, 0);
camera.lookAt(0, 1.5, -1);
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: 'high-performance' });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
renderer.xr.enabled = true;
renderer.xr.setReferenceSpaceType('local-floor');
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 1.5, -1);
controls.update();
const splats = [$SPLAT_ARRAY];
let currentIndex = 0;
let currentSplat = null;
function loadSplat(url) {
if (currentSplat) scene.remove(currentSplat);
currentSplat = new SplatMesh({
url: url,
enableControls: false,
halfPrecision: true,
antialiased: false
});
currentSplat.position.set(0, 1.5, -1);
currentSplat.rotation.set(0, Math.PI, Math.PI);
currentSplat.scale.set(0.3, 0.3, 0.3);
scene.add(currentSplat);
}
function createButton(text, position, onClick) {
const geometry = new THREE.PlaneGeometry(0.3, 0.1);
const material = new THREE.MeshBasicMaterial({ color: 0x4444ff });
const button = new THREE.Mesh(geometry, material);
button.position.copy(position);
const canvas = document.createElement('canvas');
canvas.width = 256;
canvas.height = 128;
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'white';
ctx.font = 'bold 48px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(text, 128, 64);
const texture = new THREE.CanvasTexture(canvas);
const textMaterial = new THREE.MeshBasicMaterial({ map: texture, transparent: true, side: THREE.DoubleSide });
const textMesh = new THREE.Mesh(geometry, textMaterial);
textMesh.position.z = 0.01;
button.add(textMesh);
button.userData.onClick = onClick;
button.userData.isButton = true;
return button;
}
const prevButton = createButton('PREV', new THREE.Vector3(-0.3, 1.3, -0.5), () => {
currentIndex = (currentIndex - 1 + splats.length) % splats.length;
loadSplat(splats[currentIndex]);
});
scene.add(prevButton);
const nextButton = createButton('NEXT', new THREE.Vector3(0.3, 1.3, -0.5), () => {
currentIndex = (currentIndex + 1) % splats.length;
loadSplat(splats[currentIndex]);
});
scene.add(nextButton);
const controllerModelFactory = new XRControllerModelFactory();
const raycaster = new THREE.Raycaster();
// Desktop mouse click support
const mouse = new THREE.Vector2();
window.addEventListener('click', (event) => {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(scene.children, true);
for (let intersect of intersects) {
let obj = intersect.object;
while (obj.parent && !obj.userData.isButton) obj = obj.parent;
if (obj.userData.isButton && obj.userData.onClick) {
obj.userData.onClick();
break;
}
}
});
for (let i = 0; i <= 1; i++) {
const controller = renderer.xr.getController(i);
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, -1)
]);
const line = new THREE.Line(geometry, new THREE.LineBasicMaterial({ color: 0xffffff }));
controller.add(line);
controller.addEventListener('selectstart', () => {
raycaster.setFromXRController(controller);
const intersects = raycaster.intersectObjects(scene.children, true);
for (let intersect of intersects) {
let obj = intersect.object;
while (obj.parent && !obj.userData.isButton) obj = obj.parent;
if (obj.userData.isButton && obj.userData.onClick) {
obj.userData.onClick();
break;
}
}
});
scene.add(controller);
const grip = renderer.xr.getControllerGrip(i);
grip.add(controllerModelFactory.createControllerModel(grip));
scene.add(grip);
}
loadSplat(splats[0]);
document.body.appendChild(VRButton.createButton(renderer));
renderer.setAnimationLoop(function() {
controls.update();
renderer.render(scene, camera);
});
</script>
EOM
echo "Website generated successfully in ./website/"