How to Orbit Around Selected Object's Pivot Point

Hello TechSoft3D Team,

Could you please guide me on how to implement orbit controls around the center point of a selected object?

Thank you!

Please see the function setOrbitTarget which is part of the CameraOrbitOperator class.

Also worth mentioning is that the orbit operator has a built-in feature of rotating about the point of selection on a face. To activate this, use the middle mouse button to intiate the orbit.

      const selectionItems = viewer.selectionManager.getResults();
      console.log(selectionItems);
      const nodeIds = selectionItems
        .map((item) => item.getNodeId?.())
        .filter((id) => id !== undefined);

      console.log(nodeIds);
      const bounding = await viewer.model.getNodesBounding(nodeIds);
      // Calculate center
      const center = new Point3(
        (bounding.min.x + bounding.max.x) / 2,
        (bounding.min.y + bounding.max.y) / 2,
        (bounding.min.z + bounding.max.z) / 2,
      );
      console.log(center);
      const operator = new Operators.Camera.CameraOrbitOperator(viewer, viewer.view);
      operator.setOrbitTarget(center);

I implemented the code mentioned above, but orbiting around the selected objects is still not working. Is this implementation correct? How should it be properly implemented?

In addition to calling setOrbitTarget, you will also need to call setOrbitFallbackMode, that is:

operator.setOrbitTarget(center);
operator.setOrbitFallbackMode(Communicator.OrbitFallbackMode.OrbitTarget);

Also the Box class, has the method Box.center() to get the center of the bounding box as another option to get the center point.

      const selectionItems = viewer.selectionManager.getResults();
      console.log(selectionItems);
      const nodeIds = selectionItems
        .map((item) => item.getNodeId?.())
        .filter((id) => id !== undefined);

      console.log(nodeIds);
      const bounding = await viewer.model.getNodesBounding(nodeIds);
      const center = bounding.center();
      console.log(center);
      const operator = new Operators.Camera.CameraOrbitOperator(viewer, viewer.view);
      operator.setOrbitTarget(center);
      operator.setOrbitFallbackMode(OrbitFallbackMode.OrbitTarget);

I updated the code as you suggested. Still its not working.

Let’s try this:

const selectionItems = viewer.selectionManager.getResults();
      console.log(selectionItems);
      const nodeIds = selectionItems
        .map((item) => item.getNodeId?.())
        .filter((id) => id !== undefined);

      console.log(nodeIds);
      const bounding = await viewer.model.getNodesBounding(nodeIds);
      const center = bounding.center();
      console.log(center);
      const om = viewer.operatorManager;
      const operator = om.getOperator(OperatorId.Orbit);
      operator.setOrbitTarget(center);
      operator.setOrbitFallbackMode(OrbitFallbackMode.OrbitTarget);

Thanks, it’s working perfectly!