@mlightcad/three-viewcube
Version:
A highly customizable standalone view cube addon for three.js
80 lines • 2.82 kB
JavaScript
import * as THREE from 'three';
/**
* A highly customizable standalone view cube addon for three.js.
*/
var SimpleCameraControls = /** @class */ (function () {
/**
* Construct one instance of view cube helper
* @param camera Camera used in your canvas
* @param renderer Renderer used in your canvas
* @param options Options to customize view cube helper
*/
function SimpleCameraControls(camera) {
this.camera = camera;
this.animating = false;
this.turnRate = 2 * Math.PI; // turn rate in angles per second
this.target = new THREE.Vector3();
this.q1 = new THREE.Quaternion();
this.q2 = new THREE.Quaternion();
this.radius = 0;
this.clock = new THREE.Clock();
}
/**
* Set associated obit controls
* @param controls The associated orbit controls
*/
SimpleCameraControls.prototype.setControls = function (controls) {
if (!controls)
return;
this.controls = controls;
};
/**
* Animation loop
*/
SimpleCameraControls.prototype.update = function () {
var _a;
if (this.animating === false)
return;
var delta = this.clock.getDelta();
var step = delta * this.turnRate;
// animate position by doing a slerp and then scaling the position on the unit sphere
this.q1.rotateTowards(this.q2, step);
this.camera.position
.set(0, 0, 1)
.applyQuaternion(this.q1)
.multiplyScalar(this.radius)
.add(this.target);
// animate orientation
this.camera.quaternion.rotateTowards(this.q2, step);
this.camera.updateProjectionMatrix();
(_a = this.controls) === null || _a === void 0 ? void 0 : _a.update();
if (this.q1.angleTo(this.q2) <= 0.00001) {
this.animating = false;
this.clock.stop();
}
};
/**
* Fly with the target quaterion
* @param quaternion
*/
SimpleCameraControls.prototype.flyTo = function (quaternion) {
var focusPoint = new THREE.Vector3();
var targetPosition = new THREE.Vector3(0, 0, 1);
this.radius = this.camera.position.distanceTo(focusPoint);
targetPosition
.applyQuaternion(quaternion)
.multiplyScalar(this.radius)
.add(focusPoint);
var dummy = new THREE.Object3D();
dummy.position.copy(focusPoint);
dummy.lookAt(this.camera.position);
this.q1.copy(dummy.quaternion);
dummy.lookAt(targetPosition);
this.q2.copy(dummy.quaternion);
this.animating = true;
this.clock.start();
};
return SimpleCameraControls;
}());
export { SimpleCameraControls };
//# sourceMappingURL=simpleCameraControls.js.map