babylon.targetCamera.js 8.1 KB

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