123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- "use strict";
- var BABYLON = BABYLON || {};
- (function () {
- BABYLON.FreeCamera = function (name, position, scene) {
- BABYLON.Camera.call(this, name, position, scene);
-
- this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
- this.cameraRotation = new BABYLON.Vector2(0, 0);
- this.rotation = new BABYLON.Vector3(0, 0, 0);
- this.ellipsoid = new BABYLON.Vector3(0.5, 1, 0.5);
- this._keys = [];
- this.keysUp = [38];
- this.keysDown = [40];
- this.keysLeft = [37];
- this.keysRight = [39];
- // Collisions
- this._collider = new BABYLON.Collider();
- this._needMoveForGravity = true;
- // Internals
- this._currentTarget = BABYLON.Vector3.Zero();
- this._viewMatrix = BABYLON.Matrix.Zero();
- this._camMatrix = BABYLON.Matrix.Zero();
- this._cameraTransformMatrix = BABYLON.Matrix.Zero();
- this._cameraRotationMatrix = BABYLON.Matrix.Zero();
- this._referencePoint = BABYLON.Vector3.Zero();
- this._transformedReferencePoint = BABYLON.Vector3.Zero();
- this._oldPosition = BABYLON.Vector3.Zero();
- this._diffPosition = BABYLON.Vector3.Zero();
- this._newPosition = BABYLON.Vector3.Zero();
- this._lookAtTemp = BABYLON.Matrix.Zero();
- this._tempMatrix = BABYLON.Matrix.Zero();
- };
- BABYLON.FreeCamera.prototype = Object.create(BABYLON.Camera.prototype);
- // Members
- BABYLON.FreeCamera.prototype.speed = 2.0;
- BABYLON.FreeCamera.prototype.checkCollisions = false;
- BABYLON.FreeCamera.prototype.applyGravity = false;
- BABYLON.FreeCamera.prototype.noRotationConstraint = false;
- BABYLON.FreeCamera.prototype.angularSensibility = 2000.0;
- BABYLON.FreeCamera.prototype.lockedTarget = null;
- // Methods
- BABYLON.FreeCamera.prototype._computeLocalCameraSpeed = function () {
- return this.speed * ((BABYLON.Tools.GetDeltaTime() / (BABYLON.Tools.GetFps() * 10.0)));
- };
- // Target
- BABYLON.FreeCamera.prototype.setTarget = function (target) {
- this.upVector.normalize();
-
- BABYLON.Matrix.LookAtLHToRef(this.position, target, this.upVector, this._camMatrix);
- this._camMatrix.invert();
- this.rotation.x = Math.atan(this._camMatrix.m[6] / this._camMatrix.m[10]);
- var vDir = target.subtract(this.position);
- if (vDir.x >= 0.0) {
- this.rotation.y = (-Math.atan(vDir.z / vDir.x) + Math.PI / 2.0);
- } else {
- this.rotation.y = (-Math.atan(vDir.z / vDir.x) - Math.PI / 2.0);
- }
- this.rotation.z = -Math.acos(BABYLON.Vector3.Dot(new BABYLON.Vector3(0, 1.0, 0), this.upVector));
- if (isNaN(this.rotation.x))
- this.rotation.x = 0;
- if (isNaN(this.rotation.y))
- this.rotation.y = 0;
- if (isNaN(this.rotation.z))
- this.rotation.z = 0;
- };
- // Controls
- BABYLON.FreeCamera.prototype.attachControl = function (canvas, noPreventDefault) {
- var previousPosition;
- var that = this;
- var engine = this._scene.getEngine();
-
- if (this._attachedCanvas) {
- return;
- }
- this._attachedCanvas = canvas;
- 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;
- that._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;
- }
- that.cameraRotation.y += offsetX / that.angularSensibility;
- that.cameraRotation.x += offsetY / that.angularSensibility;
- previousPosition = {
- x: evt.clientX,
- y: evt.clientY
- };
- if (!noPreventDefault) {
- evt.preventDefault();
- }
- };
- this._onKeyDown = function (evt) {
- if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
- that.keysDown.indexOf(evt.keyCode) !== -1 ||
- that.keysLeft.indexOf(evt.keyCode) !== -1 ||
- that.keysRight.indexOf(evt.keyCode) !== -1) {
- var index = that._keys.indexOf(evt.keyCode);
- if (index === -1) {
- that._keys.push(evt.keyCode);
- }
- if (!noPreventDefault) {
- evt.preventDefault();
- }
- }
- };
- this._onKeyUp = function (evt) {
- if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
- that.keysDown.indexOf(evt.keyCode) !== -1 ||
- that.keysLeft.indexOf(evt.keyCode) !== -1 ||
- that.keysRight.indexOf(evt.keyCode) !== -1) {
- var index = that._keys.indexOf(evt.keyCode);
- if (index >= 0) {
- that._keys.splice(index, 1);
- }
- if (!noPreventDefault) {
- evt.preventDefault();
- }
- }
- };
- this._onLostFocus = function () {
- that._keys = [];
- };
- this._reset = function() {
- that._keys = [];
- previousPosition = null;
- that.cameraDirection = new BABYLON.Vector3(0, 0, 0);
- that.cameraRotation = new BABYLON.Vector2(0, 0);
- };
- }
- canvas.addEventListener("mousedown", this._onMouseDown, false);
- canvas.addEventListener("mouseup", this._onMouseUp, false);
- canvas.addEventListener("mouseout", this._onMouseOut, false);
- canvas.addEventListener("mousemove", this._onMouseMove, false);
- window.addEventListener("keydown", this._onKeyDown, false);
- window.addEventListener("keyup", this._onKeyUp, false);
- window.addEventListener("blur", this._onLostFocus, false);
- };
- BABYLON.FreeCamera.prototype.detachControl = function (canvas) {
- if (this._attachedCanvas != canvas) {
- return;
- }
- canvas.removeEventListener("mousedown", this._onMouseDown);
- canvas.removeEventListener("mouseup", this._onMouseUp);
- canvas.removeEventListener("mouseout", this._onMouseOut);
- canvas.removeEventListener("mousemove", this._onMouseMove);
- window.removeEventListener("keydown", this._onKeyDown);
- window.removeEventListener("keyup", this._onKeyUp);
- window.removeEventListener("blur", this._onLostFocus);
-
- this._attachedCanvas = null;
- if (this._reset) {
- this._reset();
- }
- };
- BABYLON.FreeCamera.prototype._collideWithWorld = function (velocity) {
- this.position.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPosition);
- this._collider.radius = this.ellipsoid;
- this._scene._getNewPosition(this._oldPosition, velocity, this._collider, 3, this._newPosition);
- this._newPosition.subtractToRef(this._oldPosition, this._diffPosition);
- if (this._diffPosition.length() > BABYLON.Engine.collisionsEpsilon) {
- this.position.addInPlace(this._diffPosition);
- }
- };
- BABYLON.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);
- }
- };
- BABYLON.FreeCamera.prototype._update = function () {
- this._checkInputs();
- var needToMove = this._needMoveForGravity || Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
- var needToRotate = Math.abs(this.cameraRotation.x) > 0 || Math.abs(this.cameraRotation.y) > 0;
- // Move
- if (needToMove) {
- if (this.checkCollisions && this._scene.collisionsEnabled) {
- this._collideWithWorld(this.cameraDirection);
- if (this.applyGravity) {
- var oldPosition = this.position;
- this._collideWithWorld(this._scene.gravity);
- this._needMoveForGravity = (BABYLON.Vector3.DistanceSquared(oldPosition, this.position) != 0);
- }
- } else {
- this.position.addInPlace(this.cameraDirection);
- }
- }
- // Rotate
- if (needToRotate) {
- this.rotation.x += this.cameraRotation.x;
- this.rotation.y += this.cameraRotation.y;
- if (!this.noRotationConstraint) {
- var limit = (Math.PI / 2) * 0.95;
- if (this.rotation.x > limit)
- this.rotation.x = limit;
- if (this.rotation.x < -limit)
- this.rotation.x = -limit;
- }
- }
- // Inertia
- if (needToMove) {
- this.cameraDirection.scaleInPlace(this.inertia);
- }
- if (needToRotate) {
- this.cameraRotation.scaleInPlace(this.inertia);
- }
- };
- BABYLON.FreeCamera.prototype._getViewMatrix = function () {
- BABYLON.Vector3.FromFloatsToRef(0, 0, 1, this._referencePoint);
- if (!this.lockedTarget) {
- // Compute
- if (this.upVector.x != 0 || this.upVector.y != 1.0 || this.upVector.z != 0) {
- BABYLON.Matrix.LookAtLHToRef(BABYLON.Vector3.Zero(), this._referencePoint, this.upVector, this._lookAtTemp);
- BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
- this._lookAtTemp.multiplyToRef(this._cameraRotationMatrix, this._tempMatrix);
- this._lookAtTemp.invert();
- this._tempMatrix.multiplyToRef(this._lookAtTemp, this._cameraRotationMatrix);
- } else {
- BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
- }
- BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
- // Computing target and final matrix
- this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
- } else {
- if (this.lockedTarget.position) {
- this._currentTarget.copyFrom(this.lockedTarget.position);
- } else {
- this._currentTarget.copyFrom(this.lockedTarget);
- }
- }
-
- BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
- return this._viewMatrix;
- };
- })();
|