From c5489754e8571fce582978546443c1250b739942 Mon Sep 17 00:00:00 2001 From: Andreas Plesch Date: Fri, 9 Feb 2024 23:30:18 -0500 Subject: [PATCH] fix fit for OrthoViewpoint (#1318) * factor out dist function * Update CHANGELOG.md --- CHANGELOG.md | 1 + src/Viewarea.js | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 601ceb0579..3be72c43a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ * more accurate startTime update * tune PhysicalMaterial for compatible light intensity * Bugfixes + * fix fitAll for OrthoViewpoint * fix nav. type field updates * fix mipmaps for compressed dds * allow turntable as initial navigation diff --git a/src/Viewarea.js b/src/Viewarea.js index 0ff30f1611..e7afd72023 100755 --- a/src/Viewarea.js +++ b/src/Viewarea.js @@ -993,7 +993,8 @@ x3dom.Viewarea.prototype.fit = function ( min, max, updateCenterOfRotation ) if ( x3dom.isa( viewpoint, x3dom.nodeTypes.OrthoViewpoint ) ) { - this.orthoAnimateTo( dist / 2.01, Math.abs( viewpoint._fieldOfView[ 0 ] ) ); + var dist = this.getFitViewCenterDist( min, max, viewpoint ); + this.orthoAnimateTo( dist.dist / 2.01, Math.abs( viewpoint._fieldOfView[ 0 ] ) ); this.animateTo( viewmat, viewpoint ); } else @@ -1002,6 +1003,29 @@ x3dom.Viewarea.prototype.fit = function ( min, max, updateCenterOfRotation ) } }; +/** + * get center and dist for fitViewMatrix + * + * @param min + * @param max + * @param viewpoint + * @returns .center and .dist for fitting + */ +x3dom.Viewarea.prototype.getFitViewCenterDist = function ( min, max, viewpoint ) +{ + var dia2 = max.subtract( min ).multiply( 0.5 ); // half diameter + var center = min.add( dia2 ); // center in wc + var bsr = dia2.length(); // bounding sphere radius + + var fov = viewpoint.getFieldOfView(); + + var aspect = Math.min( this._width / this._height, 1 ); + + var tanfov2 = Math.tan( fov / 2.0 ); + var dist = bsr / tanfov2 / aspect; + return { center: center, dist: dist }; +}; + /** * get fitViewMatrix and optionally set cor * @@ -1018,11 +1042,10 @@ x3dom.Viewarea.prototype.getFitViewMatrix = function ( min, max, viewpoint, upda updateCenterOfRotation = true; } - var dia2 = max.subtract( min ).multiply( 0.5 ); // half diameter - var center = min.add( dia2 ); // center in wc - var bsr = dia2.length(); // bounding sphere radius + var centerDist = this.getFitViewCenterDist( min, max, viewpoint ); - var fov = viewpoint.getFieldOfView(); + var center = centerDist.center; // center in wc + var dist = centerDist.dist; var viewmat = x3dom.fields.SFMatrix4f.copy( this.getViewMatrix() ); @@ -1030,11 +1053,6 @@ x3dom.Viewarea.prototype.getFitViewMatrix = function ( min, max, viewpoint, upda var upDir = new x3dom.fields.SFVec3f( viewmat._10, viewmat._11, viewmat._12 ); var viewDir = new x3dom.fields.SFVec3f( viewmat._20, viewmat._21, viewmat._22 ); - var aspect = Math.min( this._width / this._height, 1 ); - - var tanfov2 = Math.tan( fov / 2.0 ); - var dist = bsr / tanfov2 / aspect; - var eyePos = center.add( viewDir.multiply( dist ) ); viewmat._03 = -rightDir.dot( eyePos );