Cesium3DTileFeature.js 9.8 KB

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