PerspectiveFrustum.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import Check from "./Check.js";
  2. import defaultValue from "./defaultValue.js";
  3. import defined from "./defined.js";
  4. import DeveloperError from "./DeveloperError.js";
  5. import CesiumMath from "./Math.js";
  6. import PerspectiveOffCenterFrustum from "./PerspectiveOffCenterFrustum.js";
  7. /**
  8. * The viewing frustum is defined by 6 planes.
  9. * Each plane is represented by a {@link Cartesian4} object, where the x, y, and z components
  10. * define the unit vector normal to the plane, and the w component is the distance of the
  11. * plane from the origin/camera position.
  12. *
  13. * @alias PerspectiveFrustum
  14. * @constructor
  15. *
  16. * @param {Object} [options] An object with the following properties:
  17. * @param {Number} [options.fov] The angle of the field of view (FOV), in radians.
  18. * @param {Number} [options.aspectRatio] The aspect ratio of the frustum's width to it's height.
  19. * @param {Number} [options.near=1.0] The distance of the near plane.
  20. * @param {Number} [options.far=500000000.0] The distance of the far plane.
  21. * @param {Number} [options.xOffset=0.0] The offset in the x direction.
  22. * @param {Number} [options.yOffset=0.0] The offset in the y direction.
  23. *
  24. * @example
  25. * const frustum = new Cesium.PerspectiveFrustum({
  26. * fov : Cesium.Math.PI_OVER_THREE,
  27. * aspectRatio : canvas.clientWidth / canvas.clientHeight
  28. * near : 1.0,
  29. * far : 1000.0
  30. * });
  31. *
  32. * @see PerspectiveOffCenterFrustum
  33. */
  34. function PerspectiveFrustum(options) {
  35. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  36. this._offCenterFrustum = new PerspectiveOffCenterFrustum();
  37. /**
  38. * The angle of the field of view (FOV), in radians. This angle will be used
  39. * as the horizontal FOV if the width is greater than the height, otherwise
  40. * it will be the vertical FOV.
  41. * @type {Number}
  42. * @default undefined
  43. */
  44. this.fov = options.fov;
  45. this._fov = undefined;
  46. this._fovy = undefined;
  47. this._sseDenominator = undefined;
  48. /**
  49. * The aspect ratio of the frustum's width to it's height.
  50. * @type {Number}
  51. * @default undefined
  52. */
  53. this.aspectRatio = options.aspectRatio;
  54. this._aspectRatio = undefined;
  55. /**
  56. * The distance of the near plane.
  57. * @type {Number}
  58. * @default 1.0
  59. */
  60. this.near = defaultValue(options.near, 1.0);
  61. this._near = this.near;
  62. /**
  63. * The distance of the far plane.
  64. * @type {Number}
  65. * @default 500000000.0
  66. */
  67. this.far = defaultValue(options.far, 500000000.0);
  68. this._far = this.far;
  69. /**
  70. * Offsets the frustum in the x direction.
  71. * @type {Number}
  72. * @default 0.0
  73. */
  74. this.xOffset = defaultValue(options.xOffset, 0.0);
  75. this._xOffset = this.xOffset;
  76. /**
  77. * Offsets the frustum in the y direction.
  78. * @type {Number}
  79. * @default 0.0
  80. */
  81. this.yOffset = defaultValue(options.yOffset, 0.0);
  82. this._yOffset = this.yOffset;
  83. }
  84. /**
  85. * The number of elements used to pack the object into an array.
  86. * @type {Number}
  87. */
  88. PerspectiveFrustum.packedLength = 6;
  89. /**
  90. * Stores the provided instance into the provided array.
  91. *
  92. * @param {PerspectiveFrustum} value The value to pack.
  93. * @param {Number[]} array The array to pack into.
  94. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  95. *
  96. * @returns {Number[]} The array that was packed into
  97. */
  98. PerspectiveFrustum.pack = function (value, array, startingIndex) {
  99. //>>includeStart('debug', pragmas.debug);
  100. Check.typeOf.object("value", value);
  101. Check.defined("array", array);
  102. //>>includeEnd('debug');
  103. startingIndex = defaultValue(startingIndex, 0);
  104. array[startingIndex++] = value.fov;
  105. array[startingIndex++] = value.aspectRatio;
  106. array[startingIndex++] = value.near;
  107. array[startingIndex++] = value.far;
  108. array[startingIndex++] = value.xOffset;
  109. array[startingIndex] = value.yOffset;
  110. return array;
  111. };
  112. /**
  113. * Retrieves an instance from a packed array.
  114. *
  115. * @param {Number[]} array The packed array.
  116. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  117. * @param {PerspectiveFrustum} [result] The object into which to store the result.
  118. * @returns {PerspectiveFrustum} The modified result parameter or a new PerspectiveFrustum instance if one was not provided.
  119. */
  120. PerspectiveFrustum.unpack = function (array, startingIndex, result) {
  121. //>>includeStart('debug', pragmas.debug);
  122. Check.defined("array", array);
  123. //>>includeEnd('debug');
  124. startingIndex = defaultValue(startingIndex, 0);
  125. if (!defined(result)) {
  126. result = new PerspectiveFrustum();
  127. }
  128. result.fov = array[startingIndex++];
  129. result.aspectRatio = array[startingIndex++];
  130. result.near = array[startingIndex++];
  131. result.far = array[startingIndex++];
  132. result.xOffset = array[startingIndex++];
  133. result.yOffset = array[startingIndex];
  134. return result;
  135. };
  136. function update(frustum) {
  137. //>>includeStart('debug', pragmas.debug);
  138. if (
  139. !defined(frustum.fov) ||
  140. !defined(frustum.aspectRatio) ||
  141. !defined(frustum.near) ||
  142. !defined(frustum.far)
  143. ) {
  144. throw new DeveloperError(
  145. "fov, aspectRatio, near, or far parameters are not set."
  146. );
  147. }
  148. //>>includeEnd('debug');
  149. const f = frustum._offCenterFrustum;
  150. if (
  151. frustum.fov !== frustum._fov ||
  152. frustum.aspectRatio !== frustum._aspectRatio ||
  153. frustum.near !== frustum._near ||
  154. frustum.far !== frustum._far ||
  155. frustum.xOffset !== frustum._xOffset ||
  156. frustum.yOffset !== frustum._yOffset
  157. ) {
  158. //>>includeStart('debug', pragmas.debug);
  159. if (frustum.fov < 0 || frustum.fov >= Math.PI) {
  160. throw new DeveloperError("fov must be in the range [0, PI).");
  161. }
  162. if (frustum.aspectRatio < 0) {
  163. throw new DeveloperError("aspectRatio must be positive.");
  164. }
  165. if (frustum.near < 0 || frustum.near > frustum.far) {
  166. throw new DeveloperError(
  167. "near must be greater than zero and less than far."
  168. );
  169. }
  170. //>>includeEnd('debug');
  171. frustum._aspectRatio = frustum.aspectRatio;
  172. frustum._fov = frustum.fov;
  173. frustum._fovy =
  174. frustum.aspectRatio <= 1
  175. ? frustum.fov
  176. : Math.atan(Math.tan(frustum.fov * 0.5) / frustum.aspectRatio) * 2.0;
  177. frustum._near = frustum.near;
  178. frustum._far = frustum.far;
  179. frustum._sseDenominator = 2.0 * Math.tan(0.5 * frustum._fovy);
  180. frustum._xOffset = frustum.xOffset;
  181. frustum._yOffset = frustum.yOffset;
  182. f.top = frustum.near * Math.tan(0.5 * frustum._fovy);
  183. f.bottom = -f.top;
  184. f.right = frustum.aspectRatio * f.top;
  185. f.left = -f.right;
  186. f.near = frustum.near;
  187. f.far = frustum.far;
  188. f.right += frustum.xOffset;
  189. f.left += frustum.xOffset;
  190. f.top += frustum.yOffset;
  191. f.bottom += frustum.yOffset;
  192. }
  193. }
  194. Object.defineProperties(PerspectiveFrustum.prototype, {
  195. /**
  196. * Gets the perspective projection matrix computed from the view frustum.
  197. * @memberof PerspectiveFrustum.prototype
  198. * @type {Matrix4}
  199. * @readonly
  200. *
  201. * @see PerspectiveFrustum#infiniteProjectionMatrix
  202. */
  203. projectionMatrix: {
  204. get: function () {
  205. update(this);
  206. return this._offCenterFrustum.projectionMatrix;
  207. },
  208. },
  209. /**
  210. * The perspective projection matrix computed from the view frustum with an infinite far plane.
  211. * @memberof PerspectiveFrustum.prototype
  212. * @type {Matrix4}
  213. * @readonly
  214. *
  215. * @see PerspectiveFrustum#projectionMatrix
  216. */
  217. infiniteProjectionMatrix: {
  218. get: function () {
  219. update(this);
  220. return this._offCenterFrustum.infiniteProjectionMatrix;
  221. },
  222. },
  223. /**
  224. * Gets the angle of the vertical field of view, in radians.
  225. * @memberof PerspectiveFrustum.prototype
  226. * @type {Number}
  227. * @readonly
  228. * @default undefined
  229. */
  230. fovy: {
  231. get: function () {
  232. update(this);
  233. return this._fovy;
  234. },
  235. },
  236. /**
  237. * @readonly
  238. * @private
  239. */
  240. sseDenominator: {
  241. get: function () {
  242. update(this);
  243. return this._sseDenominator;
  244. },
  245. },
  246. });
  247. /**
  248. * Creates a culling volume for this frustum.
  249. *
  250. * @param {Cartesian3} position The eye position.
  251. * @param {Cartesian3} direction The view direction.
  252. * @param {Cartesian3} up The up direction.
  253. * @returns {CullingVolume} A culling volume at the given position and orientation.
  254. *
  255. * @example
  256. * // Check if a bounding volume intersects the frustum.
  257. * const cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
  258. * const intersect = cullingVolume.computeVisibility(boundingVolume);
  259. */
  260. PerspectiveFrustum.prototype.computeCullingVolume = function (
  261. position,
  262. direction,
  263. up
  264. ) {
  265. update(this);
  266. return this._offCenterFrustum.computeCullingVolume(position, direction, up);
  267. };
  268. /**
  269. * Returns the pixel's width and height in meters.
  270. *
  271. * @param {Number} drawingBufferWidth The width of the drawing buffer.
  272. * @param {Number} drawingBufferHeight The height of the drawing buffer.
  273. * @param {Number} distance The distance to the near plane in meters.
  274. * @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
  275. * @param {Cartesian2} result The object onto which to store the result.
  276. * @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.
  277. *
  278. * @exception {DeveloperError} drawingBufferWidth must be greater than zero.
  279. * @exception {DeveloperError} drawingBufferHeight must be greater than zero.
  280. * @exception {DeveloperError} pixelRatio must be greater than zero.
  281. *
  282. * @example
  283. * // Example 1
  284. * // Get the width and height of a pixel.
  285. * const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, scene.pixelRatio, new Cesium.Cartesian2());
  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. * const position = camera.position;
  292. * const direction = camera.direction;
  293. * const toCenter = Cesium.Cartesian3.subtract(primitive.boundingVolume.center, position, new Cesium.Cartesian3()); // vector from camera to a primitive
  294. * const toCenterProj = Cesium.Cartesian3.multiplyByScalar(direction, Cesium.Cartesian3.dot(direction, toCenter), new Cesium.Cartesian3()); // project vector onto camera direction vector
  295. * const distance = Cesium.Cartesian3.magnitude(toCenterProj);
  296. * const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, scene.pixelRatio, new Cesium.Cartesian2());
  297. */
  298. PerspectiveFrustum.prototype.getPixelDimensions = function (
  299. drawingBufferWidth,
  300. drawingBufferHeight,
  301. distance,
  302. pixelRatio,
  303. result
  304. ) {
  305. update(this);
  306. return this._offCenterFrustum.getPixelDimensions(
  307. drawingBufferWidth,
  308. drawingBufferHeight,
  309. distance,
  310. pixelRatio,
  311. result
  312. );
  313. };
  314. /**
  315. * Returns a duplicate of a PerspectiveFrustum instance.
  316. *
  317. * @param {PerspectiveFrustum} [result] The object onto which to store the result.
  318. * @returns {PerspectiveFrustum} The modified result parameter or a new PerspectiveFrustum instance if one was not provided.
  319. */
  320. PerspectiveFrustum.prototype.clone = function (result) {
  321. if (!defined(result)) {
  322. result = new PerspectiveFrustum();
  323. }
  324. result.aspectRatio = this.aspectRatio;
  325. result.fov = this.fov;
  326. result.near = this.near;
  327. result.far = this.far;
  328. // force update of clone to compute matrices
  329. result._aspectRatio = undefined;
  330. result._fov = undefined;
  331. result._near = undefined;
  332. result._far = undefined;
  333. this._offCenterFrustum.clone(result._offCenterFrustum);
  334. return result;
  335. };
  336. /**
  337. * Compares the provided PerspectiveFrustum componentwise and returns
  338. * <code>true</code> if they are equal, <code>false</code> otherwise.
  339. *
  340. * @param {PerspectiveFrustum} [other] The right hand side PerspectiveFrustum.
  341. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  342. */
  343. PerspectiveFrustum.prototype.equals = function (other) {
  344. if (!defined(other) || !(other instanceof PerspectiveFrustum)) {
  345. return false;
  346. }
  347. update(this);
  348. update(other);
  349. return (
  350. this.fov === other.fov &&
  351. this.aspectRatio === other.aspectRatio &&
  352. this._offCenterFrustum.equals(other._offCenterFrustum)
  353. );
  354. };
  355. /**
  356. * Compares the provided PerspectiveFrustum componentwise and returns
  357. * <code>true</code> if they pass an absolute or relative tolerance test,
  358. * <code>false</code> otherwise.
  359. *
  360. * @param {PerspectiveFrustum} other The right hand side PerspectiveFrustum.
  361. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  362. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  363. * @returns {Boolean} <code>true</code> if this and other are within the provided epsilon, <code>false</code> otherwise.
  364. */
  365. PerspectiveFrustum.prototype.equalsEpsilon = function (
  366. other,
  367. relativeEpsilon,
  368. absoluteEpsilon
  369. ) {
  370. if (!defined(other) || !(other instanceof PerspectiveFrustum)) {
  371. return false;
  372. }
  373. update(this);
  374. update(other);
  375. return (
  376. CesiumMath.equalsEpsilon(
  377. this.fov,
  378. other.fov,
  379. relativeEpsilon,
  380. absoluteEpsilon
  381. ) &&
  382. CesiumMath.equalsEpsilon(
  383. this.aspectRatio,
  384. other.aspectRatio,
  385. relativeEpsilon,
  386. absoluteEpsilon
  387. ) &&
  388. this._offCenterFrustum.equalsEpsilon(
  389. other._offCenterFrustum,
  390. relativeEpsilon,
  391. absoluteEpsilon
  392. )
  393. );
  394. };
  395. export default PerspectiveFrustum;