babylon.targetCamera.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. var __extends = 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. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var TargetCamera = (function (_super) {
  10. __extends(TargetCamera, _super);
  11. function TargetCamera(name, position, scene) {
  12. _super.call(this, name, position, scene);
  13. this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  14. this.cameraRotation = new BABYLON.Vector2(0, 0);
  15. this.rotation = new BABYLON.Vector3(0, 0, 0);
  16. this.speed = 2.0;
  17. this.noRotationConstraint = false;
  18. this.lockedTarget = null;
  19. this._currentTarget = BABYLON.Vector3.Zero();
  20. this._viewMatrix = BABYLON.Matrix.Zero();
  21. this._camMatrix = BABYLON.Matrix.Zero();
  22. this._cameraTransformMatrix = BABYLON.Matrix.Zero();
  23. this._cameraRotationMatrix = BABYLON.Matrix.Zero();
  24. this._referencePoint = new BABYLON.Vector3(0, 0, 1);
  25. this._transformedReferencePoint = BABYLON.Vector3.Zero();
  26. this._lookAtTemp = BABYLON.Matrix.Zero();
  27. this._tempMatrix = BABYLON.Matrix.Zero();
  28. }
  29. TargetCamera.prototype._getLockedTargetPosition = function () {
  30. if (!this.lockedTarget) {
  31. return null;
  32. }
  33. return this.lockedTarget.position || this.lockedTarget;
  34. };
  35. // Cache
  36. TargetCamera.prototype._initCache = function () {
  37. _super.prototype._initCache.call(this);
  38. this._cache.lockedTarget = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  39. this._cache.rotation = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  40. };
  41. TargetCamera.prototype._updateCache = function (ignoreParentClass) {
  42. if (!ignoreParentClass) {
  43. _super.prototype._updateCache.call(this);
  44. }
  45. var lockedTargetPosition = this._getLockedTargetPosition();
  46. if (!lockedTargetPosition) {
  47. this._cache.lockedTarget = null;
  48. } else {
  49. if (!this._cache.lockedTarget) {
  50. this._cache.lockedTarget = lockedTargetPosition.clone();
  51. } else {
  52. this._cache.lockedTarget.copyFrom(lockedTargetPosition);
  53. }
  54. }
  55. this._cache.rotation.copyFrom(this.rotation);
  56. };
  57. // Synchronized
  58. TargetCamera.prototype._isSynchronizedViewMatrix = function () {
  59. if (!_super.prototype._isSynchronizedViewMatrix.call(this)) {
  60. return false;
  61. }
  62. var lockedTargetPosition = this._getLockedTargetPosition();
  63. return (this._cache.lockedTarget ? this._cache.lockedTarget.equals(lockedTargetPosition) : !lockedTargetPosition) && this._cache.rotation.equals(this.rotation);
  64. };
  65. // Methods
  66. TargetCamera.prototype._computeLocalCameraSpeed = function () {
  67. var engine = this.getEngine();
  68. return this.speed * ((engine.getDeltaTime() / (engine.getFps() * 10.0)));
  69. };
  70. // Target
  71. TargetCamera.prototype.setTarget = function (target) {
  72. this.upVector.normalize();
  73. BABYLON.Matrix.LookAtLHToRef(this.position, target, this.upVector, this._camMatrix);
  74. this._camMatrix.invert();
  75. this.rotation.x = Math.atan(this._camMatrix.m[6] / this._camMatrix.m[10]);
  76. var vDir = target.subtract(this.position);
  77. if (vDir.x >= 0.0) {
  78. this.rotation.y = (-Math.atan(vDir.z / vDir.x) + Math.PI / 2.0);
  79. } else {
  80. this.rotation.y = (-Math.atan(vDir.z / vDir.x) - Math.PI / 2.0);
  81. }
  82. this.rotation.z = -Math.acos(BABYLON.Vector3.Dot(new BABYLON.Vector3(0, 1.0, 0), this.upVector));
  83. if (isNaN(this.rotation.x)) {
  84. this.rotation.x = 0;
  85. }
  86. if (isNaN(this.rotation.y)) {
  87. this.rotation.y = 0;
  88. }
  89. if (isNaN(this.rotation.z)) {
  90. this.rotation.z = 0;
  91. }
  92. };
  93. TargetCamera.prototype.getTarget = function () {
  94. return this._currentTarget;
  95. };
  96. TargetCamera.prototype._decideIfNeedsToMove = function () {
  97. return Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  98. };
  99. TargetCamera.prototype._updatePosition = function () {
  100. this.position.addInPlace(this.cameraDirection);
  101. };
  102. TargetCamera.prototype._update = function () {
  103. var needToMove = this._decideIfNeedsToMove();
  104. var needToRotate = Math.abs(this.cameraRotation.x) > 0 || Math.abs(this.cameraRotation.y) > 0;
  105. // Move
  106. if (needToMove) {
  107. this._updatePosition();
  108. }
  109. // Rotate
  110. if (needToRotate) {
  111. this.rotation.x += this.cameraRotation.x;
  112. this.rotation.y += this.cameraRotation.y;
  113. if (!this.noRotationConstraint) {
  114. var limit = (Math.PI / 2) * 0.95;
  115. if (this.rotation.x > limit)
  116. this.rotation.x = limit;
  117. if (this.rotation.x < -limit)
  118. this.rotation.x = -limit;
  119. }
  120. }
  121. // Inertia
  122. if (needToMove) {
  123. if (Math.abs(this.cameraDirection.x) < BABYLON.Engine.Epsilon) {
  124. this.cameraDirection.x = 0;
  125. }
  126. if (Math.abs(this.cameraDirection.y) < BABYLON.Engine.Epsilon) {
  127. this.cameraDirection.y = 0;
  128. }
  129. if (Math.abs(this.cameraDirection.z) < BABYLON.Engine.Epsilon) {
  130. this.cameraDirection.z = 0;
  131. }
  132. this.cameraDirection.scaleInPlace(this.inertia);
  133. }
  134. if (needToRotate) {
  135. if (Math.abs(this.cameraRotation.x) < BABYLON.Engine.Epsilon) {
  136. this.cameraRotation.x = 0;
  137. }
  138. if (Math.abs(this.cameraRotation.y) < BABYLON.Engine.Epsilon) {
  139. this.cameraRotation.y = 0;
  140. }
  141. this.cameraRotation.scaleInPlace(this.inertia);
  142. }
  143. };
  144. TargetCamera.prototype._getViewMatrix = function () {
  145. if (!this.lockedTarget) {
  146. // Compute
  147. if (this.upVector.x != 0 || this.upVector.y != 1.0 || this.upVector.z != 0) {
  148. BABYLON.Matrix.LookAtLHToRef(BABYLON.Vector3.Zero(), this._referencePoint, this.upVector, this._lookAtTemp);
  149. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  150. this._lookAtTemp.multiplyToRef(this._cameraRotationMatrix, this._tempMatrix);
  151. this._lookAtTemp.invert();
  152. this._tempMatrix.multiplyToRef(this._lookAtTemp, this._cameraRotationMatrix);
  153. } else {
  154. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  155. }
  156. BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
  157. // Computing target and final matrix
  158. this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
  159. } else {
  160. this._currentTarget.copyFrom(this._getLockedTargetPosition());
  161. }
  162. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
  163. return this._viewMatrix;
  164. };
  165. return TargetCamera;
  166. })(BABYLON.Camera);
  167. BABYLON.TargetCamera = TargetCamera;
  168. })(BABYLON || (BABYLON = {}));
  169. //# sourceMappingURL=babylon.targetCamera.js.map