Search code examples
autodesk-viewer

Zoom into object like double click event


I tried to create a function zoom into object like double click event in viewer. i found that fitToView method make sure the selected in viewer but not zoom in. Follow the answer here: Autodesk forge viewer zooming to bounding box i need to find boundingbox of object i will get what i need. I base on this article to determine object boundingbox https://forge.autodesk.com/blog/working-2d-and-3d-scenes-and-geometry-forge-viewer. I have some question: why 1 object have many fragId and each fragId have bounding box for own, how i can determine bounding box of whole object. Thanks in advance.


Solution

  • Implementing zoom-to-object

    You can definitely use the fitToView method to achieve the same "zoom-to-object" functionality as when double-clicking an object in Forge Viewer. Just keep in mind that the first argument to the method must be an array. So, for example, if you want to zoom in on an object with dbID 1234, the method call would be viewer.fitToView([1234]);

    Why is there 1-to-many mapping between objects and fragments?

    A single selectable object in Forge Viewer (for example, a door) can often consist of multiple parts with different materials (for example, a wooden frame, and a metal door knob). These parts would then be represented as individual fragments, with each fragment specifying its own geometry, material, transform, and bounding box.

    Computing a union of multiple bounding boxes

    Bounding boxes in Forge Viewer are typically represented using the THREE.Box3 class which provides several helper methods incl. union. This method can be used to expand an existing bounding box so that it "includes" another bounding box.

    Here's how you could compute the bounding box of all fragments of a specific object:

    function computeObjectBounds(model, dbid) {
      const frags = model.getFragmentList();
      const tree = model.getInstanceTree();
      let objectBounds = new THREE.Box3();
      let fragBounds = new THREE.Box3();
      tree.enumNodeFragments(dbid, function (fragid) {
        frags.getWorldBounds(fragid, fragBounds);
        objectBounds.union(fragBounds);
      });
      return objectBounds;
    }