babylon.freeCamera.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  7. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  8. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  9. 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;
  10. return c > 3 && r && Object.defineProperty(target, key, r), r;
  11. };
  12. var BABYLON;
  13. (function (BABYLON) {
  14. var FreeCamera = (function (_super) {
  15. __extends(FreeCamera, _super);
  16. function FreeCamera(name, position, scene) {
  17. var _this = this;
  18. _super.call(this, name, position, scene);
  19. this.ellipsoid = new BABYLON.Vector3(0.5, 1, 0.5);
  20. this.checkCollisions = false;
  21. this.applyGravity = false;
  22. this._collider = new BABYLON.Collider();
  23. this._needMoveForGravity = false;
  24. this._oldPosition = BABYLON.Vector3.Zero();
  25. this._diffPosition = BABYLON.Vector3.Zero();
  26. this._newPosition = BABYLON.Vector3.Zero();
  27. this._onCollisionPositionChange = function (collisionId, newPosition, collidedMesh) {
  28. if (collidedMesh === void 0) { collidedMesh = null; }
  29. //TODO move this to the collision coordinator!
  30. if (_this.getScene().workerCollisions)
  31. newPosition.multiplyInPlace(_this._collider.radius);
  32. var updatePosition = function (newPos) {
  33. _this._newPosition.copyFrom(newPos);
  34. _this._newPosition.subtractToRef(_this._oldPosition, _this._diffPosition);
  35. var oldPosition = _this.position.clone();
  36. if (_this._diffPosition.length() > BABYLON.Engine.CollisionsEpsilon) {
  37. _this.position.addInPlace(_this._diffPosition);
  38. if (_this.onCollide && collidedMesh) {
  39. _this.onCollide(collidedMesh);
  40. }
  41. }
  42. };
  43. updatePosition(newPosition);
  44. };
  45. this.inputs = new BABYLON.FreeCameraInputsManager(this);
  46. this.inputs.addKeyboard().addMouse();
  47. }
  48. Object.defineProperty(FreeCamera.prototype, "angularSensibility", {
  49. //-- begin properties for backward compatibility for inputs
  50. get: function () {
  51. var mouse = this.inputs.attached["mouse"];
  52. if (mouse)
  53. return mouse.angularSensibility;
  54. },
  55. set: function (value) {
  56. var mouse = this.inputs.attached["mouse"];
  57. if (mouse)
  58. mouse.angularSensibility = value;
  59. },
  60. enumerable: true,
  61. configurable: true
  62. });
  63. Object.defineProperty(FreeCamera.prototype, "keysUp", {
  64. get: function () {
  65. var keyboard = this.inputs.attached["keyboard"];
  66. if (keyboard)
  67. return keyboard.keysUp;
  68. },
  69. set: function (value) {
  70. var keyboard = this.inputs.attached["keyboard"];
  71. if (keyboard)
  72. keyboard.keysUp = value;
  73. },
  74. enumerable: true,
  75. configurable: true
  76. });
  77. Object.defineProperty(FreeCamera.prototype, "keysDown", {
  78. get: function () {
  79. var keyboard = this.inputs.attached["keyboard"];
  80. if (keyboard)
  81. return keyboard.keysDown;
  82. },
  83. set: function (value) {
  84. var keyboard = this.inputs.attached["keyboard"];
  85. if (keyboard)
  86. keyboard.keysDown = value;
  87. },
  88. enumerable: true,
  89. configurable: true
  90. });
  91. Object.defineProperty(FreeCamera.prototype, "keysLeft", {
  92. get: function () {
  93. var keyboard = this.inputs.attached["keyboard"];
  94. if (keyboard)
  95. return keyboard.keysLeft;
  96. },
  97. set: function (value) {
  98. var keyboard = this.inputs.attached["keyboard"];
  99. if (keyboard)
  100. keyboard.keysLeft = value;
  101. },
  102. enumerable: true,
  103. configurable: true
  104. });
  105. Object.defineProperty(FreeCamera.prototype, "keysRight", {
  106. get: function () {
  107. var keyboard = this.inputs.attached["keyboard"];
  108. if (keyboard)
  109. return keyboard.keysRight;
  110. },
  111. set: function (value) {
  112. var keyboard = this.inputs.attached["keyboard"];
  113. if (keyboard)
  114. keyboard.keysRight = value;
  115. },
  116. enumerable: true,
  117. configurable: true
  118. });
  119. // Controls
  120. FreeCamera.prototype.attachControl = function (element, noPreventDefault) {
  121. this.inputs.attachElement(element, noPreventDefault);
  122. };
  123. FreeCamera.prototype.detachControl = function (element) {
  124. this.inputs.detachElement(element);
  125. this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  126. this.cameraRotation = new BABYLON.Vector2(0, 0);
  127. };
  128. FreeCamera.prototype._collideWithWorld = function (velocity) {
  129. var globalPosition;
  130. if (this.parent) {
  131. globalPosition = BABYLON.Vector3.TransformCoordinates(this.position, this.parent.getWorldMatrix());
  132. }
  133. else {
  134. globalPosition = this.position;
  135. }
  136. globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPosition);
  137. this._collider.radius = this.ellipsoid;
  138. //no need for clone, as long as gravity is not on.
  139. var actualVelocity = velocity;
  140. //add gravity to the velocity to prevent the dual-collision checking
  141. if (this.applyGravity) {
  142. //this prevents mending with cameraDirection, a global variable of the free camera class.
  143. actualVelocity = velocity.add(this.getScene().gravity);
  144. }
  145. this.getScene().collisionCoordinator.getNewPosition(this._oldPosition, actualVelocity, this._collider, 3, null, this._onCollisionPositionChange, this.uniqueId);
  146. };
  147. FreeCamera.prototype._checkInputs = function () {
  148. if (!this._localDirection) {
  149. this._localDirection = BABYLON.Vector3.Zero();
  150. this._transformedDirection = BABYLON.Vector3.Zero();
  151. }
  152. this.inputs.checkInputs();
  153. _super.prototype._checkInputs.call(this);
  154. };
  155. FreeCamera.prototype._decideIfNeedsToMove = function () {
  156. return this._needMoveForGravity || Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  157. };
  158. FreeCamera.prototype._updatePosition = function () {
  159. if (this.checkCollisions && this.getScene().collisionsEnabled) {
  160. this._collideWithWorld(this.cameraDirection);
  161. }
  162. else {
  163. this.position.addInPlace(this.cameraDirection);
  164. }
  165. };
  166. FreeCamera.prototype.dispose = function () {
  167. this.inputs.clear();
  168. _super.prototype.dispose.call(this);
  169. };
  170. FreeCamera.prototype.getTypeName = function () {
  171. return "FreeCamera";
  172. };
  173. __decorate([
  174. BABYLON.serializeAsVector3()
  175. ], FreeCamera.prototype, "ellipsoid", void 0);
  176. __decorate([
  177. BABYLON.serialize()
  178. ], FreeCamera.prototype, "checkCollisions", void 0);
  179. __decorate([
  180. BABYLON.serialize()
  181. ], FreeCamera.prototype, "applyGravity", void 0);
  182. return FreeCamera;
  183. })(BABYLON.TargetCamera);
  184. BABYLON.FreeCamera = FreeCamera;
  185. })(BABYLON || (BABYLON = {}));