ModelFeature.js 7.3 KB

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