Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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.

Use a turntable drag in the model viewer

f865b269cec588f02dc39892e053d19a463896d5 · Keenan Nemetz · 9d ago · parent d4764b08

Use a turntable drag in the model viewer

Spin a horizontal drag about the model's own up axis and tip a vertical drag about the screen's horizontal axis. The two mix without roll and the model can be carried over the top. Replace the per-gesture axis lock; the orientation cube and snap views follow the same turn.

server/web/static/models/model-viewer.js +33−40

@@ -7,14 +7,14 @@
7 7 // file.
8 8 //
9 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.
10 // A drag turns the model the way a turntable turns what stands on it: sideways
11 // spins it about its own up axis, up and down tips it over the top, and the two
12 // mix without ever rolling it. The wheel or a pinch zooms, and the buttons
13 // repeat the zoom, reset the view and toggle a wireframe, so a reader who only
14 // clicks can still move around. The cube puts a named side of the model toward
15 // the reader. Two viewers in one diff turn together unless the reader unlinks
16 // them. The model is centred and scaled to a unit sphere, so a part exported in
17 // millimetres and one in metres frame the same way.
18 18
19 19 import {
20 20 WebGLRenderer, Scene, PerspectiveCamera, HemisphereLight, DirectionalLight,
@@ -253,23 +253,28 @@ export const upgrade = (host) => {
253 253 }
254 254 };
255 255
256 // A drag turns the model, not the camera. Each move is a rotation about the
257 // screen's own right and up axes, so the surface follows the pointer and the
258 // model can be rolled over and over in any direction. A drag the height of the
259 // canvas is one full turn, the rate the orbit used.
256 // A drag turns the model, not the camera, the way a turntable turns what is
257 // standing on it: a horizontal drag spins the model about its own up (Y) axis,
258 // so the side facing the reader follows the pointer left and right, and a
259 // vertical drag tips it about the screen's horizontal axis. Spin about the
260 // model's own up rather than the scene's vertical is what keeps a drag free of
261 // roll: that axis cannot move itself, so the model's up stays in the plane the
262 // reader sees as vertical however the two axes are mixed inside one diagonal
263 // drag, and a tip can carry the model over the top and out upside down with no
264 // pole to stop at. This is the arrangement Blender calls a turntable. Spinning
265 // about the scene's vertical after a tip does roll, because then the tipped up
266 // axis sweeps sideways. A drag across the width, or up the height, of the
267 // canvas is one full turn.
260 268 const pointers = new Set();
261 269 let turning = null;
262 270 let turnX = 0;
263 271 let turnY = 0;
264 // A gesture commits to one axis once the pointer has moved this far, so the
265 // few pixels a hand spends on the other axis do not roll the model.
266 const lockPx = 5;
267 let axis = null; // 'horizontal' or 'vertical' once the drag has chosen
268 let alongX = 0; // travel since the drag began, until it has chosen
269 let alongY = 0;
270 272 const screenRight = new Vector3();
271 const screenUp = new Vector3();
272 const screenOut = new Vector3();
273 const upAxis = new Vector3(0, 1, 0);
274 const spinAxis = new Vector3();
275 // extractBasis wants three vectors; only the right one is read.
276 const scratchUp = new Vector3();
277 const scratchOut = new Vector3();
273 278
274 279 const endTurn = (event) => {
275 280 pointers.delete(event.pointerId);
@@ -280,9 +285,6 @@ export const upgrade = (host) => {
280 285 canvas.addEventListener('pointerdown', (event) => {
281 286 pointers.add(event.pointerId);
282 287 canvas.setPointerCapture(event.pointerId);
283 axis = null;
284 alongX = 0;
285 alongY = 0;
286 288 // A second pointer is a pinch, which belongs to OrbitControls' zoom: it
287 289 // cancels the turn instead of turning while the fingers spread.
288 290 if (!root || event.button !== 0 || pointers.size !== 1) {
@@ -302,24 +304,15 @@ export const upgrade = (host) => {
302 304 turnX = event.clientX;
303 305 turnY = event.clientY;
304 306 if (dx === 0 && dy === 0) return;
305 // A hand-drawn drag is never straight. The first few pixels decide which
306 // axis the reader is asking for and the rest of the drag stays on it, so a
307 // turn cannot roll the model a little more on every stroke.
308 let moveX = dx;
309 let moveY = dy;
310 if (!axis) {
311 alongX += dx;
312 alongY += dy;
313 if (Math.abs(alongX) < lockPx && Math.abs(alongY) < lockPx) return;
314 axis = Math.abs(alongX) >= Math.abs(alongY) ? 'horizontal' : 'vertical';
315 moveX = alongX;
316 moveY = alongY;
317 }
318 307 camera.updateMatrixWorld();
319 camera.matrixWorld.extractBasis(screenRight, screenUp, screenOut);
320 const rate = (2 * Math.PI) / Math.max(1, canvas.clientHeight);
321 if (axis === 'horizontal') spin.rotateOnWorldAxis(screenUp, moveX * rate);
322 else spin.rotateOnWorldAxis(screenRight, moveY * rate);
308 camera.matrixWorld.extractBasis(screenRight, scratchUp, scratchOut);
309 // spinAxis is the model's own up axis in world space. It always lies in the
310 // camera's vertical plane and the camera's right is perpendicular to that
311 // plane, so the two axes below are always square to each other and neither
312 // drag can turn into the other.
313 spinAxis.copy(upAxis).applyQuaternion(spin.quaternion);
314 spin.rotateOnWorldAxis(spinAxis, dx * (2 * Math.PI) / Math.max(1, canvas.clientWidth));
315 spin.rotateOnWorldAxis(screenRight, dy * (2 * Math.PI) / Math.max(1, canvas.clientHeight));
323 316 render();
324 317 publish();
325 318 });