Skip to content

Commit

Permalink
fix(Camera): resize preserves the scale
Browse files Browse the repository at this point in the history
  • Loading branch information
mgermerie authored and gchoqueux committed Mar 10, 2021
1 parent b6dd383 commit e44de7f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
30 changes: 20 additions & 10 deletions src/Renderer/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class Camera {
break;
}
}
this.camera3D.aspect = this.camera3D.aspect !== undefined ? this.camera3D.aspect : 1;

this._viewMatrix = new THREE.Matrix4();
this.width = width;
Expand All @@ -136,23 +137,32 @@ class Camera {
}
}

/**
* Resize the camera to a given width and height
*
* @param {number} width The width to resize the camera to.
* @param {number} height The height to resize the camera to.
*/
resize(width, height) {
this.width = width;
this.height = height;

const ratio = width / height;
updatePreSse(this, this.height, this.camera3D.fov);

if (this.camera3D.aspect !== ratio) {
this.camera3D.aspect = ratio;
if (this.camera3D.isOrthographicCamera) {
const halfH = (this.camera3D.right - this.camera3D.left) * 0.5 / ratio;
const y = (this.camera3D.top + this.camera3D.bottom) * 0.5;
this.camera3D.top = y + halfH;
this.camera3D.bottom = y - halfH;
this.camera3D.zoom *= this.width / width;
const halfH = this.camera3D.top * this.camera3D.aspect / ratio;
this.camera3D.bottom = -halfH;
this.camera3D.top = halfH;
} else if (this.camera3D.isPerspectiveCamera) {
this.camera3D.fov = 2 * THREE.Math.radToDeg(Math.atan(
(height / this.height) * Math.tan(THREE.Math.degToRad(this.camera3D.fov) / 2),
));
}
this.camera3D.aspect = ratio;
}

this.width = width;
this.height = height;
updatePreSse(this, this.height, this.camera3D.fov);

if (this.camera3D.updateProjectionMatrix) {
this.camera3D.updateProjectionMatrix();
this._viewMatrixNeedsUpdate = true;
Expand Down
22 changes: 21 additions & 1 deletion test/unit/camera.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
import Camera from 'Renderer/Camera';
import Camera, { CAMERA_TYPE } from 'Renderer/Camera';
import Coordinates from 'Core/Geographic/Coordinates';

function compareWithEpsilon(a, b, epsilon) {
Expand Down Expand Up @@ -30,4 +30,24 @@ describe('camera', function () {
assert.ok(compareWithEpsilon(resultCoordinates.latitude, coordinates.latitude, 10e-8));
assert.ok(compareWithEpsilon(resultCoordinates.altitude, coordinates.altitude, 10e-8));
});
it('should correctly resize camera', function () {
const dimensions = { width: 78, height: 89 };

const cameraPerspective = new Camera(
'EPSG:4978',
100, 50,
);
cameraPerspective.resize(dimensions.width, dimensions.height);
assert.equal(cameraPerspective.width, dimensions.width);
assert.equal(cameraPerspective.height, dimensions.height);

const cameraOrthographic = new Camera(
'EPSG:4978',
100, 50,
{ type: CAMERA_TYPE.ORTHOGRAPHIC },
);
cameraOrthographic.resize(dimensions.width, dimensions.height);
assert.equal(cameraOrthographic.width, dimensions.width);
assert.equal(cameraOrthographic.height, dimensions.height);
});
});

0 comments on commit e44de7f

Please sign in to comment.