'Autodesk Forge Viewer transform 2D to 3D coordinates

I am testing the possibilities of the forge V7 viewer on a web browser with a revit model.

The idea of ​​the test is: display 2 viewers simultaneously with 1 3D view and a 2D view click on a point of the 2D view and display a sphere in the 2D view on this point display another sphere in the 3D view at the equivalent point the spheres are added with SceneBuilder (to allow them to be selected later)

I tried to follow the behavior of https://forge.autodesk.com/ja/node/1765 and https://github.com/Autodesk-Forge/viewer-navigation.sample

and i have questions

1 - the 2D view must be a sheet, using a 2D view directly (a floor view for example) does not allow the calculation, is that correct?

2 - https://forge.autodesk.com/ja/node/1765 seems to have calculation and/or precision issues (cf forge1.png generated from the test in Heroku and forge2.png generated by my program)

3 - the method only works if you click on an object how can I retrieve the coordinates if I click in an empty area?

4 - the spheres are identified in the viewer (cf forge3.png), but a - how to give them a name (and not "object") b - how to replace the name "model" by the name of the scene?

Can you help me ?

Thanks in advance

Luc

forge1.png forge2.png forge3.png

Here is my code

// click listener on 2d viewer 
function listenerScene(ev){
var intersection = viewer2d.hitTest(ev.offsetX, ev.offsetY);
if (intersection) {
    AddItem(intersection.intersectPoint.x, intersection.intersectPoint.y,
            intersection.intersectPoint.z);
    AddItem3d(intersection);
}

// add in 2d
async function AddItem(x,y,z){
    ... add a sphere at (x,y,z)
}

// add in 3d
async function AddItem3d(intersection) {
    const worldPos = sheetToWorld(intersection.intersectPoint, 
                                   viewer2d.model,viewer.model);
    if (worldPos) {
    ... add a sphere at worldPos
    }
}

// compute 3d point
function sheetToWorld(sheetPos, model2d, model3d) {
    const viewportExt = viewer2d.getExtension('Autodesk.AEC.ViewportsExtension');
    const viewport = viewportExt.findViewportAtPoint(model2d, new THREE.Vector2(sheetPos.x, sheetPos.y));
    if (!viewport) {
        return null;
    }
    const sheetUnitScale = model2d.getUnitScale();
    const globalOffset = model3d.getData().globalOffset;
    const matrix = viewport.get2DTo3DMatrix(sheetUnitScale);
    var worldPos = sheetPos.clone().applyMatrix4(matrix);
    worldPos = worldPos.sub(globalOffset);
    return worldPos;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source