Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PerspectiveOffCenterFrustum.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. import Cartesian3 from './Cartesian3.js';
  2. import Cartesian4 from './Cartesian4.js';
  3. import CullingVolume from './CullingVolume.js';
  4. import defaultValue from './defaultValue.js';
  5. import defined from './defined.js';
  6. import defineProperties from './defineProperties.js';
  7. import DeveloperError from './DeveloperError.js';
  8. import CesiumMath from './Math.js';
  9. import Matrix4 from './Matrix4.js';
  10. /**
  11. * The viewing frustum is defined by 6 planes.
  12. * Each plane is represented by a {@link Cartesian4} object, where the x, y, and z components
  13. * define the unit vector normal to the plane, and the w component is the distance of the
  14. * plane from the origin/camera position.
  15. *
  16. * @alias PerspectiveOffCenterFrustum
  17. * @constructor
  18. *
  19. * @param {Object} [options] An object with the following properties:
  20. * @param {Number} [options.left] The left clipping plane distance.
  21. * @param {Number} [options.right] The right clipping plane distance.
  22. * @param {Number} [options.top] The top clipping plane distance.
  23. * @param {Number} [options.bottom] The bottom clipping plane distance.
  24. * @param {Number} [options.near=1.0] The near clipping plane distance.
  25. * @param {Number} [options.far=500000000.0] The far clipping plane distance.
  26. *
  27. * @example
  28. * var frustum = new Cesium.PerspectiveOffCenterFrustum({
  29. * left : -1.0,
  30. * right : 1.0,
  31. * top : 1.0,
  32. * bottom : -1.0,
  33. * near : 1.0,
  34. * far : 100.0
  35. * });
  36. *
  37. * @see PerspectiveFrustum
  38. */
  39. function PerspectiveOffCenterFrustum(options) {
  40. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  41. /**
  42. * Defines the left clipping plane.
  43. * @type {Number}
  44. * @default undefined
  45. */
  46. this.left = options.left;
  47. this._left = undefined;
  48. /**
  49. * Defines the right clipping plane.
  50. * @type {Number}
  51. * @default undefined
  52. */
  53. this.right = options.right;
  54. this._right = undefined;
  55. /**
  56. * Defines the top clipping plane.
  57. * @type {Number}
  58. * @default undefined
  59. */
  60. this.top = options.top;
  61. this._top = undefined;
  62. /**
  63. * Defines the bottom clipping plane.
  64. * @type {Number}
  65. * @default undefined
  66. */
  67. this.bottom = options.bottom;
  68. this._bottom = undefined;
  69. /**
  70. * The distance of the near plane.
  71. * @type {Number}
  72. * @default 1.0
  73. */
  74. this.near = defaultValue(options.near, 1.0);
  75. this._near = this.near;
  76. /**
  77. * The distance of the far plane.
  78. * @type {Number}
  79. * @default 500000000.0
  80. */
  81. this.far = defaultValue(options.far, 500000000.0);
  82. this._far = this.far;
  83. this._cullingVolume = new CullingVolume();
  84. this._perspectiveMatrix = new Matrix4();
  85. this._infinitePerspective = 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. var t = frustum.top;
  96. var b = frustum.bottom;
  97. var r = frustum.right;
  98. var l = frustum.left;
  99. var n = frustum.near;
  100. var f = frustum.far;
  101. if (t !== frustum._top || b !== frustum._bottom ||
  102. l !== frustum._left || r !== frustum._right ||
  103. n !== frustum._near || f !== frustum._far) {
  104. //>>includeStart('debug', pragmas.debug);
  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 = l;
  110. frustum._right = r;
  111. frustum._top = t;
  112. frustum._bottom = b;
  113. frustum._near = n;
  114. frustum._far = f;
  115. frustum._perspectiveMatrix = Matrix4.computePerspectiveOffCenter(l, r, b, t, n, f, frustum._perspectiveMatrix);
  116. frustum._infinitePerspective = Matrix4.computeInfinitePerspectiveOffCenter(l, r, b, t, n, frustum._infinitePerspective);
  117. }
  118. }
  119. defineProperties(PerspectiveOffCenterFrustum.prototype, {
  120. /**
  121. * Gets the perspective projection matrix computed from the view frustum.
  122. * @memberof PerspectiveOffCenterFrustum.prototype
  123. * @type {Matrix4}
  124. * @readonly
  125. *
  126. * @see PerspectiveOffCenterFrustum#infiniteProjectionMatrix
  127. */
  128. projectionMatrix : {
  129. get : function() {
  130. update(this);
  131. return this._perspectiveMatrix;
  132. }
  133. },
  134. /**
  135. * Gets the perspective projection matrix computed from the view frustum with an infinite far plane.
  136. * @memberof PerspectiveOffCenterFrustum.prototype
  137. * @type {Matrix4}
  138. * @readonly
  139. *
  140. * @see PerspectiveOffCenterFrustum#projectionMatrix
  141. */
  142. infiniteProjectionMatrix : {
  143. get : function() {
  144. update(this);
  145. return this._infinitePerspective;
  146. }
  147. }
  148. });
  149. var getPlanesRight = new Cartesian3();
  150. var getPlanesNearCenter = new Cartesian3();
  151. var getPlanesFarCenter = new Cartesian3();
  152. var getPlanesNormal = new Cartesian3();
  153. /**
  154. * Creates a culling volume for this frustum.
  155. *
  156. * @param {Cartesian3} position The eye position.
  157. * @param {Cartesian3} direction The view direction.
  158. * @param {Cartesian3} up The up direction.
  159. * @returns {CullingVolume} A culling volume at the given position and orientation.
  160. *
  161. * @example
  162. * // Check if a bounding volume intersects the frustum.
  163. * var cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
  164. * var intersect = cullingVolume.computeVisibility(boundingVolume);
  165. */
  166. PerspectiveOffCenterFrustum.prototype.computeCullingVolume = function(position, direction, up) {
  167. //>>includeStart('debug', pragmas.debug);
  168. if (!defined(position)) {
  169. throw new DeveloperError('position is required.');
  170. }
  171. if (!defined(direction)) {
  172. throw new DeveloperError('direction is required.');
  173. }
  174. if (!defined(up)) {
  175. throw new DeveloperError('up is required.');
  176. }
  177. //>>includeEnd('debug');
  178. var planes = this._cullingVolume.planes;
  179. var t = this.top;
  180. var b = this.bottom;
  181. var r = this.right;
  182. var l = this.left;
  183. var n = this.near;
  184. var f = this.far;
  185. var right = Cartesian3.cross(direction, up, getPlanesRight);
  186. var nearCenter = getPlanesNearCenter;
  187. Cartesian3.multiplyByScalar(direction, n, nearCenter);
  188. Cartesian3.add(position, nearCenter, nearCenter);
  189. var farCenter = getPlanesFarCenter;
  190. Cartesian3.multiplyByScalar(direction, f, farCenter);
  191. Cartesian3.add(position, farCenter, farCenter);
  192. var normal = getPlanesNormal;
  193. //Left plane computation
  194. Cartesian3.multiplyByScalar(right, l, normal);
  195. Cartesian3.add(nearCenter, normal, normal);
  196. Cartesian3.subtract(normal, position, normal);
  197. Cartesian3.normalize(normal, normal);
  198. Cartesian3.cross(normal, up, normal);
  199. Cartesian3.normalize(normal, 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.cross(up, normal, normal);
  213. Cartesian3.normalize(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.cross(right, normal, normal);
  227. Cartesian3.normalize(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.cross(normal, right, normal);
  241. Cartesian3.normalize(normal, 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 {Number} drawingBufferWidth The width of the drawing buffer.
  275. * @param {Number} drawingBufferHeight The height of the drawing buffer.
  276. * @param {Number} distance The distance to the near plane in meters.
  277. * @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
  278. * @param {Cartesian2} result The object onto which to store the result.
  279. * @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.
  280. *
  281. * @exception {DeveloperError} drawingBufferWidth must be greater than zero.
  282. * @exception {DeveloperError} drawingBufferHeight must be greater than zero.
  283. * @exception {DeveloperError} pixelRatio must be greater than zero.
  284. *
  285. * @example
  286. * // Example 1
  287. * // Get the width and height of a pixel.
  288. * var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, scene.pixelRatio, new Cesium.Cartesian2());
  289. *
  290. * @example
  291. * // Example 2
  292. * // Get the width and height of a pixel if the near plane was set to 'distance'.
  293. * // For example, get the size of a pixel of an image on a billboard.
  294. * var position = camera.position;
  295. * var direction = camera.direction;
  296. * var toCenter = Cesium.Cartesian3.subtract(primitive.boundingVolume.center, position, new Cesium.Cartesian3()); // vector from camera to a primitive
  297. * var toCenterProj = Cesium.Cartesian3.multiplyByScalar(direction, Cesium.Cartesian3.dot(direction, toCenter), new Cesium.Cartesian3()); // project vector onto camera direction vector
  298. * var distance = Cesium.Cartesian3.magnitude(toCenterProj);
  299. * var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, scene.pixelRatio, new Cesium.Cartesian2());
  300. */
  301. PerspectiveOffCenterFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result) {
  302. update(this);
  303. //>>includeStart('debug', pragmas.debug);
  304. if (!defined(drawingBufferWidth) || !defined(drawingBufferHeight)) {
  305. throw new DeveloperError('Both drawingBufferWidth and drawingBufferHeight are required.');
  306. }
  307. if (drawingBufferWidth <= 0) {
  308. throw new DeveloperError('drawingBufferWidth must be greater than zero.');
  309. }
  310. if (drawingBufferHeight <= 0) {
  311. throw new DeveloperError('drawingBufferHeight must be greater than zero.');
  312. }
  313. if (!defined(distance)) {
  314. throw new DeveloperError('distance is required.');
  315. }
  316. if (!defined(pixelRatio)) {
  317. throw new DeveloperError('pixelRatio is required');
  318. }
  319. if (pixelRatio <= 0) {
  320. throw new DeveloperError('pixelRatio must be greater than zero.');
  321. }
  322. if (!defined(result)) {
  323. throw new DeveloperError('A result object is required.');
  324. }
  325. //>>includeEnd('debug');
  326. var inverseNear = 1.0 / this.near;
  327. var tanTheta = this.top * inverseNear;
  328. var pixelHeight = 2.0 * pixelRatio * distance * tanTheta / drawingBufferHeight;
  329. tanTheta = this.right * inverseNear;
  330. var pixelWidth = 2.0 * pixelRatio * distance * tanTheta / drawingBufferWidth;
  331. result.x = pixelWidth;
  332. result.y = pixelHeight;
  333. return result;
  334. };
  335. /**
  336. * Returns a duplicate of a PerspectiveOffCenterFrustum instance.
  337. *
  338. * @param {PerspectiveOffCenterFrustum} [result] The object onto which to store the result.
  339. * @returns {PerspectiveOffCenterFrustum} The modified result parameter or a new PerspectiveFrustum instance if one was not provided.
  340. */
  341. PerspectiveOffCenterFrustum.prototype.clone = function(result) {
  342. if (!defined(result)) {
  343. result = new PerspectiveOffCenterFrustum();
  344. }
  345. result.right = this.right;
  346. result.left = this.left;
  347. result.top = this.top;
  348. result.bottom = this.bottom;
  349. result.near = this.near;
  350. result.far = this.far;
  351. // force update of clone to compute matrices
  352. result._left = undefined;
  353. result._right = undefined;
  354. result._top = undefined;
  355. result._bottom = undefined;
  356. result._near = undefined;
  357. result._far = undefined;
  358. return result;
  359. };
  360. /**
  361. * Compares the provided PerspectiveOffCenterFrustum componentwise and returns
  362. * <code>true</code> if they are equal, <code>false</code> otherwise.
  363. *
  364. * @param {PerspectiveOffCenterFrustum} [other] The right hand side PerspectiveOffCenterFrustum.
  365. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  366. */
  367. PerspectiveOffCenterFrustum.prototype.equals = function(other) {
  368. return (defined(other) && other instanceof PerspectiveOffCenterFrustum &&
  369. this.right === other.right &&
  370. this.left === other.left &&
  371. this.top === other.top &&
  372. this.bottom === other.bottom &&
  373. this.near === other.near &&
  374. this.far === other.far);
  375. };
  376. /**
  377. * Compares the provided PerspectiveOffCenterFrustum componentwise and returns
  378. * <code>true</code> if they pass an absolute or relative tolerance test,
  379. * <code>false</code> otherwise.
  380. *
  381. * @param {PerspectiveOffCenterFrustum} other The right hand side PerspectiveOffCenterFrustum.
  382. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  383. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  384. * @returns {Boolean} <code>true</code> if this and other are within the provided epsilon, <code>false</code> otherwise.
  385. */
  386. PerspectiveOffCenterFrustum.prototype.equalsEpsilon = function(other, relativeEpsilon, absoluteEpsilon) {
  387. return (other === this) ||
  388. (defined(other) &&
  389. other instanceof PerspectiveOffCenterFrustum &&
  390. CesiumMath.equalsEpsilon(this.right, other.right, relativeEpsilon, absoluteEpsilon) &&
  391. CesiumMath.equalsEpsilon(this.left, other.left, relativeEpsilon, absoluteEpsilon) &&
  392. CesiumMath.equalsEpsilon(this.top, other.top, relativeEpsilon, absoluteEpsilon) &&
  393. CesiumMath.equalsEpsilon(this.bottom, other.bottom, relativeEpsilon, absoluteEpsilon) &&
  394. CesiumMath.equalsEpsilon(this.near, other.near, relativeEpsilon, absoluteEpsilon) &&
  395. CesiumMath.equalsEpsilon(this.far, other.far, relativeEpsilon, absoluteEpsilon));
  396. };
  397. export default PerspectiveOffCenterFrustum;