Search code examples
autodesk-forgeautodesk-viewerautodesk-model-derivativeautodesk-data-management

Forge Viewer - getWorldCoordinates is giving different values on different occasions


I created below function to get worldCoordinates back, but it gives different values on two occasions.

While clicking a dbId, I get dbid cordinates and I pass it to below function which give me world coordinates, but you can see while I save that dbId selection to DB and reloading page next time to see it back, it gives me different coordinates. Why it happen so?

saving dbid phase

dbid coordinates x: -26.277027130126953 y: 18.102033615112305 z: -7.173819303512573

getWorldCoordinates x: 256.76347287180107 y: 306.8180434914181 z: 0

relaoding page phase

dbid coordinates x: -26.277027130126953 y: 18.102033615112305 z: -7.173819303512573

getWorldCoordinates x: 422.50000131979897 y: 249.49997927733767 z: 0

    function getWorldCoordinates(position){
        var screenpoint = viewer.worldToClient(
                    new THREE.Vector3(position.x,
                                      position.y,
                                      position.z,));
        return screenpoint
    }


function getObjPosition(dbId) {

function getObjPosition(dbId) {
    const model = viewer.model;
    const instanceTree = model.getData().instanceTree;
    const fragList = model.getFragmentList();

    let bounds = new THREE.Box3();

    instanceTree.enumNodeFragments( dbId, ( fragId ) => {
        let box = new THREE.Box3();
        fragList.getWorldBounds( fragId, box );
        bounds.union( box );
    }, true );

    const position = bounds.center();
    return position;
}


Solution

  • Unfortunately I was unable to reproduce the issue...

    Try live demo here - refresh the page and see the world coords prompted when the model completes loading ...

    Be sure to convert the coords after the model finishes loading (e.g. after the TEXTURES_LOADED_EVENT) otherwise you may get erratic results:

    NOP_VIEWER.addEventListener(Autodesk.Viewing.TEXTURES_LOADED_EVENT,()=>{
     alert(JSON.stringify(NOP_VIEWER.worldToClient(
                        new THREE.Vector3(-26.277027130126953,
                                          18.102033615112305,
                                          -7.173819303512573,))))
    })