babylon.composableCamera.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 ComposableCamera = (function (_super) {
  15. __extends(ComposableCamera, _super);
  16. function ComposableCamera(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.ComposableCameraInputsManager(this);
  46. this.inputs.addKeyboard().addMouse();
  47. }
  48. // Controls
  49. ComposableCamera.prototype.attachControl = function (element, noPreventDefault) {
  50. if (this._attachedElement) {
  51. return;
  52. }
  53. this._noPreventDefault = noPreventDefault;
  54. this._attachedElement = element;
  55. this.inputs.attachElement(element, noPreventDefault);
  56. };
  57. ComposableCamera.prototype.detachControl = function (element) {
  58. if (this._attachedElement !== element) {
  59. return;
  60. }
  61. this.inputs.detachElement(this._attachedElement);
  62. this._attachedElement = null;
  63. this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  64. this.cameraRotation = new BABYLON.Vector2(0, 0);
  65. };
  66. ComposableCamera.prototype._collideWithWorld = function (velocity) {
  67. var globalPosition;
  68. if (this.parent) {
  69. globalPosition = BABYLON.Vector3.TransformCoordinates(this.position, this.parent.getWorldMatrix());
  70. }
  71. else {
  72. globalPosition = this.position;
  73. }
  74. globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPosition);
  75. this._collider.radius = this.ellipsoid;
  76. //no need for clone, as long as gravity is not on.
  77. var actualVelocity = velocity;
  78. //add gravity to the velocity to prevent the dual-collision checking
  79. if (this.applyGravity) {
  80. //this prevents mending with cameraDirection, a global variable of the free camera class.
  81. actualVelocity = velocity.add(this.getScene().gravity);
  82. }
  83. this.getScene().collisionCoordinator.getNewPosition(this._oldPosition, actualVelocity, this._collider, 3, null, this._onCollisionPositionChange, this.uniqueId);
  84. };
  85. ComposableCamera.prototype._checkInputs = function () {
  86. if (!this._localDirection) {
  87. this._localDirection = BABYLON.Vector3.Zero();
  88. this._transformedDirection = BABYLON.Vector3.Zero();
  89. }
  90. this.inputs.checkInputs();
  91. _super.prototype._checkInputs.call(this);
  92. };
  93. ComposableCamera.prototype._decideIfNeedsToMove = function () {
  94. return this._needMoveForGravity || Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  95. };
  96. ComposableCamera.prototype._updatePosition = function () {
  97. if (this.checkCollisions && this.getScene().collisionsEnabled) {
  98. this._collideWithWorld(this.cameraDirection);
  99. }
  100. else {
  101. this.position.addInPlace(this.cameraDirection);
  102. }
  103. };
  104. ComposableCamera.prototype.dispose = function () {
  105. this.inputs.clear();
  106. _super.prototype.dispose.call(this);
  107. };
  108. ComposableCamera.prototype.getTypeName = function () {
  109. return "FreeCamera";
  110. };
  111. __decorate([
  112. BABYLON.serializeAsVector3()
  113. ], ComposableCamera.prototype, "ellipsoid", void 0);
  114. __decorate([
  115. BABYLON.serialize()
  116. ], ComposableCamera.prototype, "checkCollisions", void 0);
  117. __decorate([
  118. BABYLON.serialize()
  119. ], ComposableCamera.prototype, "applyGravity", void 0);
  120. return ComposableCamera;
  121. }(BABYLON.TargetCamera));
  122. BABYLON.ComposableCamera = ComposableCamera;
  123. })(BABYLON || (BABYLON = {}));