简体   繁体   中英

How do you implement drag controls in the AutoDesk forge viewer?

I am trying to implement drag controls on this text geometry that I am creating in the viewer. I create the text like so:

createText(params) {
const textGeometry = new TextGeometry(params.text,
      Object.assign({}, {
        font: new Font(FontJson),
        params
      }));
const geometry = new THREE.BufferGeometry;

geometry.fromGeometry(textGeometry);

const material = this.createColorMaterial(
      params.color);

const text = new THREE.Mesh(
      geometry, material);

text.scale.set(params.scale, params.scale, params.scale);

text.position.set(
      params.position.x,
      params.position.y,
      10);


this.intersectMeshes.push(text);
this.viewer.impl.scene.add(text);

this.viewer.impl.sceneUpdated(true);

return text;

}

This works great, the meshes get added to the viewer, I can see them. Fantastic! Thus far, it is good. Now, I want to be able to drag them around with my mouse after I have added them. I noticed that Three.js already has drag controls built in, so I just implemented them like so:

  enableDragging(){
let controls = new THREE.DragControls( this.viewer, this.viewer.impl.camera.perspectiveCamera, this.viewer.impl.canvas );
controls.addEventListener( 'dragstart', dragStartCallback );
let startColor;
controls.addEventListener( 'dragend', dragendCallback );
function dragStartCallback(event) {
  startColor = event.object.material.color.getHex();
  event.object.material.color.setHex(0x000000);
}

function dragendCallback(event) {
  event.object.material.color.setColor(startColor);
}

}

After a big of debugging, I have seen where the problem occurs. For some reason, when I click on one of the meshes, the raycaster doesn't find any intersections. IE the array I get back is empty. No matter where I click on these objects.

Is my implementation wrong, or did I provision these meshes wrong to make them draggable? I have gotten the drag controls to work outside of the viewer, just not within it.

This will not work, looking at the code of DragControls, the viewer implementation is too different in the way it implements the camera. You would need to either implement a custom version of DragControls or take a look at my transform tool and adapt it for custom meshes:

Moving visually your components in the viewer using the TransformTool

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