I want to set my camera in a way that it will always fit the 1x1 aspect in any fov or camera type but defaultly threejs always fits height.
I’m sure you have already searched the forum and found a bunch of ideas like this one for orthographic camera and that one for perspective camera.
Good luck with your project.
Yeah XD I found these but I wanted something that doesn’t change camera.zoom cause it will make problems when I need to use custom zoom with orthographic camera
camera.position.z = (size/2)/(Math.tan(camera.fov*(Math.PI/180)/2))+size/2;
camera.resize = () => {
renderer.setSize(window.innerWidth,window.innerHeight);
camera.aspect = window.innerWidth/window.innerHeight;
camera.zoom = camera.aspect<=1?camera.aspect:1;
camera.updateProjectionMatrix();
}
camera.resize();
window.addEventListener("resize", camera.resize);
I need to find out is it possible to change the zoom directly and also change between perspective or orthographic fast
Maybe .setViewport
is what you need? You can limit the rendering area to a square (i.e. 1:1 aspect) of your choice.
I was killing myself trying to find correct fix and now see that there is inbuilt function to do it Thanks a lot!
You may also just make the canvas square and centered it in the web page. There is no need to cover the whole window with the canvas if you will only draw in its center.
If someone else was looking for the code here it is
//--s is distance and f is camera fov--//
const findDist = (s,f) => {
return s/2/(Math.tan(f*(Math.PI/180)/2))+s/2
}
//--resize the camera and change aspect ration to fit the screen--//
camera.resize = () => {
renderer.setSize(window.innerWidth,window.innerHeight);
camera.aspect = window.innerWidth/window.innerHeight;
camera.zoom = camera.aspect<=1?camera.aspect:1;
camera.updateProjectionMatrix();
}
window.addEventListener("resize",camera.resize);
camera.resize();
camera.position.z = findDist(32,camera.fov);
Also if you use this in your code and need to customize zoom you must use a seperate variable for the zoom and apply it in the resize function