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.

Turn the model freely instead of orbiting to a pole

6064a74550d5c9d86c5b899010dc9a5cc7ba5622 · Keenan Nemetz · 11d ago · parent 0be96824

Turn the model freely instead of orbiting to a pole

OrbitControls clamps its polar angle to [0, π], so a vertical drag stopped
after half a turn: the reader could not roll the model over. Horizontal
orbiting wrapped, but the model could never be seen from the far side of a
pole.

The pointer now turns the model itself, about the screen's own right and up
axes, which has no pole: each move is a rotation applied to a group that
holds the centred model, so it can be rolled over and over in any direction
and stays centred on the model's own middle. A drag the height of the canvas
is still one full turn, so the feel is the one the orbit had. Two pointers
cancel the turn and stay with OrbitControls, which keeps the wheel, pinch
zoom, the pan and the distance limits the buttons share.

Reset puts the turn back to identity. It also recomputes the spin group's
world matrix before measuring the model: the bounding box is measured in
world space, so a reset after a turn would otherwise measure the model at
the angle it had just been left at and place it off centre.

Checked in headless Chromium against the bundled three.js, with an
asymmetric tetrahedron: five vertical drags moved the frame every time,
where the orbit version moved it once and then stopped; horizontal drags
still turn it; reset and the zoom and wireframe buttons still work.

server/web/static/models/model-viewer.js +79−16

@@ -6,11 +6,11 @@
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 four controls. Drag orbits, the wheel or a
10 // pinch zooms, and the buttons repeat the zoom, reset the view and toggle a
11 // wireframe, so a reader who only clicks can still move around. The model is
12 // centred and scaled to a unit sphere, so a part exported in millimetres and one
13 // in metres frame the same way.
9 // A viewer is a canvas, a pointer and four controls. Drag turns the model over
10 // in any direction, the wheel or a pinch zooms, and the buttons repeat the zoom,
11 // reset the view and toggle a wireframe, so a reader who only clicks can still
12 // move around. The model is centred and scaled to a unit sphere, so a part
13 // exported in millimetres and one in metres frame the same way.
14 14
15 15 import {
16 16 WebGLRenderer, Scene, PerspectiveCamera, HemisphereLight, DirectionalLight,
@@ -123,10 +123,19 @@ export const upgrade = (host) => {
123 123 renderer.outputColorSpace = SRGBColorSpace;
124 124
125 125 const scene = new Scene();
126 // The placed model lives under this group, and a drag turns the group. The
127 // camera keeps its line to the model, so the turn has no pole to stop at.
128 const spin = new Group();
129 scene.add(spin);
126 130 const camera = new PerspectiveCamera(38, 1, 0.01, 100);
127 131 const controls = new OrbitControls(camera, canvas);
128 132 controls.enableDamping = false;
129 133 controls.screenSpacePanning = true;
134 // An orbit clamps its polar angle to [0, π], so a vertical drag stops after
135 // half a turn. The pointer handler below turns the model itself instead, which
136 // has no such stop; OrbitControls is kept for the zoom, the pan and the
137 // distance limits the buttons share.
138 controls.enableRotate = false;
130 139 controls.minDistance = 0.8;
131 140 controls.maxDistance = 30;
132 141
@@ -151,11 +160,60 @@ export const upgrade = (host) => {
151 160 };
152 161
153 162 // OrbitControls moves the camera and fires change; it does not draw. This
154 // viewer renders on demand, so without this a drag orbits nothing the reader
155 // can see: the frame on screen stays the one before the drag, and the wheel
156 // zooms a picture that never changes.
163 // viewer renders on demand, so without this the wheel would zoom a picture
164 // that never changes.
157 165 controls.addEventListener('change', render);
158 166
167 // A drag turns the model, not the camera. Each move is a rotation about the
168 // screen's own right and up axes, so the surface follows the pointer and the
169 // model can be rolled over and over in any direction. A drag the height of the
170 // canvas is one full turn, the rate the orbit used.
171 const pointers = new Set();
172 let turning = null;
173 let turnX = 0;
174 let turnY = 0;
175 const screenRight = new Vector3();
176 const screenUp = new Vector3();
177 const screenOut = new Vector3();
178
179 const endTurn = (event) => {
180 pointers.delete(event.pointerId);
181 if (canvas.hasPointerCapture(event.pointerId)) canvas.releasePointerCapture(event.pointerId);
182 if (turning === event.pointerId) turning = null;
183 };
184
185 canvas.addEventListener('pointerdown', (event) => {
186 pointers.add(event.pointerId);
187 canvas.setPointerCapture(event.pointerId);
188 // A second pointer is a pinch, which belongs to OrbitControls' zoom: it
189 // cancels the turn instead of turning while the fingers spread.
190 if (!root || event.button !== 0 || pointers.size !== 1) {
191 turning = null;
192 return;
193 }
194 turning = event.pointerId;
195 turnX = event.clientX;
196 turnY = event.clientY;
197 });
198
199 canvas.addEventListener('pointermove', (event) => {
200 if (turning !== event.pointerId) return;
201 const dx = event.clientX - turnX;
202 const dy = event.clientY - turnY;
203 turnX = event.clientX;
204 turnY = event.clientY;
205 if (dx === 0 && dy === 0) return;
206 camera.updateMatrixWorld();
207 camera.matrixWorld.extractBasis(screenRight, screenUp, screenOut);
208 const rate = (2 * Math.PI) / Math.max(1, canvas.clientHeight);
209 spin.rotateOnWorldAxis(screenUp, dx * rate);
210 spin.rotateOnWorldAxis(screenRight, dy * rate);
211 render();
212 });
213
214 canvas.addEventListener('pointerup', endTurn);
215 canvas.addEventListener('pointercancel', endTurn);
216
159 217 // fitDistance is the camera distance that puts a unit sphere inside the
160 218 // narrower of the two field-of-view angles, so a narrow pane does not crop
161 219 // the model.
@@ -192,12 +250,17 @@ export const upgrade = (host) => {
192 250 };
193 251
194 252 // place centres the loaded model at the origin, scales its bounding sphere to
195 // a unit radius, and frames it. The controls' target is the origin, so orbit
196 // and zoom stay around what is actually drawn.
253 // a unit radius, and frames it. The controls' target is the origin, so zoom
254 // stays around what is actually drawn, and the turn is about the model's own
255 // centre rather than a corner.
197 256 const place = () => {
198 257 if (!root) return;
199 // Reset before measuring: a previous place() left the root scaled, and
200 // measuring that would compound the scale instead of replacing it.
258 // Reset the turn and the scale before measuring: the box is measured in world
259 // space, and a previous turn would measure the model at an angle. The world
260 // matrix is recomputed here because the measurement below reads the parent's
261 // stored matrix rather than rebuilding it.
262 spin.quaternion.identity();
263 spin.updateMatrixWorld();
201 264 root.scale.setScalar(1);
202 265 root.position.set(0, 0, 0);
203 266 const box = new Box3().setFromObject(root);
@@ -205,7 +268,7 @@ export const upgrade = (host) => {
205 268 const size = box.getSize(new Vector3());
206 269 const center = box.getCenter(new Vector3());
207 270 // The half-diagonal is the box's bounding-sphere radius, so the whole box
208 // stays inside the frame whatever angle the reader orbits to.
271 // stays inside the frame whatever angle the reader turns it to.
209 272 const radius = size.length() / 2;
210 273 if (!(radius > 0)) throw new Error('empty model');
211 274 const scale = 1 / radius;
@@ -274,7 +337,7 @@ export const upgrade = (host) => {
274 337 }
275 338 root = new Group();
276 339 root.add(object);
277 scene.add(root);
340 spin.add(root);
278 341 try {
279 342 place();
280 343 } catch (_) {
@@ -286,8 +349,8 @@ export const upgrade = (host) => {
286 349
287 350 const tools = document.createElement('div');
288 351 tools.className = 'model-tools';
289 // Orbit covers the pointer: drag orbits and the wheel or a pinch zooms, and
290 // these two repeat the zoom for a reader who only clicks.
352 // The pointer turns the model and the wheel or a pinch zooms, and these two
353 // repeat the zoom for a reader who only clicks.
291 354 const zoomOut = button('\u2212', 'Zoom out', () => zoomBy(1.25));
292 355 const zoomIn = button('+', 'Zoom in', () => zoomBy(0.8));
293 356 const reset = button('Reset view', 'Reset view', place);