1

I want to add a button to my viewer that will present the "reverse angle" of our model to the user. I'd like the result to be essentially what the user see when they free orbit the model dragging exactly horizontally across the screen, through the centre of the model.

The use case here is where we want to be able to lock the view, but give the user the ability to pan and to see the reverse angle - without enabling things like orbit or free orbit.

Here's what I've done so far:

        var currentPosition = viewer.navigation.getPosition();
        var newX = -currentPosition.x;
        var newY = -currentPosition.y;
        var newZ = -currentPosition.z;

        var newPosition = {
          x: newX,
          y: newY,
          z: newZ
        };

        viewer.navigation.setView(newPosition,
            viewer.navigation.getTarget());

But this gives us some undesired behaviour when the model is panned away from the pivot - this code takes a model sitting right of centre on the screen and lands it left of centre. I'd like to rotate the model around itself, as a horizontal drag in free orbit would.

1 Answers1

0

Instead of just changing the signs of the camera position (which effectively mirrors the position through world origin), try the following:

const oldPos = viewer.navigation.getPosition();
const oldTarget = viewer.navigation.getTarget();
const newPos = {
    x: oldPos.x + 2.0 * (oldTarget.x - oldPos.x),
    y: oldPos.y + 2.0 * (oldTarget.y - oldPos.y),
    z: oldPos.z + 2.0 * (oldTarget.z - oldPos.z)
};
viewer.navigation.setView(newPos, oldTarget);
Petr Broz
  • 8,891
  • 2
  • 15
  • 24
  • This produces similar behaviour to what I have - a model in the left of the viewer moves to the right, and rotates rather than just pivoting through its own centre (as our default orbit does). This is true on the models I'm using at least. – Dave Miller Oct 05 '18 at 09:50
  • In that case you'll probably need to mirror not only the position of the camera, but the target point as well. Find the center point of your model, and mirror both the camera position and the camera target through this point. – Petr Broz Oct 05 '18 at 10:08
  • how do I find the centrepoint of my model? This is related to a question I've just raised here: https://stackoverflow.com/questions/52761235/finding-the-centre-point-of-the-visible-parts-in-the-autodesk-viewer – Dave Miller Oct 11 '18 at 13:41
  • 1
    When a model is loaded, you can find its center using something like this: `let bbox = viewer.model.getBoundingBox(); let center = bbox.center();` – Petr Broz Oct 11 '18 at 13:44
  • In order to get the bounds of just the visible parts, you can use an internal method called `getVisibleBounds(includeGhosted, includeOverlays)` which returns a `THREE.Box3` object. The previous example would look like this: `let bbox = viewer.impl.getVisibleBounds(false, false); let center = bbox.center();`. – Petr Broz Oct 12 '18 at 07:07
  • Sorry for being unclear, this bounding box is for the whole model, rather than just the visible part(s). How would I find the bounding box for just the visible parts? Calling fit to view seems to centre on the centre point of the visible parts, and I'd like to use that point to translate through. – Dave Miller Oct 12 '18 at 07:07
  • 1
    No problem @DaveMiller. See my previous comment fo an updated version of the snippet. – Petr Broz Oct 12 '18 at 07:18
  • Much better. This is working well. I'm translating both the position and the target through the centre of the visible bounds now and that has helped. I still have a quirk where a model that's been panned into the top half of the viewer appears in the bottom half, but I continue to work on it. – Dave Miller Oct 12 '18 at 14:09
  • 1
    I'm glad we're making progress :) Thinking about it some more, you'll probably want to mirror only the X/Z components and leave the Y component (height) untouched. That way you'll be basically rotating the camera position and camera target around the vertical axis of your model by 180 degrees. – Petr Broz Oct 12 '18 at 14:40
  • This doesn't help. I think I cannot be certain on the orientation of the y-axis of the model position, relative to the screen. We allow the model to be orbited, so the y-axis may not be vertically up on-screen. It is true though that regardless of the orientation of the model, using the code you posted originally, but translating through the centre of the visible bounds does what I want except if the model has been vertically panned, then there is some vertical movement (around the vertical centre of the screen, as I look at the screen) – Dave Miller Oct 17 '18 at 11:00