suwnei
suwnei

Reputation: 3

How to get coordinate of an object

I tried to get the coordinates of an object in the 2D model loaded by the viewer, but the position of all the objects is Vector(0,0,0). I want to get the coordinates of an object based on the origin.

I get the position of the object by viewer.impl.getFragmentProxy()。

onSelectionEvent(event) {
    const result = {
      fragId: [],
      matrix: [],
    };
    const viewer: any = this;
    viewer.model.getData().instanceTree.enumNodeFragments(event.nodeArray[0], (frag) => {
      const fragProxy = viewer.impl.getFragmentProxy(viewer.model, frag);
      const matrix = new THREE.Matrix4();
      fragProxy.getWorldMatrix(matrix);

      result.fragId.push(frag);
      result.matrix.push(matrix);

      console.log(viewer.impl.getRenderProxy(viewer.model, frag));
    });

    console.log(event, result, result.matrix[0].getPosition());
  }

Upvotes: 0

Views: 556

Answers (1)

Petr Broz
Petr Broz

Reputation: 9942

This is most likely because the individual 2D shapes have the transform baked into their vertices. Instead of extracting the position from the transform. Try the following:

const tree = viewer.model.getInstanceTree();
const frags = viewer.model.getFragmentList();
tree.enumNodeFragments(dbid, function(fragid) {
    let bounds = new THREE.Box3();
    frags.getWorldBounds(fragid, bounds);
    console.log(bounds);
}, true);

Upvotes: 2

Related Questions