Skip to content

Commit

Permalink
Merge pull request #391 from x3dom/master
Browse files Browse the repository at this point in the history
zapier insta merge...
  • Loading branch information
andreasplesch committed Feb 10, 2024
2 parents 15a7c9e + c548975 commit e3f33c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 28 additions & 10 deletions src/Viewarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*
Expand All @@ -1018,23 +1042,17 @@ 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() );

var rightDir = new x3dom.fields.SFVec3f( viewmat._00, viewmat._01, viewmat._02 );
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 );
Expand Down

0 comments on commit e3f33c9

Please sign in to comment.