fitToBox
resets my camera rotation. how can I keep the angle?
#573
Replies: 2 comments
-
Inside the fitToBox source code there is a comment stating I also assumed, the fitToBox method would actually only zoom the camera to make it fit the box, which is somewhat more complex than to just rotate to the right axis. I think I know a solution for this problem, if I am successful I will post it. |
Beta Was this translation helpful? Give feedback.
-
Solved it! :D @yomotsu Maybe this is worth a look. Unfortunately, I'm not familiar enough with your codebase (and TypeScript) to make a commit myself, but the logic behind my dollyToFitBox() function could serve well as an alternative to the existing fitToBox() function. It could be named fitToBoxWithoutRotating() or the fitToBox() function could be given a new parameter that optionally switches to the new logic. Like the OP, I also initially thought that fitToBox wouldn't rotate the camera, because in my view, there are more use cases that require the camera rotation to remain unchanged. What is the logic behind my code? The camera has an FOV in the x-direction and an FOV in the y-direction. Both must be taken into account so that the box leaves neither the horizontal nor the vertical bounds of the viewport. When the camera is positioned as in this sketch that I found online, one can derive the formula Because the camera does not always match this sketch (in the sketch, x = 0) but can also look at the box from an angle from above, we switch the coordinate system to one for which this sketch is always valid. The new x-coordinate points in the horizontal viewport direction, the new y-coordinate points in the vertical viewport direction, and the new z-coordinate points from the camera to the target. In the sketch, the horizontal axis is then no longer the x-axis but the z-axis, and the vertical axis is no longer the y-axis but either the x or y-axis. This results in We now know the required camera distance for each point to be within the viewport (once for the horizontal and once for the vertical viewport axis). Ultimately, we just have to set the camera to the maximum of these minimum distances, and then even the last point will be just barely inside the viewport. I hope this code helps someone :D
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I am trying to set my camera to show a top-left view of my scene (like rotated 30° to the left and 15° down) when a button is clicked. Here's the code I am using:
The problem is, the
fitToBox
call seems to override my rotation and kind of resets?Is there a way to fit the scene while keeping the rotation I want?
(and/or a better way to show the top-left view of my object while fitting it within the camera?)
I am still learning three.js and drei etc., so please bear with me.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions