0

Updating camera and target based on dbid of a selected node. The code starts with MobileVR function. I am updating camera and target according to frag mesh retrieved with dbid and then moving to VR mode. Currently I have an event listeners for GEOMETRY_LOADED_EVENT, OBJECT_TREE_CREATED_EVENT and EXTENSION_LOADED_EVENT. Currently it works with using a timeout setTimeout(() => { onSpaceObjectTreeCreated(); }, 3000); see image 1, but not without the the timeout image 2. Is there some other event that I should wait before running the code or updating the camera?

function onSpaceObjectTreeCreated() {
  const nav = viewer.navigation;
  const cam = nav.getCamera();
  const it = viewer.model.getData().instanceTree;

  let xPos, yPos, zPos;    

  it.enumNodeFragments(nodeId, (frag) => {
    const mesh = viewer.impl.getRenderProxy(viewer.model, frag);
    xPos = mesh.matrixWorld.elements[12];
    yPos = mesh.matrixWorld.elements[13];
    zPos = mesh.matrixWorld.elements[14];
    console.log('x: ' + xPos + ' y: ' + yPos + ' z: ' + zPos);
  }, false);
  zPos = -41000;

  cam.position.set(xPos, yPos, zPos);
  cam.target.set(xPos, yPos + 10000, zPos);
}

function onViewerGeometryLoaded() {
  const nav = viewer.navigation;
  const cam = nav.getCamera();

  if (nodeId == -1) {
    viewer.setGroundShadow(false);
    let xValue = viewer.getCamera().position.x;
    let yValue = viewer.getCamera().position.y;
    let zValue = viewer.getCamera().position.z;
    let bbz = viewer.model.getData().bbox.min.z;
    let zValue2 = zValue - bbz;

    zValue = zValue * 0.3;
    yValue = (zValue2 * 0.7071) * -1;

    let nav = viewer.navigation;
    let cam = viewer.getCamera();
    cam.position.set(xValue, yValue, zValue);        
  } else {
    setTimeout(() => {
        onSpaceObjectTreeCreated();
    }, 3000);
  }

  viewer.impl.sceneUpdated();
  viewer.navigation.updateCamera();
  document.getElementById("toolbar-vrTool").click();
};

function afterViewerEvents() {
  var events = [
    Autodesk.Viewing.GEOMETRY_LOADED_EVENT,
    Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT,
    Autodesk.Viewing.EXTENSION_LOADED_EVENT
  ];

  async.each(events,
    function (event, callback) {            
        var handler = function (ev) {
            viewer.removeEventListener(
                event, handler);
            console.log('Event: ' + event);
            console.log('Ev: ' + ev.extensionId);
            callback();
        };
        viewer.addEventListener(
            event, handler);
    },
    function (err) {
        onViewerGeometryLoaded();
    });
}

function mobileVR(arkUrn: string, lviUrn: string, zOffset: number, spaceId: number) {

  let element = document.getElementById("mobileViewer");
  viewer = new Autodesk.Viewing.Private.GuiViewer3D(element);
  let options = {
    'env': 'AutodeskProduction',
    'getAccessToken': getToken,
    'refreshToken': getToken
  };
  av.Initializer(
    options,
    () => {
        viewer.initialize();

        loadDocument(arkUrn, zOffset);
        if (lviUrn != "") {
            loadDocument(lviUrn, zOffset);
        }            
        viewer.loadExtension('Autodesk.Viewing.WebVR');
     }
  );

  nodeId = spaceId;
  afterViewerEvents();
}

Image 1

Image 2

Joni Turunen
  • 137
  • 1
  • 13

1 Answers1

1

Try to hookup the events after initializing the viewer and before loading the document:

viewer.initialize();

afterViewerEvents();

loadDocument(arkUrn, zOffset);

Also I don't get why you are using Autodesk.Viewing.EXTENSION_LOADED_EVENT, several extensions are being loaded automatically by the viewer upon startup or model loading, this event will be fired multiple times. If you are looking for a specific extension being loaded you need to check the extensionId and remove the handler only if this is the extension you are waiting for...

Hope that helps

Felipe
  • 4,325
  • 1
  • 14
  • 19
  • Even though there are several extension loaded, the extension which triggers the callback seems to be the correct one, this is what I get in the console. Event: extensionLoaded Ev: Autodesk.Viewing.WebVR. I also changed the code so the events are hooked up after viewer.initialize() and before loadDocument, but this does not fix the problem. Is there anything else that could be the reason? Or something else I have wrong in my code? – Joni Turunen Dec 20 '17 at 13:21