Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add direction parameters to fitTo method #88

Merged
merged 4 commits into from
Apr 2, 2020

Conversation

juliendargelos
Copy link
Contributor

This feature allows to specify which side of the bounding box to fit when using fitTo.
The corresponding example has also been updated so the grid always shows the right unit size regarding the camera position.

Usage

controls.fitTo(mesh, true, {
  side: 'back'
})

These are the available side values:

export enum SIDE {
  UP    = 'up',
  DOWN  = 'down',
  RIGHT = 'right',
  LEFT  = 'left',
  FRONT = 'front',
  BACK  = 'back'
}

The default value is 'front' so the feature isn't a breaking change. I haven't written the doc yet, let me know if you would merge this and I'll do it.

@yomotsu
Copy link
Owner

yomotsu commented Mar 22, 2020

Thanks for your contribution.
How about just using the camera direction rather than the side option?

Here is my idea

  1. get the closest corner of the bounding box
  2. make a plane with the closest corner and camera direction
  3. project all corners to the plane
  4. make a rectangle that contains all corners on the plane
  5. get the distance to contain the rect
  6. move the camera to the distance

@juliendargelos
Copy link
Contributor Author

The current direction ?

@yomotsu
Copy link
Owner

yomotsu commented Mar 22, 2020

yesyes

@juliendargelos
Copy link
Contributor Author

but the point is to choose a side to look at, whatever the current camera state is

@juliendargelos
Copy link
Contributor Author

Thanks for your contribution.
How about just using the camera direction rather than the side option?

Here is my idea

  1. get the closest corner of the bounding box
  2. make a plane with the closest corner and camera direction
  3. project all corners to the plane
  4. make a rectangle that contains all corners on the plane
  5. get the distance to contain the rect
  6. move the camera to the distance

Yes this would be more flexible since it allows any point of view. But it still requires extra parameters for direction.

@yomotsu
Copy link
Owner

yomotsu commented Mar 22, 2020

I guess choosing a side can be done in userland if the library provides free direction fit

@juliendargelos
Copy link
Contributor Author

Yes, I'll work on that

@yomotsu
Copy link
Owner

yomotsu commented Mar 22, 2020

oh btw, don't add generated files (such as .js) for pull requests please. this causes too many changes

@juliendargelos
Copy link
Contributor Author

juliendargelos commented Mar 23, 2020

The last changes introduce two new polarAngle and azimuthAngle parameters.

controls.fitTo(mesh, true, {
  polarAngle: Math.PI / 2,
  azimuthAngle: Math.PI
})

If you specify a side, polarAngle and azimuthAngle will have the side angles as default value.

controls.fitTo(mesh, true, {
  side: 'up'
})

This behaviour depends on parameters destructuring and default values:

fitTo( box3OrObject: _THREE.Box3 | _THREE.Object3D, enableTransition: boolean, {
  side = SIDE.FRONT,
  azimuthAngle = SIDES[ side ].azimuthAngle,
  polarAngle = SIDES[ side ].polarAngle,
  paddingLeft = 0,
  paddingRight = 0,
  paddingBottom = 0,
  paddingTop = 0
}: Partial<FitToOptions> = {} ): void

And the available side angles are defined in a constant:

export const SIDES: { [side in SIDE]: Direction } = {
  [ SIDE.FRONT ]: { polarAngle:   PI_HALF, azimuthAngle:         0 },
  [ SIDE.BACK  ]: { polarAngle:   PI_HALF, azimuthAngle:   Math.PI },
  [ SIDE.UP    ]: { polarAngle: - PI_HALF, azimuthAngle:         0 },
  [ SIDE.DOWN  ]: { polarAngle:   Math.PI, azimuthAngle:         0 },
  [ SIDE.RIGHT ]: { polarAngle:   PI_HALF, azimuthAngle:   PI_HALF },
  [ SIDE.LEFT  ]: { polarAngle:   PI_HALF, azimuthAngle: - PI_HALF }
};

As you proposed, each corner of the bounding box is projected on a plane, then the camera distance is computed from the bounding rect containing all projected points.

@yomotsu
Copy link
Owner

yomotsu commented Mar 24, 2020

Thanks i will take a look

@juliendargelos juliendargelos changed the title Add side parameter to fitTo method Add directions parameters to fitTo method Mar 24, 2020
@juliendargelos juliendargelos changed the title Add directions parameters to fitTo method Add direction parameters to fitTo method Mar 24, 2020
@dguay
Copy link

dguay commented Apr 2, 2020

Thank you for this PR I'd need this to fit the object from a top-down view. Any chance this gets merged/released soon?

@yomotsu
Copy link
Owner

yomotsu commented Apr 2, 2020

sorry for the delay.
I will merge this for now but let me change a little bit.

Thanks for the PR!

@yomotsu yomotsu merged commit 386ce80 into yomotsu:dev Apr 2, 2020
@dguay
Copy link

dguay commented Apr 2, 2020

@yomotsu Any idea when the next release will be so I can update to be able to use this feature? Thank you !

@yomotsu
Copy link
Owner

yomotsu commented Apr 3, 2020

I'll work on that this weekend. maybe early next week!

@dguay
Copy link

dguay commented Apr 16, 2020

@yomotsu You have done some releases since but this is still not release. Any chance we can get a release for this soon?

@yomotsu
Copy link
Owner

yomotsu commented Apr 16, 2020

This feature is already available.
see https://yomotsu.github.io/camera-controls/examples/fit-and-padding.html

@dguay
Copy link

dguay commented Apr 16, 2020

I updated to 1.21.0 and I can't use the new side option. Also I don't see the new code in this repo (master or dev branches)

@dguay
Copy link

dguay commented May 7, 2020

@yomotsu This is working well when you want a perpendicular view on the object but this is not working if I want to do an isometric view for example. rotateTo then fitTo --> works for perpendicular views. If you want isometric style view you need to do fitTo then rotateTo but this doesn't work well for perpendicular views. Am I missing something?

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