PointCloud3DTileContent.js 9.6 KB

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