ModelFeatureTable.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 ModelExperimentalType from "./ModelExperimentalType.js";
  11. /**
  12. * Manages the {@link ModelFeature}s in a {@link ModelExperimental}.
  13. * Extracts the properties from a {@link PropertyTable}.
  14. *
  15. * @param {Object} options An object containing the following options:
  16. * @param {ModelExperimental} 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. export default 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. * A flag to indicate whether or not the types of style commands needed by this feature table have changed.
  74. *
  75. * @memberof ModelFeatureTable.prototype
  76. *
  77. * @type {Boolean}
  78. * @readonly
  79. *
  80. * @private
  81. */
  82. styleCommandsNeededDirty: {
  83. get: function () {
  84. return this._styleCommandsNeededDirty;
  85. },
  86. },
  87. });
  88. function initialize(modelFeatureTable) {
  89. const model = modelFeatureTable._model;
  90. const is3DTiles = ModelExperimentalType.is3DTiles(model.type);
  91. const featuresLength = modelFeatureTable._propertyTable.count;
  92. if (featuresLength === 0) {
  93. return;
  94. }
  95. let i;
  96. const features = new Array(featuresLength);
  97. if (is3DTiles) {
  98. const content = model.content;
  99. for (i = 0; i < featuresLength; i++) {
  100. features[i] = new Cesium3DTileFeature(content, i);
  101. }
  102. } else {
  103. for (i = 0; i < featuresLength; i++) {
  104. features[i] = new ModelFeature({
  105. model: model,
  106. featureId: i,
  107. featureTable: modelFeatureTable,
  108. });
  109. }
  110. }
  111. modelFeatureTable._features = features;
  112. modelFeatureTable._featuresLength = featuresLength;
  113. modelFeatureTable._batchTexture = new BatchTexture({
  114. featuresLength: featuresLength,
  115. owner: modelFeatureTable,
  116. statistics: is3DTiles
  117. ? model.content.tileset.statistics
  118. : modelFeatureTable._statistics,
  119. });
  120. }
  121. /**
  122. * Creates/updates the batch texture.
  123. *
  124. * @param {FrameState} frameState The frame state.
  125. *
  126. * @private
  127. */
  128. ModelFeatureTable.prototype.update = function (frameState) {
  129. // Assume the number of translucent features has not changed.
  130. this._styleCommandsNeededDirty = false;
  131. this._batchTexture.update(undefined, frameState);
  132. const currentStyleCommandsNeeded = StyleCommandsNeeded.getStyleCommandsNeeded(
  133. this._featuresLength,
  134. this._batchTexture.translucentFeaturesLength
  135. );
  136. if (this._styleCommandsNeeded !== currentStyleCommandsNeeded) {
  137. this._styleCommandsNeededDirty = true;
  138. this._styleCommandsNeeded = currentStyleCommandsNeeded;
  139. }
  140. };
  141. ModelFeatureTable.prototype.setShow = function (featureId, show) {
  142. this._batchTexture.setShow(featureId, show);
  143. };
  144. ModelFeatureTable.prototype.setAllShow = function (show) {
  145. this._batchTexture.setAllShow(show);
  146. };
  147. ModelFeatureTable.prototype.getShow = function (featureId) {
  148. return this._batchTexture.getShow(featureId);
  149. };
  150. ModelFeatureTable.prototype.setColor = function (featureId, color) {
  151. this._batchTexture.setColor(featureId, color);
  152. };
  153. ModelFeatureTable.prototype.setAllColor = function (color) {
  154. this._batchTexture.setAllColor(color);
  155. };
  156. ModelFeatureTable.prototype.getColor = function (featureId, result) {
  157. return this._batchTexture.getColor(featureId, result);
  158. };
  159. ModelFeatureTable.prototype.getPickColor = function (featureId) {
  160. return this._batchTexture.getPickColor(featureId);
  161. };
  162. ModelFeatureTable.prototype.getFeature = function (featureId) {
  163. return this._features[featureId];
  164. };
  165. ModelFeatureTable.prototype.hasProperty = function (featureId, propertyName) {
  166. return this._propertyTable.hasProperty(featureId, propertyName);
  167. };
  168. ModelFeatureTable.prototype.hasPropertyBySemantic = function (
  169. featureId,
  170. propertyName
  171. ) {
  172. return this._propertyTable.hasPropertyBySemantic(featureId, propertyName);
  173. };
  174. ModelFeatureTable.prototype.getProperty = function (featureId, name) {
  175. return this._propertyTable.getProperty(featureId, name);
  176. };
  177. ModelFeatureTable.prototype.getPropertyBySemantic = function (
  178. featureId,
  179. semantic
  180. ) {
  181. return this._propertyTable.getPropertyBySemantic(featureId, semantic);
  182. };
  183. ModelFeatureTable.prototype.getPropertyNames = function (results) {
  184. return this._propertyTable.getPropertyIds(results);
  185. };
  186. ModelFeatureTable.prototype.setProperty = function (featureId, name, value) {
  187. return this._propertyTable.setProperty(featureId, name, value);
  188. };
  189. const scratchColor = new Color();
  190. /**
  191. * @private
  192. */
  193. ModelFeatureTable.prototype.applyStyle = function (style) {
  194. if (!defined(style)) {
  195. this.setAllColor(BatchTexture.DEFAULT_COLOR_VALUE);
  196. this.setAllShow(BatchTexture.DEFAULT_SHOW_VALUE);
  197. return;
  198. }
  199. for (let i = 0; i < this._featuresLength; i++) {
  200. const feature = this.getFeature(i);
  201. const color = defined(style.color)
  202. ? defaultValue(
  203. style.color.evaluateColor(feature, scratchColor),
  204. BatchTexture.DEFAULT_COLOR_VALUE
  205. )
  206. : BatchTexture.DEFAULT_COLOR_VALUE;
  207. const show = defined(style.show)
  208. ? defaultValue(
  209. style.show.evaluate(feature),
  210. BatchTexture.DEFAULT_SHOW_VALUE
  211. )
  212. : BatchTexture.DEFAULT_SHOW_VALUE;
  213. this.setColor(i, color);
  214. this.setShow(i, show);
  215. }
  216. };
  217. /**
  218. * Returns true if this object was destroyed; otherwise, false.
  219. * <p>
  220. * If this object was destroyed, it should not be used; calling any function other than
  221. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  222. * </p>
  223. *
  224. * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  225. *
  226. * @see ModelFeatureTable#destroy
  227. * @private
  228. */
  229. ModelFeatureTable.prototype.isDestroyed = function () {
  230. return false;
  231. };
  232. /**
  233. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  234. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  235. * <p>
  236. * Once an object is destroyed, it should not be used; calling any function other than
  237. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  238. * assign the return value (<code>undefined</code>) to the object as done in the example.
  239. * </p>
  240. *
  241. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  242. *
  243. * @example
  244. * e = e && e.destroy();
  245. *
  246. * @see ModelFeatureTable#isDestroyed
  247. * @private
  248. */
  249. ModelFeatureTable.prototype.destroy = function (frameState) {
  250. this._batchTexture.destroy();
  251. destroyObject(this);
  252. };