智慧水务管理系统 - 精河县供水工程综合管理平台

ClippingPlaneCollection.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. import AttributeCompression from "../Core/AttributeCompression.js";
  2. import Cartesian2 from "../Core/Cartesian2.js";
  3. import Cartesian3 from "../Core/Cartesian3.js";
  4. import Cartesian4 from "../Core/Cartesian4.js";
  5. import Check from "../Core/Check.js";
  6. import Color from "../Core/Color.js";
  7. import Frozen from "../Core/Frozen.js";
  8. import defined from "../Core/defined.js";
  9. import destroyObject from "../Core/destroyObject.js";
  10. import DeveloperError from "../Core/DeveloperError.js";
  11. import Event from "../Core/Event.js";
  12. import Intersect from "../Core/Intersect.js";
  13. import Matrix4 from "../Core/Matrix4.js";
  14. import PixelFormat from "../Core/PixelFormat.js";
  15. import Plane from "../Core/Plane.js";
  16. import ContextLimits from "../Renderer/ContextLimits.js";
  17. import PixelDatatype from "../Renderer/PixelDatatype.js";
  18. import Sampler from "../Renderer/Sampler.js";
  19. import Texture from "../Renderer/Texture.js";
  20. import ClippingPlane from "./ClippingPlane.js";
  21. /**
  22. * Specifies a set of clipping planes. Clipping planes selectively disable rendering in a region on the
  23. * outside of the specified list of {@link ClippingPlane} objects for a single gltf model, 3D Tileset, or the globe.
  24. * <p>
  25. * In general the clipping planes' coordinates are relative to the object they're attached to, so a plane with distance set to 0 will clip
  26. * through the center of the object.
  27. * </p>
  28. * <p>
  29. * For 3D Tiles, the root tile's transform is used to position the clipping planes. If a transform is not defined, the root tile's {@link Cesium3DTile#boundingSphere} is used instead.
  30. * </p>
  31. *
  32. * @alias ClippingPlaneCollection
  33. * @constructor
  34. *
  35. * @param {object} [options] Object with the following properties:
  36. * @param {ClippingPlane[]} [options.planes=[]] An array of {@link ClippingPlane} objects used to selectively disable rendering on the outside of each plane.
  37. * @param {boolean} [options.enabled=true] Determines whether the clipping planes are active.
  38. * @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The 4x4 transformation matrix specifying an additional transform relative to the clipping planes original coordinate system.
  39. * @param {boolean} [options.unionClippingRegions=false] If true, a region will be clipped if it is on the outside of any plane in the collection. Otherwise, a region will only be clipped if it is on the outside of every plane.
  40. * @param {Color} [options.edgeColor=Color.WHITE] The color applied to highlight the edge along which an object is clipped.
  41. * @param {number} [options.edgeWidth=0.0] The width, in pixels, of the highlight applied to the edge along which an object is clipped.
  42. *
  43. * @demo {@link https://sandcastle.cesium.com/?id=3d-tiles-clipping-planes|Clipping 3D Tiles and glTF models.}
  44. * @demo {@link https://sandcastle.cesium.com/?id=terrain-clipping-planes|Clipping the Globe.}
  45. *
  46. * @example
  47. * // This clipping plane's distance is positive, which means its normal
  48. * // is facing the origin. This will clip everything that is behind
  49. * // the plane, which is anything with y coordinate < -5.
  50. * const clippingPlanes = new Cesium.ClippingPlaneCollection({
  51. * planes : [
  52. * new Cesium.ClippingPlane(new Cesium.Cartesian3(0.0, 1.0, 0.0), 5.0)
  53. * ],
  54. * });
  55. * // Create an entity and attach the ClippingPlaneCollection to the model.
  56. * const entity = viewer.entities.add({
  57. * position : Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, 10000),
  58. * model : {
  59. * uri : 'model.gltf',
  60. * minimumPixelSize : 128,
  61. * maximumScale : 20000,
  62. * clippingPlanes : clippingPlanes
  63. * }
  64. * });
  65. * viewer.zoomTo(entity);
  66. */
  67. function ClippingPlaneCollection(options) {
  68. options = options ?? Frozen.EMPTY_OBJECT;
  69. this._planes = [];
  70. // Do partial texture updates if just one plane is dirty.
  71. // If many planes are dirty, refresh the entire texture.
  72. this._dirtyIndex = -1;
  73. this._multipleDirtyPlanes = false;
  74. this._enabled = options.enabled ?? true;
  75. /**
  76. * The 4x4 transformation matrix specifying an additional transform relative to the clipping planes
  77. * original coordinate system.
  78. *
  79. * @type {Matrix4}
  80. * @default Matrix4.IDENTITY
  81. */
  82. this.modelMatrix = Matrix4.clone(options.modelMatrix ?? Matrix4.IDENTITY);
  83. /**
  84. * The color applied to highlight the edge along which an object is clipped.
  85. *
  86. * @type {Color}
  87. * @default Color.WHITE
  88. */
  89. this.edgeColor = Color.clone(options.edgeColor ?? Color.WHITE);
  90. /**
  91. * The width, in pixels, of the highlight applied to the edge along which an object is clipped.
  92. *
  93. * @type {number}
  94. * @default 0.0
  95. */
  96. this.edgeWidth = options.edgeWidth ?? 0.0;
  97. /**
  98. * An event triggered when a new clipping plane is added to the collection. Event handlers
  99. * are passed the new plane and the index at which it was added.
  100. * @type {Event}
  101. * @readonly
  102. */
  103. this.planeAdded = new Event();
  104. /**
  105. * An event triggered when a new clipping plane is removed from the collection. Event handlers
  106. * are passed the new plane and the index from which it was removed.
  107. * @type {Event}
  108. * @readonly
  109. */
  110. this.planeRemoved = new Event();
  111. // If this ClippingPlaneCollection has an owner, only its owner should update or destroy it.
  112. // This is because in a Cesium3DTileset multiple models may reference the tileset's ClippingPlaneCollection.
  113. this._owner = undefined;
  114. const unionClippingRegions = options.unionClippingRegions ?? false;
  115. this._unionClippingRegions = unionClippingRegions;
  116. this._testIntersection = unionClippingRegions
  117. ? unionIntersectFunction
  118. : defaultIntersectFunction;
  119. this._uint8View = undefined;
  120. this._float32View = undefined;
  121. this._clippingPlanesTexture = undefined;
  122. // Add each ClippingPlane object.
  123. const planes = options.planes;
  124. if (defined(planes)) {
  125. const planesLength = planes.length;
  126. for (let i = 0; i < planesLength; ++i) {
  127. this.add(planes[i]);
  128. }
  129. }
  130. }
  131. function unionIntersectFunction(value) {
  132. return value === Intersect.OUTSIDE;
  133. }
  134. function defaultIntersectFunction(value) {
  135. return value === Intersect.INSIDE;
  136. }
  137. Object.defineProperties(ClippingPlaneCollection.prototype, {
  138. /**
  139. * Returns the number of planes in this collection. This is commonly used with
  140. * {@link ClippingPlaneCollection#get} to iterate over all the planes
  141. * in the collection.
  142. *
  143. * @memberof ClippingPlaneCollection.prototype
  144. * @type {number}
  145. * @readonly
  146. */
  147. length: {
  148. get: function () {
  149. return this._planes.length;
  150. },
  151. },
  152. /**
  153. * If true, a region will be clipped if it is on the outside of any plane in the
  154. * collection. Otherwise, a region will only be clipped if it is on the
  155. * outside of every plane.
  156. *
  157. * @memberof ClippingPlaneCollection.prototype
  158. * @type {boolean}
  159. * @default false
  160. */
  161. unionClippingRegions: {
  162. get: function () {
  163. return this._unionClippingRegions;
  164. },
  165. set: function (value) {
  166. if (this._unionClippingRegions === value) {
  167. return;
  168. }
  169. this._unionClippingRegions = value;
  170. this._testIntersection = value
  171. ? unionIntersectFunction
  172. : defaultIntersectFunction;
  173. },
  174. },
  175. /**
  176. * If true, clipping will be enabled.
  177. *
  178. * @memberof ClippingPlaneCollection.prototype
  179. * @type {boolean}
  180. * @default true
  181. */
  182. enabled: {
  183. get: function () {
  184. return this._enabled;
  185. },
  186. set: function (value) {
  187. if (this._enabled === value) {
  188. return;
  189. }
  190. this._enabled = value;
  191. },
  192. },
  193. /**
  194. * Returns a texture containing packed, untransformed clipping planes.
  195. *
  196. * @memberof ClippingPlaneCollection.prototype
  197. * @type {Texture}
  198. * @readonly
  199. * @private
  200. */
  201. texture: {
  202. get: function () {
  203. return this._clippingPlanesTexture;
  204. },
  205. },
  206. /**
  207. * A reference to the ClippingPlaneCollection's owner, if any.
  208. *
  209. * @memberof ClippingPlaneCollection.prototype
  210. * @readonly
  211. * @private
  212. */
  213. owner: {
  214. get: function () {
  215. return this._owner;
  216. },
  217. },
  218. /**
  219. * Returns a Number encapsulating the state for this ClippingPlaneCollection.
  220. *
  221. * Clipping mode is encoded in the sign of the number, which is just the plane count.
  222. * If this value changes, then shader regeneration is necessary.
  223. *
  224. * @memberof ClippingPlaneCollection.prototype
  225. * @returns {number} A Number that describes the ClippingPlaneCollection's state.
  226. * @readonly
  227. * @private
  228. */
  229. clippingPlanesState: {
  230. get: function () {
  231. return this._unionClippingRegions
  232. ? this._planes.length
  233. : -this._planes.length;
  234. },
  235. },
  236. });
  237. function setIndexDirty(collection, index) {
  238. // If there's already a different _dirtyIndex set, more than one plane has changed since update.
  239. // Entire texture must be reloaded
  240. collection._multipleDirtyPlanes =
  241. collection._multipleDirtyPlanes ||
  242. (collection._dirtyIndex !== -1 && collection._dirtyIndex !== index);
  243. collection._dirtyIndex = index;
  244. }
  245. /**
  246. * Adds the specified {@link ClippingPlane} to the collection to be used to selectively disable rendering
  247. * on the outside of each plane. Use {@link ClippingPlaneCollection#unionClippingRegions} to modify
  248. * how modify the clipping behavior of multiple planes.
  249. *
  250. * @param {ClippingPlane} plane The ClippingPlane to add to the collection.
  251. *
  252. * @see ClippingPlaneCollection#unionClippingRegions
  253. * @see ClippingPlaneCollection#remove
  254. * @see ClippingPlaneCollection#removeAll
  255. */
  256. ClippingPlaneCollection.prototype.add = function (plane) {
  257. const newPlaneIndex = this._planes.length;
  258. const that = this;
  259. plane.onChangeCallback = function (index) {
  260. setIndexDirty(that, index);
  261. };
  262. plane.index = newPlaneIndex;
  263. setIndexDirty(this, newPlaneIndex);
  264. this._planes.push(plane);
  265. this.planeAdded.raiseEvent(plane, newPlaneIndex);
  266. };
  267. /**
  268. * Returns the plane in the collection at the specified index. Indices are zero-based
  269. * and increase as planes are added. Removing a plane shifts all planes after
  270. * it to the left, changing their indices. This function is commonly used with
  271. * {@link ClippingPlaneCollection#length} to iterate over all the planes
  272. * in the collection.
  273. *
  274. * @param {number} index The zero-based index of the plane.
  275. * @returns {ClippingPlane} The ClippingPlane at the specified index.
  276. *
  277. * @see ClippingPlaneCollection#length
  278. */
  279. ClippingPlaneCollection.prototype.get = function (index) {
  280. //>>includeStart('debug', pragmas.debug);
  281. Check.typeOf.number("index", index);
  282. //>>includeEnd('debug');
  283. return this._planes[index];
  284. };
  285. function indexOf(planes, plane) {
  286. const length = planes.length;
  287. for (let i = 0; i < length; ++i) {
  288. if (Plane.equals(planes[i], plane)) {
  289. return i;
  290. }
  291. }
  292. return -1;
  293. }
  294. /**
  295. * Checks whether this collection contains a ClippingPlane equal to the given ClippingPlane.
  296. *
  297. * @param {ClippingPlane} [clippingPlane] The ClippingPlane to check for.
  298. * @returns {boolean} true if this collection contains the ClippingPlane, false otherwise.
  299. *
  300. * @see ClippingPlaneCollection#get
  301. */
  302. ClippingPlaneCollection.prototype.contains = function (clippingPlane) {
  303. return indexOf(this._planes, clippingPlane) !== -1;
  304. };
  305. /**
  306. * Removes the first occurrence of the given ClippingPlane from the collection.
  307. *
  308. * @param {ClippingPlane} clippingPlane
  309. * @returns {boolean} <code>true</code> if the plane was removed; <code>false</code> if the plane was not found in the collection.
  310. *
  311. * @see ClippingPlaneCollection#add
  312. * @see ClippingPlaneCollection#contains
  313. * @see ClippingPlaneCollection#removeAll
  314. */
  315. ClippingPlaneCollection.prototype.remove = function (clippingPlane) {
  316. const planes = this._planes;
  317. const index = indexOf(planes, clippingPlane);
  318. if (index === -1) {
  319. return false;
  320. }
  321. // Unlink this ClippingPlaneCollection from the ClippingPlane
  322. if (clippingPlane instanceof ClippingPlane) {
  323. clippingPlane.onChangeCallback = undefined;
  324. clippingPlane.index = -1;
  325. }
  326. // Shift and update indices
  327. const length = planes.length - 1;
  328. for (let i = index; i < length; ++i) {
  329. const planeToKeep = planes[i + 1];
  330. planes[i] = planeToKeep;
  331. if (planeToKeep instanceof ClippingPlane) {
  332. planeToKeep.index = i;
  333. }
  334. }
  335. // Indicate planes texture is dirty
  336. this._multipleDirtyPlanes = true;
  337. planes.length = length;
  338. this.planeRemoved.raiseEvent(clippingPlane, index);
  339. return true;
  340. };
  341. /**
  342. * Removes all planes from the collection.
  343. *
  344. * @see ClippingPlaneCollection#add
  345. * @see ClippingPlaneCollection#remove
  346. */
  347. ClippingPlaneCollection.prototype.removeAll = function () {
  348. // Dereference this ClippingPlaneCollection from all ClippingPlanes
  349. const planes = this._planes;
  350. const planesCount = planes.length;
  351. for (let i = 0; i < planesCount; ++i) {
  352. const plane = planes[i];
  353. if (plane instanceof ClippingPlane) {
  354. plane.onChangeCallback = undefined;
  355. plane.index = -1;
  356. }
  357. this.planeRemoved.raiseEvent(plane, i);
  358. }
  359. this._multipleDirtyPlanes = true;
  360. this._planes = [];
  361. };
  362. const distanceEncodeScratch = new Cartesian4();
  363. const oct32EncodeScratch = new Cartesian4();
  364. function packPlanesAsUint8(clippingPlaneCollection, startIndex, endIndex) {
  365. const uint8View = clippingPlaneCollection._uint8View;
  366. const planes = clippingPlaneCollection._planes;
  367. let byteIndex = 0;
  368. for (let i = startIndex; i < endIndex; ++i) {
  369. const plane = planes[i];
  370. const oct32Normal = AttributeCompression.octEncodeToCartesian4(
  371. plane.normal,
  372. oct32EncodeScratch,
  373. );
  374. uint8View[byteIndex] = oct32Normal.x;
  375. uint8View[byteIndex + 1] = oct32Normal.y;
  376. uint8View[byteIndex + 2] = oct32Normal.z;
  377. uint8View[byteIndex + 3] = oct32Normal.w;
  378. const encodedDistance = Cartesian4.packFloat(
  379. plane.distance,
  380. distanceEncodeScratch,
  381. );
  382. uint8View[byteIndex + 4] = encodedDistance.x;
  383. uint8View[byteIndex + 5] = encodedDistance.y;
  384. uint8View[byteIndex + 6] = encodedDistance.z;
  385. uint8View[byteIndex + 7] = encodedDistance.w;
  386. byteIndex += 8;
  387. }
  388. }
  389. // Pack starting at the beginning of the buffer to allow partial update
  390. function packPlanesAsFloats(clippingPlaneCollection, startIndex, endIndex) {
  391. const float32View = clippingPlaneCollection._float32View;
  392. const planes = clippingPlaneCollection._planes;
  393. let floatIndex = 0;
  394. for (let i = startIndex; i < endIndex; ++i) {
  395. const plane = planes[i];
  396. const normal = plane.normal;
  397. float32View[floatIndex] = normal.x;
  398. float32View[floatIndex + 1] = normal.y;
  399. float32View[floatIndex + 2] = normal.z;
  400. float32View[floatIndex + 3] = plane.distance;
  401. floatIndex += 4; // each plane is 4 floats
  402. }
  403. }
  404. function computeTextureResolution(pixelsNeeded, result) {
  405. const maxSize = ContextLimits.maximumTextureSize;
  406. result.x = Math.min(pixelsNeeded, maxSize);
  407. result.y = Math.ceil(pixelsNeeded / result.x);
  408. return result;
  409. }
  410. const textureResolutionScratch = new Cartesian2();
  411. /**
  412. * Called when {@link Viewer} or {@link CesiumWidget} render the scene to
  413. * build the resources for clipping planes.
  414. * <p>
  415. * Do not call this function directly.
  416. * </p>
  417. */
  418. ClippingPlaneCollection.prototype.update = function (frameState) {
  419. let clippingPlanesTexture = this._clippingPlanesTexture;
  420. const context = frameState.context;
  421. const useFloatTexture = ClippingPlaneCollection.useFloatTexture(context);
  422. // Compute texture requirements for current planes
  423. // In RGBA FLOAT, A plane is 4 floats packed to a RGBA.
  424. // In RGBA UNSIGNED_BYTE, A plane is a float in [0, 1) packed to RGBA and an Oct32 quantized normal,
  425. // so 8 bytes or 2 pixels in RGBA.
  426. const pixelsNeeded = useFloatTexture ? this.length : this.length * 2;
  427. if (defined(clippingPlanesTexture)) {
  428. const currentPixelCount =
  429. clippingPlanesTexture.width * clippingPlanesTexture.height;
  430. // Recreate the texture to double current requirement if it isn't big enough or is 4 times larger than it needs to be.
  431. // Optimization note: this isn't exactly the classic resizeable array algorithm
  432. // * not necessarily checking for resize after each add/remove operation
  433. // * random-access deletes instead of just pops
  434. // * alloc ops likely more expensive than demonstrable via big-O analysis
  435. if (
  436. currentPixelCount < pixelsNeeded ||
  437. pixelsNeeded < 0.25 * currentPixelCount
  438. ) {
  439. clippingPlanesTexture.destroy();
  440. clippingPlanesTexture = undefined;
  441. this._clippingPlanesTexture = undefined;
  442. }
  443. }
  444. // If there are no clipping planes, there's nothing to update.
  445. if (this.length === 0) {
  446. return;
  447. }
  448. if (!defined(clippingPlanesTexture)) {
  449. const requiredResolution = computeTextureResolution(
  450. pixelsNeeded,
  451. textureResolutionScratch,
  452. );
  453. // Allocate twice as much space as needed to avoid frequent texture reallocation.
  454. // Allocate in the Y direction, since texture may be as wide as context texture support.
  455. requiredResolution.y *= 2;
  456. if (useFloatTexture) {
  457. clippingPlanesTexture = new Texture({
  458. context: context,
  459. width: requiredResolution.x,
  460. height: requiredResolution.y,
  461. pixelFormat: PixelFormat.RGBA,
  462. pixelDatatype: PixelDatatype.FLOAT,
  463. sampler: Sampler.NEAREST,
  464. flipY: false,
  465. });
  466. this._float32View = new Float32Array(
  467. requiredResolution.x * requiredResolution.y * 4,
  468. );
  469. } else {
  470. clippingPlanesTexture = new Texture({
  471. context: context,
  472. width: requiredResolution.x,
  473. height: requiredResolution.y,
  474. pixelFormat: PixelFormat.RGBA,
  475. pixelDatatype: PixelDatatype.UNSIGNED_BYTE,
  476. sampler: Sampler.NEAREST,
  477. flipY: false,
  478. });
  479. this._uint8View = new Uint8Array(
  480. requiredResolution.x * requiredResolution.y * 4,
  481. );
  482. }
  483. this._clippingPlanesTexture = clippingPlanesTexture;
  484. this._multipleDirtyPlanes = true;
  485. }
  486. const dirtyIndex = this._dirtyIndex;
  487. if (!this._multipleDirtyPlanes && dirtyIndex === -1) {
  488. return;
  489. }
  490. if (!this._multipleDirtyPlanes) {
  491. // partial updates possible
  492. let offsetX = 0;
  493. let offsetY = 0;
  494. if (useFloatTexture) {
  495. offsetY = Math.floor(dirtyIndex / clippingPlanesTexture.width);
  496. offsetX = Math.floor(dirtyIndex - offsetY * clippingPlanesTexture.width);
  497. packPlanesAsFloats(this, dirtyIndex, dirtyIndex + 1);
  498. clippingPlanesTexture.copyFrom({
  499. source: {
  500. width: 1,
  501. height: 1,
  502. arrayBufferView: this._float32View,
  503. },
  504. xOffset: offsetX,
  505. yOffset: offsetY,
  506. });
  507. } else {
  508. offsetY = Math.floor((dirtyIndex * 2) / clippingPlanesTexture.width);
  509. offsetX = Math.floor(
  510. dirtyIndex * 2 - offsetY * clippingPlanesTexture.width,
  511. );
  512. packPlanesAsUint8(this, dirtyIndex, dirtyIndex + 1);
  513. clippingPlanesTexture.copyFrom({
  514. source: {
  515. width: 2,
  516. height: 1,
  517. arrayBufferView: this._uint8View,
  518. },
  519. xOffset: offsetX,
  520. yOffset: offsetY,
  521. });
  522. }
  523. } else if (useFloatTexture) {
  524. packPlanesAsFloats(this, 0, this._planes.length);
  525. clippingPlanesTexture.copyFrom({
  526. source: {
  527. width: clippingPlanesTexture.width,
  528. height: clippingPlanesTexture.height,
  529. arrayBufferView: this._float32View,
  530. },
  531. });
  532. } else {
  533. packPlanesAsUint8(this, 0, this._planes.length);
  534. clippingPlanesTexture.copyFrom({
  535. source: {
  536. width: clippingPlanesTexture.width,
  537. height: clippingPlanesTexture.height,
  538. arrayBufferView: this._uint8View,
  539. },
  540. });
  541. }
  542. this._multipleDirtyPlanes = false;
  543. this._dirtyIndex = -1;
  544. };
  545. const scratchMatrix = new Matrix4();
  546. const scratchPlane = new Plane(Cartesian3.UNIT_X, 0.0);
  547. /**
  548. * Determines the type intersection with the planes of this ClippingPlaneCollection instance and the specified {@link TileBoundingVolume}.
  549. * @private
  550. *
  551. * @param {object} tileBoundingVolume The volume to determine the intersection with the planes.
  552. * @param {Matrix4} [transform] An optional, additional matrix to transform the plane to world coordinates.
  553. * @returns {Intersect} {@link Intersect.INSIDE} if the entire volume is on the side of the planes
  554. * the normal is pointing and should be entirely rendered, {@link Intersect.OUTSIDE}
  555. * if the entire volume is on the opposite side and should be clipped, and
  556. * {@link Intersect.INTERSECTING} if the volume intersects the planes.
  557. */
  558. ClippingPlaneCollection.prototype.computeIntersectionWithBoundingVolume =
  559. function (tileBoundingVolume, transform) {
  560. const planes = this._planes;
  561. const length = planes.length;
  562. let modelMatrix = this.modelMatrix;
  563. if (defined(transform)) {
  564. modelMatrix = Matrix4.multiply(transform, modelMatrix, scratchMatrix);
  565. }
  566. // If the collection is not set to union the clipping regions, the volume must be outside of all planes to be
  567. // considered completely clipped. If the collection is set to union the clipping regions, if the volume can be
  568. // outside any the planes, it is considered completely clipped.
  569. // Lastly, if not completely clipped, if any plane is intersecting, more calculations must be performed.
  570. let intersection = Intersect.INSIDE;
  571. if (!this.unionClippingRegions && length > 0) {
  572. intersection = Intersect.OUTSIDE;
  573. }
  574. for (let i = 0; i < length; ++i) {
  575. const plane = planes[i];
  576. Plane.transform(plane, modelMatrix, scratchPlane); // ClippingPlane can be used for Plane math
  577. const value = tileBoundingVolume.intersectPlane(scratchPlane);
  578. if (value === Intersect.INTERSECTING) {
  579. intersection = value;
  580. } else if (this._testIntersection(value)) {
  581. return value;
  582. }
  583. }
  584. return intersection;
  585. };
  586. /**
  587. * Sets the owner for the input ClippingPlaneCollection if there wasn't another owner.
  588. * Destroys the owner's previous ClippingPlaneCollection if setting is successful.
  589. *
  590. * @param {ClippingPlaneCollection} [clippingPlaneCollection] A ClippingPlaneCollection (or undefined) being attached to an object
  591. * @param {object} owner An Object that should receive the new ClippingPlaneCollection
  592. * @param {string} key The Key for the Object to reference the ClippingPlaneCollection
  593. * @private
  594. */
  595. ClippingPlaneCollection.setOwner = function (
  596. clippingPlaneCollection,
  597. owner,
  598. key,
  599. ) {
  600. // Don't destroy the ClippingPlaneCollection if it is already owned by newOwner
  601. if (clippingPlaneCollection === owner[key]) {
  602. return;
  603. }
  604. // Destroy the existing ClippingPlaneCollection, if any
  605. owner[key] = owner[key] && owner[key].destroy();
  606. if (defined(clippingPlaneCollection)) {
  607. //>>includeStart('debug', pragmas.debug);
  608. if (defined(clippingPlaneCollection._owner)) {
  609. throw new DeveloperError(
  610. "ClippingPlaneCollection should only be assigned to one object",
  611. );
  612. }
  613. //>>includeEnd('debug');
  614. clippingPlaneCollection._owner = owner;
  615. owner[key] = clippingPlaneCollection;
  616. }
  617. };
  618. /**
  619. * Function for checking if the context will allow clipping planes with floating point textures.
  620. *
  621. * @param {Context} context The Context that will contain clipped objects and clipping textures.
  622. * @returns {boolean} <code>true</code> if floating point textures can be used for clipping planes.
  623. * @private
  624. */
  625. ClippingPlaneCollection.useFloatTexture = function (context) {
  626. return context.floatingPointTexture;
  627. };
  628. /**
  629. * Function for getting the clipping plane collection's texture resolution.
  630. * If the ClippingPlaneCollection hasn't been updated, returns the resolution that will be
  631. * allocated based on the current plane count.
  632. *
  633. * @param {ClippingPlaneCollection} clippingPlaneCollection The clipping plane collection
  634. * @param {Context} context The rendering context
  635. * @param {Cartesian2} result A Cartesian2 for the result.
  636. * @returns {Cartesian2} The required resolution.
  637. * @private
  638. */
  639. ClippingPlaneCollection.getTextureResolution = function (
  640. clippingPlaneCollection,
  641. context,
  642. result,
  643. ) {
  644. const texture = clippingPlaneCollection.texture;
  645. if (defined(texture)) {
  646. result.x = texture.width;
  647. result.y = texture.height;
  648. return result;
  649. }
  650. const pixelsNeeded = ClippingPlaneCollection.useFloatTexture(context)
  651. ? clippingPlaneCollection.length
  652. : clippingPlaneCollection.length * 2;
  653. const requiredResolution = computeTextureResolution(pixelsNeeded, result);
  654. // Allocate twice as much space as needed to avoid frequent texture reallocation.
  655. requiredResolution.y *= 2;
  656. return requiredResolution;
  657. };
  658. /**
  659. * Returns true if this object was destroyed; otherwise, false.
  660. * <br /><br />
  661. * If this object was destroyed, it should not be used; calling any function other than
  662. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  663. *
  664. * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  665. *
  666. * @see ClippingPlaneCollection#destroy
  667. */
  668. ClippingPlaneCollection.prototype.isDestroyed = function () {
  669. return false;
  670. };
  671. /**
  672. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  673. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  674. * <br /><br />
  675. * Once an object is destroyed, it should not be used; calling any function other than
  676. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  677. * assign the return value (<code>undefined</code>) to the object as done in the example.
  678. *
  679. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  680. *
  681. *
  682. * @example
  683. * clippingPlanes = clippingPlanes && clippingPlanes.destroy();
  684. *
  685. * @see ClippingPlaneCollection#isDestroyed
  686. */
  687. ClippingPlaneCollection.prototype.destroy = function () {
  688. this._clippingPlanesTexture =
  689. this._clippingPlanesTexture && this._clippingPlanesTexture.destroy();
  690. return destroyObject(this);
  691. };
  692. export default ClippingPlaneCollection;