I’ve been scratching my head on this problem all day.
I want to be able to calculate how far away from a mesh I need to stand to see the whole of the mesh.
The mesh is box with an image on one face. The image can be in either landscape or portrait aspect.
The problem is that the user can also have their browser window in any aspect ratio.
I worked out the scale based on the ratio of the original image compared to the ratio of the browser window, but I cant workout how to translate that into a distance from the mesh.
Sorry, I couldn’t work out how to relate the example to my issue.
Also camera.zoomOn([mesh]) throws an unknown function error outside of the PG.
This doesn’t answer your question. I just want to make sure that you know about camera framing behavior if that’s what you’re trying to do.
Something like?:
const bb = object.getHierarchyBoundingVectors()
const size = bb.max.subtract(bb.min)
camera.radius = Math.max(size.x, size,y, size.z)
I’ve copied the relevant script into the playground.
I need to be able to calculate the distance so that the mesh just fits within the frustum.
https://playground.babylonjs.com/#CVIF83#26
I’m old and maths was never my strong point
Framing Behaviour only seems to be relevant to Arc Cameras. I’m currently moving around the scene using a universal camera. Should I convert the universal to an arc to frame the mesh, and then convert back to a universal ? … I feel like there is a mathematical solution. I can query the mesh dimensions, and the scene window dimensions, it’s just then the mathematics to determine the distance from the mesh, to width either the maximum height or width depending on which is the largest.
@Adrianlines What problem you are trying to solve with this, maybe there’s an easier way? This is how I would approach the problem:
If your mesh is 200 units tall, and your camera FOV is default (.8 radians or ~46 degrees), your distance to fill the height would be 100 / tan(23) = 235.6
Camera FOV is bound to the height of the viewport by default (fovMode = FOVMODE_VERTICAL_FIXED) with the horizontal FOV stretching to fill the screen. There is probably a way to figure out the angle for the width but I’m not sure how you would do that, if you can figure that part out you could calculate the distance in the same way and pick the smaller one.