ClippingPlaneCollection.js 26 KB

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