babylon.targetCamera.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 TargetCamera = (function (_super) {
  15. __extends(TargetCamera, _super);
  16. function TargetCamera(name, position, scene) {
  17. _super.call(this, name, position, scene);
  18. this.cameraDirection = new BABYLON.Vector3(0, 0, 0);
  19. this.cameraRotation = new BABYLON.Vector2(0, 0);
  20. this.rotation = new BABYLON.Vector3(0, 0, 0);
  21. this.speed = 2.0;
  22. this.noRotationConstraint = false;
  23. this.lockedTarget = null;
  24. this._currentTarget = BABYLON.Vector3.Zero();
  25. this._viewMatrix = BABYLON.Matrix.Zero();
  26. this._camMatrix = BABYLON.Matrix.Zero();
  27. this._cameraTransformMatrix = BABYLON.Matrix.Zero();
  28. this._cameraRotationMatrix = BABYLON.Matrix.Zero();
  29. this._referencePoint = new BABYLON.Vector3(0, 0, 1);
  30. this._transformedReferencePoint = BABYLON.Vector3.Zero();
  31. this._lookAtTemp = BABYLON.Matrix.Zero();
  32. this._tempMatrix = BABYLON.Matrix.Zero();
  33. }
  34. TargetCamera.prototype.getFrontPosition = function (distance) {
  35. var direction = this.getTarget().subtract(this.position);
  36. direction.normalize();
  37. direction.scaleInPlace(distance);
  38. return this.globalPosition.add(direction);
  39. };
  40. TargetCamera.prototype._getLockedTargetPosition = function () {
  41. if (!this.lockedTarget) {
  42. return null;
  43. }
  44. return this.lockedTarget.position || this.lockedTarget;
  45. };
  46. // Cache
  47. TargetCamera.prototype._initCache = function () {
  48. _super.prototype._initCache.call(this);
  49. this._cache.lockedTarget = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  50. this._cache.rotation = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  51. };
  52. TargetCamera.prototype._updateCache = function (ignoreParentClass) {
  53. if (!ignoreParentClass) {
  54. _super.prototype._updateCache.call(this);
  55. }
  56. var lockedTargetPosition = this._getLockedTargetPosition();
  57. if (!lockedTargetPosition) {
  58. this._cache.lockedTarget = null;
  59. }
  60. else {
  61. if (!this._cache.lockedTarget) {
  62. this._cache.lockedTarget = lockedTargetPosition.clone();
  63. }
  64. else {
  65. this._cache.lockedTarget.copyFrom(lockedTargetPosition);
  66. }
  67. }
  68. this._cache.rotation.copyFrom(this.rotation);
  69. };
  70. // Synchronized
  71. TargetCamera.prototype._isSynchronizedViewMatrix = function () {
  72. if (!_super.prototype._isSynchronizedViewMatrix.call(this)) {
  73. return false;
  74. }
  75. var lockedTargetPosition = this._getLockedTargetPosition();
  76. return (this._cache.lockedTarget ? this._cache.lockedTarget.equals(lockedTargetPosition) : !lockedTargetPosition)
  77. && this._cache.rotation.equals(this.rotation);
  78. };
  79. // Methods
  80. TargetCamera.prototype._computeLocalCameraSpeed = function () {
  81. var engine = this.getEngine();
  82. return this.speed * ((engine.getDeltaTime() / (engine.getFps() * 10.0)));
  83. };
  84. // Target
  85. TargetCamera.prototype.setTarget = function (target) {
  86. this.upVector.normalize();
  87. BABYLON.Matrix.LookAtLHToRef(this.position, target, this.upVector, this._camMatrix);
  88. this._camMatrix.invert();
  89. this.rotation.x = Math.atan(this._camMatrix.m[6] / this._camMatrix.m[10]);
  90. var vDir = target.subtract(this.position);
  91. if (vDir.x >= 0.0) {
  92. this.rotation.y = (-Math.atan(vDir.z / vDir.x) + Math.PI / 2.0);
  93. }
  94. else {
  95. this.rotation.y = (-Math.atan(vDir.z / vDir.x) - Math.PI / 2.0);
  96. }
  97. this.rotation.z = -Math.acos(BABYLON.Vector3.Dot(new BABYLON.Vector3(0, 1.0, 0), this.upVector));
  98. if (isNaN(this.rotation.x)) {
  99. this.rotation.x = 0;
  100. }
  101. if (isNaN(this.rotation.y)) {
  102. this.rotation.y = 0;
  103. }
  104. if (isNaN(this.rotation.z)) {
  105. this.rotation.z = 0;
  106. }
  107. };
  108. TargetCamera.prototype.getTarget = function () {
  109. return this._currentTarget;
  110. };
  111. TargetCamera.prototype._decideIfNeedsToMove = function () {
  112. return Math.abs(this.cameraDirection.x) > 0 || Math.abs(this.cameraDirection.y) > 0 || Math.abs(this.cameraDirection.z) > 0;
  113. };
  114. TargetCamera.prototype._updatePosition = function () {
  115. this.position.addInPlace(this.cameraDirection);
  116. };
  117. TargetCamera.prototype._checkInputs = function () {
  118. var needToMove = this._decideIfNeedsToMove();
  119. var needToRotate = Math.abs(this.cameraRotation.x) > 0 || Math.abs(this.cameraRotation.y) > 0;
  120. // Move
  121. if (needToMove) {
  122. this._updatePosition();
  123. }
  124. // Rotate
  125. if (needToRotate) {
  126. this.rotation.x += this.cameraRotation.x;
  127. this.rotation.y += this.cameraRotation.y;
  128. if (!this.noRotationConstraint) {
  129. var limit = (Math.PI / 2) * 0.95;
  130. if (this.rotation.x > limit)
  131. this.rotation.x = limit;
  132. if (this.rotation.x < -limit)
  133. this.rotation.x = -limit;
  134. }
  135. }
  136. // Inertia
  137. if (needToMove) {
  138. if (Math.abs(this.cameraDirection.x) < BABYLON.Epsilon) {
  139. this.cameraDirection.x = 0;
  140. }
  141. if (Math.abs(this.cameraDirection.y) < BABYLON.Epsilon) {
  142. this.cameraDirection.y = 0;
  143. }
  144. if (Math.abs(this.cameraDirection.z) < BABYLON.Epsilon) {
  145. this.cameraDirection.z = 0;
  146. }
  147. this.cameraDirection.scaleInPlace(this.inertia);
  148. }
  149. if (needToRotate) {
  150. if (Math.abs(this.cameraRotation.x) < BABYLON.Epsilon) {
  151. this.cameraRotation.x = 0;
  152. }
  153. if (Math.abs(this.cameraRotation.y) < BABYLON.Epsilon) {
  154. this.cameraRotation.y = 0;
  155. }
  156. this.cameraRotation.scaleInPlace(this.inertia);
  157. }
  158. _super.prototype._checkInputs.call(this);
  159. };
  160. TargetCamera.prototype._getViewMatrix = function () {
  161. if (!this.lockedTarget) {
  162. // Compute
  163. if (this.upVector.x !== 0 || this.upVector.y !== 1.0 || this.upVector.z !== 0) {
  164. BABYLON.Matrix.LookAtLHToRef(BABYLON.Vector3.Zero(), this._referencePoint, this.upVector, this._lookAtTemp);
  165. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  166. this._lookAtTemp.multiplyToRef(this._cameraRotationMatrix, this._tempMatrix);
  167. this._lookAtTemp.invert();
  168. this._tempMatrix.multiplyToRef(this._lookAtTemp, this._cameraRotationMatrix);
  169. }
  170. else {
  171. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  172. }
  173. BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
  174. // Computing target and final matrix
  175. this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
  176. }
  177. else {
  178. this._currentTarget.copyFrom(this._getLockedTargetPosition());
  179. }
  180. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
  181. return this._viewMatrix;
  182. };
  183. TargetCamera.prototype._getVRViewMatrix = function () {
  184. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  185. BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
  186. BABYLON.Vector3.TransformNormalToRef(this.upVector, this._cameraRotationMatrix, this._cameraRigParams.vrActualUp);
  187. // Computing target and final matrix
  188. this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
  189. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this._cameraRigParams.vrActualUp, this._cameraRigParams.vrWorkMatrix);
  190. this._cameraRigParams.vrWorkMatrix.multiplyToRef(this._cameraRigParams.vrPreViewMatrix, this._viewMatrix);
  191. return this._viewMatrix;
  192. };
  193. /**
  194. * @override
  195. * Override Camera.createRigCamera
  196. */
  197. TargetCamera.prototype.createRigCamera = function (name, cameraIndex) {
  198. if (this.cameraRigMode !== BABYLON.Camera.RIG_MODE_NONE) {
  199. var rigCamera = new TargetCamera(name, this.position.clone(), this.getScene());
  200. if (this.cameraRigMode === BABYLON.Camera.RIG_MODE_VR) {
  201. rigCamera._cameraRigParams = {};
  202. rigCamera._cameraRigParams.vrActualUp = new BABYLON.Vector3(0, 0, 0);
  203. rigCamera._getViewMatrix = rigCamera._getVRViewMatrix;
  204. }
  205. return rigCamera;
  206. }
  207. return null;
  208. };
  209. /**
  210. * @override
  211. * Override Camera._updateRigCameras
  212. */
  213. TargetCamera.prototype._updateRigCameras = function () {
  214. switch (this.cameraRigMode) {
  215. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  216. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  217. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  218. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  219. case BABYLON.Camera.RIG_MODE_VR:
  220. var camLeft = this._rigCameras[0];
  221. var camRight = this._rigCameras[1];
  222. if (this.cameraRigMode === BABYLON.Camera.RIG_MODE_VR) {
  223. camLeft.rotation.x = camRight.rotation.x = this.rotation.x;
  224. camLeft.rotation.y = camRight.rotation.y = this.rotation.y;
  225. camLeft.rotation.z = camRight.rotation.z = this.rotation.z;
  226. camLeft.position.copyFrom(this.position);
  227. camRight.position.copyFrom(this.position);
  228. }
  229. else {
  230. //provisionnaly using _cameraRigParams.stereoHalfAngle instead of calculations based on _cameraRigParams.interaxialDistance:
  231. this._getRigCamPosition(-this._cameraRigParams.stereoHalfAngle, camLeft.position);
  232. this._getRigCamPosition(this._cameraRigParams.stereoHalfAngle, camRight.position);
  233. camLeft.setTarget(this.getTarget());
  234. camRight.setTarget(this.getTarget());
  235. }
  236. break;
  237. }
  238. _super.prototype._updateRigCameras.call(this);
  239. };
  240. TargetCamera.prototype._getRigCamPosition = function (halfSpace, result) {
  241. if (!this._rigCamTransformMatrix) {
  242. this._rigCamTransformMatrix = new BABYLON.Matrix();
  243. }
  244. var target = this.getTarget();
  245. BABYLON.Matrix.Translation(-target.x, -target.y, -target.z).multiplyToRef(BABYLON.Matrix.RotationY(halfSpace), this._rigCamTransformMatrix);
  246. this._rigCamTransformMatrix = this._rigCamTransformMatrix.multiply(BABYLON.Matrix.Translation(target.x, target.y, target.z));
  247. BABYLON.Vector3.TransformCoordinatesToRef(this.position, this._rigCamTransformMatrix, result);
  248. };
  249. TargetCamera.prototype.getTypeName = function () {
  250. return "TargetCamera";
  251. };
  252. __decorate([
  253. BABYLON.serializeAsVector3()
  254. ], TargetCamera.prototype, "rotation", void 0);
  255. __decorate([
  256. BABYLON.serialize()
  257. ], TargetCamera.prototype, "speed", void 0);
  258. __decorate([
  259. BABYLON.serializeAsMeshReference("lockedTargetId")
  260. ], TargetCamera.prototype, "lockedTarget", void 0);
  261. return TargetCamera;
  262. })(BABYLON.Camera);
  263. BABYLON.TargetCamera = TargetCamera;
  264. })(BABYLON || (BABYLON = {}));