0

I have a three.js scene with a cuboid and an OrthographicCamera:

var scene = new THREE.Scene();

var aspect = canvas.width/canvas.height;
var size = 1000;

var camera = new THREE.OrthographicCamera(size * aspect / - 2, size * aspect / 2, size / 2, size / - 2, 1, 2000);

camera.position.x = -400;
camera.position.y = 200;
camera.position.z = 400;

camera.lookAt(new THREE.Vector3(0, 0, 0));

var box = new THREE.Mesh(
  new THREE.BoxGeometry(someWidth, someHeight, someDepth), 
  new THREE.MeshLambertMaterial({ color: 0xffffff })
);

scene.add(box);

The size and proportions of the cuboid vary. How do I derive the camera properties from the cuboid, so that it fits neatly in the center of the camera view (with a small margin on all sides)?

GLR
  • 1,070
  • 1
  • 11
  • 29
Rasmus B
  • 67
  • 12
  • If you understand what the orthographic camera constructor parameters represent, you can answer this question yourself. See [this answer](https://stackoverflow.com/questions/17558085/three-js-orthographic-camera/17567292#17567292). – WestLangley Sep 11 '17 at 16:57
  • Well, if I understood this stuff I wouldn't have to ask at all - but the answer you linked to is quite helpful. I added some code to the question, hopefully that will make it easier to answer. I guess the main problem I'm having is to calculate the bounding box of the cube when it is at an angle with respect to the camera... the rest I should be able to figure out with a bit of trial and error. – Rasmus B Sep 11 '17 at 18:11
  • Check out this one: https://stackoverflow.com/questions/45995313/three-js-orthographic-camera-zoom-all. An example of your problem can be found here: https://stackoverflow.com/questions/45996637/three-js-orthographic-camera-zoom-all-for-a-cube-with-perspective – GLR Sep 12 '17 at 08:04
  • @GLR: Looks promising... I will give it a go tonight. – Rasmus B Sep 12 '17 at 08:29
  • Possible duplicate of [three.js orthographic camera: zoom all for a cube with perspective](https://stackoverflow.com/questions/45996637/three-js-orthographic-camera-zoom-all-for-a-cube-with-perspective) – Rasmus B Sep 22 '17 at 18:21

0 Answers0