Set 3D Object to View
There are several ways to place a 3D object to view the camera.
- Move the camera back or forward.
- Increase or decrease the focal length (this also affects the FOV field (of View field))
- Change the scale of world space or the scale of the local space of an object to make the object larger or smaller.
You can undo the last option, since in most cases this is impractical. (although I notice that you are doing this in the specified code, this answer has enough information to determine how to scale the object to fit. But I do not recommend you to do this)
Thus, you just have to move the camera or keep it motionless and scale. This is the same as with a real camera, you are approaching or approaching.
Both methods have their pros and cons.
Transfer
Moving the camera (trolley) is best suited for most situations, since it keeps the perspective the same (how quickly the lines converge to the vanishing point), and thus does not distort the object in the field of view. In 3D, there are 3 problems with this method.
- The review node (the volume in which the scene is displayed) has (the maximum distance at which the object will be displayed) and the font (the closest object can be displayed). Moving the camera to a very large or very small object can cause the object to be outside the front or back planes and be cropped partially or completely.
- Moving planes in accordance with an object may also have undesirable results. Moving both the front and back planes to hold the object can bring objects closer or further in the scene to be cropped.
- An extension of the total distance between the rear and front plane can also cause Z-buffer smoothing artifacts. But these problems apply only to very large or very small objects and scenes.
Click to enlarge
Zoom means changing the focal length of the camera. In the library that you use, this is done by adjusting the FOV (Field of View), this is the angle between the left and right sides of the view, indicated in degrees. Reducing FOV effectively increases focal length and zoom (3D graphics do not have a focal length, such as a camera). Increases FOV increase. There are problems with this method.
- As the FOV decreases, the perspective (parallax) decreases, making the scenes appear less and less 3D. As FOV increases perspective, it increases distorting objects and makes objects at a distance very small.
- Since the camera does not move, the front and rear planes remain in place, but an increase or decrease in objects near the rear or front plane can still cause an aliasing z-buffer artifact.
Which method to use ip for you, you can use one or the other or combine both.
How it's done
To a question. We need to know how large the object will be displayed on the stage and use this information to change the camera's setting to the desired effect (namely, set the object on the display).
Figure 1. Camera, object, and view.
So there are some meanings that are needed. See Fig. 1 for a visual explanation.
- FOV. I will convert it to Radians.
- Object distance from camera
- The radius of the limited boundary of the sphere.
- Frontal plane
- Back plane
- Screen pixel size
You will need to calculate the bounding sphere of the object. Or use a different value that approximates the bounding sphere. I will leave it to you.
Code
var oL,cL;
To determine how large a bounding ball appears in a view, it is simply a trigger.
When we use the right triangle (see Fig. 1), we multiply the result by 2 to get the total angular size
// trig inverse tan of opposite over adjacent. var objectAngularSize = Math.atan( (objectRadius) / distToObject ) * 2;
Get the FOV share that the object occupies.
var objectView = objectAngularSize / FOV;
And finally, you get the pixel size of the object.
var objectPixelSize = objectView * displayWidth;
That’s all you need to know to do what you ask. This will help you understand if you are using the above code and math to try to reorder the calculations so that you can occupy the required pixel size by moving the camera or FOV. Copying the code does not reproach you very much, using the above information, applying it, it will be configured at your discretion and will do more other processes that you will need for 3D in the future.
This means that copying code is a quick fix and is what all frameworks and libraries are. You don’t need to know how you should study.
Zoom in to size.
It is the easiest and gets the size of the angular object and adjusts the FOV to fit. (NOTE: I use radians. Three.js uses degrees for FOV that you need to convert)
var requieredObjectPixelSize = 900; var distToObject = Math.sqrt(Math.pow(oL.x - cL.x, 2) + Math.pow(oL.y - cL.y, 2) + Math.pow(oL.z - cL.z, 2)); var objectAngularSize = Math.atan( (objectRadius) / distToObject ) * 2;
Convert FOV to degrees and use it to create a camera.
Translate to
Move the camera to fit the subject. This is a bit more, but this is the best method.
Now move the camera. It should move along the vector between the object and the camera.
Normalize the vector. This means that the length of the vector is 1 and is performed by dividing each component (x, y, z) by the length of the vector.
// First length var len = Math.sqrt(Math.pow(toCam.x, 2) + Math.pow(toCam.y, 2) + Math.pow(toCam.z, 2)); // Then divide to normalise (you may want to test for divide by zero) toCam.x /= len; toCam.y /= len; toCam.z /= len;
Now you can scale the vector so that it is equal to the distance that the camera should be from the object.
toCam.x *= distToObject; toCam.y *= distToObject; toCam.z *= distToObject;
Then it’s just a matter of adding a vector to the location of the object and placing it at the location of the camera.
cL.x = oL.x + toCam.x; cL.y = oL.y + toCam.y; cL.z = oL.z + toCam.z;
cl now contains the location of the camera.
Last thing. You need to check if the object is inside the view.
if (distToObject - objectRadius < nearPlane) { nearPlane = (distToObject - objectRadius) * 0.8;
There is one more problem. The subject, if very small, can be so close that there is a camera in front of the subject. If this happens, you will need to use the zoom method and move the camera back. This will only be for very selective cases and can be ignored for the most part.
I did not provide information on how to integrate this with Three.js, but this is for a general answer that applies to all 3D packages. You will need to consult the three.js documentation on how to change various camera settings. It is simple and applicable to a perspective camera.
Well, a great answer, and I need to forget it a little, since I do not see typos and errors without a break. I will come back and fix it later that day.
Hope this helps