简体   繁体   中英

How to find the position(x,y,z coordinates) in the 3D model using autodesk forge viewer

I tried to find the position(x,y,z coordinates) of a surface in the 3D model when it is clicked using this repo . I tried clicking all over the model, but I am getting the same position (x,y,z value) for every click. Click here to see the position when i click on a surface and when I try clicking ( somewhere else ) in the model, I am getting the same position(x,y,z value) for that too. I have tried exactly the same thing given in the repo link for this model (urn:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6YXBwdGVzdGJ1Y2tldG5pc2hhbnQyL2dhdGVob3VzZSUyMDEubndk). How to find the position(x,y,z coordinates) in the model? Thanks in advance.

This is often an issue with computing the cursor coordinates relative to the canvas hosting the Forge Viewer, so make sure that these are correct in the first place. For example, clicking close to the top-left corner of the canvas should give you relative cursor coordinates close to (0,0). These relative coordinates are then passed to the Viewer APIs to do the ray casting and hit testing.

Here's another sample code that's doing the same thing (finding the world coordinates of the closest hit after clicking on the canvas):

   $('#viewer').on('click', function(ev) {
        let intersections = [];
        const bounds = document.getElementById('viewer').getBoundingClientRect();
        mainViewer.impl.castRayViewport(mainViewer.impl.clientToViewport(ev.clientX - bounds.left, ev.clientY - bounds.top), false, null, null, intersections);
        if (intersections.length > 0) {
            const intersection = intersections[0];
            $('#issue-part').val(intersection.dbId);
            $('#issue-position-x').val(intersection.point.x.toFixed(2));
            $('#issue-position-y').val(intersection.point.y.toFixed(2));
            $('#issue-position-z').val(intersection.point.z.toFixed(2));
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM