Skip to content

Commit

Permalink
feature(View): dispatch an event when camera is moved
Browse files Browse the repository at this point in the history
  • Loading branch information
mgermerie committed Jan 7, 2022
1 parent b7be8e9 commit 37cfb90
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Controls/GlobeControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Coordinates from 'Core/Geographic/Coordinates';
import { ellipsoidSizes } from 'Core/Math/Ellipsoid';
import CameraUtils from 'Utils/CameraUtils';
import StateControl from 'Controls/StateControl';
import { VIEW_EVENTS } from 'Core/View';

// private members
const EPS = 0.000001;
Expand Down Expand Up @@ -38,6 +39,7 @@ let dollyScale;
// Globe move
const moveAroundGlobe = new THREE.Quaternion();
const cameraTarget = new THREE.Object3D();
const coordCameraTarget = new Coordinates('EPSG:4978');
cameraTarget.matrixWorldInverse = new THREE.Matrix4();

const xyz = new Coordinates('EPSG:4978', 0, 0, 0);
Expand Down Expand Up @@ -309,6 +311,8 @@ class GlobeControls extends THREE.EventDispatcher {
}
positionObject(xyz, cameraTarget);
this.lookAtCoordinate(placement, false);

coordCameraTarget.crs = this.view.referenceCrs;
}

get dollyInScale() {
Expand Down Expand Up @@ -539,6 +543,14 @@ class GlobeControls extends THREE.EventDispatcher {
this.player.setCallback(() => { this.update(this.states.ORBIT); });
this.player.playLater(durationDampingOrbital, 2);
}

this.view.dispatchEvent({
type: VIEW_EVENTS.CAMERA_MOVED,
coord: coordCameraTarget.setFromVector3(cameraTarget.position),
range: spherical.radius,
heading: -THREE.MathUtils.radToDeg(spherical.theta),
tilt: 90 - THREE.MathUtils.radToDeg(spherical.phi),
});
}

onStateChange(event) {
Expand Down
1 change: 1 addition & 0 deletions src/Core/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const VIEW_EVENTS = {
LAYER_ADDED: 'layer-added',
INITIALIZED: 'initialized',
COLOR_LAYERS_ORDER_CHANGED,
CAMERA_MOVED: 'camera-moved',
};

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Utils/CameraUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MAIN_LOOP_EVENTS } from 'Core/MainLoop';
import Coordinates from 'Core/Geographic/Coordinates';
import Ellipsoid from 'Core/Math/Ellipsoid';
import OBB from 'Renderer/OBB';
import { VIEW_EVENTS } from 'Core/View';

THREE.Object3D.DefaultUp.set(0, 0, 1);
const targetPosition = new THREE.Vector3();
Expand Down Expand Up @@ -100,6 +101,13 @@ class CameraRig extends THREE.Object3D {
} else {
this.camera.matrixWorld.decompose(camera.position, camera.quaternion, camera.scale);
}
view.dispatchEvent({
type: VIEW_EVENTS.CAMERA_MOVED,
coord: this.coord,
range: this.range,
heading: this.heading,
tilt: this.tilt,
});
}

setProxy(view, camera) {
Expand Down
1 change: 1 addition & 0 deletions test/unit/CameraUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('Camera utils unit test', function () {
view.addFrameRequester = () => {};
view.removeFrameRequester = () => {};
view.notifyChange = () => { camera.updateMatrixWorld(true); };
view.dispatchEvent = () => {};

const range = 25000000;
const coord = new Coordinates('EPSG:4326', 2.35, 48.85, 0);
Expand Down

0 comments on commit 37cfb90

Please sign in to comment.