PerspectiveOffCenterFrustum.js 14 KB

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