Adjust orbit center after panning #194

This commit is contained in:
kovacsv 2021-12-21 20:47:25 +01:00
parent c8f3511708
commit 9e4aa58de2
3 changed files with 69 additions and 23 deletions

View File

@ -34,10 +34,14 @@ OV.BoundingBoxCalculator3D = class
new OV.Coord3D (Infinity, Infinity, Infinity), new OV.Coord3D (Infinity, Infinity, Infinity),
new OV.Coord3D (-Infinity, -Infinity, -Infinity) new OV.Coord3D (-Infinity, -Infinity, -Infinity)
); );
this.isValid = false;
} }
GetBox () GetBox ()
{ {
if (!this.isValid) {
return null;
}
return this.box; return this.box;
} }
@ -49,5 +53,6 @@ OV.BoundingBoxCalculator3D = class
this.box.max.x = Math.max (this.box.max.x, point.x); this.box.max.x = Math.max (this.box.max.x, point.x);
this.box.max.y = Math.max (this.box.max.y, point.y); this.box.max.y = Math.max (this.box.max.y, point.y);
this.box.max.z = Math.max (this.box.max.z, point.z); this.box.max.z = Math.max (this.box.max.z, point.z);
this.isValid = true;
} }
}; };

View File

@ -262,18 +262,19 @@ OV.NavigationType =
OV.Navigation = class OV.Navigation = class
{ {
constructor (canvas, camera) constructor (canvas, camera, callbacks)
{ {
this.canvas = canvas; this.canvas = canvas;
this.camera = camera; this.camera = camera;
this.callbacks = callbacks;
this.orbitCenter = this.camera.center.Clone (); this.orbitCenter = this.camera.center.Clone ();
this.fixUpVector = true; this.fixUpVector = true;
this.mouse = new OV.MouseInteraction (); this.mouse = new OV.MouseInteraction ();
this.touch = new OV.TouchInteraction (); this.touch = new OV.TouchInteraction ();
this.clickDetector = new OV.ClickDetector (); this.clickDetector = new OV.ClickDetector ();
this.lastNavigationType = OV.NavigationType.None;
this.onUpdate = null;
this.onMouseClick = null; this.onMouseClick = null;
this.onMouseMove = null; this.onMouseMove = null;
this.onContext = null; this.onContext = null;
@ -294,11 +295,6 @@ OV.Navigation = class
} }
} }
SetUpdateHandler (onUpdate)
{
this.onUpdate = onUpdate;
}
SetMouseClickHandler (onMouseClick) SetMouseClickHandler (onMouseClick)
{ {
this.onMouseClick = onMouseClick; this.onMouseClick = onMouseClick;
@ -464,6 +460,7 @@ OV.Navigation = class
this.Zoom (-moveDiff.y * zoomRatio); this.Zoom (-moveDiff.y * zoomRatio);
} }
this.lastNavigationType = navigationType;
this.Update (); this.Update ();
} }
@ -476,6 +473,11 @@ OV.Navigation = class
let mouseCoords = this.mouse.GetPosition (); let mouseCoords = this.mouse.GetPosition ();
this.Click (ev.which, mouseCoords); this.Click (ev.which, mouseCoords);
} }
if (this.lastNavigationType === OV.NavigationType.Pan) {
this.UpdateOrbitCenter ();
}
this.lastNavigationType = OV.NavigationType.None;
} }
OnMouseLeave (ev) OnMouseLeave (ev)
@ -505,16 +507,25 @@ OV.Navigation = class
let moveDiff = this.touch.GetMoveDiff (); let moveDiff = this.touch.GetMoveDiff ();
let distanceDiff = this.touch.GetDistanceDiff (); let distanceDiff = this.touch.GetDistanceDiff ();
let fingerCount = this.touch.GetFingerCount (); let fingerCount = this.touch.GetFingerCount ();
let navigationType = OV.NavigationType.None;
if (fingerCount === 1) { if (fingerCount === 1) {
navigationType = OV.NavigationType.Orbit;
} else if (fingerCount === 2) {
navigationType = OV.NavigationType.Pan;
}
if (navigationType === OV.NavigationType.Orbit) {
let orbitRatio = 0.5; let orbitRatio = 0.5;
this.Orbit (moveDiff.x * orbitRatio, moveDiff.y * orbitRatio); this.Orbit (moveDiff.x * orbitRatio, moveDiff.y * orbitRatio);
} else if (fingerCount === 2) { } else if (navigationType === OV.NavigationType.Pan) {
let zoomRatio = 0.005; let zoomRatio = 0.005;
this.Zoom (distanceDiff * zoomRatio); this.Zoom (distanceDiff * zoomRatio);
let panRatio = 0.001 * OV.CoordDistance3D (this.camera.eye, this.camera.center); let panRatio = 0.001 * OV.CoordDistance3D (this.camera.eye, this.camera.center);
this.Pan (moveDiff.x * panRatio, moveDiff.y * panRatio); this.Pan (moveDiff.x * panRatio, moveDiff.y * panRatio);
} }
this.lastNavigationType = navigationType;
this.Update (); this.Update ();
} }
@ -531,6 +542,11 @@ OV.Navigation = class
this.Click (1, touchCoords); this.Click (1, touchCoords);
} }
} }
if (this.lastNavigationType === OV.NavigationType.Pan) {
this.UpdateOrbitCenter ();
}
this.lastNavigationType = OV.NavigationType.None;
} }
OnMouseWheel (ev) OnMouseWheel (ev)
@ -616,9 +632,7 @@ OV.Navigation = class
Update () Update ()
{ {
if (this.onUpdate) { this.callbacks.onUpdate ();
this.onUpdate ();
}
} }
Click (button, mouseCoords) Click (button, mouseCoords)
@ -639,4 +653,12 @@ OV.Navigation = class
this.onContext (globalCoords, localCoords); this.onContext (globalCoords, localCoords);
} }
} }
UpdateOrbitCenter ()
{
let centerCoord = this.callbacks.getCenterIntersection ();
if (centerCoord !== null) {
this.orbitCenter = centerCoord;
}
}
}; };

View File

@ -488,7 +488,7 @@ OV.Viewer = class
this.extraGeometry = new OV.ViewerExtraGeometry (this.scene); this.extraGeometry = new OV.ViewerExtraGeometry (this.scene);
this.axis = new OV.ViewerAxis (this.scene); this.axis = new OV.ViewerAxis (this.scene);
this.InitCamera (); this.InitNavigation ();
this.InitShading (); this.InitShading ();
this.Render (); this.Render ();
@ -742,13 +742,8 @@ OV.Viewer = class
GetMeshIntersectionUnderMouse (mouseCoords) GetMeshIntersectionUnderMouse (mouseCoords)
{ {
let width = this.canvas.width; let canvasSize = this.GetCanvasSize ();
let height = this.canvas.height; let intersection = this.geometry.GetMeshIntersectionUnderMouse (mouseCoords, this.camera, canvasSize.width, canvasSize.height);
if (window.devicePixelRatio) {
width /= window.devicePixelRatio;
height /= window.devicePixelRatio;
}
let intersection = this.geometry.GetMeshIntersectionUnderMouse (mouseCoords, this.camera, width, height);
if (intersection === null) { if (intersection === null) {
return null; return null;
} }
@ -791,7 +786,7 @@ OV.Viewer = class
}); });
} }
InitCamera () InitNavigation ()
{ {
this.camera = new THREE.PerspectiveCamera (45.0, this.canvas.width / this.canvas.height, 0.1, 1000.0); this.camera = new THREE.PerspectiveCamera (45.0, this.canvas.width / this.canvas.height, 0.1, 1000.0);
this.scene.add (this.camera); this.scene.add (this.camera);
@ -799,9 +794,19 @@ OV.Viewer = class
let canvasElem = this.renderer.domElement; let canvasElem = this.renderer.domElement;
let camera = OV.GetDefaultCamera (OV.Direction.Z); let camera = OV.GetDefaultCamera (OV.Direction.Z);
this.navigation = new OV.Navigation (canvasElem, camera); this.navigation = new OV.Navigation (canvasElem, camera, {
this.navigation.SetUpdateHandler (() => { onUpdate : () => {
this.Render (); this.Render ();
},
getCenterIntersection : () => {
let canvasSize = this.GetCanvasSize ();
let centerCoord = new OV.Coord2D (canvasSize.width / 2.0, canvasSize.height / 2.0);
let intersection = this.GetMeshIntersectionUnderMouse (centerCoord);
if (intersection === null) {
return null;
}
return new OV.Coord3D (intersection.point.x, intersection.point.y, intersection.point.z);
}
}); });
this.upVector = new OV.UpVector (); this.upVector = new OV.UpVector ();
@ -822,6 +827,20 @@ OV.Viewer = class
}; };
} }
GetCanvasSize ()
{
let width = this.canvas.width;
let height = this.canvas.height;
if (window.devicePixelRatio) {
width /= window.devicePixelRatio;
height /= window.devicePixelRatio;
}
return {
width : width,
height : height
};
}
GetImageAsDataUrl (width, height) GetImageAsDataUrl (width, height)
{ {
let originalSize = this.GetImageSize (); let originalSize = this.GetImageSize ();