Although this question has already received an answer, I wanted to contribute a little something for the sake of posterity and to ease the frustration levels of future readers.
In my experience with Three.js, I have primarily used the Perspective Camera over the orthographic camera. I recently came across a solution to the discussed problem related to setting up the perspective camera correctly:
- fov (vertical field of view angle)
- aspect ratio (width / height value) <-- Crucial!
- near clipping plane (object proximity threshold for visibility)
- far clipping plane (object distance threshold for invisibility)
The aspect ratio of the camera is essential for rendering properly in the viewport, especially when resizing the browser window. An inconsistency between viewport size and camera/renderer settings can result in distorted or incomplete images. To address this, you need to update the camera and renderer when the window size changes.
As suggested by Juan Mellado, you can adjust the viewport size using renderer.setSize(w,h)
. Here's how you can ensure that the camera and renderer adapt to window resize events:
function onResize()
{
var width, height;
width = window.innerWidth; // for example
height = window.innerHeight; // for example
//Update the camera's aspect ratio: width / height
camera.aspect = width/height;
//Don't forget to call updateProjectionMatrix() after making changes to the camera
camera.updateProjectionMatrix();
//Reset the renderer size to match the new dimensions
renderer.setSize(width, height);
}
To bind this callback function to the window.resize event, you can use either jQuery or plain JavaScript:
jQuery(window).resize(onResize);
or
window.onresize = onResize;
Summary:
1.) Make sure the values used to calculate Camera Aspect Ratio align with the renderer size values passed.
2.) Bind a callback function to the window.onresize event to ensure the camera aspect ratio and renderer size scale with the browser's dimensions.
3.) After modifying the camera post-creation, remember to invoke "updateProjectionMatrix()" to apply your changes.
If everything is set up correctly, the camera aspect ratio and renderer size will adjust automatically when resizing the window.
Happy coding!