| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- var __extends = (this && this.__extends) || function (d, b) {
- for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var BABYLON;
- (function (BABYLON) {
- var FreeCamera = (function (_super) {
- __extends(FreeCamera, _super);
- function FreeCamera(name, position, scene) {
- var _this = this;
- _super.call(this, name, position, scene);
- this.ellipsoid = new BABYLON.Vector3(0.5, 1, 0.5);
- this.keysUp = [38];
- this.keysDown = [40];
- this.keysLeft = [37];
- this.keysRight = [39];
- this.checkCollisions = false;
- this.applyGravity = false;
- this.angularSensibility = 2000.0;
- this._keys = [];
- this._collider = new BABYLON.Collider();
- this._needMoveForGravity = false;
- this._oldPosition = BABYLON.Vector3.Zero();
- this._diffPosition = BABYLON.Vector3.Zero();
- this._newPosition = BABYLON.Vector3.Zero();
- this._onCollisionPositionChange = function (collisionId, newPosition, collidedMesh) {
- if (collidedMesh === void 0) { collidedMesh = null; }
- //TODO move this to the collision coordinator!
- if (_this.getScene().workerCollisions)
- newPosition.multiplyInPlace(_this._collider.radius);
- var updatePosition = function (newPos) {
- _this._newPosition.copyFrom(newPos);
- _this._newPosition.subtractToRef(_this._oldPosition, _this._diffPosition);
- var oldPosition = _this.position.clone();
- if (_this._diffPosition.length() > BABYLON.Engine.CollisionsEpsilon) {
- _this.position.addInPlace(_this._diffPosition);
- if (_this.onCollide && collidedMesh) {
- _this.onCollide(collidedMesh);
- }
- }
- };
- updatePosition(newPosition);
- };
- }
- FreeCamera.prototype._onLostFocus = function (e) {
- this._keys = [];
- };
- // Controls
- FreeCamera.prototype.attachControl = function (element, noPreventDefault) {
- var _this = this;
- var previousPosition;
- var engine = this.getEngine();
- if (this._attachedElement) {
- return;
- }
- this._attachedElement = element;
- if (this._onMouseDown === undefined) {
- this._onMouseDown = function (evt) {
- previousPosition = {
- x: evt.clientX,
- y: evt.clientY
- };
- if (!noPreventDefault) {
- evt.preventDefault();
- }
- };
- this._onMouseUp = function (evt) {
- previousPosition = null;
- if (!noPreventDefault) {
- evt.preventDefault();
- }
- };
- this._onMouseOut = function (evt) {
- previousPosition = null;
- _this._keys = [];
- if (!noPreventDefault) {
- evt.preventDefault();
- }
- };
- this._onMouseMove = function (evt) {
- if (!previousPosition && !engine.isPointerLock) {
- return;
- }
- var offsetX;
- var offsetY;
- if (!engine.isPointerLock) {
- offsetX = evt.clientX - previousPosition.x;
- offsetY = evt.clientY - previousPosition.y;
- }
- else {
- offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
- offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
- }
- _this.cameraRotation.y += offsetX / _this.angularSensibility;
- _this.cameraRotation.x += offsetY / _this.angularSensibility;
- previousPosition = {
- x: evt.clientX,
- y: evt.clientY
- };
- if (!noPreventDefault) {
- evt.preventDefault();
- }
- };
- this._onKeyDown = function (evt) {
- if (_this.keysUp.indexOf(evt.keyCode) !== -1 ||
- _this.keysDown.indexOf(evt.keyCode) !== -1 ||
- _this.keysLeft.indexOf(evt.keyCode) !== -1 ||
- _this.keysRight.indexOf(evt.keyCode) !== -1) {
- var index = _this._keys.indexOf(evt.keyCode);
- if (index === -1) {
- _this._keys.push(evt.keyCode);
- }
- if (!noPreventDefault) {
- evt.preventDefault();
- }
- }
- };
- this._onKeyUp = function (evt) {
- if (_this.keysUp.indexOf(evt.keyCode) !== -1 ||
- _this.keysDown.indexOf(evt.keyCode) !== -1 ||
- _this.keysLeft.indexOf(evt.keyCode) !== -1 ||
- _this.keysRight.indexOf(evt.keyCode) !== -1) {
- var index = _this._keys.indexOf(evt.keyCode);
- if (index >= 0) {
- _this._keys.splice(index, 1);
- }
- if (!noPreventDefault) {
- evt.preventDefault();
- }
- }
- };
- this._reset = function () {
- _this._keys = [];
- previousPosition = null;
- _this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
- _this.cameraRotation = new BABYLON.Vector2(0, 0);
- };
- }
- element.addEventListener("mousedown", this._onMouseDown, false);
- element.addEventListener("mouseup", this._onMouseUp, false);
- element.addEventListener("mouseout", this._onMouseOut, false);
- element.addEventListener("mousemove", this._onMouseMove, false);
- BABYLON.Tools.RegisterTopRootEvents([
- { name: "keydown", handler: this._onKeyDown },
- { name: "keyup", handler: this._onKeyUp },
- { name: "blur", handler: this._onLostFocus }
- ]);
- };
- FreeCamera.prototype.detachControl = function (element) {
- if (this._attachedElement !== element) {
- return;
- }
- element.removeEventListener("mousedown", this._onMouseDown);
- element.removeEventListener("mouseup", this._onMouseUp);
- element.removeEventListener("mouseout", this._onMouseOut);
- element.removeEventListener("mousemove", this._onMouseMove);
- BABYLON.Tools.UnregisterTopRootEvents([
- { name: "keydown", handler: this._onKeyDown },
- { name: "keyup", handler: this._onKeyUp },
- { name: "blur", handler: this._onLostFocus }
- ]);
- this._attachedElement = null;
- if (this._reset) {
- this._reset();
- }
- };
- FreeCamera.prototype._collideWithWorld = function (velocity) {
- var globalPosition;
- if (this.parent) {
- globalPosition = BABYLON.Vector3.TransformCoordinates(this.position, this.parent.getWorldMatrix());
- }
- else {
- globalPosition = this.position;
- }
- globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPosition);
- this._collider.radius = this.ellipsoid;
- //no need for clone, as long as gravity is not on.
- var actualVelocity = velocity;
- //add gravity to the velocity to prevent the dual-collision checking
- if (this.applyGravity) {
- //this prevents mending with cameraDirection, a global variable of the free camera class.
- actualVelocity = velocity.add(this.getScene().gravity);
- }
- this.getScene().collisionCoordinator.getNewPosition(this._oldPosition, actualVelocity, this._collider, 3, null, this._onCollisionPositionChange, this.uniqueId);
- };
- FreeCamera.prototype._checkInputs = function () {
- if (!this._localDirection) {
- this._localDirection = BABYLON.Vector3.Zero();
- this._transformedDirection = BABYLON.Vector3.Zero();
- }
- // Keyboard
- for (var index = 0; index < this._keys.length; index++) {
- var keyCode = this._keys[index];
- var speed = this._computeLocalCameraSpeed();
- if (this.keysLeft.indexOf(keyCode) !== -1) {
- this._localDirection.copyFromFloats(-speed, 0, 0);
- }
- else if (this.keysUp.indexOf(keyCode) !== -1) {
- this._localDirection.copyFromFloats(0, 0, speed);
- }
- else if (this.keysRight.indexOf(keyCode) !== -1) {
- this._localDirection.copyFromFloats(speed, 0, 0);
- }
- else if (this.keysDown.indexOf(keyCode) !== -1) {
- this._localDirection.copyFromFloats(0, 0, -speed);
- }
- this.getViewMatrix().invertToRef(this._cameraTransformMatrix);
- BABYLON.Vector3.TransformNormalToRef(this._localDirection, this._cameraTransformMatrix, this._transformedDirection);
- this.cameraDirection.addInPlace(this._transformedDirection);
- }
- _super.prototype._checkInputs.call(this);
- };
- FreeCamera.prototype._decideIfNeedsToMove = function () {
- return this._needMoveForGravity || Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
- };
- FreeCamera.prototype._updatePosition = function () {
- if (this.checkCollisions && this.getScene().collisionsEnabled) {
- this._collideWithWorld(this.cameraDirection);
- }
- else {
- this.position.addInPlace(this.cameraDirection);
- }
- };
- FreeCamera.prototype.getTypeName = function () {
- return "FreeCamera";
- };
- __decorate([
- BABYLON.serializeAsVector3()
- ], FreeCamera.prototype, "ellipsoid", void 0);
- __decorate([
- BABYLON.serialize()
- ], FreeCamera.prototype, "keysUp", void 0);
- __decorate([
- BABYLON.serialize()
- ], FreeCamera.prototype, "keysDown", void 0);
- __decorate([
- BABYLON.serialize()
- ], FreeCamera.prototype, "keysLeft", void 0);
- __decorate([
- BABYLON.serialize()
- ], FreeCamera.prototype, "keysRight", void 0);
- __decorate([
- BABYLON.serialize()
- ], FreeCamera.prototype, "checkCollisions", void 0);
- __decorate([
- BABYLON.serialize()
- ], FreeCamera.prototype, "applyGravity", void 0);
- __decorate([
- BABYLON.serialize()
- ], FreeCamera.prototype, "angularSensibility", void 0);
- return FreeCamera;
- })(BABYLON.TargetCamera);
- BABYLON.FreeCamera = FreeCamera;
- })(BABYLON || (BABYLON = {}));
- //# sourceMappingURL=babylon.freeCamera.js.map
|