Scaling & Distance

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)

If you can’t solve a problem, it’s because you don’t have enough information. You just need more debug data. It helps to isolate the essential problem and look at it separately.

An opportunity to design an example in the Playground for us to help out.

A little smear into a scientific approach:

  1. collect information
  2. make a prediction
  3. build an experiment to test the prediction.
  4. if the experiment fails, go back to point 1.

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 :slight_smile:

1 Like

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.