| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<title>Filament Remote</title> |
| 5 |
<meta charset="utf-8"> |
| 6 |
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1"> |
| 7 |
<link href="../favicon.png" rel="icon" type="image/x-icon" /> |
| 8 |
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet"> |
| 9 |
<style> |
| 10 |
html, body { |
| 11 |
height: 100%; |
| 12 |
} |
| 13 |
|
| 14 |
body { |
| 15 |
margin: 0; |
| 16 |
overflow: hidden; |
| 17 |
} |
| 18 |
|
| 19 |
.container { |
| 20 |
font-family: "Open Sans"; |
| 21 |
display: flex; |
| 22 |
flex-direction: column; |
| 23 |
justify-content: center; |
| 24 |
align-items: center; |
| 25 |
width: 100%; |
| 26 |
height: 100%; |
| 27 |
} |
| 28 |
|
| 29 |
.status-area { |
| 30 |
display: flex; |
| 31 |
justify-content: center; |
| 32 |
align-items: center; |
| 33 |
width: 100%; |
| 34 |
height: 100%; |
| 35 |
max-width: 640px; |
| 36 |
max-height: 32px; |
| 37 |
background: burlywood; |
| 38 |
border: solid 2px black; |
| 39 |
border-bottom: none; |
| 40 |
font-size: 12px; |
| 41 |
} |
| 42 |
|
| 43 |
canvas { |
| 44 |
flex-direction: column; |
| 45 |
display: flex; |
| 46 |
justify-content: center; |
| 47 |
align-items: center; |
| 48 |
width: 100%; |
| 49 |
height: 100%; |
| 50 |
max-width: 640px; |
| 51 |
max-height: 640px; |
| 52 |
border: solid 2px black; |
| 53 |
} |
| 54 |
|
| 55 |
.dropbox-area { |
| 56 |
flex-direction: column; |
| 57 |
display: flex; |
| 58 |
justify-content: center; |
| 59 |
align-items: center; |
| 60 |
width: 100%; |
| 61 |
height: 100%; |
| 62 |
max-width: 640px; |
| 63 |
max-height: 160px; |
| 64 |
background: rgb(189, 189, 189); |
| 65 |
border: solid 2px black; |
| 66 |
border-top: none; |
| 67 |
} |
| 68 |
|
| 69 |
.dropbox-area p { text-align: center; } |
| 70 |
.instructions-area p { margin: 4px; } |
| 71 |
.instructions-area { margin-bottom: 12px; } |
| 72 |
a { text-decoration: none; } |
| 73 |
a:visited { color: rgb(26, 65, 78); } |
| 74 |
.bad { background: lightcoral; } |
| 75 |
.good { background: #45d48d; } |
| 76 |
</style> |
| 77 |
</head> |
| 78 |
|
| 79 |
<body> |
| 80 |
<div class="container"> |
| 81 |
<div id="status" class="status-area">Disconnected</div> |
| 82 |
<canvas id="webgl2-canvas"></canvas> |
| 83 |
<div id="dropbox" class="dropbox-area"> |
| 84 |
<div> |
| 85 |
<p>Drop a <b>glb</b> or a <b>zip</b> file here.</p> |
| 86 |
</div> |
| 87 |
</div> |
| 88 |
<div id="instructions" class="instructions-area"> |
| 89 |
<p><code>adb forward tcp:8082 tcp:8082</code></p> |
| 90 |
<button id="copyButton">Copy adb command to clipboard</button> |
| 91 |
</div> |
| 92 |
</div> |
| 93 |
|
| 94 |
<script src="filament.js"></script> |
| 95 |
|
| 96 |
<script> |
| 97 |
"use strict"; |
| 98 |
|
| 99 |
document.getElementById("copyButton").addEventListener("click", () => { |
| 100 |
navigator.clipboard.writeText("adb forward tcp:8082 tcp:8082"); |
| 101 |
}); |
| 102 |
|
| 103 |
["dragenter", "dragover", "dragleave", "drop"].forEach(eventName => { |
| 104 |
dropbox.addEventListener(eventName, e => { e.preventDefault(); e.stopPropagation() }, false) |
| 105 |
}) |
| 106 |
|
| 107 |
Filament.init([], () => window.app = new App() ); |
| 108 |
|
| 109 |
class App { |
| 110 |
constructor(canvas) { |
| 111 |
this.websocket = null; |
| 112 |
this.pollForServer = null; |
| 113 |
this.pendingFile = null; |
| 114 |
|
| 115 |
this.dropbox = document.getElementById("dropbox"); |
| 116 |
this.status = document.getElementById("status"); |
| 117 |
this.canvas = document.getElementsByTagName("canvas")[0]; |
| 118 |
|
| 119 |
const engine = this.engine = Filament.Engine.create(this.canvas); |
| 120 |
const scene = this.scene = engine.createScene(); |
| 121 |
const view = this.view = engine.createView(); |
| 122 |
const uiview = this.uiview = engine.createView(); |
| 123 |
|
| 124 |
this.swapChain = engine.createSwapChain(); |
| 125 |
this.renderer = engine.createRenderer(); |
| 126 |
this.camera = engine.createCamera(Filament.EntityManager.get().create()); |
| 127 |
this.serializer = new Filament.JsonSerializer(); |
| 128 |
this.previousSettingsJson = ""; |
| 129 |
|
| 130 |
view.setScene(scene); |
| 131 |
view.setCamera(this.camera); |
| 132 |
view.setPostProcessingEnabled(false); |
| 133 |
|
| 134 |
// For now, we initialize the "sidebar" such that it stretches across the entire viewport. |
| 135 |
// In the future we might want to draw stuff in the central 3D viewport. |
| 136 |
const kInitialSidebarWidth = this.canvas.clientWidth * window.devicePixelRatio; |
| 137 |
|
| 138 |
// Clear the central 3D viewport to light gray, which is not visible by default. |
| 139 |
const L = 189 / 255; |
| 140 |
const kBackgroundColor = [L, L, L, 1]; |
| 141 |
this.renderer.setClearOptions({clearColor: kBackgroundColor, clear: true}); |
| 142 |
|
| 143 |
this.render = this.render.bind(this); |
| 144 |
this.simpleViewer = new Filament.SimpleViewer(engine, scene, view, kInitialSidebarWidth); |
| 145 |
|
| 146 |
this.mouseX = -1; |
| 147 |
this.mouseY = -1; |
| 148 |
this.mouseButton = null; |
| 149 |
this.mouseButtonEvents = []; |
| 150 |
this.mouseWheelY = 0; |
| 151 |
|
| 152 |
Object.seal(this); |
| 153 |
|
| 154 |
this.canvas.addEventListener("pointermove", e => { |
| 155 |
this.mouseX = e.offsetX * window.devicePixelRatio; |
| 156 |
this.mouseY = e.offsetY * window.devicePixelRatio; |
| 157 |
}); |
| 158 |
|
| 159 |
this.canvas.addEventListener("pointerup", e => this.mouseButtonEvents.push(null)); |
| 160 |
this.canvas.addEventListener("pointerdown", e => this.mouseButtonEvents.push(e)); |
| 161 |
this.canvas.addEventListener("mousewheel", e => this.mouseWheelY = e.deltaY / 8); |
| 162 |
|
| 163 |
this.canvas.addEventListener("contextmenu", event => event.preventDefault()); |
| 164 |
|
| 165 |
document.addEventListener("keydown", e => this.simpleViewer.keyDownEvent(e.keyCode)); |
| 166 |
document.addEventListener("keyup", e => this.simpleViewer.keyUpEvent(e.keyCode)); |
| 167 |
document.addEventListener("keypress", e => this.simpleViewer.keyPressEvent(e.charCode)); |
| 168 |
|
| 169 |
const resize = () => { |
| 170 |
const dpr = window.devicePixelRatio; |
| 171 |
const width = this.canvas.clientWidth * dpr; |
| 172 |
const height = this.canvas.clientHeight * dpr; |
| 173 |
this.resize(width, height); |
| 174 |
}; |
| 175 |
window.addEventListener("resize", resize); |
| 176 |
resize(); |
| 177 |
|
| 178 |
const dropbox = this.dropbox; |
| 179 |
const upload = this.uploadFile.bind(this); |
| 180 |
|
| 181 |
dropbox.addEventListener("dragover", dragEvent => { |
| 182 |
dropbox.classList.add("bad"); |
| 183 |
if (!event.dataTransfer) return; |
| 184 |
if (event.dataTransfer.items[0].kind !== "file") return; |
| 185 |
dropbox.classList.remove("bad"); |
| 186 |
dropbox.classList.add("good"); |
| 187 |
}, false); |
| 188 |
|
| 189 |
dropbox.addEventListener("dragleave", () => { |
| 190 |
dropbox.classList.remove("good", "bad"); |
| 191 |
}, false); |
| 192 |
|
| 193 |
dropbox.addEventListener("drop", dragEvent => { |
| 194 |
dropbox.classList.remove("good", "bad"); |
| 195 |
if (!event.dataTransfer) return; |
| 196 |
if (event.dataTransfer.items[0].kind !== "file") return; |
| 197 |
const file = event.dataTransfer.items[0].getAsFile(); |
| 198 |
const is_glb = file.name.match(/\.(glb)$/i); |
| 199 |
const is_zip = file.name.match(/\.(zip)$/i); |
| 200 |
if (!is_glb && !is_zip) return; |
| 201 |
const files = event.dataTransfer.files; |
| 202 |
([...files]).forEach(upload); |
| 203 |
}, false); |
| 204 |
|
| 205 |
this.startSocket(); |
| 206 |
window.requestAnimationFrame(this.render); |
| 207 |
} |
| 208 |
|
| 209 |
uploadFile(file) { |
| 210 |
const connection = this.websocket; |
| 211 |
if (connection && connection.readyState == connection.OPEN) { |
| 212 |
console.info(`Uploading ${file.name}`); |
| 213 |
file.arrayBuffer().then(buffer => { |
| 214 |
connection.send(file.name); |
| 215 |
connection.send(buffer); |
| 216 |
}); |
| 217 |
} else { |
| 218 |
this.pendingFile = file; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
updateDom() { |
| 223 |
this.status.innerHTML = this.websocket ? "Connected" : "Disconnected"; |
| 224 |
this.status.style.backgroundColor = this.websocket ? "#45d48d" : "burlywood"; |
| 225 |
} |
| 226 |
|
| 227 |
startSocket() { |
| 228 |
const ws = new WebSocket("ws://localhost:8082"); |
| 229 |
|
| 230 |
ws.addEventListener("open", () => { |
| 231 |
clearTimeout(this.pollForServer); |
| 232 |
this.websocket = ws; |
| 233 |
this.updateDom(); |
| 234 |
this.previousSettingsJson = ""; |
| 235 |
if (this.pendingFile) { |
| 236 |
this.uploadFile(this.pendingFile); |
| 237 |
this.pendingFile = null; |
| 238 |
} |
| 239 |
}); |
| 240 |
|
| 241 |
ws.addEventListener("close", (e) => { |
| 242 |
this.websocket = null; |
| 243 |
this.pollForServer = setTimeout(() => this.startSocket(), 3000); |
| 244 |
this.updateDom(); |
| 245 |
}); |
| 246 |
|
| 247 |
ws.addEventListener("message", event => { |
| 248 |
if (!event.data) return; |
| 249 |
let commands = []; |
| 250 |
try { |
| 251 |
commands = JSON.parse(event.data); |
| 252 |
} |
| 253 |
catch (err) { |
| 254 |
console.error(err, event.data); |
| 255 |
return; |
| 256 |
} |
| 257 |
for (const command of commands) { |
| 258 |
// TODO: process incoming messages here. |
| 259 |
// Currently we do not send messages from the app on the device to the web client. |
| 260 |
} |
| 261 |
}); |
| 262 |
} |
| 263 |
|
| 264 |
render() { |
| 265 |
const connection = this.websocket; |
| 266 |
|
| 267 |
// Process only a single mouse button event to ensure that ImGui detects a touch event |
| 268 |
// even when a down-up pair occurs between consecutive frames. |
| 269 |
let mouseButton = this.mouseButton; |
| 270 |
if (this.mouseButtonEvents.length > 0) { |
| 271 |
this.mouseButton = this.mouseButtonEvents.shift(); |
| 272 |
} |
| 273 |
const mouseWheel = this.mouseWheelY; |
| 274 |
this.mouseWheelY = 0; |
| 275 |
this.simpleViewer.mouseEvent(this.mouseX, this.mouseY, !!this.mouseButton, |
| 276 |
mouseWheel, this.mouseButton && this.mouseButton.ctrlKey); |
| 277 |
|
| 278 |
// If there's no connection, let Filament clear the canvas, but do not render the UI view. |
| 279 |
if (!connection) { |
| 280 |
this.renderer.beginFrame(this.swapChain); |
| 281 |
this.renderer.renderView(this.view); |
| 282 |
this.renderer.endFrame(); |
| 283 |
this.engine.execute(); |
| 284 |
window.requestAnimationFrame(this.render); |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
// Draw the UI and potentially mutate the settings object. |
| 289 |
const deltaTime = 1.0 / 60.0; |
| 290 |
this.simpleViewer.renderUserInterface(deltaTime, this.uiview, window.devicePixelRatio); |
| 291 |
|
| 292 |
// Check if the user has changed any settings. |
| 293 |
const settingsJson = this.serializer.writeJson(this.simpleViewer.getSettings()).slice(); |
| 294 |
if (this.previousSettingsJson != settingsJson) { |
| 295 |
if (connection && connection.readyState == connection.OPEN) { |
| 296 |
connection.send("setttings.json"); |
| 297 |
connection.send(settingsJson); |
| 298 |
} |
| 299 |
this.previousSettingsJson = settingsJson; |
| 300 |
} |
| 301 |
|
| 302 |
// Use Filament to render the 3D viewport and the UI. |
| 303 |
this.renderer.beginFrame(this.swapChain); |
| 304 |
this.renderer.renderView(this.view); |
| 305 |
this.renderer.renderView(this.uiview); |
| 306 |
this.renderer.endFrame(); |
| 307 |
this.engine.execute(); |
| 308 |
window.requestAnimationFrame(this.render); |
| 309 |
} |
| 310 |
|
| 311 |
resize(width, height) { |
| 312 |
const Projection = Filament.Camera$Projection; |
| 313 |
this.canvas.width = width; |
| 314 |
this.canvas.height = height; |
| 315 |
this.view.setViewport([0, 0, width, height]); |
| 316 |
this.uiview.setViewport([0, 0, width, height]); |
| 317 |
this.camera.setProjection(Projection.ORTHO, 0, width, height, 0, 0, 1); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
</script> |
| 322 |
</body> |
| 323 |
</html> |