Skip to content

Adjustable FOV - #563

Merged
elalish merged 19 commits into
masterfrom
FOV
Jun 7, 2019
Merged

elalish merged 19 commits into
masterfrom
FOV

Conversation

@elalish

@elalish elalish commented May 21, 2019

Copy link
Copy Markdown
Contributor

Reference Issue

Fixes #232, #537

This adds the camera-FOV attribute to the element, allowing control of the camera's field of view. In the process of making SmoothControls properly interpolate FOV, I also rewrote the interpolation scheme to be simpler, with fewer parameters and extensible to any number of interpolated states, which should help when we want to add configurable targets. I also removed the camera from controls.ts, such that SmoothControls is now the only place where the camera is updated.

One thing I have not done, but I mentioned in #232 was to change the definition of FOV from vertical FOV to widest FOV. I think this would be a good change to make, but the rest of this PR should not affect framing at all, so that might be best in a separate one, assuming others agree to the change. Also, it looks like the fidelity goldens are out of date again.

@elalish
elalish requested a review from cdata May 21, 2019 20:36
@elalish elalish self-assigned this May 21, 2019

@cdata cdata 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.

This is really exciting \o/ nothing major needs changing, mostly just cleanup and bike shedding on my part. I do think that we might be allowing framing to bleed into SmoothControls a bit, and that we should probably strive to keep a clean separation between those concepts. I'm interested to hear your thoughts on this.

Comment thread src/conversions.ts
return null;
};

export const deserializeAngleToDeg = (angleString: string): number|null => {

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.

Please add tests ☝️

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 do

Comment thread src/conversions.ts
try {
const angleValueNode = parseValues(angleString);

if (angleValueNode.length === 1) {

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.

Does the length imply a unit?

The unit should probably be inferred from angleValueNode.unit

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.

No, that's the desired (output) units I'm specifying there; the input units are inferred.

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.

My mistake, I misread it

Comment thread src/three-components/SmoothControls.ts Outdated
if (typeof destRadius === 'string') {
switch (destRadius) {
default:
case 'auto':

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.

The notion of "auto" is domain-specific to <model-viewer>. SmoothControls is domain-agnostic. Do we gain something from moving this domain-specific concept into SmoothControls?

@elalish elalish May 29, 2019 •

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.

The "auto" keyword means apply the ideal camera distance, which I've moved into SmoothControls because I think it is more generically useful than to just <model-viewer>. However, we could tweak the API shape to clean it up a bit; maybe skip the string and use null to represent auto?

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 put this back.

Comment thread src/three-components/SmoothControls.ts Outdated
*
*/
updateFramedHeight(framedHeight: number) {
updateFraming(framedHeight: number, modelDepth: number, aspect: 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.

Why should camera controls care about framing? Should there be a higher-order construct that understands framing so that the camera controls don't have to?

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.

Framing seems pretty central to camera controls to me. What was strange was that both controls.ts and SmoothControls.ts were manipulating the camera, and when I was working on them I was running into a lot of bugs because it was unclear when one was stomping on the other. Also they were passing a lot of values back and forth, which is an indicator to me that the concepts are not well isolated. If you want to go down the bike shed route, I might rename SmoothControls to CameraControls and controls to CameraAttributes to make the current separation more clear.

@cdata cdata May 30, 2019 •

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.

These are the questions that are running through my mind as I consider this:

  • If I used SmoothControls in a Three.js application that had nothing to do with <model-viewer>, would "framing" have meaning to me?
  • If I replaced Three.js with another renderer (thus requiring a new version of SmoothControls that worked in terms of that renderer's API), would I have to re-implement the notion of framing entirely?
  • Can the concept of framing be implemented without direct access to the private implementation details of SmoothControls?

Framing as a concept is currently part of the ModelScene. This makes sense after a fashion. After all, what purpose does the scene have if not to set the stage for the viewer?

OTOH it may make sense to extract it from ModelScene and implement it in a new construct that integrates a ModelScene and SmoothControls. But, adding it directly into SmoothControls is conflating two concepts without clear justification. Yes, it's obvious that there is overlap in terms of values that are used in both cases. But it's not obvious that there is conceptual overlap (or even API access requirements) to justify moving the implementation here.

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.

Put this back too, but keeping camera ownership in one place.

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!

We spoke f2f about this topic. SmoothControls is responsible for manipulating the position (and now FOV) of the camera, while another entity (currently ModelScene) is responsible for determining the "framing" of the model by adjusting the frustum and repositioning the camera as needed.

Position / FOV manipulation is the domain of SmoothControls, so ModelScene should never perform those changes directly on the camera instance.

FOV sits at an intersection between these responsibilities. It seems like there is a lot of meat left on the bone for improvement in how the camera reference is manipulated by these different constructs.

Comment thread src/features/controls.ts Outdated
cameraOrbit: string = DEFAULT_CAMERA_ORBIT;

@property(
{type: String, attribute: 'camera-FOV', hasChanged: () => true})

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.

FYI HTML5 DOM attribute names are case insensitive

Comment thread src/three-components/SmoothControls.ts Outdated
return x;
// Critically damped
const acceleration = NATURAL_FREQ * NATURAL_FREQ * (xDest - x) -
2 * NATURAL_FREQ * this[$velocity];

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.

Avoid abbreviations (NATURAL_FREQ and xDest in this case)

Comment thread src/three-components/SmoothControls.ts Outdated
if (Math.abs(this[$velocity]) < NIL_SPEED * xNormalization &&
acceleration * (xDest - x) <= 0) {
// This ensures the controls settle and stop calling this function instead
// of asymptotically approaching their destination.

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.

🙌

Comment thread src/three-components/SmoothControls.ts Outdated
private[$destSpherical]: Spherical = new Spherical();
private[$thetaDamper]: Damper = new Damper();
private[$phiDamper]: Damper = new Damper();
private[$radiusDamper]: Damper = new Damper();

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.

We are completely inconsistent with this, but I recently learned that TypeScript will infer a class field or variable's type based on initial assignment. Feel free to take advantage of this for brevity whenever you feel like it 💁‍♂

this[$options] = Object.assign({}, DEFAULT_OPTIONS);

this.setOrbit();
this.setOrbit(0, Math.PI / 2, 1);

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.

Previously, initial orbital position was inferred from the configured camera's position. One reason why this seems preferable is that it gives the user the ability to change camera control schemes without forcing the camera to reset its position to some fixed default.

OTOH this is of marginal benefit to <model-viewer>. But, the fixed defaults used here seem separable from SmoothControls implementation...

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 removed that because I thought it was confusing. Sure, it uses the camera's existing position, but then ignores its orientation and instead points at the target, which you were not allowed to configure yet. Plus the most common case is that the camera is at the origin, which leads to a divide by zero, which was only solved by some surprising side effects of the default minimum radius. I think this will be more clear for developers.

private[$upQuaternion]: Quaternion = new Quaternion();
private[$upQuaternionInverse]: Quaternion = new Quaternion();
private[$spherical]: Spherical = new Spherical();
private[$targetSpherical]: Spherical = new 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.

Thanks for fixing our terminology usage here 🍻

@elalish
elalish requested a review from cdata May 30, 2019 23:33
@cdata cdata modified the milestones: 04, v0.4.0 Jun 4, 2019

@cdata cdata 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.

A bit more cleanup requested 🙇

Comment thread src/features/controls.ts Outdated

@property(
{type: String, attribute: 'camera-fov', hasChanged: () => true})
fieldOfView: string = DEFAULT_CAMERA_FOV;

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.

🍻

Comment thread src/features/controls.ts Outdated
cameraOrbit: string = DEFAULT_CAMERA_ORBIT;

@property(
{type: String, attribute: 'camera-fov', hasChanged: () => true})

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.

Make sure to update the attribute name here to field-of-view

Comment thread src/features/controls.ts Outdated

protected[$controls]: SmoothControls;

protected[$idealCameraDistance] = -1.0;

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 -1.0 a stand-in for "no value" here? If so, I would recommend using null instead, as it is more conventionally consistent with the rest of the library.

Comment thread src/features/controls.ts Outdated
return {theta, phi, radius};
}

getCameraFov(): 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.

Let's change this to getFieldOfView() to be inline with the property/attribute

Comment thread src/features/controls.ts Outdated
}
}

[$updateCameraFov]() {

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.

Ditto, update naming here

});
});

suite('deserializeAngleToDeg', () => {

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.

💯

export const settleControls = (controls: SmoothControls) =>
controls.update(performance.now(), FIFTY_FRAME_DELTA);

suite('Damper', () => {

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.

💯


update(
x: number, xGoal: number, timeStepMilliseconds: number,
xNormalization: number): 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.

thx clang-format 🙄 very readable

this[$velocity] = 0;
return xGoal;
} else
return x + this[$velocity] * timeStepMilliseconds;

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 should be enforced by linting in some way, but conventionally we always express if/else clauses as blocks. Please wrap this clause and the others above in braces { }

Filed #608 to make improvements here

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.

Not sure why this doesn't show as outdated, but I did fix this, along with any other unbracketed if statements I could find. I'm used to the Google C++ style guide where the brackets are omitted.

this[$zoomMeters] = framedHeight / 10;
updateIntrinsics(
nearPlane: number, farPlane: number, aspect: number,
zoomSensitivity: 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.

👍

@elalish
elalish requested a review from cdata June 6, 2019 17:08

@cdata cdata 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.

:shipit: Sweet! So excited for this...

@elalish
elalish merged commit f5b3b55 into master Jun 7, 2019
mkeblx pushed a commit to mkeblx/model-viewer that referenced this pull request Jun 10, 2019
* pulled camera out of controls.ts

* interaction is smooth, 15 tests failing

* fixed some tests, 4 to go

* test assumption problem

* two tests failing

* all tests passing

* added tests

* fixed google#537

* moved framing back to controls.ts

* fixed resize bug

* added conversion tests

* rename dest -> goal

* addressing feedback

* added damper tests

* addressing feedback
@elalish
elalish deleted the FOV branch September 27, 2019 16:32
elalish added a commit to elalish/model-viewer that referenced this pull request Feb 4, 2020
 
Adjustable FOV (google#563)

* pulled camera out of controls.ts

* interaction is smooth, 15 tests failing

* fixed some tests, 4 to go

* test assumption problem

* two tests failing

* all tests passing

* added tests

* fixed google#537

* moved framing back to controls.ts

* fixed resize bug

* added conversion tests

* rename dest -> goal

* addressing feedback

* added damper tests

* addressing feedback
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.

Adjustable camera FOV

2 participants