FrustumGeometry-85c5ebd2.js 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.95
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['exports', './Transforms-273eeb44', './Matrix2-9e1c22e2', './RuntimeError-4f8ec8a2', './ComponentDatatype-4eeb6d9b', './defaultValue-97284df2', './GeometryAttribute-9be2d2e5', './GeometryAttributes-734a3446', './Plane-76b84425', './VertexFormat-563ab2cc'], (function (exports, Transforms, Matrix2, RuntimeError, ComponentDatatype, defaultValue, GeometryAttribute, GeometryAttributes, Plane, VertexFormat) { 'use strict';
  26. /**
  27. * The culling volume defined by planes.
  28. *
  29. * @alias CullingVolume
  30. * @constructor
  31. *
  32. * @param {Cartesian4[]} [planes] An array of clipping planes.
  33. */
  34. function CullingVolume(planes) {
  35. /**
  36. * Each plane is represented by a Cartesian4 object, where the x, y, and z components
  37. * define the unit vector normal to the plane, and the w component is the distance of the
  38. * plane from the origin.
  39. * @type {Cartesian4[]}
  40. * @default []
  41. */
  42. this.planes = defaultValue.defaultValue(planes, []);
  43. }
  44. const faces = [new Matrix2.Cartesian3(), new Matrix2.Cartesian3(), new Matrix2.Cartesian3()];
  45. Matrix2.Cartesian3.clone(Matrix2.Cartesian3.UNIT_X, faces[0]);
  46. Matrix2.Cartesian3.clone(Matrix2.Cartesian3.UNIT_Y, faces[1]);
  47. Matrix2.Cartesian3.clone(Matrix2.Cartesian3.UNIT_Z, faces[2]);
  48. const scratchPlaneCenter = new Matrix2.Cartesian3();
  49. const scratchPlaneNormal = new Matrix2.Cartesian3();
  50. const scratchPlane = new Plane.Plane(new Matrix2.Cartesian3(1.0, 0.0, 0.0), 0.0);
  51. /**
  52. * Constructs a culling volume from a bounding sphere. Creates six planes that create a box containing the sphere.
  53. * The planes are aligned to the x, y, and z axes in world coordinates.
  54. *
  55. * @param {BoundingSphere} boundingSphere The bounding sphere used to create the culling volume.
  56. * @param {CullingVolume} [result] The object onto which to store the result.
  57. * @returns {CullingVolume} The culling volume created from the bounding sphere.
  58. */
  59. CullingVolume.fromBoundingSphere = function (boundingSphere, result) {
  60. //>>includeStart('debug', pragmas.debug);
  61. if (!defaultValue.defined(boundingSphere)) {
  62. throw new RuntimeError.DeveloperError("boundingSphere is required.");
  63. }
  64. //>>includeEnd('debug');
  65. if (!defaultValue.defined(result)) {
  66. result = new CullingVolume();
  67. }
  68. const length = faces.length;
  69. const planes = result.planes;
  70. planes.length = 2 * length;
  71. const center = boundingSphere.center;
  72. const radius = boundingSphere.radius;
  73. let planeIndex = 0;
  74. for (let i = 0; i < length; ++i) {
  75. const faceNormal = faces[i];
  76. let plane0 = planes[planeIndex];
  77. let plane1 = planes[planeIndex + 1];
  78. if (!defaultValue.defined(plane0)) {
  79. plane0 = planes[planeIndex] = new Matrix2.Cartesian4();
  80. }
  81. if (!defaultValue.defined(plane1)) {
  82. plane1 = planes[planeIndex + 1] = new Matrix2.Cartesian4();
  83. }
  84. Matrix2.Cartesian3.multiplyByScalar(faceNormal, -radius, scratchPlaneCenter);
  85. Matrix2.Cartesian3.add(center, scratchPlaneCenter, scratchPlaneCenter);
  86. plane0.x = faceNormal.x;
  87. plane0.y = faceNormal.y;
  88. plane0.z = faceNormal.z;
  89. plane0.w = -Matrix2.Cartesian3.dot(faceNormal, scratchPlaneCenter);
  90. Matrix2.Cartesian3.multiplyByScalar(faceNormal, radius, scratchPlaneCenter);
  91. Matrix2.Cartesian3.add(center, scratchPlaneCenter, scratchPlaneCenter);
  92. plane1.x = -faceNormal.x;
  93. plane1.y = -faceNormal.y;
  94. plane1.z = -faceNormal.z;
  95. plane1.w = -Matrix2.Cartesian3.dot(
  96. Matrix2.Cartesian3.negate(faceNormal, scratchPlaneNormal),
  97. scratchPlaneCenter
  98. );
  99. planeIndex += 2;
  100. }
  101. return result;
  102. };
  103. /**
  104. * Determines whether a bounding volume intersects the culling volume.
  105. *
  106. * @param {Object} boundingVolume The bounding volume whose intersection with the culling volume is to be tested.
  107. * @returns {Intersect} Intersect.OUTSIDE, Intersect.INTERSECTING, or Intersect.INSIDE.
  108. */
  109. CullingVolume.prototype.computeVisibility = function (boundingVolume) {
  110. //>>includeStart('debug', pragmas.debug);
  111. if (!defaultValue.defined(boundingVolume)) {
  112. throw new RuntimeError.DeveloperError("boundingVolume is required.");
  113. }
  114. //>>includeEnd('debug');
  115. const planes = this.planes;
  116. let intersecting = false;
  117. for (let k = 0, len = planes.length; k < len; ++k) {
  118. const result = boundingVolume.intersectPlane(
  119. Plane.Plane.fromCartesian4(planes[k], scratchPlane)
  120. );
  121. if (result === Transforms.Intersect.OUTSIDE) {
  122. return Transforms.Intersect.OUTSIDE;
  123. } else if (result === Transforms.Intersect.INTERSECTING) {
  124. intersecting = true;
  125. }
  126. }
  127. return intersecting ? Transforms.Intersect.INTERSECTING : Transforms.Intersect.INSIDE;
  128. };
  129. /**
  130. * Determines whether a bounding volume intersects the culling volume.
  131. *
  132. * @param {Object} boundingVolume The bounding volume whose intersection with the culling volume is to be tested.
  133. * @param {Number} parentPlaneMask A bit mask from the boundingVolume's parent's check against the same culling
  134. * volume, such that if (planeMask & (1 << planeIndex) === 0), for k < 31, then
  135. * the parent (and therefore this) volume is completely inside plane[planeIndex]
  136. * and that plane check can be skipped.
  137. * @returns {Number} A plane mask as described above (which can be applied to this boundingVolume's children).
  138. *
  139. * @private
  140. */
  141. CullingVolume.prototype.computeVisibilityWithPlaneMask = function (
  142. boundingVolume,
  143. parentPlaneMask
  144. ) {
  145. //>>includeStart('debug', pragmas.debug);
  146. if (!defaultValue.defined(boundingVolume)) {
  147. throw new RuntimeError.DeveloperError("boundingVolume is required.");
  148. }
  149. if (!defaultValue.defined(parentPlaneMask)) {
  150. throw new RuntimeError.DeveloperError("parentPlaneMask is required.");
  151. }
  152. //>>includeEnd('debug');
  153. if (
  154. parentPlaneMask === CullingVolume.MASK_OUTSIDE ||
  155. parentPlaneMask === CullingVolume.MASK_INSIDE
  156. ) {
  157. // parent is completely outside or completely inside, so this child is as well.
  158. return parentPlaneMask;
  159. }
  160. // Start with MASK_INSIDE (all zeros) so that after the loop, the return value can be compared with MASK_INSIDE.
  161. // (Because if there are fewer than 31 planes, the upper bits wont be changed.)
  162. let mask = CullingVolume.MASK_INSIDE;
  163. const planes = this.planes;
  164. for (let k = 0, len = planes.length; k < len; ++k) {
  165. // For k greater than 31 (since 31 is the maximum number of INSIDE/INTERSECTING bits we can store), skip the optimization.
  166. const flag = k < 31 ? 1 << k : 0;
  167. if (k < 31 && (parentPlaneMask & flag) === 0) {
  168. // boundingVolume is known to be INSIDE this plane.
  169. continue;
  170. }
  171. const result = boundingVolume.intersectPlane(
  172. Plane.Plane.fromCartesian4(planes[k], scratchPlane)
  173. );
  174. if (result === Transforms.Intersect.OUTSIDE) {
  175. return CullingVolume.MASK_OUTSIDE;
  176. } else if (result === Transforms.Intersect.INTERSECTING) {
  177. mask |= flag;
  178. }
  179. }
  180. return mask;
  181. };
  182. /**
  183. * For plane masks (as used in {@link CullingVolume#computeVisibilityWithPlaneMask}), this special value
  184. * represents the case where the object bounding volume is entirely outside the culling volume.
  185. *
  186. * @type {Number}
  187. * @private
  188. */
  189. CullingVolume.MASK_OUTSIDE = 0xffffffff;
  190. /**
  191. * For plane masks (as used in {@link CullingVolume.prototype.computeVisibilityWithPlaneMask}), this value
  192. * represents the case where the object bounding volume is entirely inside the culling volume.
  193. *
  194. * @type {Number}
  195. * @private
  196. */
  197. CullingVolume.MASK_INSIDE = 0x00000000;
  198. /**
  199. * For plane masks (as used in {@link CullingVolume.prototype.computeVisibilityWithPlaneMask}), this value
  200. * represents the case where the object bounding volume (may) intersect all planes of the culling volume.
  201. *
  202. * @type {Number}
  203. * @private
  204. */
  205. CullingVolume.MASK_INDETERMINATE = 0x7fffffff;
  206. /**
  207. * The viewing frustum is defined by 6 planes.
  208. * Each plane is represented by a {@link Cartesian4} object, where the x, y, and z components
  209. * define the unit vector normal to the plane, and the w component is the distance of the
  210. * plane from the origin/camera position.
  211. *
  212. * @alias OrthographicOffCenterFrustum
  213. * @constructor
  214. *
  215. * @param {Object} [options] An object with the following properties:
  216. * @param {Number} [options.left] The left clipping plane distance.
  217. * @param {Number} [options.right] The right clipping plane distance.
  218. * @param {Number} [options.top] The top clipping plane distance.
  219. * @param {Number} [options.bottom] The bottom clipping plane distance.
  220. * @param {Number} [options.near=1.0] The near clipping plane distance.
  221. * @param {Number} [options.far=500000000.0] The far clipping plane distance.
  222. *
  223. * @example
  224. * const maxRadii = ellipsoid.maximumRadius;
  225. *
  226. * const frustum = new Cesium.OrthographicOffCenterFrustum();
  227. * frustum.right = maxRadii * Cesium.Math.PI;
  228. * frustum.left = -c.frustum.right;
  229. * frustum.top = c.frustum.right * (canvas.clientHeight / canvas.clientWidth);
  230. * frustum.bottom = -c.frustum.top;
  231. * frustum.near = 0.01 * maxRadii;
  232. * frustum.far = 50.0 * maxRadii;
  233. */
  234. function OrthographicOffCenterFrustum(options) {
  235. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  236. /**
  237. * The left clipping plane.
  238. * @type {Number}
  239. * @default undefined
  240. */
  241. this.left = options.left;
  242. this._left = undefined;
  243. /**
  244. * The right clipping plane.
  245. * @type {Number}
  246. * @default undefined
  247. */
  248. this.right = options.right;
  249. this._right = undefined;
  250. /**
  251. * The top clipping plane.
  252. * @type {Number}
  253. * @default undefined
  254. */
  255. this.top = options.top;
  256. this._top = undefined;
  257. /**
  258. * The bottom clipping plane.
  259. * @type {Number}
  260. * @default undefined
  261. */
  262. this.bottom = options.bottom;
  263. this._bottom = undefined;
  264. /**
  265. * The distance of the near plane.
  266. * @type {Number}
  267. * @default 1.0
  268. */
  269. this.near = defaultValue.defaultValue(options.near, 1.0);
  270. this._near = this.near;
  271. /**
  272. * The distance of the far plane.
  273. * @type {Number}
  274. * @default 500000000.0;
  275. */
  276. this.far = defaultValue.defaultValue(options.far, 500000000.0);
  277. this._far = this.far;
  278. this._cullingVolume = new CullingVolume();
  279. this._orthographicMatrix = new Matrix2.Matrix4();
  280. }
  281. function update$3(frustum) {
  282. //>>includeStart('debug', pragmas.debug);
  283. if (
  284. !defaultValue.defined(frustum.right) ||
  285. !defaultValue.defined(frustum.left) ||
  286. !defaultValue.defined(frustum.top) ||
  287. !defaultValue.defined(frustum.bottom) ||
  288. !defaultValue.defined(frustum.near) ||
  289. !defaultValue.defined(frustum.far)
  290. ) {
  291. throw new RuntimeError.DeveloperError(
  292. "right, left, top, bottom, near, or far parameters are not set."
  293. );
  294. }
  295. //>>includeEnd('debug');
  296. if (
  297. frustum.top !== frustum._top ||
  298. frustum.bottom !== frustum._bottom ||
  299. frustum.left !== frustum._left ||
  300. frustum.right !== frustum._right ||
  301. frustum.near !== frustum._near ||
  302. frustum.far !== frustum._far
  303. ) {
  304. //>>includeStart('debug', pragmas.debug);
  305. if (frustum.left > frustum.right) {
  306. throw new RuntimeError.DeveloperError("right must be greater than left.");
  307. }
  308. if (frustum.bottom > frustum.top) {
  309. throw new RuntimeError.DeveloperError("top must be greater than bottom.");
  310. }
  311. if (frustum.near <= 0 || frustum.near > frustum.far) {
  312. throw new RuntimeError.DeveloperError(
  313. "near must be greater than zero and less than far."
  314. );
  315. }
  316. //>>includeEnd('debug');
  317. frustum._left = frustum.left;
  318. frustum._right = frustum.right;
  319. frustum._top = frustum.top;
  320. frustum._bottom = frustum.bottom;
  321. frustum._near = frustum.near;
  322. frustum._far = frustum.far;
  323. frustum._orthographicMatrix = Matrix2.Matrix4.computeOrthographicOffCenter(
  324. frustum.left,
  325. frustum.right,
  326. frustum.bottom,
  327. frustum.top,
  328. frustum.near,
  329. frustum.far,
  330. frustum._orthographicMatrix
  331. );
  332. }
  333. }
  334. Object.defineProperties(OrthographicOffCenterFrustum.prototype, {
  335. /**
  336. * Gets the orthographic projection matrix computed from the view frustum.
  337. * @memberof OrthographicOffCenterFrustum.prototype
  338. * @type {Matrix4}
  339. * @readonly
  340. */
  341. projectionMatrix: {
  342. get: function () {
  343. update$3(this);
  344. return this._orthographicMatrix;
  345. },
  346. },
  347. });
  348. const getPlanesRight$1 = new Matrix2.Cartesian3();
  349. const getPlanesNearCenter$1 = new Matrix2.Cartesian3();
  350. const getPlanesPoint = new Matrix2.Cartesian3();
  351. const negateScratch = new Matrix2.Cartesian3();
  352. /**
  353. * Creates a culling volume for this frustum.
  354. *
  355. * @param {Cartesian3} position The eye position.
  356. * @param {Cartesian3} direction The view direction.
  357. * @param {Cartesian3} up The up direction.
  358. * @returns {CullingVolume} A culling volume at the given position and orientation.
  359. *
  360. * @example
  361. * // Check if a bounding volume intersects the frustum.
  362. * const cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
  363. * const intersect = cullingVolume.computeVisibility(boundingVolume);
  364. */
  365. OrthographicOffCenterFrustum.prototype.computeCullingVolume = function (
  366. position,
  367. direction,
  368. up
  369. ) {
  370. //>>includeStart('debug', pragmas.debug);
  371. if (!defaultValue.defined(position)) {
  372. throw new RuntimeError.DeveloperError("position is required.");
  373. }
  374. if (!defaultValue.defined(direction)) {
  375. throw new RuntimeError.DeveloperError("direction is required.");
  376. }
  377. if (!defaultValue.defined(up)) {
  378. throw new RuntimeError.DeveloperError("up is required.");
  379. }
  380. //>>includeEnd('debug');
  381. const planes = this._cullingVolume.planes;
  382. const t = this.top;
  383. const b = this.bottom;
  384. const r = this.right;
  385. const l = this.left;
  386. const n = this.near;
  387. const f = this.far;
  388. const right = Matrix2.Cartesian3.cross(direction, up, getPlanesRight$1);
  389. Matrix2.Cartesian3.normalize(right, right);
  390. const nearCenter = getPlanesNearCenter$1;
  391. Matrix2.Cartesian3.multiplyByScalar(direction, n, nearCenter);
  392. Matrix2.Cartesian3.add(position, nearCenter, nearCenter);
  393. const point = getPlanesPoint;
  394. // Left plane
  395. Matrix2.Cartesian3.multiplyByScalar(right, l, point);
  396. Matrix2.Cartesian3.add(nearCenter, point, point);
  397. let plane = planes[0];
  398. if (!defaultValue.defined(plane)) {
  399. plane = planes[0] = new Matrix2.Cartesian4();
  400. }
  401. plane.x = right.x;
  402. plane.y = right.y;
  403. plane.z = right.z;
  404. plane.w = -Matrix2.Cartesian3.dot(right, point);
  405. // Right plane
  406. Matrix2.Cartesian3.multiplyByScalar(right, r, point);
  407. Matrix2.Cartesian3.add(nearCenter, point, point);
  408. plane = planes[1];
  409. if (!defaultValue.defined(plane)) {
  410. plane = planes[1] = new Matrix2.Cartesian4();
  411. }
  412. plane.x = -right.x;
  413. plane.y = -right.y;
  414. plane.z = -right.z;
  415. plane.w = -Matrix2.Cartesian3.dot(Matrix2.Cartesian3.negate(right, negateScratch), point);
  416. // Bottom plane
  417. Matrix2.Cartesian3.multiplyByScalar(up, b, point);
  418. Matrix2.Cartesian3.add(nearCenter, point, point);
  419. plane = planes[2];
  420. if (!defaultValue.defined(plane)) {
  421. plane = planes[2] = new Matrix2.Cartesian4();
  422. }
  423. plane.x = up.x;
  424. plane.y = up.y;
  425. plane.z = up.z;
  426. plane.w = -Matrix2.Cartesian3.dot(up, point);
  427. // Top plane
  428. Matrix2.Cartesian3.multiplyByScalar(up, t, point);
  429. Matrix2.Cartesian3.add(nearCenter, point, point);
  430. plane = planes[3];
  431. if (!defaultValue.defined(plane)) {
  432. plane = planes[3] = new Matrix2.Cartesian4();
  433. }
  434. plane.x = -up.x;
  435. plane.y = -up.y;
  436. plane.z = -up.z;
  437. plane.w = -Matrix2.Cartesian3.dot(Matrix2.Cartesian3.negate(up, negateScratch), point);
  438. // Near plane
  439. plane = planes[4];
  440. if (!defaultValue.defined(plane)) {
  441. plane = planes[4] = new Matrix2.Cartesian4();
  442. }
  443. plane.x = direction.x;
  444. plane.y = direction.y;
  445. plane.z = direction.z;
  446. plane.w = -Matrix2.Cartesian3.dot(direction, nearCenter);
  447. // Far plane
  448. Matrix2.Cartesian3.multiplyByScalar(direction, f, point);
  449. Matrix2.Cartesian3.add(position, point, point);
  450. plane = planes[5];
  451. if (!defaultValue.defined(plane)) {
  452. plane = planes[5] = new Matrix2.Cartesian4();
  453. }
  454. plane.x = -direction.x;
  455. plane.y = -direction.y;
  456. plane.z = -direction.z;
  457. plane.w = -Matrix2.Cartesian3.dot(Matrix2.Cartesian3.negate(direction, negateScratch), point);
  458. return this._cullingVolume;
  459. };
  460. /**
  461. * Returns the pixel's width and height in meters.
  462. *
  463. * @param {Number} drawingBufferWidth The width of the drawing buffer.
  464. * @param {Number} drawingBufferHeight The height of the drawing buffer.
  465. * @param {Number} distance The distance to the near plane in meters.
  466. * @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
  467. * @param {Cartesian2} result The object onto which to store the result.
  468. * @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.
  469. *
  470. * @exception {DeveloperError} drawingBufferWidth must be greater than zero.
  471. * @exception {DeveloperError} drawingBufferHeight must be greater than zero.
  472. * @exception {DeveloperError} pixelRatio must be greater than zero.
  473. *
  474. * @example
  475. * // Example 1
  476. * // Get the width and height of a pixel.
  477. * const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 0.0, scene.pixelRatio, new Cesium.Cartesian2());
  478. */
  479. OrthographicOffCenterFrustum.prototype.getPixelDimensions = function (
  480. drawingBufferWidth,
  481. drawingBufferHeight,
  482. distance,
  483. pixelRatio,
  484. result
  485. ) {
  486. update$3(this);
  487. //>>includeStart('debug', pragmas.debug);
  488. if (!defaultValue.defined(drawingBufferWidth) || !defaultValue.defined(drawingBufferHeight)) {
  489. throw new RuntimeError.DeveloperError(
  490. "Both drawingBufferWidth and drawingBufferHeight are required."
  491. );
  492. }
  493. if (drawingBufferWidth <= 0) {
  494. throw new RuntimeError.DeveloperError("drawingBufferWidth must be greater than zero.");
  495. }
  496. if (drawingBufferHeight <= 0) {
  497. throw new RuntimeError.DeveloperError("drawingBufferHeight must be greater than zero.");
  498. }
  499. if (!defaultValue.defined(distance)) {
  500. throw new RuntimeError.DeveloperError("distance is required.");
  501. }
  502. if (!defaultValue.defined(pixelRatio)) {
  503. throw new RuntimeError.DeveloperError("pixelRatio is required.");
  504. }
  505. if (pixelRatio <= 0) {
  506. throw new RuntimeError.DeveloperError("pixelRatio must be greater than zero.");
  507. }
  508. if (!defaultValue.defined(result)) {
  509. throw new RuntimeError.DeveloperError("A result object is required.");
  510. }
  511. //>>includeEnd('debug');
  512. const frustumWidth = this.right - this.left;
  513. const frustumHeight = this.top - this.bottom;
  514. const pixelWidth = (pixelRatio * frustumWidth) / drawingBufferWidth;
  515. const pixelHeight = (pixelRatio * frustumHeight) / drawingBufferHeight;
  516. result.x = pixelWidth;
  517. result.y = pixelHeight;
  518. return result;
  519. };
  520. /**
  521. * Returns a duplicate of a OrthographicOffCenterFrustum instance.
  522. *
  523. * @param {OrthographicOffCenterFrustum} [result] The object onto which to store the result.
  524. * @returns {OrthographicOffCenterFrustum} The modified result parameter or a new OrthographicOffCenterFrustum instance if one was not provided.
  525. */
  526. OrthographicOffCenterFrustum.prototype.clone = function (result) {
  527. if (!defaultValue.defined(result)) {
  528. result = new OrthographicOffCenterFrustum();
  529. }
  530. result.left = this.left;
  531. result.right = this.right;
  532. result.top = this.top;
  533. result.bottom = this.bottom;
  534. result.near = this.near;
  535. result.far = this.far;
  536. // force update of clone to compute matrices
  537. result._left = undefined;
  538. result._right = undefined;
  539. result._top = undefined;
  540. result._bottom = undefined;
  541. result._near = undefined;
  542. result._far = undefined;
  543. return result;
  544. };
  545. /**
  546. * Compares the provided OrthographicOffCenterFrustum componentwise and returns
  547. * <code>true</code> if they are equal, <code>false</code> otherwise.
  548. *
  549. * @param {OrthographicOffCenterFrustum} [other] The right hand side OrthographicOffCenterFrustum.
  550. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  551. */
  552. OrthographicOffCenterFrustum.prototype.equals = function (other) {
  553. return (
  554. defaultValue.defined(other) &&
  555. other instanceof OrthographicOffCenterFrustum &&
  556. this.right === other.right &&
  557. this.left === other.left &&
  558. this.top === other.top &&
  559. this.bottom === other.bottom &&
  560. this.near === other.near &&
  561. this.far === other.far
  562. );
  563. };
  564. /**
  565. * Compares the provided OrthographicOffCenterFrustum componentwise and returns
  566. * <code>true</code> if they pass an absolute or relative tolerance test,
  567. * <code>false</code> otherwise.
  568. *
  569. * @param {OrthographicOffCenterFrustum} other The right hand side OrthographicOffCenterFrustum.
  570. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  571. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  572. * @returns {Boolean} <code>true</code> if this and other are within the provided epsilon, <code>false</code> otherwise.
  573. */
  574. OrthographicOffCenterFrustum.prototype.equalsEpsilon = function (
  575. other,
  576. relativeEpsilon,
  577. absoluteEpsilon
  578. ) {
  579. return (
  580. other === this ||
  581. (defaultValue.defined(other) &&
  582. other instanceof OrthographicOffCenterFrustum &&
  583. ComponentDatatype.CesiumMath.equalsEpsilon(
  584. this.right,
  585. other.right,
  586. relativeEpsilon,
  587. absoluteEpsilon
  588. ) &&
  589. ComponentDatatype.CesiumMath.equalsEpsilon(
  590. this.left,
  591. other.left,
  592. relativeEpsilon,
  593. absoluteEpsilon
  594. ) &&
  595. ComponentDatatype.CesiumMath.equalsEpsilon(
  596. this.top,
  597. other.top,
  598. relativeEpsilon,
  599. absoluteEpsilon
  600. ) &&
  601. ComponentDatatype.CesiumMath.equalsEpsilon(
  602. this.bottom,
  603. other.bottom,
  604. relativeEpsilon,
  605. absoluteEpsilon
  606. ) &&
  607. ComponentDatatype.CesiumMath.equalsEpsilon(
  608. this.near,
  609. other.near,
  610. relativeEpsilon,
  611. absoluteEpsilon
  612. ) &&
  613. ComponentDatatype.CesiumMath.equalsEpsilon(
  614. this.far,
  615. other.far,
  616. relativeEpsilon,
  617. absoluteEpsilon
  618. ))
  619. );
  620. };
  621. /**
  622. * The viewing frustum is defined by 6 planes.
  623. * Each plane is represented by a {@link Cartesian4} object, where the x, y, and z components
  624. * define the unit vector normal to the plane, and the w component is the distance of the
  625. * plane from the origin/camera position.
  626. *
  627. * @alias OrthographicFrustum
  628. * @constructor
  629. *
  630. * @param {Object} [options] An object with the following properties:
  631. * @param {Number} [options.width] The width of the frustum in meters.
  632. * @param {Number} [options.aspectRatio] The aspect ratio of the frustum's width to it's height.
  633. * @param {Number} [options.near=1.0] The distance of the near plane.
  634. * @param {Number} [options.far=500000000.0] The distance of the far plane.
  635. *
  636. * @example
  637. * const maxRadii = ellipsoid.maximumRadius;
  638. *
  639. * const frustum = new Cesium.OrthographicFrustum();
  640. * frustum.near = 0.01 * maxRadii;
  641. * frustum.far = 50.0 * maxRadii;
  642. */
  643. function OrthographicFrustum(options) {
  644. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  645. this._offCenterFrustum = new OrthographicOffCenterFrustum();
  646. /**
  647. * The horizontal width of the frustum in meters.
  648. * @type {Number}
  649. * @default undefined
  650. */
  651. this.width = options.width;
  652. this._width = undefined;
  653. /**
  654. * The aspect ratio of the frustum's width to it's height.
  655. * @type {Number}
  656. * @default undefined
  657. */
  658. this.aspectRatio = options.aspectRatio;
  659. this._aspectRatio = undefined;
  660. /**
  661. * The distance of the near plane.
  662. * @type {Number}
  663. * @default 1.0
  664. */
  665. this.near = defaultValue.defaultValue(options.near, 1.0);
  666. this._near = this.near;
  667. /**
  668. * The distance of the far plane.
  669. * @type {Number}
  670. * @default 500000000.0;
  671. */
  672. this.far = defaultValue.defaultValue(options.far, 500000000.0);
  673. this._far = this.far;
  674. }
  675. /**
  676. * The number of elements used to pack the object into an array.
  677. * @type {Number}
  678. */
  679. OrthographicFrustum.packedLength = 4;
  680. /**
  681. * Stores the provided instance into the provided array.
  682. *
  683. * @param {OrthographicFrustum} value The value to pack.
  684. * @param {Number[]} array The array to pack into.
  685. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  686. *
  687. * @returns {Number[]} The array that was packed into
  688. */
  689. OrthographicFrustum.pack = function (value, array, startingIndex) {
  690. //>>includeStart('debug', pragmas.debug);
  691. RuntimeError.Check.typeOf.object("value", value);
  692. RuntimeError.Check.defined("array", array);
  693. //>>includeEnd('debug');
  694. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  695. array[startingIndex++] = value.width;
  696. array[startingIndex++] = value.aspectRatio;
  697. array[startingIndex++] = value.near;
  698. array[startingIndex] = value.far;
  699. return array;
  700. };
  701. /**
  702. * Retrieves an instance from a packed array.
  703. *
  704. * @param {Number[]} array The packed array.
  705. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  706. * @param {OrthographicFrustum} [result] The object into which to store the result.
  707. * @returns {OrthographicFrustum} The modified result parameter or a new OrthographicFrustum instance if one was not provided.
  708. */
  709. OrthographicFrustum.unpack = function (array, startingIndex, result) {
  710. //>>includeStart('debug', pragmas.debug);
  711. RuntimeError.Check.defined("array", array);
  712. //>>includeEnd('debug');
  713. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  714. if (!defaultValue.defined(result)) {
  715. result = new OrthographicFrustum();
  716. }
  717. result.width = array[startingIndex++];
  718. result.aspectRatio = array[startingIndex++];
  719. result.near = array[startingIndex++];
  720. result.far = array[startingIndex];
  721. return result;
  722. };
  723. function update$2(frustum) {
  724. //>>includeStart('debug', pragmas.debug);
  725. if (
  726. !defaultValue.defined(frustum.width) ||
  727. !defaultValue.defined(frustum.aspectRatio) ||
  728. !defaultValue.defined(frustum.near) ||
  729. !defaultValue.defined(frustum.far)
  730. ) {
  731. throw new RuntimeError.DeveloperError(
  732. "width, aspectRatio, near, or far parameters are not set."
  733. );
  734. }
  735. //>>includeEnd('debug');
  736. const f = frustum._offCenterFrustum;
  737. if (
  738. frustum.width !== frustum._width ||
  739. frustum.aspectRatio !== frustum._aspectRatio ||
  740. frustum.near !== frustum._near ||
  741. frustum.far !== frustum._far
  742. ) {
  743. //>>includeStart('debug', pragmas.debug);
  744. if (frustum.aspectRatio < 0) {
  745. throw new RuntimeError.DeveloperError("aspectRatio must be positive.");
  746. }
  747. if (frustum.near < 0 || frustum.near > frustum.far) {
  748. throw new RuntimeError.DeveloperError(
  749. "near must be greater than zero and less than far."
  750. );
  751. }
  752. //>>includeEnd('debug');
  753. frustum._aspectRatio = frustum.aspectRatio;
  754. frustum._width = frustum.width;
  755. frustum._near = frustum.near;
  756. frustum._far = frustum.far;
  757. const ratio = 1.0 / frustum.aspectRatio;
  758. f.right = frustum.width * 0.5;
  759. f.left = -f.right;
  760. f.top = ratio * f.right;
  761. f.bottom = -f.top;
  762. f.near = frustum.near;
  763. f.far = frustum.far;
  764. }
  765. }
  766. Object.defineProperties(OrthographicFrustum.prototype, {
  767. /**
  768. * Gets the orthographic projection matrix computed from the view frustum.
  769. * @memberof OrthographicFrustum.prototype
  770. * @type {Matrix4}
  771. * @readonly
  772. */
  773. projectionMatrix: {
  774. get: function () {
  775. update$2(this);
  776. return this._offCenterFrustum.projectionMatrix;
  777. },
  778. },
  779. });
  780. /**
  781. * Creates a culling volume for this frustum.
  782. *
  783. * @param {Cartesian3} position The eye position.
  784. * @param {Cartesian3} direction The view direction.
  785. * @param {Cartesian3} up The up direction.
  786. * @returns {CullingVolume} A culling volume at the given position and orientation.
  787. *
  788. * @example
  789. * // Check if a bounding volume intersects the frustum.
  790. * const cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
  791. * const intersect = cullingVolume.computeVisibility(boundingVolume);
  792. */
  793. OrthographicFrustum.prototype.computeCullingVolume = function (
  794. position,
  795. direction,
  796. up
  797. ) {
  798. update$2(this);
  799. return this._offCenterFrustum.computeCullingVolume(position, direction, up);
  800. };
  801. /**
  802. * Returns the pixel's width and height in meters.
  803. *
  804. * @param {Number} drawingBufferWidth The width of the drawing buffer.
  805. * @param {Number} drawingBufferHeight The height of the drawing buffer.
  806. * @param {Number} distance The distance to the near plane in meters.
  807. * @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
  808. * @param {Cartesian2} result The object onto which to store the result.
  809. * @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.
  810. *
  811. * @exception {DeveloperError} drawingBufferWidth must be greater than zero.
  812. * @exception {DeveloperError} drawingBufferHeight must be greater than zero.
  813. * @exception {DeveloperError} pixelRatio must be greater than zero.
  814. *
  815. * @example
  816. * // Example 1
  817. * // Get the width and height of a pixel.
  818. * const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 0.0, scene.pixelRatio, new Cesium.Cartesian2());
  819. */
  820. OrthographicFrustum.prototype.getPixelDimensions = function (
  821. drawingBufferWidth,
  822. drawingBufferHeight,
  823. distance,
  824. pixelRatio,
  825. result
  826. ) {
  827. update$2(this);
  828. return this._offCenterFrustum.getPixelDimensions(
  829. drawingBufferWidth,
  830. drawingBufferHeight,
  831. distance,
  832. pixelRatio,
  833. result
  834. );
  835. };
  836. /**
  837. * Returns a duplicate of a OrthographicFrustum instance.
  838. *
  839. * @param {OrthographicFrustum} [result] The object onto which to store the result.
  840. * @returns {OrthographicFrustum} The modified result parameter or a new OrthographicFrustum instance if one was not provided.
  841. */
  842. OrthographicFrustum.prototype.clone = function (result) {
  843. if (!defaultValue.defined(result)) {
  844. result = new OrthographicFrustum();
  845. }
  846. result.aspectRatio = this.aspectRatio;
  847. result.width = this.width;
  848. result.near = this.near;
  849. result.far = this.far;
  850. // force update of clone to compute matrices
  851. result._aspectRatio = undefined;
  852. result._width = undefined;
  853. result._near = undefined;
  854. result._far = undefined;
  855. this._offCenterFrustum.clone(result._offCenterFrustum);
  856. return result;
  857. };
  858. /**
  859. * Compares the provided OrthographicFrustum componentwise and returns
  860. * <code>true</code> if they are equal, <code>false</code> otherwise.
  861. *
  862. * @param {OrthographicFrustum} [other] The right hand side OrthographicFrustum.
  863. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  864. */
  865. OrthographicFrustum.prototype.equals = function (other) {
  866. if (!defaultValue.defined(other) || !(other instanceof OrthographicFrustum)) {
  867. return false;
  868. }
  869. update$2(this);
  870. update$2(other);
  871. return (
  872. this.width === other.width &&
  873. this.aspectRatio === other.aspectRatio &&
  874. this._offCenterFrustum.equals(other._offCenterFrustum)
  875. );
  876. };
  877. /**
  878. * Compares the provided OrthographicFrustum componentwise and returns
  879. * <code>true</code> if they pass an absolute or relative tolerance test,
  880. * <code>false</code> otherwise.
  881. *
  882. * @param {OrthographicFrustum} other The right hand side OrthographicFrustum.
  883. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  884. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  885. * @returns {Boolean} <code>true</code> if this and other are within the provided epsilon, <code>false</code> otherwise.
  886. */
  887. OrthographicFrustum.prototype.equalsEpsilon = function (
  888. other,
  889. relativeEpsilon,
  890. absoluteEpsilon
  891. ) {
  892. if (!defaultValue.defined(other) || !(other instanceof OrthographicFrustum)) {
  893. return false;
  894. }
  895. update$2(this);
  896. update$2(other);
  897. return (
  898. ComponentDatatype.CesiumMath.equalsEpsilon(
  899. this.width,
  900. other.width,
  901. relativeEpsilon,
  902. absoluteEpsilon
  903. ) &&
  904. ComponentDatatype.CesiumMath.equalsEpsilon(
  905. this.aspectRatio,
  906. other.aspectRatio,
  907. relativeEpsilon,
  908. absoluteEpsilon
  909. ) &&
  910. this._offCenterFrustum.equalsEpsilon(
  911. other._offCenterFrustum,
  912. relativeEpsilon,
  913. absoluteEpsilon
  914. )
  915. );
  916. };
  917. /**
  918. * The viewing frustum is defined by 6 planes.
  919. * Each plane is represented by a {@link Cartesian4} object, where the x, y, and z components
  920. * define the unit vector normal to the plane, and the w component is the distance of the
  921. * plane from the origin/camera position.
  922. *
  923. * @alias PerspectiveOffCenterFrustum
  924. * @constructor
  925. *
  926. * @param {Object} [options] An object with the following properties:
  927. * @param {Number} [options.left] The left clipping plane distance.
  928. * @param {Number} [options.right] The right clipping plane distance.
  929. * @param {Number} [options.top] The top clipping plane distance.
  930. * @param {Number} [options.bottom] The bottom clipping plane distance.
  931. * @param {Number} [options.near=1.0] The near clipping plane distance.
  932. * @param {Number} [options.far=500000000.0] The far clipping plane distance.
  933. *
  934. * @example
  935. * const frustum = new Cesium.PerspectiveOffCenterFrustum({
  936. * left : -1.0,
  937. * right : 1.0,
  938. * top : 1.0,
  939. * bottom : -1.0,
  940. * near : 1.0,
  941. * far : 100.0
  942. * });
  943. *
  944. * @see PerspectiveFrustum
  945. */
  946. function PerspectiveOffCenterFrustum(options) {
  947. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  948. /**
  949. * Defines the left clipping plane.
  950. * @type {Number}
  951. * @default undefined
  952. */
  953. this.left = options.left;
  954. this._left = undefined;
  955. /**
  956. * Defines the right clipping plane.
  957. * @type {Number}
  958. * @default undefined
  959. */
  960. this.right = options.right;
  961. this._right = undefined;
  962. /**
  963. * Defines the top clipping plane.
  964. * @type {Number}
  965. * @default undefined
  966. */
  967. this.top = options.top;
  968. this._top = undefined;
  969. /**
  970. * Defines the bottom clipping plane.
  971. * @type {Number}
  972. * @default undefined
  973. */
  974. this.bottom = options.bottom;
  975. this._bottom = undefined;
  976. /**
  977. * The distance of the near plane.
  978. * @type {Number}
  979. * @default 1.0
  980. */
  981. this.near = defaultValue.defaultValue(options.near, 1.0);
  982. this._near = this.near;
  983. /**
  984. * The distance of the far plane.
  985. * @type {Number}
  986. * @default 500000000.0
  987. */
  988. this.far = defaultValue.defaultValue(options.far, 500000000.0);
  989. this._far = this.far;
  990. this._cullingVolume = new CullingVolume();
  991. this._perspectiveMatrix = new Matrix2.Matrix4();
  992. this._infinitePerspective = new Matrix2.Matrix4();
  993. }
  994. function update$1(frustum) {
  995. //>>includeStart('debug', pragmas.debug);
  996. if (
  997. !defaultValue.defined(frustum.right) ||
  998. !defaultValue.defined(frustum.left) ||
  999. !defaultValue.defined(frustum.top) ||
  1000. !defaultValue.defined(frustum.bottom) ||
  1001. !defaultValue.defined(frustum.near) ||
  1002. !defaultValue.defined(frustum.far)
  1003. ) {
  1004. throw new RuntimeError.DeveloperError(
  1005. "right, left, top, bottom, near, or far parameters are not set."
  1006. );
  1007. }
  1008. //>>includeEnd('debug');
  1009. const t = frustum.top;
  1010. const b = frustum.bottom;
  1011. const r = frustum.right;
  1012. const l = frustum.left;
  1013. const n = frustum.near;
  1014. const f = frustum.far;
  1015. if (
  1016. t !== frustum._top ||
  1017. b !== frustum._bottom ||
  1018. l !== frustum._left ||
  1019. r !== frustum._right ||
  1020. n !== frustum._near ||
  1021. f !== frustum._far
  1022. ) {
  1023. //>>includeStart('debug', pragmas.debug);
  1024. if (frustum.near <= 0 || frustum.near > frustum.far) {
  1025. throw new RuntimeError.DeveloperError(
  1026. "near must be greater than zero and less than far."
  1027. );
  1028. }
  1029. //>>includeEnd('debug');
  1030. frustum._left = l;
  1031. frustum._right = r;
  1032. frustum._top = t;
  1033. frustum._bottom = b;
  1034. frustum._near = n;
  1035. frustum._far = f;
  1036. frustum._perspectiveMatrix = Matrix2.Matrix4.computePerspectiveOffCenter(
  1037. l,
  1038. r,
  1039. b,
  1040. t,
  1041. n,
  1042. f,
  1043. frustum._perspectiveMatrix
  1044. );
  1045. frustum._infinitePerspective = Matrix2.Matrix4.computeInfinitePerspectiveOffCenter(
  1046. l,
  1047. r,
  1048. b,
  1049. t,
  1050. n,
  1051. frustum._infinitePerspective
  1052. );
  1053. }
  1054. }
  1055. Object.defineProperties(PerspectiveOffCenterFrustum.prototype, {
  1056. /**
  1057. * Gets the perspective projection matrix computed from the view frustum.
  1058. * @memberof PerspectiveOffCenterFrustum.prototype
  1059. * @type {Matrix4}
  1060. * @readonly
  1061. *
  1062. * @see PerspectiveOffCenterFrustum#infiniteProjectionMatrix
  1063. */
  1064. projectionMatrix: {
  1065. get: function () {
  1066. update$1(this);
  1067. return this._perspectiveMatrix;
  1068. },
  1069. },
  1070. /**
  1071. * Gets the perspective projection matrix computed from the view frustum with an infinite far plane.
  1072. * @memberof PerspectiveOffCenterFrustum.prototype
  1073. * @type {Matrix4}
  1074. * @readonly
  1075. *
  1076. * @see PerspectiveOffCenterFrustum#projectionMatrix
  1077. */
  1078. infiniteProjectionMatrix: {
  1079. get: function () {
  1080. update$1(this);
  1081. return this._infinitePerspective;
  1082. },
  1083. },
  1084. });
  1085. const getPlanesRight = new Matrix2.Cartesian3();
  1086. const getPlanesNearCenter = new Matrix2.Cartesian3();
  1087. const getPlanesFarCenter = new Matrix2.Cartesian3();
  1088. const getPlanesNormal = new Matrix2.Cartesian3();
  1089. /**
  1090. * Creates a culling volume for this frustum.
  1091. *
  1092. * @param {Cartesian3} position The eye position.
  1093. * @param {Cartesian3} direction The view direction.
  1094. * @param {Cartesian3} up The up direction.
  1095. * @returns {CullingVolume} A culling volume at the given position and orientation.
  1096. *
  1097. * @example
  1098. * // Check if a bounding volume intersects the frustum.
  1099. * const cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
  1100. * const intersect = cullingVolume.computeVisibility(boundingVolume);
  1101. */
  1102. PerspectiveOffCenterFrustum.prototype.computeCullingVolume = function (
  1103. position,
  1104. direction,
  1105. up
  1106. ) {
  1107. //>>includeStart('debug', pragmas.debug);
  1108. if (!defaultValue.defined(position)) {
  1109. throw new RuntimeError.DeveloperError("position is required.");
  1110. }
  1111. if (!defaultValue.defined(direction)) {
  1112. throw new RuntimeError.DeveloperError("direction is required.");
  1113. }
  1114. if (!defaultValue.defined(up)) {
  1115. throw new RuntimeError.DeveloperError("up is required.");
  1116. }
  1117. //>>includeEnd('debug');
  1118. const planes = this._cullingVolume.planes;
  1119. const t = this.top;
  1120. const b = this.bottom;
  1121. const r = this.right;
  1122. const l = this.left;
  1123. const n = this.near;
  1124. const f = this.far;
  1125. const right = Matrix2.Cartesian3.cross(direction, up, getPlanesRight);
  1126. const nearCenter = getPlanesNearCenter;
  1127. Matrix2.Cartesian3.multiplyByScalar(direction, n, nearCenter);
  1128. Matrix2.Cartesian3.add(position, nearCenter, nearCenter);
  1129. const farCenter = getPlanesFarCenter;
  1130. Matrix2.Cartesian3.multiplyByScalar(direction, f, farCenter);
  1131. Matrix2.Cartesian3.add(position, farCenter, farCenter);
  1132. const normal = getPlanesNormal;
  1133. //Left plane computation
  1134. Matrix2.Cartesian3.multiplyByScalar(right, l, normal);
  1135. Matrix2.Cartesian3.add(nearCenter, normal, normal);
  1136. Matrix2.Cartesian3.subtract(normal, position, normal);
  1137. Matrix2.Cartesian3.normalize(normal, normal);
  1138. Matrix2.Cartesian3.cross(normal, up, normal);
  1139. Matrix2.Cartesian3.normalize(normal, normal);
  1140. let plane = planes[0];
  1141. if (!defaultValue.defined(plane)) {
  1142. plane = planes[0] = new Matrix2.Cartesian4();
  1143. }
  1144. plane.x = normal.x;
  1145. plane.y = normal.y;
  1146. plane.z = normal.z;
  1147. plane.w = -Matrix2.Cartesian3.dot(normal, position);
  1148. //Right plane computation
  1149. Matrix2.Cartesian3.multiplyByScalar(right, r, normal);
  1150. Matrix2.Cartesian3.add(nearCenter, normal, normal);
  1151. Matrix2.Cartesian3.subtract(normal, position, normal);
  1152. Matrix2.Cartesian3.cross(up, normal, normal);
  1153. Matrix2.Cartesian3.normalize(normal, normal);
  1154. plane = planes[1];
  1155. if (!defaultValue.defined(plane)) {
  1156. plane = planes[1] = new Matrix2.Cartesian4();
  1157. }
  1158. plane.x = normal.x;
  1159. plane.y = normal.y;
  1160. plane.z = normal.z;
  1161. plane.w = -Matrix2.Cartesian3.dot(normal, position);
  1162. //Bottom plane computation
  1163. Matrix2.Cartesian3.multiplyByScalar(up, b, normal);
  1164. Matrix2.Cartesian3.add(nearCenter, normal, normal);
  1165. Matrix2.Cartesian3.subtract(normal, position, normal);
  1166. Matrix2.Cartesian3.cross(right, normal, normal);
  1167. Matrix2.Cartesian3.normalize(normal, normal);
  1168. plane = planes[2];
  1169. if (!defaultValue.defined(plane)) {
  1170. plane = planes[2] = new Matrix2.Cartesian4();
  1171. }
  1172. plane.x = normal.x;
  1173. plane.y = normal.y;
  1174. plane.z = normal.z;
  1175. plane.w = -Matrix2.Cartesian3.dot(normal, position);
  1176. //Top plane computation
  1177. Matrix2.Cartesian3.multiplyByScalar(up, t, normal);
  1178. Matrix2.Cartesian3.add(nearCenter, normal, normal);
  1179. Matrix2.Cartesian3.subtract(normal, position, normal);
  1180. Matrix2.Cartesian3.cross(normal, right, normal);
  1181. Matrix2.Cartesian3.normalize(normal, normal);
  1182. plane = planes[3];
  1183. if (!defaultValue.defined(plane)) {
  1184. plane = planes[3] = new Matrix2.Cartesian4();
  1185. }
  1186. plane.x = normal.x;
  1187. plane.y = normal.y;
  1188. plane.z = normal.z;
  1189. plane.w = -Matrix2.Cartesian3.dot(normal, position);
  1190. //Near plane computation
  1191. plane = planes[4];
  1192. if (!defaultValue.defined(plane)) {
  1193. plane = planes[4] = new Matrix2.Cartesian4();
  1194. }
  1195. plane.x = direction.x;
  1196. plane.y = direction.y;
  1197. plane.z = direction.z;
  1198. plane.w = -Matrix2.Cartesian3.dot(direction, nearCenter);
  1199. //Far plane computation
  1200. Matrix2.Cartesian3.negate(direction, normal);
  1201. plane = planes[5];
  1202. if (!defaultValue.defined(plane)) {
  1203. plane = planes[5] = new Matrix2.Cartesian4();
  1204. }
  1205. plane.x = normal.x;
  1206. plane.y = normal.y;
  1207. plane.z = normal.z;
  1208. plane.w = -Matrix2.Cartesian3.dot(normal, farCenter);
  1209. return this._cullingVolume;
  1210. };
  1211. /**
  1212. * Returns the pixel's width and height in meters.
  1213. *
  1214. * @param {Number} drawingBufferWidth The width of the drawing buffer.
  1215. * @param {Number} drawingBufferHeight The height of the drawing buffer.
  1216. * @param {Number} distance The distance to the near plane in meters.
  1217. * @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
  1218. * @param {Cartesian2} result The object onto which to store the result.
  1219. * @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.
  1220. *
  1221. * @exception {DeveloperError} drawingBufferWidth must be greater than zero.
  1222. * @exception {DeveloperError} drawingBufferHeight must be greater than zero.
  1223. * @exception {DeveloperError} pixelRatio must be greater than zero.
  1224. *
  1225. * @example
  1226. * // Example 1
  1227. * // Get the width and height of a pixel.
  1228. * const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, scene.pixelRatio, new Cesium.Cartesian2());
  1229. *
  1230. * @example
  1231. * // Example 2
  1232. * // Get the width and height of a pixel if the near plane was set to 'distance'.
  1233. * // For example, get the size of a pixel of an image on a billboard.
  1234. * const position = camera.position;
  1235. * const direction = camera.direction;
  1236. * const toCenter = Cesium.Cartesian3.subtract(primitive.boundingVolume.center, position, new Cesium.Cartesian3()); // vector from camera to a primitive
  1237. * const toCenterProj = Cesium.Cartesian3.multiplyByScalar(direction, Cesium.Cartesian3.dot(direction, toCenter), new Cesium.Cartesian3()); // project vector onto camera direction vector
  1238. * const distance = Cesium.Cartesian3.magnitude(toCenterProj);
  1239. * const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, scene.pixelRatio, new Cesium.Cartesian2());
  1240. */
  1241. PerspectiveOffCenterFrustum.prototype.getPixelDimensions = function (
  1242. drawingBufferWidth,
  1243. drawingBufferHeight,
  1244. distance,
  1245. pixelRatio,
  1246. result
  1247. ) {
  1248. update$1(this);
  1249. //>>includeStart('debug', pragmas.debug);
  1250. if (!defaultValue.defined(drawingBufferWidth) || !defaultValue.defined(drawingBufferHeight)) {
  1251. throw new RuntimeError.DeveloperError(
  1252. "Both drawingBufferWidth and drawingBufferHeight are required."
  1253. );
  1254. }
  1255. if (drawingBufferWidth <= 0) {
  1256. throw new RuntimeError.DeveloperError("drawingBufferWidth must be greater than zero.");
  1257. }
  1258. if (drawingBufferHeight <= 0) {
  1259. throw new RuntimeError.DeveloperError("drawingBufferHeight must be greater than zero.");
  1260. }
  1261. if (!defaultValue.defined(distance)) {
  1262. throw new RuntimeError.DeveloperError("distance is required.");
  1263. }
  1264. if (!defaultValue.defined(pixelRatio)) {
  1265. throw new RuntimeError.DeveloperError("pixelRatio is required");
  1266. }
  1267. if (pixelRatio <= 0) {
  1268. throw new RuntimeError.DeveloperError("pixelRatio must be greater than zero.");
  1269. }
  1270. if (!defaultValue.defined(result)) {
  1271. throw new RuntimeError.DeveloperError("A result object is required.");
  1272. }
  1273. //>>includeEnd('debug');
  1274. const inverseNear = 1.0 / this.near;
  1275. let tanTheta = this.top * inverseNear;
  1276. const pixelHeight =
  1277. (2.0 * pixelRatio * distance * tanTheta) / drawingBufferHeight;
  1278. tanTheta = this.right * inverseNear;
  1279. const pixelWidth =
  1280. (2.0 * pixelRatio * distance * tanTheta) / drawingBufferWidth;
  1281. result.x = pixelWidth;
  1282. result.y = pixelHeight;
  1283. return result;
  1284. };
  1285. /**
  1286. * Returns a duplicate of a PerspectiveOffCenterFrustum instance.
  1287. *
  1288. * @param {PerspectiveOffCenterFrustum} [result] The object onto which to store the result.
  1289. * @returns {PerspectiveOffCenterFrustum} The modified result parameter or a new PerspectiveFrustum instance if one was not provided.
  1290. */
  1291. PerspectiveOffCenterFrustum.prototype.clone = function (result) {
  1292. if (!defaultValue.defined(result)) {
  1293. result = new PerspectiveOffCenterFrustum();
  1294. }
  1295. result.right = this.right;
  1296. result.left = this.left;
  1297. result.top = this.top;
  1298. result.bottom = this.bottom;
  1299. result.near = this.near;
  1300. result.far = this.far;
  1301. // force update of clone to compute matrices
  1302. result._left = undefined;
  1303. result._right = undefined;
  1304. result._top = undefined;
  1305. result._bottom = undefined;
  1306. result._near = undefined;
  1307. result._far = undefined;
  1308. return result;
  1309. };
  1310. /**
  1311. * Compares the provided PerspectiveOffCenterFrustum componentwise and returns
  1312. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1313. *
  1314. * @param {PerspectiveOffCenterFrustum} [other] The right hand side PerspectiveOffCenterFrustum.
  1315. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1316. */
  1317. PerspectiveOffCenterFrustum.prototype.equals = function (other) {
  1318. return (
  1319. defaultValue.defined(other) &&
  1320. other instanceof PerspectiveOffCenterFrustum &&
  1321. this.right === other.right &&
  1322. this.left === other.left &&
  1323. this.top === other.top &&
  1324. this.bottom === other.bottom &&
  1325. this.near === other.near &&
  1326. this.far === other.far
  1327. );
  1328. };
  1329. /**
  1330. * Compares the provided PerspectiveOffCenterFrustum componentwise and returns
  1331. * <code>true</code> if they pass an absolute or relative tolerance test,
  1332. * <code>false</code> otherwise.
  1333. *
  1334. * @param {PerspectiveOffCenterFrustum} other The right hand side PerspectiveOffCenterFrustum.
  1335. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  1336. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1337. * @returns {Boolean} <code>true</code> if this and other are within the provided epsilon, <code>false</code> otherwise.
  1338. */
  1339. PerspectiveOffCenterFrustum.prototype.equalsEpsilon = function (
  1340. other,
  1341. relativeEpsilon,
  1342. absoluteEpsilon
  1343. ) {
  1344. return (
  1345. other === this ||
  1346. (defaultValue.defined(other) &&
  1347. other instanceof PerspectiveOffCenterFrustum &&
  1348. ComponentDatatype.CesiumMath.equalsEpsilon(
  1349. this.right,
  1350. other.right,
  1351. relativeEpsilon,
  1352. absoluteEpsilon
  1353. ) &&
  1354. ComponentDatatype.CesiumMath.equalsEpsilon(
  1355. this.left,
  1356. other.left,
  1357. relativeEpsilon,
  1358. absoluteEpsilon
  1359. ) &&
  1360. ComponentDatatype.CesiumMath.equalsEpsilon(
  1361. this.top,
  1362. other.top,
  1363. relativeEpsilon,
  1364. absoluteEpsilon
  1365. ) &&
  1366. ComponentDatatype.CesiumMath.equalsEpsilon(
  1367. this.bottom,
  1368. other.bottom,
  1369. relativeEpsilon,
  1370. absoluteEpsilon
  1371. ) &&
  1372. ComponentDatatype.CesiumMath.equalsEpsilon(
  1373. this.near,
  1374. other.near,
  1375. relativeEpsilon,
  1376. absoluteEpsilon
  1377. ) &&
  1378. ComponentDatatype.CesiumMath.equalsEpsilon(
  1379. this.far,
  1380. other.far,
  1381. relativeEpsilon,
  1382. absoluteEpsilon
  1383. ))
  1384. );
  1385. };
  1386. /**
  1387. * The viewing frustum is defined by 6 planes.
  1388. * Each plane is represented by a {@link Cartesian4} object, where the x, y, and z components
  1389. * define the unit vector normal to the plane, and the w component is the distance of the
  1390. * plane from the origin/camera position.
  1391. *
  1392. * @alias PerspectiveFrustum
  1393. * @constructor
  1394. *
  1395. * @param {Object} [options] An object with the following properties:
  1396. * @param {Number} [options.fov] The angle of the field of view (FOV), in radians.
  1397. * @param {Number} [options.aspectRatio] The aspect ratio of the frustum's width to it's height.
  1398. * @param {Number} [options.near=1.0] The distance of the near plane.
  1399. * @param {Number} [options.far=500000000.0] The distance of the far plane.
  1400. * @param {Number} [options.xOffset=0.0] The offset in the x direction.
  1401. * @param {Number} [options.yOffset=0.0] The offset in the y direction.
  1402. *
  1403. * @example
  1404. * const frustum = new Cesium.PerspectiveFrustum({
  1405. * fov : Cesium.Math.PI_OVER_THREE,
  1406. * aspectRatio : canvas.clientWidth / canvas.clientHeight
  1407. * near : 1.0,
  1408. * far : 1000.0
  1409. * });
  1410. *
  1411. * @see PerspectiveOffCenterFrustum
  1412. */
  1413. function PerspectiveFrustum(options) {
  1414. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  1415. this._offCenterFrustum = new PerspectiveOffCenterFrustum();
  1416. /**
  1417. * The angle of the field of view (FOV), in radians. This angle will be used
  1418. * as the horizontal FOV if the width is greater than the height, otherwise
  1419. * it will be the vertical FOV.
  1420. * @type {Number}
  1421. * @default undefined
  1422. */
  1423. this.fov = options.fov;
  1424. this._fov = undefined;
  1425. this._fovy = undefined;
  1426. this._sseDenominator = undefined;
  1427. /**
  1428. * The aspect ratio of the frustum's width to it's height.
  1429. * @type {Number}
  1430. * @default undefined
  1431. */
  1432. this.aspectRatio = options.aspectRatio;
  1433. this._aspectRatio = undefined;
  1434. /**
  1435. * The distance of the near plane.
  1436. * @type {Number}
  1437. * @default 1.0
  1438. */
  1439. this.near = defaultValue.defaultValue(options.near, 1.0);
  1440. this._near = this.near;
  1441. /**
  1442. * The distance of the far plane.
  1443. * @type {Number}
  1444. * @default 500000000.0
  1445. */
  1446. this.far = defaultValue.defaultValue(options.far, 500000000.0);
  1447. this._far = this.far;
  1448. /**
  1449. * Offsets the frustum in the x direction.
  1450. * @type {Number}
  1451. * @default 0.0
  1452. */
  1453. this.xOffset = defaultValue.defaultValue(options.xOffset, 0.0);
  1454. this._xOffset = this.xOffset;
  1455. /**
  1456. * Offsets the frustum in the y direction.
  1457. * @type {Number}
  1458. * @default 0.0
  1459. */
  1460. this.yOffset = defaultValue.defaultValue(options.yOffset, 0.0);
  1461. this._yOffset = this.yOffset;
  1462. }
  1463. /**
  1464. * The number of elements used to pack the object into an array.
  1465. * @type {Number}
  1466. */
  1467. PerspectiveFrustum.packedLength = 6;
  1468. /**
  1469. * Stores the provided instance into the provided array.
  1470. *
  1471. * @param {PerspectiveFrustum} value The value to pack.
  1472. * @param {Number[]} array The array to pack into.
  1473. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1474. *
  1475. * @returns {Number[]} The array that was packed into
  1476. */
  1477. PerspectiveFrustum.pack = function (value, array, startingIndex) {
  1478. //>>includeStart('debug', pragmas.debug);
  1479. RuntimeError.Check.typeOf.object("value", value);
  1480. RuntimeError.Check.defined("array", array);
  1481. //>>includeEnd('debug');
  1482. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1483. array[startingIndex++] = value.fov;
  1484. array[startingIndex++] = value.aspectRatio;
  1485. array[startingIndex++] = value.near;
  1486. array[startingIndex++] = value.far;
  1487. array[startingIndex++] = value.xOffset;
  1488. array[startingIndex] = value.yOffset;
  1489. return array;
  1490. };
  1491. /**
  1492. * Retrieves an instance from a packed array.
  1493. *
  1494. * @param {Number[]} array The packed array.
  1495. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1496. * @param {PerspectiveFrustum} [result] The object into which to store the result.
  1497. * @returns {PerspectiveFrustum} The modified result parameter or a new PerspectiveFrustum instance if one was not provided.
  1498. */
  1499. PerspectiveFrustum.unpack = function (array, startingIndex, result) {
  1500. //>>includeStart('debug', pragmas.debug);
  1501. RuntimeError.Check.defined("array", array);
  1502. //>>includeEnd('debug');
  1503. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1504. if (!defaultValue.defined(result)) {
  1505. result = new PerspectiveFrustum();
  1506. }
  1507. result.fov = array[startingIndex++];
  1508. result.aspectRatio = array[startingIndex++];
  1509. result.near = array[startingIndex++];
  1510. result.far = array[startingIndex++];
  1511. result.xOffset = array[startingIndex++];
  1512. result.yOffset = array[startingIndex];
  1513. return result;
  1514. };
  1515. function update(frustum) {
  1516. //>>includeStart('debug', pragmas.debug);
  1517. if (
  1518. !defaultValue.defined(frustum.fov) ||
  1519. !defaultValue.defined(frustum.aspectRatio) ||
  1520. !defaultValue.defined(frustum.near) ||
  1521. !defaultValue.defined(frustum.far)
  1522. ) {
  1523. throw new RuntimeError.DeveloperError(
  1524. "fov, aspectRatio, near, or far parameters are not set."
  1525. );
  1526. }
  1527. //>>includeEnd('debug');
  1528. const f = frustum._offCenterFrustum;
  1529. if (
  1530. frustum.fov !== frustum._fov ||
  1531. frustum.aspectRatio !== frustum._aspectRatio ||
  1532. frustum.near !== frustum._near ||
  1533. frustum.far !== frustum._far ||
  1534. frustum.xOffset !== frustum._xOffset ||
  1535. frustum.yOffset !== frustum._yOffset
  1536. ) {
  1537. //>>includeStart('debug', pragmas.debug);
  1538. if (frustum.fov < 0 || frustum.fov >= Math.PI) {
  1539. throw new RuntimeError.DeveloperError("fov must be in the range [0, PI).");
  1540. }
  1541. if (frustum.aspectRatio < 0) {
  1542. throw new RuntimeError.DeveloperError("aspectRatio must be positive.");
  1543. }
  1544. if (frustum.near < 0 || frustum.near > frustum.far) {
  1545. throw new RuntimeError.DeveloperError(
  1546. "near must be greater than zero and less than far."
  1547. );
  1548. }
  1549. //>>includeEnd('debug');
  1550. frustum._aspectRatio = frustum.aspectRatio;
  1551. frustum._fov = frustum.fov;
  1552. frustum._fovy =
  1553. frustum.aspectRatio <= 1
  1554. ? frustum.fov
  1555. : Math.atan(Math.tan(frustum.fov * 0.5) / frustum.aspectRatio) * 2.0;
  1556. frustum._near = frustum.near;
  1557. frustum._far = frustum.far;
  1558. frustum._sseDenominator = 2.0 * Math.tan(0.5 * frustum._fovy);
  1559. frustum._xOffset = frustum.xOffset;
  1560. frustum._yOffset = frustum.yOffset;
  1561. f.top = frustum.near * Math.tan(0.5 * frustum._fovy);
  1562. f.bottom = -f.top;
  1563. f.right = frustum.aspectRatio * f.top;
  1564. f.left = -f.right;
  1565. f.near = frustum.near;
  1566. f.far = frustum.far;
  1567. f.right += frustum.xOffset;
  1568. f.left += frustum.xOffset;
  1569. f.top += frustum.yOffset;
  1570. f.bottom += frustum.yOffset;
  1571. }
  1572. }
  1573. Object.defineProperties(PerspectiveFrustum.prototype, {
  1574. /**
  1575. * Gets the perspective projection matrix computed from the view frustum.
  1576. * @memberof PerspectiveFrustum.prototype
  1577. * @type {Matrix4}
  1578. * @readonly
  1579. *
  1580. * @see PerspectiveFrustum#infiniteProjectionMatrix
  1581. */
  1582. projectionMatrix: {
  1583. get: function () {
  1584. update(this);
  1585. return this._offCenterFrustum.projectionMatrix;
  1586. },
  1587. },
  1588. /**
  1589. * The perspective projection matrix computed from the view frustum with an infinite far plane.
  1590. * @memberof PerspectiveFrustum.prototype
  1591. * @type {Matrix4}
  1592. * @readonly
  1593. *
  1594. * @see PerspectiveFrustum#projectionMatrix
  1595. */
  1596. infiniteProjectionMatrix: {
  1597. get: function () {
  1598. update(this);
  1599. return this._offCenterFrustum.infiniteProjectionMatrix;
  1600. },
  1601. },
  1602. /**
  1603. * Gets the angle of the vertical field of view, in radians.
  1604. * @memberof PerspectiveFrustum.prototype
  1605. * @type {Number}
  1606. * @readonly
  1607. * @default undefined
  1608. */
  1609. fovy: {
  1610. get: function () {
  1611. update(this);
  1612. return this._fovy;
  1613. },
  1614. },
  1615. /**
  1616. * @readonly
  1617. * @private
  1618. */
  1619. sseDenominator: {
  1620. get: function () {
  1621. update(this);
  1622. return this._sseDenominator;
  1623. },
  1624. },
  1625. });
  1626. /**
  1627. * Creates a culling volume for this frustum.
  1628. *
  1629. * @param {Cartesian3} position The eye position.
  1630. * @param {Cartesian3} direction The view direction.
  1631. * @param {Cartesian3} up The up direction.
  1632. * @returns {CullingVolume} A culling volume at the given position and orientation.
  1633. *
  1634. * @example
  1635. * // Check if a bounding volume intersects the frustum.
  1636. * const cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
  1637. * const intersect = cullingVolume.computeVisibility(boundingVolume);
  1638. */
  1639. PerspectiveFrustum.prototype.computeCullingVolume = function (
  1640. position,
  1641. direction,
  1642. up
  1643. ) {
  1644. update(this);
  1645. return this._offCenterFrustum.computeCullingVolume(position, direction, up);
  1646. };
  1647. /**
  1648. * Returns the pixel's width and height in meters.
  1649. *
  1650. * @param {Number} drawingBufferWidth The width of the drawing buffer.
  1651. * @param {Number} drawingBufferHeight The height of the drawing buffer.
  1652. * @param {Number} distance The distance to the near plane in meters.
  1653. * @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
  1654. * @param {Cartesian2} result The object onto which to store the result.
  1655. * @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.
  1656. *
  1657. * @exception {DeveloperError} drawingBufferWidth must be greater than zero.
  1658. * @exception {DeveloperError} drawingBufferHeight must be greater than zero.
  1659. * @exception {DeveloperError} pixelRatio must be greater than zero.
  1660. *
  1661. * @example
  1662. * // Example 1
  1663. * // Get the width and height of a pixel.
  1664. * const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, scene.pixelRatio, new Cesium.Cartesian2());
  1665. *
  1666. * @example
  1667. * // Example 2
  1668. * // Get the width and height of a pixel if the near plane was set to 'distance'.
  1669. * // For example, get the size of a pixel of an image on a billboard.
  1670. * const position = camera.position;
  1671. * const direction = camera.direction;
  1672. * const toCenter = Cesium.Cartesian3.subtract(primitive.boundingVolume.center, position, new Cesium.Cartesian3()); // vector from camera to a primitive
  1673. * const toCenterProj = Cesium.Cartesian3.multiplyByScalar(direction, Cesium.Cartesian3.dot(direction, toCenter), new Cesium.Cartesian3()); // project vector onto camera direction vector
  1674. * const distance = Cesium.Cartesian3.magnitude(toCenterProj);
  1675. * const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, scene.pixelRatio, new Cesium.Cartesian2());
  1676. */
  1677. PerspectiveFrustum.prototype.getPixelDimensions = function (
  1678. drawingBufferWidth,
  1679. drawingBufferHeight,
  1680. distance,
  1681. pixelRatio,
  1682. result
  1683. ) {
  1684. update(this);
  1685. return this._offCenterFrustum.getPixelDimensions(
  1686. drawingBufferWidth,
  1687. drawingBufferHeight,
  1688. distance,
  1689. pixelRatio,
  1690. result
  1691. );
  1692. };
  1693. /**
  1694. * Returns a duplicate of a PerspectiveFrustum instance.
  1695. *
  1696. * @param {PerspectiveFrustum} [result] The object onto which to store the result.
  1697. * @returns {PerspectiveFrustum} The modified result parameter or a new PerspectiveFrustum instance if one was not provided.
  1698. */
  1699. PerspectiveFrustum.prototype.clone = function (result) {
  1700. if (!defaultValue.defined(result)) {
  1701. result = new PerspectiveFrustum();
  1702. }
  1703. result.aspectRatio = this.aspectRatio;
  1704. result.fov = this.fov;
  1705. result.near = this.near;
  1706. result.far = this.far;
  1707. // force update of clone to compute matrices
  1708. result._aspectRatio = undefined;
  1709. result._fov = undefined;
  1710. result._near = undefined;
  1711. result._far = undefined;
  1712. this._offCenterFrustum.clone(result._offCenterFrustum);
  1713. return result;
  1714. };
  1715. /**
  1716. * Compares the provided PerspectiveFrustum componentwise and returns
  1717. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1718. *
  1719. * @param {PerspectiveFrustum} [other] The right hand side PerspectiveFrustum.
  1720. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1721. */
  1722. PerspectiveFrustum.prototype.equals = function (other) {
  1723. if (!defaultValue.defined(other) || !(other instanceof PerspectiveFrustum)) {
  1724. return false;
  1725. }
  1726. update(this);
  1727. update(other);
  1728. return (
  1729. this.fov === other.fov &&
  1730. this.aspectRatio === other.aspectRatio &&
  1731. this._offCenterFrustum.equals(other._offCenterFrustum)
  1732. );
  1733. };
  1734. /**
  1735. * Compares the provided PerspectiveFrustum componentwise and returns
  1736. * <code>true</code> if they pass an absolute or relative tolerance test,
  1737. * <code>false</code> otherwise.
  1738. *
  1739. * @param {PerspectiveFrustum} other The right hand side PerspectiveFrustum.
  1740. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  1741. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1742. * @returns {Boolean} <code>true</code> if this and other are within the provided epsilon, <code>false</code> otherwise.
  1743. */
  1744. PerspectiveFrustum.prototype.equalsEpsilon = function (
  1745. other,
  1746. relativeEpsilon,
  1747. absoluteEpsilon
  1748. ) {
  1749. if (!defaultValue.defined(other) || !(other instanceof PerspectiveFrustum)) {
  1750. return false;
  1751. }
  1752. update(this);
  1753. update(other);
  1754. return (
  1755. ComponentDatatype.CesiumMath.equalsEpsilon(
  1756. this.fov,
  1757. other.fov,
  1758. relativeEpsilon,
  1759. absoluteEpsilon
  1760. ) &&
  1761. ComponentDatatype.CesiumMath.equalsEpsilon(
  1762. this.aspectRatio,
  1763. other.aspectRatio,
  1764. relativeEpsilon,
  1765. absoluteEpsilon
  1766. ) &&
  1767. this._offCenterFrustum.equalsEpsilon(
  1768. other._offCenterFrustum,
  1769. relativeEpsilon,
  1770. absoluteEpsilon
  1771. )
  1772. );
  1773. };
  1774. const PERSPECTIVE = 0;
  1775. const ORTHOGRAPHIC = 1;
  1776. /**
  1777. * Describes a frustum at the given the origin and orientation.
  1778. *
  1779. * @alias FrustumGeometry
  1780. * @constructor
  1781. *
  1782. * @param {Object} options Object with the following properties:
  1783. * @param {PerspectiveFrustum|OrthographicFrustum} options.frustum The frustum.
  1784. * @param {Cartesian3} options.origin The origin of the frustum.
  1785. * @param {Quaternion} options.orientation The orientation of the frustum.
  1786. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  1787. */
  1788. function FrustumGeometry(options) {
  1789. //>>includeStart('debug', pragmas.debug);
  1790. RuntimeError.Check.typeOf.object("options", options);
  1791. RuntimeError.Check.typeOf.object("options.frustum", options.frustum);
  1792. RuntimeError.Check.typeOf.object("options.origin", options.origin);
  1793. RuntimeError.Check.typeOf.object("options.orientation", options.orientation);
  1794. //>>includeEnd('debug');
  1795. const frustum = options.frustum;
  1796. const orientation = options.orientation;
  1797. const origin = options.origin;
  1798. const vertexFormat = defaultValue.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT);
  1799. // This is private because it is used by DebugCameraPrimitive to draw a multi-frustum by
  1800. // creating multiple FrustumGeometrys. This way the near plane of one frustum doesn't overlap
  1801. // the far plane of another.
  1802. const drawNearPlane = defaultValue.defaultValue(options._drawNearPlane, true);
  1803. let frustumType;
  1804. let frustumPackedLength;
  1805. if (frustum instanceof PerspectiveFrustum) {
  1806. frustumType = PERSPECTIVE;
  1807. frustumPackedLength = PerspectiveFrustum.packedLength;
  1808. } else if (frustum instanceof OrthographicFrustum) {
  1809. frustumType = ORTHOGRAPHIC;
  1810. frustumPackedLength = OrthographicFrustum.packedLength;
  1811. }
  1812. this._frustumType = frustumType;
  1813. this._frustum = frustum.clone();
  1814. this._origin = Matrix2.Cartesian3.clone(origin);
  1815. this._orientation = Transforms.Quaternion.clone(orientation);
  1816. this._drawNearPlane = drawNearPlane;
  1817. this._vertexFormat = vertexFormat;
  1818. this._workerName = "createFrustumGeometry";
  1819. /**
  1820. * The number of elements used to pack the object into an array.
  1821. * @type {Number}
  1822. */
  1823. this.packedLength =
  1824. 2 +
  1825. frustumPackedLength +
  1826. Matrix2.Cartesian3.packedLength +
  1827. Transforms.Quaternion.packedLength +
  1828. VertexFormat.VertexFormat.packedLength;
  1829. }
  1830. /**
  1831. * Stores the provided instance into the provided array.
  1832. *
  1833. * @param {FrustumGeometry} value The value to pack.
  1834. * @param {Number[]} array The array to pack into.
  1835. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1836. *
  1837. * @returns {Number[]} The array that was packed into
  1838. */
  1839. FrustumGeometry.pack = function (value, array, startingIndex) {
  1840. //>>includeStart('debug', pragmas.debug);
  1841. RuntimeError.Check.typeOf.object("value", value);
  1842. RuntimeError.Check.defined("array", array);
  1843. //>>includeEnd('debug');
  1844. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1845. const frustumType = value._frustumType;
  1846. const frustum = value._frustum;
  1847. array[startingIndex++] = frustumType;
  1848. if (frustumType === PERSPECTIVE) {
  1849. PerspectiveFrustum.pack(frustum, array, startingIndex);
  1850. startingIndex += PerspectiveFrustum.packedLength;
  1851. } else {
  1852. OrthographicFrustum.pack(frustum, array, startingIndex);
  1853. startingIndex += OrthographicFrustum.packedLength;
  1854. }
  1855. Matrix2.Cartesian3.pack(value._origin, array, startingIndex);
  1856. startingIndex += Matrix2.Cartesian3.packedLength;
  1857. Transforms.Quaternion.pack(value._orientation, array, startingIndex);
  1858. startingIndex += Transforms.Quaternion.packedLength;
  1859. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  1860. startingIndex += VertexFormat.VertexFormat.packedLength;
  1861. array[startingIndex] = value._drawNearPlane ? 1.0 : 0.0;
  1862. return array;
  1863. };
  1864. const scratchPackPerspective = new PerspectiveFrustum();
  1865. const scratchPackOrthographic = new OrthographicFrustum();
  1866. const scratchPackQuaternion = new Transforms.Quaternion();
  1867. const scratchPackorigin = new Matrix2.Cartesian3();
  1868. const scratchVertexFormat = new VertexFormat.VertexFormat();
  1869. /**
  1870. * Retrieves an instance from a packed array.
  1871. *
  1872. * @param {Number[]} array The packed array.
  1873. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1874. * @param {FrustumGeometry} [result] The object into which to store the result.
  1875. */
  1876. FrustumGeometry.unpack = function (array, startingIndex, result) {
  1877. //>>includeStart('debug', pragmas.debug);
  1878. RuntimeError.Check.defined("array", array);
  1879. //>>includeEnd('debug');
  1880. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1881. const frustumType = array[startingIndex++];
  1882. let frustum;
  1883. if (frustumType === PERSPECTIVE) {
  1884. frustum = PerspectiveFrustum.unpack(
  1885. array,
  1886. startingIndex,
  1887. scratchPackPerspective
  1888. );
  1889. startingIndex += PerspectiveFrustum.packedLength;
  1890. } else {
  1891. frustum = OrthographicFrustum.unpack(
  1892. array,
  1893. startingIndex,
  1894. scratchPackOrthographic
  1895. );
  1896. startingIndex += OrthographicFrustum.packedLength;
  1897. }
  1898. const origin = Matrix2.Cartesian3.unpack(array, startingIndex, scratchPackorigin);
  1899. startingIndex += Matrix2.Cartesian3.packedLength;
  1900. const orientation = Transforms.Quaternion.unpack(
  1901. array,
  1902. startingIndex,
  1903. scratchPackQuaternion
  1904. );
  1905. startingIndex += Transforms.Quaternion.packedLength;
  1906. const vertexFormat = VertexFormat.VertexFormat.unpack(
  1907. array,
  1908. startingIndex,
  1909. scratchVertexFormat
  1910. );
  1911. startingIndex += VertexFormat.VertexFormat.packedLength;
  1912. const drawNearPlane = array[startingIndex] === 1.0;
  1913. if (!defaultValue.defined(result)) {
  1914. return new FrustumGeometry({
  1915. frustum: frustum,
  1916. origin: origin,
  1917. orientation: orientation,
  1918. vertexFormat: vertexFormat,
  1919. _drawNearPlane: drawNearPlane,
  1920. });
  1921. }
  1922. const frustumResult =
  1923. frustumType === result._frustumType ? result._frustum : undefined;
  1924. result._frustum = frustum.clone(frustumResult);
  1925. result._frustumType = frustumType;
  1926. result._origin = Matrix2.Cartesian3.clone(origin, result._origin);
  1927. result._orientation = Transforms.Quaternion.clone(orientation, result._orientation);
  1928. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  1929. result._drawNearPlane = drawNearPlane;
  1930. return result;
  1931. };
  1932. function getAttributes(
  1933. offset,
  1934. normals,
  1935. tangents,
  1936. bitangents,
  1937. st,
  1938. normal,
  1939. tangent,
  1940. bitangent
  1941. ) {
  1942. const stOffset = (offset / 3) * 2;
  1943. for (let i = 0; i < 4; ++i) {
  1944. if (defaultValue.defined(normals)) {
  1945. normals[offset] = normal.x;
  1946. normals[offset + 1] = normal.y;
  1947. normals[offset + 2] = normal.z;
  1948. }
  1949. if (defaultValue.defined(tangents)) {
  1950. tangents[offset] = tangent.x;
  1951. tangents[offset + 1] = tangent.y;
  1952. tangents[offset + 2] = tangent.z;
  1953. }
  1954. if (defaultValue.defined(bitangents)) {
  1955. bitangents[offset] = bitangent.x;
  1956. bitangents[offset + 1] = bitangent.y;
  1957. bitangents[offset + 2] = bitangent.z;
  1958. }
  1959. offset += 3;
  1960. }
  1961. st[stOffset] = 0.0;
  1962. st[stOffset + 1] = 0.0;
  1963. st[stOffset + 2] = 1.0;
  1964. st[stOffset + 3] = 0.0;
  1965. st[stOffset + 4] = 1.0;
  1966. st[stOffset + 5] = 1.0;
  1967. st[stOffset + 6] = 0.0;
  1968. st[stOffset + 7] = 1.0;
  1969. }
  1970. const scratchRotationMatrix = new Matrix2.Matrix3();
  1971. const scratchViewMatrix = new Matrix2.Matrix4();
  1972. const scratchInverseMatrix = new Matrix2.Matrix4();
  1973. const scratchXDirection = new Matrix2.Cartesian3();
  1974. const scratchYDirection = new Matrix2.Cartesian3();
  1975. const scratchZDirection = new Matrix2.Cartesian3();
  1976. const scratchNegativeX = new Matrix2.Cartesian3();
  1977. const scratchNegativeY = new Matrix2.Cartesian3();
  1978. const scratchNegativeZ = new Matrix2.Cartesian3();
  1979. const frustumSplits = new Array(3);
  1980. const frustumCornersNDC = new Array(4);
  1981. frustumCornersNDC[0] = new Matrix2.Cartesian4(-1.0, -1.0, 1.0, 1.0);
  1982. frustumCornersNDC[1] = new Matrix2.Cartesian4(1.0, -1.0, 1.0, 1.0);
  1983. frustumCornersNDC[2] = new Matrix2.Cartesian4(1.0, 1.0, 1.0, 1.0);
  1984. frustumCornersNDC[3] = new Matrix2.Cartesian4(-1.0, 1.0, 1.0, 1.0);
  1985. const scratchFrustumCorners = new Array(4);
  1986. for (let i = 0; i < 4; ++i) {
  1987. scratchFrustumCorners[i] = new Matrix2.Cartesian4();
  1988. }
  1989. FrustumGeometry._computeNearFarPlanes = function (
  1990. origin,
  1991. orientation,
  1992. frustumType,
  1993. frustum,
  1994. positions,
  1995. xDirection,
  1996. yDirection,
  1997. zDirection
  1998. ) {
  1999. const rotationMatrix = Matrix2.Matrix3.fromQuaternion(
  2000. orientation,
  2001. scratchRotationMatrix
  2002. );
  2003. let x = defaultValue.defaultValue(xDirection, scratchXDirection);
  2004. let y = defaultValue.defaultValue(yDirection, scratchYDirection);
  2005. let z = defaultValue.defaultValue(zDirection, scratchZDirection);
  2006. x = Matrix2.Matrix3.getColumn(rotationMatrix, 0, x);
  2007. y = Matrix2.Matrix3.getColumn(rotationMatrix, 1, y);
  2008. z = Matrix2.Matrix3.getColumn(rotationMatrix, 2, z);
  2009. Matrix2.Cartesian3.normalize(x, x);
  2010. Matrix2.Cartesian3.normalize(y, y);
  2011. Matrix2.Cartesian3.normalize(z, z);
  2012. Matrix2.Cartesian3.negate(x, x);
  2013. const view = Matrix2.Matrix4.computeView(origin, z, y, x, scratchViewMatrix);
  2014. let inverseView;
  2015. let inverseViewProjection;
  2016. if (frustumType === PERSPECTIVE) {
  2017. const projection = frustum.projectionMatrix;
  2018. const viewProjection = Matrix2.Matrix4.multiply(
  2019. projection,
  2020. view,
  2021. scratchInverseMatrix
  2022. );
  2023. inverseViewProjection = Matrix2.Matrix4.inverse(
  2024. viewProjection,
  2025. scratchInverseMatrix
  2026. );
  2027. } else {
  2028. inverseView = Matrix2.Matrix4.inverseTransformation(view, scratchInverseMatrix);
  2029. }
  2030. if (defaultValue.defined(inverseViewProjection)) {
  2031. frustumSplits[0] = frustum.near;
  2032. frustumSplits[1] = frustum.far;
  2033. } else {
  2034. frustumSplits[0] = 0.0;
  2035. frustumSplits[1] = frustum.near;
  2036. frustumSplits[2] = frustum.far;
  2037. }
  2038. for (let i = 0; i < 2; ++i) {
  2039. for (let j = 0; j < 4; ++j) {
  2040. let corner = Matrix2.Cartesian4.clone(
  2041. frustumCornersNDC[j],
  2042. scratchFrustumCorners[j]
  2043. );
  2044. if (!defaultValue.defined(inverseViewProjection)) {
  2045. if (defaultValue.defined(frustum._offCenterFrustum)) {
  2046. frustum = frustum._offCenterFrustum;
  2047. }
  2048. const near = frustumSplits[i];
  2049. const far = frustumSplits[i + 1];
  2050. corner.x =
  2051. (corner.x * (frustum.right - frustum.left) +
  2052. frustum.left +
  2053. frustum.right) *
  2054. 0.5;
  2055. corner.y =
  2056. (corner.y * (frustum.top - frustum.bottom) +
  2057. frustum.bottom +
  2058. frustum.top) *
  2059. 0.5;
  2060. corner.z = (corner.z * (near - far) - near - far) * 0.5;
  2061. corner.w = 1.0;
  2062. Matrix2.Matrix4.multiplyByVector(inverseView, corner, corner);
  2063. } else {
  2064. corner = Matrix2.Matrix4.multiplyByVector(
  2065. inverseViewProjection,
  2066. corner,
  2067. corner
  2068. );
  2069. // Reverse perspective divide
  2070. const w = 1.0 / corner.w;
  2071. Matrix2.Cartesian3.multiplyByScalar(corner, w, corner);
  2072. Matrix2.Cartesian3.subtract(corner, origin, corner);
  2073. Matrix2.Cartesian3.normalize(corner, corner);
  2074. const fac = Matrix2.Cartesian3.dot(z, corner);
  2075. Matrix2.Cartesian3.multiplyByScalar(corner, frustumSplits[i] / fac, corner);
  2076. Matrix2.Cartesian3.add(corner, origin, corner);
  2077. }
  2078. positions[12 * i + j * 3] = corner.x;
  2079. positions[12 * i + j * 3 + 1] = corner.y;
  2080. positions[12 * i + j * 3 + 2] = corner.z;
  2081. }
  2082. }
  2083. };
  2084. /**
  2085. * Computes the geometric representation of a frustum, including its vertices, indices, and a bounding sphere.
  2086. *
  2087. * @param {FrustumGeometry} frustumGeometry A description of the frustum.
  2088. * @returns {Geometry|undefined} The computed vertices and indices.
  2089. */
  2090. FrustumGeometry.createGeometry = function (frustumGeometry) {
  2091. const frustumType = frustumGeometry._frustumType;
  2092. const frustum = frustumGeometry._frustum;
  2093. const origin = frustumGeometry._origin;
  2094. const orientation = frustumGeometry._orientation;
  2095. const drawNearPlane = frustumGeometry._drawNearPlane;
  2096. const vertexFormat = frustumGeometry._vertexFormat;
  2097. const numberOfPlanes = drawNearPlane ? 6 : 5;
  2098. let positions = new Float64Array(3 * 4 * 6);
  2099. FrustumGeometry._computeNearFarPlanes(
  2100. origin,
  2101. orientation,
  2102. frustumType,
  2103. frustum,
  2104. positions
  2105. );
  2106. // -x plane
  2107. let offset = 3 * 4 * 2;
  2108. positions[offset] = positions[3 * 4];
  2109. positions[offset + 1] = positions[3 * 4 + 1];
  2110. positions[offset + 2] = positions[3 * 4 + 2];
  2111. positions[offset + 3] = positions[0];
  2112. positions[offset + 4] = positions[1];
  2113. positions[offset + 5] = positions[2];
  2114. positions[offset + 6] = positions[3 * 3];
  2115. positions[offset + 7] = positions[3 * 3 + 1];
  2116. positions[offset + 8] = positions[3 * 3 + 2];
  2117. positions[offset + 9] = positions[3 * 7];
  2118. positions[offset + 10] = positions[3 * 7 + 1];
  2119. positions[offset + 11] = positions[3 * 7 + 2];
  2120. // -y plane
  2121. offset += 3 * 4;
  2122. positions[offset] = positions[3 * 5];
  2123. positions[offset + 1] = positions[3 * 5 + 1];
  2124. positions[offset + 2] = positions[3 * 5 + 2];
  2125. positions[offset + 3] = positions[3];
  2126. positions[offset + 4] = positions[3 + 1];
  2127. positions[offset + 5] = positions[3 + 2];
  2128. positions[offset + 6] = positions[0];
  2129. positions[offset + 7] = positions[1];
  2130. positions[offset + 8] = positions[2];
  2131. positions[offset + 9] = positions[3 * 4];
  2132. positions[offset + 10] = positions[3 * 4 + 1];
  2133. positions[offset + 11] = positions[3 * 4 + 2];
  2134. // +x plane
  2135. offset += 3 * 4;
  2136. positions[offset] = positions[3];
  2137. positions[offset + 1] = positions[3 + 1];
  2138. positions[offset + 2] = positions[3 + 2];
  2139. positions[offset + 3] = positions[3 * 5];
  2140. positions[offset + 4] = positions[3 * 5 + 1];
  2141. positions[offset + 5] = positions[3 * 5 + 2];
  2142. positions[offset + 6] = positions[3 * 6];
  2143. positions[offset + 7] = positions[3 * 6 + 1];
  2144. positions[offset + 8] = positions[3 * 6 + 2];
  2145. positions[offset + 9] = positions[3 * 2];
  2146. positions[offset + 10] = positions[3 * 2 + 1];
  2147. positions[offset + 11] = positions[3 * 2 + 2];
  2148. // +y plane
  2149. offset += 3 * 4;
  2150. positions[offset] = positions[3 * 2];
  2151. positions[offset + 1] = positions[3 * 2 + 1];
  2152. positions[offset + 2] = positions[3 * 2 + 2];
  2153. positions[offset + 3] = positions[3 * 6];
  2154. positions[offset + 4] = positions[3 * 6 + 1];
  2155. positions[offset + 5] = positions[3 * 6 + 2];
  2156. positions[offset + 6] = positions[3 * 7];
  2157. positions[offset + 7] = positions[3 * 7 + 1];
  2158. positions[offset + 8] = positions[3 * 7 + 2];
  2159. positions[offset + 9] = positions[3 * 3];
  2160. positions[offset + 10] = positions[3 * 3 + 1];
  2161. positions[offset + 11] = positions[3 * 3 + 2];
  2162. if (!drawNearPlane) {
  2163. positions = positions.subarray(3 * 4);
  2164. }
  2165. const attributes = new GeometryAttributes.GeometryAttributes({
  2166. position: new GeometryAttribute.GeometryAttribute({
  2167. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  2168. componentsPerAttribute: 3,
  2169. values: positions,
  2170. }),
  2171. });
  2172. if (
  2173. defaultValue.defined(vertexFormat.normal) ||
  2174. defaultValue.defined(vertexFormat.tangent) ||
  2175. defaultValue.defined(vertexFormat.bitangent) ||
  2176. defaultValue.defined(vertexFormat.st)
  2177. ) {
  2178. const normals = defaultValue.defined(vertexFormat.normal)
  2179. ? new Float32Array(3 * 4 * numberOfPlanes)
  2180. : undefined;
  2181. const tangents = defaultValue.defined(vertexFormat.tangent)
  2182. ? new Float32Array(3 * 4 * numberOfPlanes)
  2183. : undefined;
  2184. const bitangents = defaultValue.defined(vertexFormat.bitangent)
  2185. ? new Float32Array(3 * 4 * numberOfPlanes)
  2186. : undefined;
  2187. const st = defaultValue.defined(vertexFormat.st)
  2188. ? new Float32Array(2 * 4 * numberOfPlanes)
  2189. : undefined;
  2190. const x = scratchXDirection;
  2191. const y = scratchYDirection;
  2192. const z = scratchZDirection;
  2193. const negativeX = Matrix2.Cartesian3.negate(x, scratchNegativeX);
  2194. const negativeY = Matrix2.Cartesian3.negate(y, scratchNegativeY);
  2195. const negativeZ = Matrix2.Cartesian3.negate(z, scratchNegativeZ);
  2196. offset = 0;
  2197. if (drawNearPlane) {
  2198. getAttributes(offset, normals, tangents, bitangents, st, negativeZ, x, y); // near
  2199. offset += 3 * 4;
  2200. }
  2201. getAttributes(offset, normals, tangents, bitangents, st, z, negativeX, y); // far
  2202. offset += 3 * 4;
  2203. getAttributes(
  2204. offset,
  2205. normals,
  2206. tangents,
  2207. bitangents,
  2208. st,
  2209. negativeX,
  2210. negativeZ,
  2211. y
  2212. ); // -x
  2213. offset += 3 * 4;
  2214. getAttributes(
  2215. offset,
  2216. normals,
  2217. tangents,
  2218. bitangents,
  2219. st,
  2220. negativeY,
  2221. negativeZ,
  2222. negativeX
  2223. ); // -y
  2224. offset += 3 * 4;
  2225. getAttributes(offset, normals, tangents, bitangents, st, x, z, y); // +x
  2226. offset += 3 * 4;
  2227. getAttributes(offset, normals, tangents, bitangents, st, y, z, negativeX); // +y
  2228. if (defaultValue.defined(normals)) {
  2229. attributes.normal = new GeometryAttribute.GeometryAttribute({
  2230. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  2231. componentsPerAttribute: 3,
  2232. values: normals,
  2233. });
  2234. }
  2235. if (defaultValue.defined(tangents)) {
  2236. attributes.tangent = new GeometryAttribute.GeometryAttribute({
  2237. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  2238. componentsPerAttribute: 3,
  2239. values: tangents,
  2240. });
  2241. }
  2242. if (defaultValue.defined(bitangents)) {
  2243. attributes.bitangent = new GeometryAttribute.GeometryAttribute({
  2244. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  2245. componentsPerAttribute: 3,
  2246. values: bitangents,
  2247. });
  2248. }
  2249. if (defaultValue.defined(st)) {
  2250. attributes.st = new GeometryAttribute.GeometryAttribute({
  2251. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  2252. componentsPerAttribute: 2,
  2253. values: st,
  2254. });
  2255. }
  2256. }
  2257. const indices = new Uint16Array(6 * numberOfPlanes);
  2258. for (let i = 0; i < numberOfPlanes; ++i) {
  2259. const indexOffset = i * 6;
  2260. const index = i * 4;
  2261. indices[indexOffset] = index;
  2262. indices[indexOffset + 1] = index + 1;
  2263. indices[indexOffset + 2] = index + 2;
  2264. indices[indexOffset + 3] = index;
  2265. indices[indexOffset + 4] = index + 2;
  2266. indices[indexOffset + 5] = index + 3;
  2267. }
  2268. return new GeometryAttribute.Geometry({
  2269. attributes: attributes,
  2270. indices: indices,
  2271. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  2272. boundingSphere: Transforms.BoundingSphere.fromVertices(positions),
  2273. });
  2274. };
  2275. exports.FrustumGeometry = FrustumGeometry;
  2276. exports.OrthographicFrustum = OrthographicFrustum;
  2277. exports.PerspectiveFrustum = PerspectiveFrustum;
  2278. }));
  2279. //# sourceMappingURL=FrustumGeometry-85c5ebd2.js.map