Skip to content

Implement SmoothControls - #288

Merged
cdata merged 2 commits into
masterfrom
better-default-controls
Jan 9, 2019
Merged

cdata merged 2 commits into
masterfrom
better-default-controls

Conversation

@cdata

@cdata cdata commented Jan 8, 2019 •

Copy link
Copy Markdown
Contributor

This change proposes SmoothControls as a more suitable alternative to OrbitControls.

  • Desired keyboard support out of the box (PatchedOrbitControls is no longer necessary)
  • Configurable, velocity-based smoothing / dampening on all motion (including for keyboard input)
  • Similar orbital position constraints to OrbitControls (pole, azimuth, radius)
  • SmoothControls constrains radius ("zoom") by default
  • Configurable behavior for event cancelling (cancel all events, or only cancel handled events)
  • Configurable behavior for zoom (always allow zoom, or only allow zoom when focused)
  • CSS "grab" cursor is used to provide a visual signal to desktop users
  • Includes public API to directly assign the orbital position of the camera
  • Panning is not currently supported (but should probably be considered for a future change)
  • Implemented in TypeScript
  • Includes a basic test suite

Basic Example

OrbitControls SmoothControls
astro-orbit

Background Image

OrbitControls SmoothControls
pbr-smooth

Keyboard Input

PatchedOrbitControls SmoothControls
keyb-orbit keyb-smooth

Scrolling Behavior

OrbitControls SmoothControls
wheel-orbit wheel-smooth
Scrolling interrupted by first hovered model Events are only captured after model is focused

Fixes #75
Fixes #178

@cdata cdata changed the title (WIP) Implement SmoothControls Implement SmoothControls Jan 8, 2019
@cdata
cdata requested review from jsantell and mrdoob January 8, 2019 03:53
jsantell
jsantell previously approved these changes Jan 8, 2019

@jsantell jsantell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, excellent work! I just reviewed the code, not the UX of the changes. A handful of comments/questions, but the gifs look soooo good, and hope & dream to see a future of these controls beyond just model-viewer 🥂

Comment thread package.json
"@polymer/paper-button": "^3.0.1",
"@polymer/paper-radio-group": "^3.0.1",
"@polymer/paper-slider": "^3.0.1",
"@types/three": "^0.93.12",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any correllation here with three version? Doesn't seem to be, but wondering how to handle when latest three is out of sync here

https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/three

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. Three.js's API is unusually stable thanks to @mrdoob 's strong discipline for maintaining backwards-compatibility. But, I suspect there is a real risk here of the types falling out of date, and I also suspect that these are only updated as people notice that the API has changed.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrdoob I would second that person's recommendation for another reason: @types/three do not currently work if we import individual modules. This would be easy to fix with "sidecar" type declarations.

But, it's also a large burden on you and your contributors to maintain those type files, so I understand if that doesn't seem like a good idea to you.

Comment thread src/test/utils-spec.js
});
});

suite('step', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For functions like step, clamp, I wonder if we could just leverage lodash or something for these small utilities (although a quick look, there is no step function in lodash, maybe under another name)? No strong feelings

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we should definitely consider that as the utils file grows. The cost of adding a third party dependency is greater than zero, though, so we should weigh it against the cost of 10s of lines of first-party JS.

Comment thread src/three-components/SmoothControls.ts Outdated
const MAXIMUM_DAMPENING_FACTOR = 0.05;
const MINIMUM_DAMPENING_FACTOR = 0.3;
const FRAME_MILLISECONDS = 1000.0 / 60.0;
const TWO_PI = 2 * Math.PI;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TAU? 😀

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* interpolating its position and rotation until it lines up with the
* designated target orbital position.
*/
update(_time: number, delta: number) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen delta be both milliseconds and seconds in three controls -- milliseconds seems more common, but would be worthwhile documenting

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread src/three-components/SmoothControls.ts Outdated
zoomPolicy: 'allow-when-focused'
});

const $constraints = Symbol('constraints');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of symbols! I think it'd be helpful having a one line comment above each group to indicate how they're used, e.g. // Mouse events

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's a bit of a mess. I'll try to clean it up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added comments to the various groupings of symbols

element.style.cursor = 'grab';

this[$previousPosition].copy(this.camera.position);
this[$positionToSpherical](this.camera.position, this[$spherical]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes the camera has no parent, and that its initial pose was set via TRS rather than a matrix, and positionToSpherical assumes the position in world space as well (since subtracting from the sceneOrigin) (which is probably ok, but should be clear on that caveat)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these are the same assumptions made by OrbitControls. I'll double check and document where we diverge.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these assumptions are the same. I'll document explicitly that we assume TRS is synced with the object's matrix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

private[$handleWheel](event: Event) {
const rootNode = this.element.getRootNode() as Document | ShadowRoot;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maximumPolarAngle?: number;
minimumAzimuthalAngle?: number;
maximumAzimuthalAngle?: number;
decelerationMargin?: number;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be helpful for some comments on decelerationMargin, acceleration, dampeningScale, eventHandlingBehavior, zoomPolicy -- when using controls like this, they're often arbitrary scales and requires some plugging in numbers to find an acceptable range, rather than something bound to time, e.g. m/s^2

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will document

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!!

Comment thread src/three-components/SmoothControls.ts Outdated
export type ZoomPolicy = 'always-allow'|'allow-when-focused';
export type TouchMode = 'rotate'|'zoom';

export interface SmoothControlsConstraints {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure of the word "constraints" (and below, e.g. applyConstraints), as it's accurate for most of the options, but less obvious for acceleration, etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I don't love it either, open to alternatives

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe configuration or options or something, but don't feel strongly (and more important if used outside of model-viewer)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SG, I'll try options

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread src/three-components/SmoothControls.ts Outdated

export interface SmoothControlsConstraints {
nearOrbitRadius?: number;
farOrbitRadius?: number;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

near and far imply (IMO) that it has something to do with the camera's near and far planes and was unclear what these values did until digging more -- maybe, similar to the polar/azimuthal config, and using terminology used elsewhere in the controls (zoom and radius), maybe something like minimumZoom/maximumZoom, minimumRadius, maxiumRadius

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't fully thought this out, but it seems like zoom and radius are distinct things and that it is better to be explicit about what is being adjusted.

I agree that near and far are weird words to use, can change that to minimum and maximum.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking again, agreed that zoom doesn't match -- minimumRadius and maximumRadius sound great to me and inline with the other options

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 will fix

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* limitations under the License.
*/

import {Camera, EventDispatcher, Quaternion, Spherical, Vector2, Vector3} from 'three';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you want to import directly from source?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do, but see comment above about type declarations #288 (comment)

mrdoob
mrdoob previously approved these changes Jan 8, 2019
@cdata
cdata dismissed stale reviews from mrdoob and jsantell via a303722 January 8, 2019 23:09
@cdata
cdata force-pushed the better-default-controls branch from a303722 to d17dfde Compare January 8, 2019 23:27
jsantell
jsantell previously approved these changes Jan 9, 2019

@jsantell jsantell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes! LGTM, but think the skysphere logic may have changed

[$tick](time, delta) {
super[$tick](time, delta);
const camera = this[$scene].getCamera();
this[$scene].skysphere.position.copy(camera.position);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this no longer needed? Wouldn't this change the skysphere behavior to 'zoom' when zooming in on the model, as opposed to it's current implementation? I think there are also some scenarios where the skysphere may not be big enough, but I think that was due to panning in orbit controls

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking subjectively, the behavior without this line looks more natural to me when moving towards / away from the model.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, and that's what we had originally -- I believe @mrdoob mentioned that a skybox moving with the camera was the common/expected skybox behavior though

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked @smalls for an additional take and he prefers skybox not zooming, so I will revert this line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backreferencing #100 so that we can track this discussion as part of that issue

maximumPolarAngle?: number;
minimumAzimuthalAngle?: number;
maximumAzimuthalAngle?: number;
decelerationMargin?: number;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants