0

I do make 3d object dynmaic and i want perspective camera zoom to fit object but it doesn't work and i don't know how to i want help please!!!

dynamic object add is this

enter code here
    function plusobject(witch,clicknum) {

   //var geometry = new THREE.BoxGeometry( 5,5,5 );
   var geometry = new THREE.RingBufferGeometry( 2.4, 6,30,1,0,6.3 );
   //var geometry = new THREE.RingGeometry( 2.4, 6,30,1,0,6.3 );
   var material = new THREE.MeshBasicMaterial( { color: 0x610B38 } );
   material.side =  THREE.DoubleSide;
   var mesh3  = new THREE.Mesh( geometry, material );
   mesh3.name =  clicknum;
   mesh3.userData.id =  clicknum;

    witch.x += 3;
    witch.y += 5;
    witch.z += 3;
    mesh3.position.set(witch.x , witch.y, witch.z);

    mesh3.castShadow = true;
    mesh3.receiveShadow = true;



    scene.add( mesh3 );
} // plusobject END 

and i do raycasting mouse down event and i want zoom to fit object code is

enter code here

 function onDocumentMouseDown(event) {
      event.preventDefault();
      mouse = new THREE.Vector2();     

      var rect = renderer.domElement.getBoundingClientRect();
      mouse.x = ( ( event.clientX - rect.left ) / rect.width ) * 2 - 1;
      mouse.y = - ( ( event.clientY - rect.top ) / rect.height ) * 2 + 1;

       var vector = new THREE.Vector3(mouse.x, mouse.y, 0.5);
        vector = vector.unproject(camera);
        var raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());
        var intersects = raycaster.intersectObjects(scene.children, true);

        if (intersects.length > 0) {
  var helper = new THREE.BoxHelper(object);
 helper.update();
 var boundingSphere = helper.setFromObject(object);
 var center = boundingSphere.geometry.boundingSphere.center;
 var radius = boundingSphere.geometry.boundingSphere.radius;
 var distance = center.distanceTo(camera.position) - radius;
 var box = new THREE.Box3().setFromObject(dd);

 var realHeight = Math.abs(box.max.y - box.min.y); 
}

i want help please

yoyowow
  • 1
  • 2
  • Try https://stackoverflow.com/questions/14614252/how-to-fit-camera-to-object – WestLangley Sep 07 '18 at 03:03
  • dist = distance from the camera to the _closest face_ of the cube i can't understand this i'm new three.js @WestLangley – yoyowow Sep 07 '18 at 03:19
  • height cube mean ==> var box = new THREE.Box3().setFromObject(object); height = box.getSize().y @WestLangley – yoyowow Sep 07 '18 at 03:25
  • Yes, and `dist` is the distance from the camera to the cube face facing the camera. – WestLangley Sep 07 '18 at 03:27
  • Also try https://stackoverflow.com/questions/22500214/calculate-camera-fov-distance-for-sphere – WestLangley Sep 07 '18 at 03:34
  • first thank you The related code works for me, but there is a shortage. The behavior I want is similar to this one: https://threejs.org/editor/ Do you know how to do it? @WestLangley – yoyowow Sep 07 '18 at 07:37
  • Try https://github.com/mrdoob/three.js/blob/dev/examples/js/controls/EditorControls.js#L40-L68 – WestLangley Sep 07 '18 at 09:50
  • Thank you very much, Sir. I'm making sketchfab, mettaport, https://showroom.littleworkshop.fr/ How do you move the mouse and annotate a good way to annotate a div? I tried https://manu.ninja/webgl-three-js-annotations this method but I can not apply it. @WestLangley – yoyowow Sep 10 '18 at 07:46
  • You need to post a new question and show your code. https://discourse.threejs.org may be better for you – WestLangley Sep 10 '18 at 14:17
  • okay thank you advice – yoyowow Sep 11 '18 at 00:51

0 Answers0