OrthographicFrustum.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*global define*/
  2. define([
  3. '../Core/Cartesian2',
  4. '../Core/Cartesian3',
  5. '../Core/Cartesian4',
  6. '../Core/defined',
  7. '../Core/defineProperties',
  8. '../Core/DeveloperError',
  9. '../Core/Matrix4',
  10. './CullingVolume'
  11. ], function(
  12. Cartesian2,
  13. Cartesian3,
  14. Cartesian4,
  15. defined,
  16. defineProperties,
  17. DeveloperError,
  18. Matrix4,
  19. CullingVolume) {
  20. "use strict";
  21. /**
  22. * The viewing frustum is defined by 6 planes.
  23. * Each plane is represented by a {@link Cartesian4} object, where the x, y, and z components
  24. * define the unit vector normal to the plane, and the w component is the distance of the
  25. * plane from the origin/camera position.
  26. *
  27. * @alias OrthographicFrustum
  28. * @constructor
  29. *
  30. * @example
  31. * var maxRadii = ellipsoid.maximumRadius;
  32. *
  33. * var frustum = new Cesium.OrthographicFrustum();
  34. * frustum.right = maxRadii * Cesium.Math.PI;
  35. * frustum.left = -c.frustum.right;
  36. * frustum.top = c.frustum.right * (canvas.clientHeight / canvas.clientWidth);
  37. * frustum.bottom = -c.frustum.top;
  38. * frustum.near = 0.01 * maxRadii;
  39. * frustum.far = 50.0 * maxRadii;
  40. */
  41. var OrthographicFrustum = function() {
  42. /**
  43. * The left clipping plane.
  44. * @type {Number}
  45. * @default undefined
  46. */
  47. this.left = undefined;
  48. this._left = undefined;
  49. /**
  50. * The right clipping plane.
  51. * @type {Number}
  52. * @default undefined
  53. */
  54. this.right = undefined;
  55. this._right = undefined;
  56. /**
  57. * The top clipping plane.
  58. * @type {Number}
  59. * @default undefined
  60. */
  61. this.top = undefined;
  62. this._top = undefined;
  63. /**
  64. * The bottom clipping plane.
  65. * @type {Number}
  66. * @default undefined
  67. */
  68. this.bottom = undefined;
  69. this._bottom = undefined;
  70. /**
  71. * The distance of the near plane.
  72. * @type {Number}
  73. * @default 1.0
  74. */
  75. this.near = 1.0;
  76. this._near = this.near;
  77. /**
  78. * The distance of the far plane.
  79. * @type {Number}
  80. * @default 500000000.0;
  81. */
  82. this.far = 500000000.0;
  83. this._far = this.far;
  84. this._cullingVolume = new CullingVolume();
  85. this._orthographicMatrix = new Matrix4();
  86. };
  87. function update(frustum) {
  88. //>>includeStart('debug', pragmas.debug);
  89. if (!defined(frustum.right) || !defined(frustum.left) ||
  90. !defined(frustum.top) || !defined(frustum.bottom) ||
  91. !defined(frustum.near) || !defined(frustum.far)) {
  92. throw new DeveloperError('right, left, top, bottom, near, or far parameters are not set.');
  93. }
  94. //>>includeEnd('debug');
  95. if (frustum.top !== frustum._top || frustum.bottom !== frustum._bottom ||
  96. frustum.left !== frustum._left || frustum.right !== frustum._right ||
  97. frustum.near !== frustum._near || frustum.far !== frustum._far) {
  98. //>>includeStart('debug', pragmas.debug);
  99. if (frustum.left > frustum.right) {
  100. throw new DeveloperError('right must be greater than left.');
  101. }
  102. if (frustum.bottom > frustum.top) {
  103. throw new DeveloperError('top must be greater than bottom.');
  104. }
  105. if (frustum.near <= 0 || frustum.near > frustum.far) {
  106. throw new DeveloperError('near must be greater than zero and less than far.');
  107. }
  108. //>>includeEnd('debug');
  109. frustum._left = frustum.left;
  110. frustum._right = frustum.right;
  111. frustum._top = frustum.top;
  112. frustum._bottom = frustum.bottom;
  113. frustum._near = frustum.near;
  114. frustum._far = frustum.far;
  115. frustum._orthographicMatrix = Matrix4.computeOrthographicOffCenter(frustum.left, frustum.right, frustum.bottom, frustum.top, frustum.near, frustum.far, frustum._orthographicMatrix);
  116. }
  117. }
  118. defineProperties(OrthographicFrustum.prototype, {
  119. /**
  120. * Gets the orthographic projection matrix computed from the view frustum.
  121. * @memberof OrthographicFrustum.prototype
  122. * @type {Matrix4}
  123. */
  124. projectionMatrix : {
  125. get : function() {
  126. update(this);
  127. return this._orthographicMatrix;
  128. }
  129. }
  130. });
  131. var getPlanesRight = new Cartesian3();
  132. var getPlanesNearCenter = new Cartesian3();
  133. var getPlanesPoint = new Cartesian3();
  134. var negateScratch = new Cartesian3();
  135. /**
  136. * Creates a culling volume for this frustum.
  137. *
  138. * @param {Cartesian3} position The eye position.
  139. * @param {Cartesian3} direction The view direction.
  140. * @param {Cartesian3} up The up direction.
  141. * @returns {CullingVolume} A culling volume at the given position and orientation.
  142. *
  143. * @example
  144. * // Check if a bounding volume intersects the frustum.
  145. * var cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
  146. * var intersect = cullingVolume.computeVisibility(boundingVolume);
  147. */
  148. OrthographicFrustum.prototype.computeCullingVolume = function(position, direction, up) {
  149. //>>includeStart('debug', pragmas.debug);
  150. if (!defined(position)) {
  151. throw new DeveloperError('position is required.');
  152. }
  153. if (!defined(direction)) {
  154. throw new DeveloperError('direction is required.');
  155. }
  156. if (!defined(up)) {
  157. throw new DeveloperError('up is required.');
  158. }
  159. //>>includeEnd('debug');
  160. var planes = this._cullingVolume.planes;
  161. var t = this.top;
  162. var b = this.bottom;
  163. var r = this.right;
  164. var l = this.left;
  165. var n = this.near;
  166. var f = this.far;
  167. var right = Cartesian3.cross(direction, up, getPlanesRight);
  168. var nearCenter = getPlanesNearCenter;
  169. Cartesian3.multiplyByScalar(direction, n, nearCenter);
  170. Cartesian3.add(position, nearCenter, nearCenter);
  171. var point = getPlanesPoint;
  172. // Left plane
  173. Cartesian3.multiplyByScalar(right, l, point);
  174. Cartesian3.add(nearCenter, point, point);
  175. var plane = planes[0];
  176. if (!defined(plane)) {
  177. plane = planes[0] = new Cartesian4();
  178. }
  179. plane.x = right.x;
  180. plane.y = right.y;
  181. plane.z = right.z;
  182. plane.w = -Cartesian3.dot(right, point);
  183. // Right plane
  184. Cartesian3.multiplyByScalar(right, r, point);
  185. Cartesian3.add(nearCenter, point, point);
  186. plane = planes[1];
  187. if (!defined(plane)) {
  188. plane = planes[1] = new Cartesian4();
  189. }
  190. plane.x = -right.x;
  191. plane.y = -right.y;
  192. plane.z = -right.z;
  193. plane.w = -Cartesian3.dot(Cartesian3.negate(right, negateScratch), point);
  194. // Bottom plane
  195. Cartesian3.multiplyByScalar(up, b, point);
  196. Cartesian3.add(nearCenter, point, point);
  197. plane = planes[2];
  198. if (!defined(plane)) {
  199. plane = planes[2] = new Cartesian4();
  200. }
  201. plane.x = up.x;
  202. plane.y = up.y;
  203. plane.z = up.z;
  204. plane.w = -Cartesian3.dot(up, point);
  205. // Top plane
  206. Cartesian3.multiplyByScalar(up, t, point);
  207. Cartesian3.add(nearCenter, point, point);
  208. plane = planes[3];
  209. if (!defined(plane)) {
  210. plane = planes[3] = new Cartesian4();
  211. }
  212. plane.x = -up.x;
  213. plane.y = -up.y;
  214. plane.z = -up.z;
  215. plane.w = -Cartesian3.dot(Cartesian3.negate(up, negateScratch), point);
  216. // Near plane
  217. plane = planes[4];
  218. if (!defined(plane)) {
  219. plane = planes[4] = new Cartesian4();
  220. }
  221. plane.x = direction.x;
  222. plane.y = direction.y;
  223. plane.z = direction.z;
  224. plane.w = -Cartesian3.dot(direction, nearCenter);
  225. // Far plane
  226. Cartesian3.multiplyByScalar(direction, f, point);
  227. Cartesian3.add(position, point, point);
  228. plane = planes[5];
  229. if (!defined(plane)) {
  230. plane = planes[5] = new Cartesian4();
  231. }
  232. plane.x = -direction.x;
  233. plane.y = -direction.y;
  234. plane.z = -direction.z;
  235. plane.w = -Cartesian3.dot(Cartesian3.negate(direction, negateScratch), point);
  236. return this._cullingVolume;
  237. };
  238. /**
  239. * Returns the pixel's width and height in meters.
  240. *
  241. * @param {Cartesian2} drawingBufferDimensions A {@link Cartesian2} with width and height in the x and y properties, respectively.
  242. * @param {Number} [distance=near plane distance] The distance to the near plane in meters.
  243. * @param {Cartesian2} [result] The object onto which to store the result.
  244. * @returns {Cartesian2} The modified result parameter or a new instance of {@link Cartesian2} with the pixel's width and height in the x and y properties, respectively.
  245. *
  246. * @exception {DeveloperError} drawingBufferDimensions.x must be greater than zero.
  247. * @exception {DeveloperError} drawingBufferDimensions.y must be greater than zero.
  248. *
  249. * @example
  250. * // Example 1
  251. * // Get the width and height of a pixel.
  252. * var pixelSize = camera.frustum.getPixelSize(new Cesium.Cartesian2(canvas.clientWidth, canvas.clientHeight));
  253. */
  254. OrthographicFrustum.prototype.getPixelSize = function(drawingBufferDimensions, distance, result) {
  255. update(this);
  256. //>>includeStart('debug', pragmas.debug);
  257. if (!defined(drawingBufferDimensions)) {
  258. throw new DeveloperError('drawingBufferDimensions is required.');
  259. }
  260. if (drawingBufferDimensions.x <= 0) {
  261. throw new DeveloperError('drawingBufferDimensions.x must be greater than zero.');
  262. }
  263. if (drawingBufferDimensions.y <= 0) {
  264. throw new DeveloperError('drawingBufferDimensions.y must be greater than zero.');
  265. }
  266. //>>includeEnd('debug');
  267. var frustumWidth = this.right - this.left;
  268. var frustumHeight = this.top - this.bottom;
  269. var pixelWidth = frustumWidth / drawingBufferDimensions.x;
  270. var pixelHeight = frustumHeight / drawingBufferDimensions.y;
  271. if (!defined(result)) {
  272. return new Cartesian2(pixelWidth, pixelHeight);
  273. }
  274. result.x = pixelWidth;
  275. result.y = pixelHeight;
  276. return result;
  277. };
  278. /**
  279. * Returns a duplicate of a OrthographicFrustum instance.
  280. *
  281. * @param {OrthographicFrustum} [result] The object onto which to store the result.
  282. * @returns {OrthographicFrustum} The modified result parameter or a new PerspectiveFrustum instance if one was not provided.
  283. */
  284. OrthographicFrustum.prototype.clone = function(result) {
  285. if (!defined(result)) {
  286. result = new OrthographicFrustum();
  287. }
  288. result.left = this.left;
  289. result.right = this.right;
  290. result.top = this.top;
  291. result.bottom = this.bottom;
  292. result.near = this.near;
  293. result.far = this.far;
  294. // force update of clone to compute matrices
  295. result._left = undefined;
  296. result._right = undefined;
  297. result._top = undefined;
  298. result._bottom = undefined;
  299. result._near = undefined;
  300. result._far = undefined;
  301. return result;
  302. };
  303. /**
  304. * Compares the provided OrthographicFrustum componentwise and returns
  305. * <code>true</code> if they are equal, <code>false</code> otherwise.
  306. *
  307. * @param {OrthographicFrustum} [other] The right hand side OrthographicFrustum.
  308. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  309. */
  310. OrthographicFrustum.prototype.equals = function(other) {
  311. return (defined(other) &&
  312. this.right === other.right &&
  313. this.left === other.left &&
  314. this.top === other.top &&
  315. this.bottom === other.bottom &&
  316. this.near === other.near &&
  317. this.far === other.far);
  318. };
  319. return OrthographicFrustum;
  320. });