ModelFeatureTable.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import BatchTexture from "../BatchTexture.js";
  2. import Cesium3DTileFeature from "../Cesium3DTileFeature.js";
  3. import Check from "../../Core/Check.js";
  4. import Color from "../../Core/Color.js";
  5. import defined from "../../Core/defined.js";
  6. import destroyObject from "../../Core/destroyObject.js";
  7. import ModelFeature from "./ModelFeature.js";
  8. import defaultValue from "../../Core/defaultValue.js";
  9. import StyleCommandsNeeded from "./StyleCommandsNeeded.js";
  10. import ModelType from "./ModelType.js";
  11. /**
  12. * Manages the {@link ModelFeature}s in a {@link Model}.
  13. * Extracts the properties from a {@link PropertyTable}.
  14. *
  15. * @param {object} options An object containing the following options:
  16. * @param {Model} options.model The model that owns this feature table.
  17. * @param {PropertyTable} options.propertyTable The property table from the model used to initialize the model.
  18. *
  19. * @alias ModelFeatureTable
  20. * @constructor
  21. *
  22. * @private
  23. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  24. */
  25. function ModelFeatureTable(options) {
  26. const model = options.model;
  27. const propertyTable = options.propertyTable;
  28. //>>includeStart('debug', pragmas.debug);
  29. Check.typeOf.object("propertyTable", propertyTable);
  30. Check.typeOf.object("model", model);
  31. //>>includeEnd('debug');
  32. this._propertyTable = propertyTable;
  33. this._model = model;
  34. this._features = undefined;
  35. this._featuresLength = 0;
  36. this._batchTexture = undefined;
  37. this._styleCommandsNeededDirty = false;
  38. this._styleCommandsNeeded = StyleCommandsNeeded.ALL_OPAQUE;
  39. initialize(this);
  40. }
  41. Object.defineProperties(ModelFeatureTable.prototype, {
  42. /**
  43. * The batch texture created for the features in this table.
  44. *
  45. * @memberof ModelFeatureTable.prototype
  46. *
  47. * @type {BatchTexture}
  48. * @readonly
  49. *
  50. * @private
  51. */
  52. batchTexture: {
  53. get: function () {
  54. return this._batchTexture;
  55. },
  56. },
  57. /**
  58. * The number of features in this table.
  59. *
  60. * @memberof ModelFeatureTable.prototype
  61. *
  62. * @type {number}
  63. * @readonly
  64. *
  65. * @private
  66. */
  67. featuresLength: {
  68. get: function () {
  69. return this._featuresLength;
  70. },
  71. },
  72. /**
  73. * Size of the batch texture. This does not count the property table size
  74. * as that is counted separately through StructuralMetadata.
  75. *
  76. * @memberof ModelFeatureTable.prototype
  77. *
  78. * @type {number}
  79. * @readonly
  80. *
  81. * @private
  82. */
  83. batchTextureByteLength: {
  84. get: function () {
  85. if (defined(this._batchTexture)) {
  86. return this._batchTexture.byteLength;
  87. }
  88. return 0;
  89. },
  90. },
  91. /**
  92. * A flag to indicate whether or not the types of style commands needed by this feature table have changed.
  93. *
  94. * @memberof ModelFeatureTable.prototype
  95. *
  96. * @type {boolean}
  97. * @readonly
  98. *
  99. * @private
  100. */
  101. styleCommandsNeededDirty: {
  102. get: function () {
  103. return this._styleCommandsNeededDirty;
  104. },
  105. },
  106. });
  107. function initialize(modelFeatureTable) {
  108. const model = modelFeatureTable._model;
  109. const is3DTiles = ModelType.is3DTiles(model.type);
  110. const featuresLength = modelFeatureTable._propertyTable.count;
  111. if (featuresLength === 0) {
  112. return;
  113. }
  114. let i;
  115. const features = new Array(featuresLength);
  116. if (is3DTiles) {
  117. const content = model.content;
  118. for (i = 0; i < featuresLength; i++) {
  119. features[i] = new Cesium3DTileFeature(content, i);
  120. }
  121. } else {
  122. for (i = 0; i < featuresLength; i++) {
  123. features[i] = new ModelFeature({
  124. model: model,
  125. featureId: i,
  126. featureTable: modelFeatureTable,
  127. });
  128. }
  129. }
  130. modelFeatureTable._features = features;
  131. modelFeatureTable._featuresLength = featuresLength;
  132. modelFeatureTable._batchTexture = new BatchTexture({
  133. featuresLength: featuresLength,
  134. owner: modelFeatureTable,
  135. statistics: is3DTiles ? model.content.tileset.statistics : undefined,
  136. });
  137. }
  138. /**
  139. * Creates/updates the batch texture.
  140. *
  141. * @param {FrameState} frameState The frame state.
  142. *
  143. * @private
  144. */
  145. ModelFeatureTable.prototype.update = function (frameState) {
  146. // Assume the number of translucent features has not changed.
  147. this._styleCommandsNeededDirty = false;
  148. this._batchTexture.update(undefined, frameState);
  149. const currentStyleCommandsNeeded = StyleCommandsNeeded.getStyleCommandsNeeded(
  150. this._featuresLength,
  151. this._batchTexture.translucentFeaturesLength
  152. );
  153. if (this._styleCommandsNeeded !== currentStyleCommandsNeeded) {
  154. this._styleCommandsNeededDirty = true;
  155. this._styleCommandsNeeded = currentStyleCommandsNeeded;
  156. }
  157. };
  158. ModelFeatureTable.prototype.setShow = function (featureId, show) {
  159. this._batchTexture.setShow(featureId, show);
  160. };
  161. ModelFeatureTable.prototype.setAllShow = function (show) {
  162. this._batchTexture.setAllShow(show);
  163. };
  164. ModelFeatureTable.prototype.getShow = function (featureId) {
  165. return this._batchTexture.getShow(featureId);
  166. };
  167. ModelFeatureTable.prototype.setColor = function (featureId, color) {
  168. this._batchTexture.setColor(featureId, color);
  169. };
  170. ModelFeatureTable.prototype.setAllColor = function (color) {
  171. this._batchTexture.setAllColor(color);
  172. };
  173. ModelFeatureTable.prototype.getColor = function (featureId, result) {
  174. return this._batchTexture.getColor(featureId, result);
  175. };
  176. ModelFeatureTable.prototype.getPickColor = function (featureId) {
  177. return this._batchTexture.getPickColor(featureId);
  178. };
  179. ModelFeatureTable.prototype.getFeature = function (featureId) {
  180. return this._features[featureId];
  181. };
  182. ModelFeatureTable.prototype.hasProperty = function (featureId, propertyName) {
  183. return this._propertyTable.hasProperty(featureId, propertyName);
  184. };
  185. ModelFeatureTable.prototype.hasPropertyBySemantic = function (
  186. featureId,
  187. propertyName
  188. ) {
  189. return this._propertyTable.hasPropertyBySemantic(featureId, propertyName);
  190. };
  191. ModelFeatureTable.prototype.getProperty = function (featureId, name) {
  192. return this._propertyTable.getProperty(featureId, name);
  193. };
  194. ModelFeatureTable.prototype.getPropertyBySemantic = function (
  195. featureId,
  196. semantic
  197. ) {
  198. return this._propertyTable.getPropertyBySemantic(featureId, semantic);
  199. };
  200. ModelFeatureTable.prototype.getPropertyIds = function (results) {
  201. return this._propertyTable.getPropertyIds(results);
  202. };
  203. ModelFeatureTable.prototype.setProperty = function (featureId, name, value) {
  204. return this._propertyTable.setProperty(featureId, name, value);
  205. };
  206. ModelFeatureTable.prototype.isClass = function (featureId, className) {
  207. return this._propertyTable.isClass(featureId, className);
  208. };
  209. ModelFeatureTable.prototype.isExactClass = function (featureId, className) {
  210. return this._propertyTable.isExactClass(featureId, className);
  211. };
  212. ModelFeatureTable.prototype.getExactClassName = function (featureId) {
  213. return this._propertyTable.getExactClassName(featureId);
  214. };
  215. const scratchColor = new Color();
  216. /**
  217. * @private
  218. */
  219. ModelFeatureTable.prototype.applyStyle = function (style) {
  220. if (!defined(style)) {
  221. this.setAllColor(BatchTexture.DEFAULT_COLOR_VALUE);
  222. this.setAllShow(BatchTexture.DEFAULT_SHOW_VALUE);
  223. return;
  224. }
  225. for (let i = 0; i < this._featuresLength; i++) {
  226. const feature = this.getFeature(i);
  227. const color = defined(style.color)
  228. ? defaultValue(
  229. style.color.evaluateColor(feature, scratchColor),
  230. BatchTexture.DEFAULT_COLOR_VALUE
  231. )
  232. : BatchTexture.DEFAULT_COLOR_VALUE;
  233. const show = defined(style.show)
  234. ? defaultValue(
  235. style.show.evaluate(feature),
  236. BatchTexture.DEFAULT_SHOW_VALUE
  237. )
  238. : BatchTexture.DEFAULT_SHOW_VALUE;
  239. this.setColor(i, color);
  240. this.setShow(i, show);
  241. }
  242. };
  243. /**
  244. * Returns true if this object was destroyed; otherwise, false.
  245. * <p>
  246. * If this object was destroyed, it should not be used; calling any function other than
  247. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  248. * </p>
  249. *
  250. * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  251. *
  252. * @see ModelFeatureTable#destroy
  253. * @private
  254. */
  255. ModelFeatureTable.prototype.isDestroyed = function () {
  256. return false;
  257. };
  258. /**
  259. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  260. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  261. * <p>
  262. * Once an object is destroyed, it should not be used; calling any function other than
  263. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  264. * assign the return value (<code>undefined</code>) to the object as done in the example.
  265. * </p>
  266. *
  267. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  268. *
  269. * @example
  270. * e = e && e.destroy();
  271. *
  272. * @see ModelFeatureTable#isDestroyed
  273. * @private
  274. */
  275. ModelFeatureTable.prototype.destroy = function (frameState) {
  276. this._batchTexture = this._batchTexture && this._batchTexture.destroy();
  277. destroyObject(this);
  278. };
  279. export default ModelFeatureTable;