I have a box (collada file) that loads within a three.js canvas. I can interact with it as expected. However, the box size varies as users can change the size.
When I load it into a 500px by 500px canvas, if the box is large, users have to zoom in before seeing it, and if it is small, it is tiny and users need to zoom in. The size changes depending on the variables that are passed.
How would I have the object (collada file) fit in the canvas on load, and then let users zoom? Here is the code that loads on click to show the 3D object in a three.js canvas:
$scope.generate3D = function () {
// 3D OBJECT - Variables
var texture0 = baseBlobURL + 'Texture_0.png';
var boxDAE = baseBlobURL + 'Box.dae';
var scene;
var camera;
var renderer;
var box;
var controls;
var newtexture;
// Update texture
newtexture = THREE.ImageUtils.loadTexture(texture0);
// Initial call to render scene, from this point, Orbit Controls render the scene per the event listener
THREE.DefaultLoadingManager.onProgress = function (item, loaded, total) {
// console.log( item, loaded, total ); // debug
if (loaded === total) render();
};
//Instantiate a Collada loader
var loader = new THREE.ColladaLoader();
loader.options.convertUpAxis = true;
loader.load(boxDAE, function (collada) {
box = collada.scene;
box.traverse(function (child) {
if (child instanceof THREE.SkinnedMesh) {
var animation = new THREE.Animation(child, child.geometry.animation);
animation.play();
}
});
box.scale.x = box.scale.y = box.scale.z = .2;
box.updateMatrix();
init();
});
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(100, window.innerWidth / window.innerHeight, 0.1, 1000);
renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0xdddddd);
//renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setSize(500, 500);
// Load the box file
scene.add(box);
// Lighting
var light = new THREE.AmbientLight();
scene.add(light);
// Camera
camera.position.x = 40;
camera.position.y = 40;
camera.position.z = 40;
camera.lookAt(scene.position);
// Rotation Controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', render);
controls.rotateSpeed = 5.0;
controls.zoomSpeed = 5;
controls.noZoom = false;
controls.noPan = false;
// Add 3D rendering to HTML5 DOM element
var myEl = angular.element(document.querySelector('#webGL-container'));
myEl.append(renderer.domElement);
}
// Render scene
function render() {
renderer.render(scene, camera);
console.log('loaded');
}
}
// Initial 3D Preview Load
$scope.generate3D();
Update: I have evaluated the solution presented here: How to Fit Camera to Object but am unsure how to define the distance for my collada file as it can be different depending on what dimensions the user enters. The collada file is generated by users sending variables to a third party vendor that returns a collada file that is subsequently loaded into three.js.
Update 2: Thanks to @Blindman67 I am closer to understanding how this interplays. When I manually up the camera.position x,y,z values, the object is in the screen. The challenge I have is how to determine what the correct x,y,z values will be as each box is dynamically changed and I literally have over 280 Million variations. I know that @Blindman67 already gave me the answer logically, but I just need a final push to discover how to get the right position for objects that vary each time so I can set the correct x,y,z.