vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Add an orientation cube to the model viewer
d4764b08e9d56c48ff2bf2c8e8d6247e70f2d3e3 · Keenan Nemetz · 9d ago · parent 7178cad6
Add an orientation cube to the model viewer Replace the Front/Top/Right text row with a six-face CSS 3D cube. The cube follows the model's turn and the main camera, and clicking a face uses the existing turnTo snap path. A visually hidden six-button list keeps the views keyboard and screen-reader accessible; it appears when focused. No new dependency or WebGL context is added.
server/web/static/models/model-viewer.js +102−25
| @@ -6,14 +6,15 @@ | |||
| 6 | 6 | // link inside, so a reader without JavaScript or without WebGL still has the | |
| 7 | 7 | // file. | |
| 8 | 8 | // | |
| 9 | // A viewer is a canvas, a pointer and a few buttons. Drag turns the model over | ||
| 10 | // in any direction, the wheel or a pinch zooms, and the buttons repeat the | ||
| 11 | // zoom, put a named side of the model toward the reader, reset the view and | ||
| 12 | // toggle a wireframe, so a reader who only clicks can still move around. A drag | ||
| 13 | // commits to the axis it starts on, so a mostly horizontal drag cannot tilt the | ||
| 14 | // model, and two viewers in one diff turn together unless the reader unlinks | ||
| 15 | // them. The model is centred and scaled to a unit sphere, so a part exported in | ||
| 16 | // millimetres and one in metres frame the same way. | ||
| 9 | // A viewer is a canvas, a pointer, a few buttons and a small orientation cube. | ||
| 10 | // Drag turns the model over in any direction, the wheel or a pinch zooms, and | ||
| 11 | // the buttons repeat the zoom, reset the view and toggle a wireframe, so a | ||
| 12 | // reader who only clicks can still move around. The cube puts a named side of | ||
| 13 | // the model toward the reader. A drag commits to the axis it starts on, so a | ||
| 14 | // mostly horizontal drag cannot tilt the model, and two viewers in one diff | ||
| 15 | // turn together unless the reader unlinks them. The model is centred and scaled | ||
| 16 | // to a unit sphere, so a part exported in millimetres and one in metres frame | ||
| 17 | // the same way. | ||
| 17 | 18 | ||
| 18 | 19 | import { | |
| 19 | 20 | WebGLRenderer, Scene, PerspectiveCamera, HemisphereLight, DirectionalLight, | |
| @@ -24,6 +25,17 @@ import { | |||
| 24 | 25 | ||
| 25 | 26 | const FORMATS = new Set(['stl', 'obj', 'gltf', 'glb', '3mf']); | |
| 26 | 27 | ||
| 28 | // VIEW_FACES is the cube's six face names and labels. The order is the DOM | ||
| 29 | // order; the buttons are laid out in CSS, not in this order. | ||
| 30 | const VIEW_FACES = [ | ||
| 31 | ['front', 'Front'], | ||
| 32 | ['back', 'Back'], | ||
| 33 | ['right', 'Right'], | ||
| 34 | ['left', 'Left'], | ||
| 35 | ['top', 'Top'], | ||
| 36 | ['bottom', 'Bottom'], | ||
| 37 | ]; | ||
| 38 | |||
| 27 | 39 | // extension is the URL's file extension, ignoring a query or fragment the raw | |
| 28 | 40 | // link may carry. | |
| 29 | 41 | const extension = (url) => { | |
| @@ -190,9 +202,15 @@ export const upgrade = (host) => { | |||
| 190 | 202 | let height = 0; | |
| 191 | 203 | let root = null; | |
| 192 | 204 | let disposed = false; | |
| 205 | // Assigned once the cube exists; render is also called during the initial | ||
| 206 | // resize, before a model has loaded. | ||
| 207 | let drawCube = () => {}; | ||
| 193 | 208 | ||
| 194 | 209 | const render = () => { | |
| 195 | if (!disposed) renderer.render(scene, camera); | ||
| 210 | if (!disposed) { | ||
| 211 | renderer.render(scene, camera); | ||
| 212 | drawCube(); | ||
| 213 | } | ||
| 196 | 214 | }; | |
| 197 | 215 | ||
| 198 | 216 | // OrbitControls moves the camera and fires change; it does not draw. This | |
| @@ -206,6 +224,8 @@ export const upgrade = (host) => { | |||
| 206 | 224 | const adoptTurn = (turn) => { | |
| 207 | 225 | if (snap) { cancelAnimationFrame(snap); snap = 0; } | |
| 208 | 226 | spin.quaternion.copy(turn); | |
| 227 | // The sibling's turn is not this viewer's named view any more. | ||
| 228 | markView(''); | ||
| 209 | 229 | render(); | |
| 210 | 230 | }; | |
| 211 | 231 | const pair = pairFor(host); | |
| @@ -222,11 +242,15 @@ export const upgrade = (host) => { | |||
| 222 | 242 | } | |
| 223 | 243 | }; | |
| 224 | 244 | ||
| 225 | // viewButtons lets the row of views show which one the model is at. A drag off | ||
| 226 | // a view clears the row, so it never claims an angle the model has left. | ||
| 245 | // viewButtons lets the cube and its accessible list show which view the | ||
| 246 | // model is at. A drag off a view clears both, so neither claims an angle the | ||
| 247 | // model has left. | ||
| 227 | 248 | const viewButtons = new Map(); | |
| 228 | 249 | const markView = (name) => { | |
| 229 | for (const [view, element] of viewButtons) pressed(element, view === name); | ||
| 250 | for (const [view, controls] of viewButtons) { | ||
| 251 | pressed(controls.accessible, view === name); | ||
| 252 | controls.face.classList.toggle('active', view === name); | ||
| 253 | } | ||
| 230 | 254 | }; | |
| 231 | 255 | ||
| 232 | 256 | // A drag turns the model, not the camera. Each move is a rotation about the | |
| @@ -315,11 +339,20 @@ export const upgrade = (host) => { | |||
| 315 | 339 | const up = new Vector3(); | |
| 316 | 340 | const out = new Vector3(); | |
| 317 | 341 | camera.matrixWorld.extractBasis(right, up, out); | |
| 318 | const basis = new Matrix4(); | ||
| 319 | if (name === 'front') basis.makeBasis(right, up, out); | ||
| 320 | else if (name === 'top') basis.makeBasis(right, out, up.clone().negate()); | ||
| 321 | else basis.makeBasis(out, up, right.clone().negate()); | ||
| 322 | return new Quaternion().setFromRotationMatrix(basis); | ||
| 342 | const neg = (v) => v.clone().negate(); | ||
| 343 | let x = right; | ||
| 344 | let y = up; | ||
| 345 | let z = out; | ||
| 346 | switch (name) { | ||
| 347 | case 'back': x = neg(right); z = neg(out); break; | ||
| 348 | case 'right': x = out; z = neg(right); break; | ||
| 349 | case 'left': x = neg(out); z = right; break; | ||
| 350 | case 'top': y = out; z = neg(up); break; | ||
| 351 | case 'bottom': y = neg(out); z = up; break; | ||
| 352 | default: break; | ||
| 353 | } | ||
| 354 | return new Quaternion().setFromRotationMatrix( | ||
| 355 | new Matrix4().makeBasis(x, y, z)); | ||
| 323 | 356 | }; | |
| 324 | 357 | ||
| 325 | 358 | // turnTo eases to a view and publishes each frame the way a drag does, so a | |
| @@ -517,21 +550,65 @@ export const upgrade = (host) => { | |||
| 517 | 550 | tools.append(sync); | |
| 518 | 551 | } | |
| 519 | 552 | ||
| 520 | // The snap views sit in their own row, low and to the left, so a narrow diff | ||
| 521 | // pane wraps one cluster rather than two. | ||
| 553 | // The orientation cube is the pointer control; its faces are decorative | ||
| 554 | // buttons for the mouse, while the visually hidden list below it is the | ||
| 555 | // keyboard and screen-reader control for all six views. | ||
| 556 | const cube = document.createElement('div'); | ||
| 557 | cube.className = 'model-cube'; | ||
| 558 | cube.setAttribute('aria-hidden', 'true'); | ||
| 559 | const stage = document.createElement('div'); | ||
| 560 | stage.className = 'model-cube-stage'; | ||
| 561 | |||
| 522 | 562 | const views = document.createElement('div'); | |
| 523 | 563 | views.className = 'model-views'; | |
| 524 | for (const name of ['Front', 'Top', 'Right']) { | ||
| 525 | const view = name.toLowerCase(); | ||
| 526 | const element = button(name, name + ' view', () => { | ||
| 564 | for (const [view, label] of VIEW_FACES) { | ||
| 565 | const face = document.createElement('button'); | ||
| 566 | face.type = 'button'; | ||
| 567 | face.className = 'model-cube-face ' + view; | ||
| 568 | face.textContent = label; | ||
| 569 | face.title = label + ' view'; | ||
| 570 | face.tabIndex = -1; | ||
| 571 | face.addEventListener('click', () => { | ||
| 527 | 572 | markView(view); | |
| 528 | 573 | turnTo(viewTurn(view)); | |
| 529 | 574 | }); | |
| 530 | viewButtons.set(view, element); | ||
| 531 | views.append(element); | ||
| 575 | stage.append(face); | ||
| 576 | |||
| 577 | const accessible = button(label, label + ' view', () => { | ||
| 578 | markView(view); | ||
| 579 | turnTo(viewTurn(view)); | ||
| 580 | }); | ||
| 581 | views.append(accessible); | ||
| 582 | viewButtons.set(view, { accessible, face }); | ||
| 532 | 583 | } | |
| 584 | cube.append(stage); | ||
| 585 | |||
| 586 | // The cube is the model's axes as the main camera sees them. three.js uses | ||
| 587 | // y up; CSS uses y down, so F maps between the two. V is world -> camera, | ||
| 588 | // S is the model's turn, and F*V*S*F is the rotation CSS should apply to | ||
| 589 | // the cube's local (CSS-axis) geometry. | ||
| 590 | const flip = new Matrix4().makeScale(1, -1, 1); | ||
| 591 | const view = new Matrix4(); | ||
| 592 | const model = new Matrix4(); | ||
| 593 | const rotation = new Matrix4(); | ||
| 594 | const cubeRight = new Vector3(); | ||
| 595 | const cubeUp = new Vector3(); | ||
| 596 | const cubeOut = new Vector3(); | ||
| 597 | drawCube = () => { | ||
| 598 | camera.updateMatrixWorld(); | ||
| 599 | camera.matrixWorld.extractBasis(cubeRight, cubeUp, cubeOut); | ||
| 600 | view.makeBasis(cubeRight, cubeUp, cubeOut).transpose(); | ||
| 601 | model.makeRotationFromQuaternion(spin.quaternion); | ||
| 602 | rotation.multiplyMatrices(flip, view) | ||
| 603 | .multiply(model) | ||
| 604 | .multiply(flip); | ||
| 605 | const e = rotation.elements; | ||
| 606 | stage.style.transform = 'matrix3d(' | ||
| 607 | + e.map((value) => value.toFixed(6)).join(',') + ')'; | ||
| 608 | }; | ||
| 609 | drawCube(); | ||
| 533 | 610 | ||
| 534 | host.append(tools, views); | ||
| 611 | host.append(tools, cube, views); | ||
| 535 | 612 | resize(); | |
| 536 | 613 | }, undefined, () => { | |
| 537 | 614 | fail('This model could not be loaded. Download it to open it in a 3D application.'); | |