Cesium3DTileFeature.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import Color from "../Core/Color.js";
  2. import defined from "../Core/defined.js";
  3. /**
  4. * A feature of a {@link Cesium3DTileset}.
  5. * <p>
  6. * Provides access to a feature's properties stored in the tile's batch table, as well
  7. * as the ability to show/hide a feature and change its highlight color via
  8. * {@link Cesium3DTileFeature#show} and {@link Cesium3DTileFeature#color}, respectively.
  9. * </p>
  10. * <p>
  11. * Modifications to a <code>Cesium3DTileFeature</code> object have the lifetime of the tile's
  12. * content. If the tile's content is unloaded, e.g., due to it going out of view and needing
  13. * to free space in the cache for visible tiles, listen to the {@link Cesium3DTileset#tileUnload} event to save any
  14. * modifications. Also listen to the {@link Cesium3DTileset#tileVisible} event to reapply any modifications.
  15. * </p>
  16. * <p>
  17. * Do not construct this directly. Access it through {@link Cesium3DTileContent#getFeature}
  18. * or picking using {@link Scene#pick} and {@link Scene#pickPosition}.
  19. * </p>
  20. *
  21. * @alias Cesium3DTileFeature
  22. * @constructor
  23. *
  24. * @example
  25. * // On mouse over, display all the properties for a feature in the console log.
  26. * handler.setInputAction(function(movement) {
  27. * var feature = scene.pick(movement.endPosition);
  28. * if (feature instanceof Cesium.Cesium3DTileFeature) {
  29. * var propertyNames = feature.getPropertyNames();
  30. * var length = propertyNames.length;
  31. * for (var i = 0; i < length; ++i) {
  32. * var propertyName = propertyNames[i];
  33. * console.log(propertyName + ': ' + feature.getProperty(propertyName));
  34. * }
  35. * }
  36. * }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  37. */
  38. function Cesium3DTileFeature(content, batchId) {
  39. this._content = content;
  40. this._batchId = batchId;
  41. this._color = undefined; // for calling getColor
  42. }
  43. Object.defineProperties(Cesium3DTileFeature.prototype, {
  44. /**
  45. * Gets or sets if the feature will be shown. This is set for all features
  46. * when a style's show is evaluated.
  47. *
  48. * @memberof Cesium3DTileFeature.prototype
  49. *
  50. * @type {Boolean}
  51. *
  52. * @default true
  53. */
  54. show: {
  55. get: function () {
  56. return this._content.batchTable.getShow(this._batchId);
  57. },
  58. set: function (value) {
  59. this._content.batchTable.setShow(this._batchId, value);
  60. },
  61. },
  62. /**
  63. * Gets or sets the highlight color multiplied with the feature's color. When
  64. * this is white, the feature's color is not changed. This is set for all features
  65. * when a style's color is evaluated.
  66. *
  67. * @memberof Cesium3DTileFeature.prototype
  68. *
  69. * @type {Color}
  70. *
  71. * @default {@link Color.WHITE}
  72. */
  73. color: {
  74. get: function () {
  75. if (!defined(this._color)) {
  76. this._color = new Color();
  77. }
  78. return this._content.batchTable.getColor(this._batchId, this._color);
  79. },
  80. set: function (value) {
  81. this._content.batchTable.setColor(this._batchId, value);
  82. },
  83. },
  84. /**
  85. * Gets the content of the tile containing the feature.
  86. *
  87. * @memberof Cesium3DTileFeature.prototype
  88. *
  89. * @type {Cesium3DTileContent}
  90. *
  91. * @readonly
  92. * @private
  93. */
  94. content: {
  95. get: function () {
  96. return this._content;
  97. },
  98. },
  99. /**
  100. * Gets the tileset containing the feature.
  101. *
  102. * @memberof Cesium3DTileFeature.prototype
  103. *
  104. * @type {Cesium3DTileset}
  105. *
  106. * @readonly
  107. */
  108. tileset: {
  109. get: function () {
  110. return this._content.tileset;
  111. },
  112. },
  113. /**
  114. * All objects returned by {@link Scene#pick} have a <code>primitive</code> property. This returns
  115. * the tileset containing the feature.
  116. *
  117. * @memberof Cesium3DTileFeature.prototype
  118. *
  119. * @type {Cesium3DTileset}
  120. *
  121. * @readonly
  122. */
  123. primitive: {
  124. get: function () {
  125. return this._content.tileset;
  126. },
  127. },
  128. /**
  129. * @private
  130. */
  131. pickId: {
  132. get: function () {
  133. return this._content.batchTable.getPickColor(this._batchId);
  134. },
  135. },
  136. });
  137. /**
  138. * Returns whether the feature contains this property. This includes properties from this feature's
  139. * class and inherited classes when using a batch table hierarchy.
  140. *
  141. * @see {@link https://github.com/CesiumGS/3d-tiles/tree/master/extensions/3DTILES_batch_table_hierarchy}
  142. *
  143. * @param {String} name The case-sensitive name of the property.
  144. * @returns {Boolean} Whether the feature contains this property.
  145. */
  146. Cesium3DTileFeature.prototype.hasProperty = function (name) {
  147. return this._content.batchTable.hasProperty(this._batchId, name);
  148. };
  149. /**
  150. * Returns an array of property names for the feature. This includes properties from this feature's
  151. * class and inherited classes when using a batch table hierarchy.
  152. *
  153. * @see {@link https://github.com/CesiumGS/3d-tiles/tree/master/extensions/3DTILES_batch_table_hierarchy}
  154. *
  155. * @param {String[]} [results] An array into which to store the results.
  156. * @returns {String[]} The names of the feature's properties.
  157. */
  158. Cesium3DTileFeature.prototype.getPropertyNames = function (results) {
  159. return this._content.batchTable.getPropertyNames(this._batchId, results);
  160. };
  161. /**
  162. * Returns a copy of the value of the feature's property with the given name. This includes properties from this feature's
  163. * class and inherited classes when using a batch table hierarchy.
  164. *
  165. * @see {@link https://github.com/CesiumGS/3d-tiles/tree/master/extensions/3DTILES_batch_table_hierarchy}
  166. *
  167. * @param {String} name The case-sensitive name of the property.
  168. * @returns {*} The value of the property or <code>undefined</code> if the property does not exist.
  169. *
  170. * @example
  171. * // Display all the properties for a feature in the console log.
  172. * var propertyNames = feature.getPropertyNames();
  173. * var length = propertyNames.length;
  174. * for (var i = 0; i < length; ++i) {
  175. * var propertyName = propertyNames[i];
  176. * console.log(propertyName + ': ' + feature.getProperty(propertyName));
  177. * }
  178. */
  179. Cesium3DTileFeature.prototype.getProperty = function (name) {
  180. return this._content.batchTable.getProperty(this._batchId, name);
  181. };
  182. /**
  183. * Sets the value of the feature's property with the given name.
  184. * <p>
  185. * If a property with the given name doesn't exist, it is created.
  186. * </p>
  187. *
  188. * @param {String} name The case-sensitive name of the property.
  189. * @param {*} value The value of the property that will be copied.
  190. *
  191. * @exception {DeveloperError} Inherited batch table hierarchy property is read only.
  192. *
  193. * @example
  194. * var height = feature.getProperty('Height'); // e.g., the height of a building
  195. *
  196. * @example
  197. * var name = 'clicked';
  198. * if (feature.getProperty(name)) {
  199. * console.log('already clicked');
  200. * } else {
  201. * feature.setProperty(name, true);
  202. * console.log('first click');
  203. * }
  204. */
  205. Cesium3DTileFeature.prototype.setProperty = function (name, value) {
  206. this._content.batchTable.setProperty(this._batchId, name, value);
  207. // PERFORMANCE_IDEA: Probably overkill, but maybe only mark the tile dirty if the
  208. // property is in one of the style's expressions or - if it can be done quickly -
  209. // if the new property value changed the result of an expression.
  210. this._content.featurePropertiesDirty = true;
  211. };
  212. /**
  213. * Returns whether the feature's class name equals <code>className</code>. Unlike {@link Cesium3DTileFeature#isClass}
  214. * this function only checks the feature's exact class and not inherited classes.
  215. * <p>
  216. * This function returns <code>false</code> if no batch table hierarchy is present.
  217. * </p>
  218. *
  219. * @param {String} className The name to check against.
  220. * @returns {Boolean} Whether the feature's class name equals <code>className</code>
  221. *
  222. * @private
  223. */
  224. Cesium3DTileFeature.prototype.isExactClass = function (className) {
  225. return this._content.batchTable.isExactClass(this._batchId, className);
  226. };
  227. /**
  228. * Returns whether the feature's class or any inherited classes are named <code>className</code>.
  229. * <p>
  230. * This function returns <code>false</code> if no batch table hierarchy is present.
  231. * </p>
  232. *
  233. * @param {String} className The name to check against.
  234. * @returns {Boolean} Whether the feature's class or inherited classes are named <code>className</code>
  235. *
  236. * @private
  237. */
  238. Cesium3DTileFeature.prototype.isClass = function (className) {
  239. return this._content.batchTable.isClass(this._batchId, className);
  240. };
  241. /**
  242. * Returns the feature's class name.
  243. * <p>
  244. * This function returns <code>undefined</code> if no batch table hierarchy is present.
  245. * </p>
  246. *
  247. * @returns {String} The feature's class name.
  248. *
  249. * @private
  250. */
  251. Cesium3DTileFeature.prototype.getExactClassName = function () {
  252. return this._content.batchTable.getExactClassName(this._batchId);
  253. };
  254. export default Cesium3DTileFeature;