babylon.followCamera.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 FollowCamera = (function (_super) {
  15. __extends(FollowCamera, _super);
  16. function FollowCamera(name, position, scene, target) {
  17. _super.call(this, name, position, scene);
  18. this.radius = 12;
  19. this.rotationOffset = 0;
  20. this.heightOffset = 4;
  21. this.cameraAcceleration = 0.05;
  22. this.maxCameraSpeed = 20;
  23. this.target = target;
  24. }
  25. FollowCamera.prototype.getRadians = function (degrees) {
  26. return degrees * Math.PI / 180;
  27. };
  28. FollowCamera.prototype.follow = function (cameraTarget) {
  29. if (!cameraTarget)
  30. return;
  31. var yRotation;
  32. if (cameraTarget.rotationQuaternion) {
  33. var rotMatrix = new BABYLON.Matrix();
  34. cameraTarget.rotationQuaternion.toRotationMatrix(rotMatrix);
  35. yRotation = Math.atan2(rotMatrix.m[8], rotMatrix.m[10]);
  36. }
  37. else {
  38. yRotation = cameraTarget.rotation.y;
  39. }
  40. var radians = this.getRadians(this.rotationOffset) + yRotation;
  41. var targetX = cameraTarget.position.x + Math.sin(radians) * this.radius;
  42. var targetZ = cameraTarget.position.z + Math.cos(radians) * this.radius;
  43. var dx = targetX - this.position.x;
  44. var dy = (cameraTarget.position.y + this.heightOffset) - this.position.y;
  45. var dz = (targetZ) - this.position.z;
  46. var vx = dx * this.cameraAcceleration * 2; //this is set to .05
  47. var vy = dy * this.cameraAcceleration;
  48. var vz = dz * this.cameraAcceleration * 2;
  49. if (vx > this.maxCameraSpeed || vx < -this.maxCameraSpeed) {
  50. vx = vx < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
  51. }
  52. if (vy > this.maxCameraSpeed || vy < -this.maxCameraSpeed) {
  53. vy = vy < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
  54. }
  55. if (vz > this.maxCameraSpeed || vz < -this.maxCameraSpeed) {
  56. vz = vz < 1 ? -this.maxCameraSpeed : this.maxCameraSpeed;
  57. }
  58. this.position = new BABYLON.Vector3(this.position.x + vx, this.position.y + vy, this.position.z + vz);
  59. this.setTarget(cameraTarget.position);
  60. };
  61. FollowCamera.prototype._checkInputs = function () {
  62. _super.prototype._checkInputs.call(this);
  63. this.follow(this.target);
  64. };
  65. FollowCamera.prototype.getTypeName = function () {
  66. return "FollowCamera";
  67. };
  68. __decorate([
  69. BABYLON.serialize()
  70. ], FollowCamera.prototype, "radius", void 0);
  71. __decorate([
  72. BABYLON.serialize()
  73. ], FollowCamera.prototype, "rotationOffset", void 0);
  74. __decorate([
  75. BABYLON.serialize()
  76. ], FollowCamera.prototype, "heightOffset", void 0);
  77. __decorate([
  78. BABYLON.serialize()
  79. ], FollowCamera.prototype, "cameraAcceleration", void 0);
  80. __decorate([
  81. BABYLON.serialize()
  82. ], FollowCamera.prototype, "maxCameraSpeed", void 0);
  83. __decorate([
  84. BABYLON.serializeAsMeshReference("lockedTargetId")
  85. ], FollowCamera.prototype, "target", void 0);
  86. return FollowCamera;
  87. })(BABYLON.TargetCamera);
  88. BABYLON.FollowCamera = FollowCamera;
  89. var ArcFollowCamera = (function (_super) {
  90. __extends(ArcFollowCamera, _super);
  91. function ArcFollowCamera(name, alpha, beta, radius, target, scene) {
  92. _super.call(this, name, BABYLON.Vector3.Zero(), scene);
  93. this.alpha = alpha;
  94. this.beta = beta;
  95. this.radius = radius;
  96. this.target = target;
  97. this._cartesianCoordinates = BABYLON.Vector3.Zero();
  98. this.follow();
  99. }
  100. ArcFollowCamera.prototype.follow = function () {
  101. this._cartesianCoordinates.x = this.radius * Math.cos(this.alpha) * Math.cos(this.beta);
  102. this._cartesianCoordinates.y = this.radius * Math.sin(this.beta);
  103. this._cartesianCoordinates.z = this.radius * Math.sin(this.alpha) * Math.cos(this.beta);
  104. this.position = this.target.position.add(this._cartesianCoordinates);
  105. this.setTarget(this.target.position);
  106. };
  107. ArcFollowCamera.prototype._checkInputs = function () {
  108. _super.prototype._checkInputs.call(this);
  109. this.follow();
  110. };
  111. ArcFollowCamera.prototype.getTypeName = function () {
  112. return "ArcFollowCamera";
  113. };
  114. return ArcFollowCamera;
  115. })(BABYLON.TargetCamera);
  116. BABYLON.ArcFollowCamera = ArcFollowCamera;
  117. })(BABYLON || (BABYLON = {}));