ModelFeature.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import Color from "../../Core/Color.js";
  2. import defined from "../../Core/defined.js";
  3. /**
  4. * A feature of a {@link Model}.
  5. * <p>
  6. * Provides access to a feature's properties stored in the model's feature table.
  7. * </p>
  8. * <p>
  9. * Modifications to a <code>ModelFeature</code> object have the lifetime of the model.
  10. * </p>
  11. * <p>
  12. * Do not construct this directly. Access it through picking using {@link Scene#pick}.
  13. * </p>
  14. *
  15. * @alias ModelFeature
  16. * @constructor
  17. *
  18. * @param {object} options Object with the following properties:
  19. * @param {Model} options.model The model the feature belongs to.
  20. * @param {number} options.featureId The unique integral identifier for this feature.
  21. *
  22. * @example
  23. * // On mouse over, display all the properties for a feature in the console log.
  24. * handler.setInputAction(function(movement) {
  25. * const feature = scene.pick(movement.endPosition);
  26. * if (feature instanceof Cesium.ModelFeature) {
  27. * console.log(feature);
  28. * }
  29. * }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  30. *
  31. */
  32. function ModelFeature(options) {
  33. this._model = options.model;
  34. // This ModelFeatureTable is not documented as an option since it is
  35. // part of the private API and should not appear in the documentation.
  36. this._featureTable = options.featureTable;
  37. this._featureId = options.featureId;
  38. this._color = undefined; // for calling getColor
  39. }
  40. Object.defineProperties(ModelFeature.prototype, {
  41. /**
  42. * Gets or sets if the feature will be shown. This is set for all features
  43. * when a style's show is evaluated.
  44. *
  45. * @memberof ModelFeature.prototype
  46. *
  47. * @type {boolean}
  48. *
  49. * @default true
  50. */
  51. show: {
  52. get: function () {
  53. return this._featureTable.getShow(this._featureId);
  54. },
  55. set: function (value) {
  56. this._featureTable.setShow(this._featureId, value);
  57. },
  58. },
  59. /**
  60. * Gets or sets the highlight color multiplied with the feature's color. When
  61. * this is white, the feature's color is not changed. This is set for all features
  62. * when a style's color is evaluated.
  63. *
  64. * @memberof ModelFeature.prototype
  65. *
  66. * @type {Color}
  67. *
  68. * @default {@link Color.WHITE}
  69. */
  70. color: {
  71. get: function () {
  72. if (!defined(this._color)) {
  73. this._color = new Color();
  74. }
  75. return this._featureTable.getColor(this._featureId, this._color);
  76. },
  77. set: function (value) {
  78. this._featureTable.setColor(this._featureId, value);
  79. },
  80. },
  81. /**
  82. * All objects returned by {@link Scene#pick} have a <code>primitive</code> property. This returns
  83. * the model containing the feature.
  84. *
  85. * @memberof ModelFeature.prototype
  86. *
  87. * @type {Model}
  88. *
  89. * @readonly
  90. * @private
  91. */
  92. primitive: {
  93. get: function () {
  94. return this._model;
  95. },
  96. },
  97. /**
  98. * The {@link ModelFeatureTable} that this feature belongs to.
  99. *
  100. * @memberof ModelFeature.prototype
  101. *
  102. * @type {ModelFeatureTable}
  103. *
  104. * @readonly
  105. * @private
  106. */
  107. featureTable: {
  108. get: function () {
  109. return this._featureTable;
  110. },
  111. },
  112. /**
  113. * Get the feature ID associated with this feature. For 3D Tiles 1.0, the
  114. * batch ID is returned. For EXT_mesh_features, this is the feature ID from
  115. * the selected feature ID set.
  116. *
  117. * @memberof ModelFeature.prototype
  118. *
  119. * @type {number}
  120. *
  121. * @readonly
  122. * @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.
  123. */
  124. featureId: {
  125. get: function () {
  126. return this._featureId;
  127. },
  128. },
  129. });
  130. /**
  131. * Returns whether the feature contains this property.
  132. *
  133. * @param {string} name The case-sensitive name of the property.
  134. * @returns {boolean} Whether the feature contains this property.
  135. */
  136. ModelFeature.prototype.hasProperty = function (name) {
  137. return this._featureTable.hasProperty(this._featureId, name);
  138. };
  139. /**
  140. * Returns a copy of the value of the feature's property with the given name.
  141. *
  142. * @param {string} name The case-sensitive name of the property.
  143. * @returns {*} The value of the property or <code>undefined</code> if the feature does not have this property.
  144. *
  145. * @example
  146. * // Display all the properties for a feature in the console log.
  147. * const propertyIds = feature.getPropertyIds();
  148. * const length = propertyIds.length;
  149. * for (let i = 0; i < length; ++i) {
  150. * const propertyId = propertyIds[i];
  151. * console.log(propertyId + ': ' + feature.getProperty(propertyId));
  152. * }
  153. */
  154. ModelFeature.prototype.getProperty = function (name) {
  155. return this._featureTable.getProperty(this._featureId, name);
  156. };
  157. /**
  158. * Returns a copy of the feature's property with the given name, examining all
  159. * the metadata from the EXT_structural_metadata and legacy EXT_feature_metadata glTF
  160. * extensions. Metadata is checked against name from most specific to most
  161. * general and the first match is returned. Metadata is checked in this order:
  162. * <ol>
  163. * <li>structural metadata property by semantic</li>
  164. * <li>structural metadata property by property ID</li>
  165. * </ol>
  166. * <p>
  167. * See the {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_structural_metadata|EXT_structural_metadata Extension} as well as the
  168. * previous {@link https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_feature_metadata|EXT_feature_metadata Extension} for glTF.
  169. * </p>
  170. *
  171. * @param {string} name The semantic or property ID of the feature. Semantics are checked before property IDs in each granularity of metadata.
  172. * @return {*} The value of the property or <code>undefined</code> if the feature does not have this property.
  173. *
  174. * @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.
  175. */
  176. ModelFeature.prototype.getPropertyInherited = function (name) {
  177. if (this._featureTable.hasPropertyBySemantic(this._featureId, name)) {
  178. return this._featureTable.getPropertyBySemantic(this._featureId, name);
  179. }
  180. return this._featureTable.getProperty(this._featureId, name);
  181. };
  182. /**
  183. * Returns an array of property IDs for the feature.
  184. *
  185. * @param {string[]} [results] An array into which to store the results.
  186. * @returns {string[]} The IDs of the feature's properties.
  187. */
  188. ModelFeature.prototype.getPropertyIds = function (results) {
  189. return this._featureTable.getPropertyIds(results);
  190. };
  191. /**
  192. * Sets the value of the feature's property with the given name.
  193. *
  194. * @param {string} name The case-sensitive name of the property.
  195. * @param {*} value The value of the property that will be copied.
  196. * @returns {boolean} <code>true</code> if the property was set, <code>false</code> otherwise.
  197. *
  198. * @exception {DeveloperError} Inherited batch table hierarchy property is read only.
  199. *
  200. * @example
  201. * const height = feature.getProperty('Height'); // e.g., the height of a building
  202. *
  203. * @example
  204. * const name = 'clicked';
  205. * if (feature.getProperty(name)) {
  206. * console.log('already clicked');
  207. * } else {
  208. * feature.setProperty(name, true);
  209. * console.log('first click');
  210. * }
  211. */
  212. ModelFeature.prototype.setProperty = function (name, value) {
  213. return this._featureTable.setProperty(this._featureId, name, value);
  214. };
  215. export default ModelFeature;