| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 
 | const box = new THREE.Box3();const delta = new THREE.Vector3();
 const center = new THREE.Vector3();
 const sphere = new THREE.Sphere();
 
 function focus ( target ) {
 
 var distance;
 
 box.setFromObject( target );
 
 if ( box.isEmpty() === false ) {
 
 box.getCenter( center );
 distance = box.getBoundingSphere( sphere ).radius;
 
 } else {
 
 
 
 center.setFromMatrixPosition( target.matrixWorld );
 distance = 0.1;
 
 }
 
 delta.set( 0, 0, 1 );
 delta.applyQuaternion( camera.quaternion );
 delta.multiplyScalar( distance * 4 );
 
 const cameraStart = camera.position.clone();
 const cameraEnd = center.add( delta );
 
 const orbitStart = orbit.target.clone();
 const orbitEnd = target.position.clone();
 
 const start = {
 cameraX:cameraStart.x,
 cameraY:cameraStart.y,
 cameraZ:cameraStart.z,
 orbitX:orbitStart.x,
 orbitY:orbitStart.y,
 orbitZ:orbitStart.x,
 };
 
 const end = {
 cameraX:cameraEnd.x,
 cameraY:cameraEnd.y,
 cameraZ:cameraEnd.z,
 orbitX:orbitEnd.x,
 orbitY:orbitEnd.y,
 orbitZ:orbitEnd.x,
 };
 
 const position = target.position;
 orbit.target.set(position.x || 0.01,position.y|| 0.01,position.z|| 0.01);
 new TWEEN.Tween(start)
 .to(end, 1000)
 .onUpdate(function (val) {
 
 camera.position.set(val.cameraX || 0, val.cameraY || 0, val.cameraZ || 0);
 
 orbit.target.set(val.orbitX || 0,val.orbitY|| 0,val.orbitZ|| 0);
 orbit.update();
 })
 .start();
 }
 
 |