Hovo216
Hovo216

Reputation: 39

How can i get correct coordinates on model in forge viewer

I want to realize pushpin extension for autodesk forge with point cloud, but with custom coordinates. I want to get custom coordinates on model click event. I cant normalize the points so that they appear where I clicked.

Pushpins appeared like this

I try to normalize points with this code, but its not working.

viewer.canvas.addEventListener( 'click', (event) => {
      var screenPoint = {
          x: event.clientX,
          y: event.clientY
      };
      var n = normalize(screenPoint);
      var dbId = /*_viewer.utilities.getHitPoint*/ getHitDbId(n.x, n.y);
      if (dbId == null) return;
  })

  function getHitDbId(x, y) {
      x = x * 2.0 - 1.0;
      y = y * 2.0 - 1.0;

      var vpVec = new THREE.Vector3(x, y, 0.5);

      var result = viewer.impl.hitTestViewport(vpVec, false);
      result.distance = 1;

      if(result){
          dummyData.push({
              icon: Math.round(Math.random()*3),
              x: result.point.x,
              y: result.point.y,
              z: result.point.z,
          });
          window.dispatchEvent(new CustomEvent('newData', {
            'detail': dummyData
        }))
      } else {
          return
      }
  };

  function normalize(screenPoint) {
      var viewport = viewer.navigation.getScreenViewport();
      var n = {
          x: (screenPoint.x - viewport.left) / viewport.width,
          y: (screenPoint.y - viewport.top) / viewport.height
      };
      console.log(n);

      return n;
  }

Edited answer. Now i have another problem after when i normalize offset. Some pushpins are appearing incorrect. You can see problem in picture.

incorrect pushpin

How can i fix it?

Upvotes: 0

Views: 1174

Answers (1)

Petr Broz
Petr Broz

Reputation: 9942

You can find the sample code for finding the corresponding world coordinates in the Forge Digital Twin code: https://github.com/petrbroz/forge-digital-twin/blob/master/public/scripts/extensions/issues.js.

Live demo: http://forge-digital-twin.autodesk.io/ (try the flag icon in the toolbar).

Upvotes: 1

Related Questions