ClippingPlaneCollection.js 29 KB

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