babylon.targetCamera.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. return this.speed * ((BABYLON.Tools.GetDeltaTime() / (BABYLON.Tools.GetFps() * 10.0)));
  68. };
  69. // Target
  70. TargetCamera.prototype.setTarget = function (target) {
  71. this.upVector.normalize();
  72. BABYLON.Matrix.LookAtLHToRef(this.position, target, this.upVector, this._camMatrix);
  73. this._camMatrix.invert();
  74. this.rotation.x = Math.atan(this._camMatrix.m[6] / this._camMatrix.m[10]);
  75. var vDir = target.subtract(this.position);
  76. if (vDir.x >= 0.0) {
  77. this.rotation.y = (-Math.atan(vDir.z / vDir.x) + Math.PI / 2.0);
  78. } else {
  79. this.rotation.y = (-Math.atan(vDir.z / vDir.x) - Math.PI / 2.0);
  80. }
  81. this.rotation.z = -Math.acos(BABYLON.Vector3.Dot(new BABYLON.Vector3(0, 1.0, 0), this.upVector));
  82. if (isNaN(this.rotation.x)) {
  83. this.rotation.x = 0;
  84. }
  85. if (isNaN(this.rotation.y)) {
  86. this.rotation.y = 0;
  87. }
  88. if (isNaN(this.rotation.z)) {
  89. this.rotation.z = 0;
  90. }
  91. };
  92. TargetCamera.prototype.getTarget = function () {
  93. return this._currentTarget;
  94. };
  95. TargetCamera.prototype._decideIfNeedsToMove = function () {
  96. return Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  97. };
  98. TargetCamera.prototype._updatePosition = function () {
  99. this.position.addInPlace(this.cameraDirection);
  100. };
  101. TargetCamera.prototype._update = function () {
  102. var needToMove = this._decideIfNeedsToMove();
  103. var needToRotate = Math.abs(this.cameraRotation.x) > 0 || Math.abs(this.cameraRotation.y) > 0;
  104. // Move
  105. if (needToMove) {
  106. this._updatePosition();
  107. }
  108. // Rotate
  109. if (needToRotate) {
  110. this.rotation.x += this.cameraRotation.x;
  111. this.rotation.y += this.cameraRotation.y;
  112. if (!this.noRotationConstraint) {
  113. var limit = (Math.PI / 2) * 0.95;
  114. if (this.rotation.x > limit)
  115. this.rotation.x = limit;
  116. if (this.rotation.x < -limit)
  117. this.rotation.x = -limit;
  118. }
  119. }
  120. // Inertia
  121. if (needToMove) {
  122. if (Math.abs(this.cameraDirection.x) < BABYLON.Engine.Epsilon) {
  123. this.cameraDirection.x = 0;
  124. }
  125. if (Math.abs(this.cameraDirection.y) < BABYLON.Engine.Epsilon) {
  126. this.cameraDirection.y = 0;
  127. }
  128. if (Math.abs(this.cameraDirection.z) < BABYLON.Engine.Epsilon) {
  129. this.cameraDirection.z = 0;
  130. }
  131. this.cameraDirection.scaleInPlace(this.inertia);
  132. }
  133. if (needToRotate) {
  134. if (Math.abs(this.cameraRotation.x) < BABYLON.Engine.Epsilon) {
  135. this.cameraRotation.x = 0;
  136. }
  137. if (Math.abs(this.cameraRotation.y) < BABYLON.Engine.Epsilon) {
  138. this.cameraRotation.y = 0;
  139. }
  140. this.cameraRotation.scaleInPlace(this.inertia);
  141. }
  142. };
  143. TargetCamera.prototype._getViewMatrix = function () {
  144. if (!this.lockedTarget) {
  145. // Compute
  146. if (this.upVector.x != 0 || this.upVector.y != 1.0 || this.upVector.z != 0) {
  147. BABYLON.Matrix.LookAtLHToRef(BABYLON.Vector3.Zero(), this._referencePoint, this.upVector, this._lookAtTemp);
  148. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  149. this._lookAtTemp.multiplyToRef(this._cameraRotationMatrix, this._tempMatrix);
  150. this._lookAtTemp.invert();
  151. this._tempMatrix.multiplyToRef(this._lookAtTemp, this._cameraRotationMatrix);
  152. } else {
  153. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  154. }
  155. BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
  156. // Computing target and final matrix
  157. this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
  158. } else {
  159. this._currentTarget.copyFrom(this._getLockedTargetPosition());
  160. }
  161. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
  162. return this._viewMatrix;
  163. };
  164. return TargetCamera;
  165. })(BABYLON.Camera);
  166. BABYLON.TargetCamera = TargetCamera;
  167. })(BABYLON || (BABYLON = {}));
  168. //# sourceMappingURL=babylon.targetCamera.js.map