PointCloud3DTileContent.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import Color from "../Core/Color.js";
  2. import combine from "../Core/combine.js";
  3. import defaultValue from "../Core/defaultValue.js";
  4. import defined from "../Core/defined.js";
  5. import destroyObject from "../Core/destroyObject.js";
  6. import DeveloperError from "../Core/DeveloperError.js";
  7. import CesiumMath from "../Core/Math.js";
  8. import Pass from "../Renderer/Pass.js";
  9. import Cesium3DTileBatchTable from "./Cesium3DTileBatchTable.js";
  10. import Cesium3DTileFeature from "./Cesium3DTileFeature.js";
  11. import Cesium3DTileRefine from "./Cesium3DTileRefine.js";
  12. import PointCloud from "./PointCloud.js";
  13. import PointCloudShading from "./PointCloudShading.js";
  14. import SceneMode from "./SceneMode.js";
  15. /**
  16. * Represents the contents of a
  17. * {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/TileFormats/PointCloud|Point Cloud}
  18. * tile in a {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification|3D Tiles} tileset.
  19. * <p>
  20. * Implements the {@link Cesium3DTileContent} interface.
  21. * </p>
  22. *
  23. * @alias PointCloud3DTileContent
  24. * @constructor
  25. *
  26. * @private
  27. */
  28. function PointCloud3DTileContent(
  29. tileset,
  30. tile,
  31. resource,
  32. arrayBuffer,
  33. byteOffset
  34. ) {
  35. this._tileset = tileset;
  36. this._tile = tile;
  37. this._resource = resource;
  38. this._metadata = undefined;
  39. this._pickId = undefined; // Only defined when batchTable is undefined
  40. this._batchTable = undefined; // Used when feature table contains BATCH_ID semantic
  41. this._styleDirty = false;
  42. this._features = undefined;
  43. this.featurePropertiesDirty = false;
  44. this._group = undefined;
  45. this._pointCloud = new PointCloud({
  46. arrayBuffer: arrayBuffer,
  47. byteOffset: byteOffset,
  48. cull: false,
  49. opaquePass: Pass.CESIUM_3D_TILE,
  50. vertexShaderLoaded: getVertexShaderLoaded(this),
  51. fragmentShaderLoaded: getFragmentShaderLoaded(this),
  52. uniformMapLoaded: getUniformMapLoaded(this),
  53. batchTableLoaded: getBatchTableLoaded(this),
  54. pickIdLoaded: getPickIdLoaded(this),
  55. });
  56. }
  57. Object.defineProperties(PointCloud3DTileContent.prototype, {
  58. featuresLength: {
  59. get: function () {
  60. if (defined(this._batchTable)) {
  61. return this._batchTable.featuresLength;
  62. }
  63. return 0;
  64. },
  65. },
  66. pointsLength: {
  67. get: function () {
  68. return this._pointCloud.pointsLength;
  69. },
  70. },
  71. trianglesLength: {
  72. get: function () {
  73. return 0;
  74. },
  75. },
  76. geometryByteLength: {
  77. get: function () {
  78. return this._pointCloud.geometryByteLength;
  79. },
  80. },
  81. texturesByteLength: {
  82. get: function () {
  83. return 0;
  84. },
  85. },
  86. batchTableByteLength: {
  87. get: function () {
  88. if (defined(this._batchTable)) {
  89. return this._batchTable.memorySizeInBytes;
  90. }
  91. return 0;
  92. },
  93. },
  94. innerContents: {
  95. get: function () {
  96. return undefined;
  97. },
  98. },
  99. readyPromise: {
  100. get: function () {
  101. return this._pointCloud.readyPromise;
  102. },
  103. },
  104. tileset: {
  105. get: function () {
  106. return this._tileset;
  107. },
  108. },
  109. tile: {
  110. get: function () {
  111. return this._tile;
  112. },
  113. },
  114. url: {
  115. get: function () {
  116. return this._resource.getUrlComponent(true);
  117. },
  118. },
  119. metadata: {
  120. get: function () {
  121. return this._metadata;
  122. },
  123. set: function (value) {
  124. this._metadata = value;
  125. },
  126. },
  127. batchTable: {
  128. get: function () {
  129. return this._batchTable;
  130. },
  131. },
  132. group: {
  133. get: function () {
  134. return this._group;
  135. },
  136. set: function (value) {
  137. this._group = value;
  138. },
  139. },
  140. });
  141. function getVertexShaderLoaded(content) {
  142. return function (vs) {
  143. if (defined(content._batchTable)) {
  144. return content._batchTable.getVertexShaderCallback(
  145. false,
  146. "a_batchId",
  147. undefined
  148. )(vs);
  149. }
  150. return vs;
  151. };
  152. }
  153. function getFragmentShaderLoaded(content) {
  154. return function (fs) {
  155. if (defined(content._batchTable)) {
  156. return content._batchTable.getFragmentShaderCallback(
  157. false,
  158. undefined,
  159. false
  160. )(fs);
  161. }
  162. return `uniform vec4 czm_pickColor;\n${fs}`;
  163. };
  164. }
  165. function getUniformMapLoaded(content) {
  166. return function (uniformMap) {
  167. if (defined(content._batchTable)) {
  168. return content._batchTable.getUniformMapCallback()(uniformMap);
  169. }
  170. return combine(uniformMap, {
  171. czm_pickColor: function () {
  172. return content._pickId.color;
  173. },
  174. });
  175. };
  176. }
  177. function getBatchTableLoaded(content) {
  178. return function (batchLength, batchTableJson, batchTableBinary) {
  179. content._batchTable = new Cesium3DTileBatchTable(
  180. content,
  181. batchLength,
  182. batchTableJson,
  183. batchTableBinary
  184. );
  185. };
  186. }
  187. function getPickIdLoaded(content) {
  188. return function () {
  189. return defined(content._batchTable)
  190. ? content._batchTable.getPickId()
  191. : "czm_pickColor";
  192. };
  193. }
  194. function getGeometricError(content) {
  195. const pointCloudShading = content._tileset.pointCloudShading;
  196. const sphereVolume = content._tile.contentBoundingVolume.boundingSphere.volume();
  197. const baseResolutionApproximation = CesiumMath.cbrt(
  198. sphereVolume / content.pointsLength
  199. );
  200. let geometricError = content._tile.geometricError;
  201. if (geometricError === 0) {
  202. if (
  203. defined(pointCloudShading) &&
  204. defined(pointCloudShading.baseResolution)
  205. ) {
  206. geometricError = pointCloudShading.baseResolution;
  207. } else {
  208. geometricError = baseResolutionApproximation;
  209. }
  210. }
  211. return geometricError;
  212. }
  213. function createFeatures(content) {
  214. const featuresLength = content.featuresLength;
  215. if (!defined(content._features) && featuresLength > 0) {
  216. const features = new Array(featuresLength);
  217. for (let i = 0; i < featuresLength; ++i) {
  218. features[i] = new Cesium3DTileFeature(content, i);
  219. }
  220. content._features = features;
  221. }
  222. }
  223. PointCloud3DTileContent.prototype.hasProperty = function (batchId, name) {
  224. if (defined(this._batchTable)) {
  225. return this._batchTable.hasProperty(batchId, name);
  226. }
  227. return false;
  228. };
  229. /**
  230. * Part of the {@link Cesium3DTileContent} interface.
  231. *
  232. * In this context a feature refers to a group of points that share the same BATCH_ID.
  233. * For example all the points that represent a door in a house point cloud would be a feature.
  234. *
  235. * Features are backed by a batch table and can be colored, shown/hidden, picked, etc like features
  236. * in b3dm and i3dm.
  237. *
  238. * When the BATCH_ID semantic is omitted and the point cloud stores per-point properties, they
  239. * are not accessible by getFeature. They are only used for dynamic styling.
  240. */
  241. PointCloud3DTileContent.prototype.getFeature = function (batchId) {
  242. if (!defined(this._batchTable)) {
  243. return undefined;
  244. }
  245. const featuresLength = this.featuresLength;
  246. //>>includeStart('debug', pragmas.debug);
  247. if (!defined(batchId) || batchId < 0 || batchId >= featuresLength) {
  248. throw new DeveloperError(
  249. `batchId is required and between zero and featuresLength - 1 (${
  250. featuresLength - 1
  251. }).`
  252. );
  253. }
  254. //>>includeEnd('debug');
  255. createFeatures(this);
  256. return this._features[batchId];
  257. };
  258. PointCloud3DTileContent.prototype.applyDebugSettings = function (
  259. enabled,
  260. color
  261. ) {
  262. this._pointCloud.color = enabled ? color : Color.WHITE;
  263. };
  264. PointCloud3DTileContent.prototype.applyStyle = function (style) {
  265. if (defined(this._batchTable)) {
  266. this._batchTable.applyStyle(style);
  267. } else {
  268. this._styleDirty = true;
  269. }
  270. };
  271. const defaultShading = new PointCloudShading();
  272. PointCloud3DTileContent.prototype.update = function (tileset, frameState) {
  273. const pointCloud = this._pointCloud;
  274. const pointCloudShading = defaultValue(
  275. tileset.pointCloudShading,
  276. defaultShading
  277. );
  278. const tile = this._tile;
  279. const batchTable = this._batchTable;
  280. const mode = frameState.mode;
  281. const clippingPlanes = tileset.clippingPlanes;
  282. if (!defined(this._pickId) && !defined(batchTable)) {
  283. this._pickId = frameState.context.createPickId({
  284. primitive: tileset,
  285. content: this,
  286. });
  287. }
  288. if (defined(batchTable)) {
  289. batchTable.update(tileset, frameState);
  290. }
  291. let boundingSphere;
  292. if (defined(tile._contentBoundingVolume)) {
  293. boundingSphere =
  294. mode === SceneMode.SCENE3D
  295. ? tile._contentBoundingVolume.boundingSphere
  296. : tile._contentBoundingVolume2D.boundingSphere;
  297. } else {
  298. boundingSphere =
  299. mode === SceneMode.SCENE3D
  300. ? tile._boundingVolume.boundingSphere
  301. : tile._boundingVolume2D.boundingSphere;
  302. }
  303. const styleDirty = this._styleDirty;
  304. this._styleDirty = false;
  305. pointCloud.clippingPlanesOriginMatrix = tileset.clippingPlanesOriginMatrix;
  306. pointCloud.style = defined(batchTable) ? undefined : tileset.style;
  307. pointCloud.styleDirty = styleDirty;
  308. pointCloud.modelMatrix = tile.computedTransform;
  309. pointCloud.time = tileset.timeSinceLoad;
  310. pointCloud.shadows = tileset.shadows;
  311. pointCloud.boundingSphere = boundingSphere;
  312. pointCloud.clippingPlanes = clippingPlanes;
  313. pointCloud.isClipped =
  314. defined(clippingPlanes) && clippingPlanes.enabled && tile._isClipped;
  315. pointCloud.clippingPlanesDirty = tile.clippingPlanesDirty;
  316. pointCloud.attenuation = pointCloudShading.attenuation;
  317. pointCloud.backFaceCulling = pointCloudShading.backFaceCulling;
  318. pointCloud.normalShading = pointCloudShading.normalShading;
  319. pointCloud.geometricError = getGeometricError(this);
  320. pointCloud.geometricErrorScale = pointCloudShading.geometricErrorScale;
  321. pointCloud.splitDirection = tileset.splitDirection;
  322. if (
  323. defined(pointCloudShading) &&
  324. defined(pointCloudShading.maximumAttenuation)
  325. ) {
  326. pointCloud.maximumAttenuation = pointCloudShading.maximumAttenuation;
  327. } else if (tile.refine === Cesium3DTileRefine.ADD) {
  328. pointCloud.maximumAttenuation = 5.0;
  329. } else {
  330. pointCloud.maximumAttenuation = tileset.maximumScreenSpaceError;
  331. }
  332. pointCloud.update(frameState);
  333. };
  334. PointCloud3DTileContent.prototype.isDestroyed = function () {
  335. return false;
  336. };
  337. PointCloud3DTileContent.prototype.destroy = function () {
  338. this._pickId = this._pickId && this._pickId.destroy();
  339. this._pointCloud = this._pointCloud && this._pointCloud.destroy();
  340. this._batchTable = this._batchTable && this._batchTable.destroy();
  341. return destroyObject(this);
  342. };
  343. export default PointCloud3DTileContent;