GeometryUpdater.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. import Check from '../Core/Check.js';
  2. import Color from '../Core/Color.js';
  3. import defaultValue from '../Core/defaultValue.js';
  4. import defined from '../Core/defined.js';
  5. import defineProperties from '../Core/defineProperties.js';
  6. import destroyObject from '../Core/destroyObject.js';
  7. import DeveloperError from '../Core/DeveloperError.js';
  8. import DistanceDisplayCondition from '../Core/DistanceDisplayCondition.js';
  9. import Event from '../Core/Event.js';
  10. import Iso8601 from '../Core/Iso8601.js';
  11. import oneTimeWarning from '../Core/oneTimeWarning.js';
  12. import ClassificationType from '../Scene/ClassificationType.js';
  13. import ShadowMode from '../Scene/ShadowMode.js';
  14. import ColorMaterialProperty from './ColorMaterialProperty.js';
  15. import ConstantProperty from './ConstantProperty.js';
  16. import Entity from './Entity.js';
  17. import Property from './Property.js';
  18. var defaultMaterial = new ColorMaterialProperty(Color.WHITE);
  19. var defaultShow = new ConstantProperty(true);
  20. var defaultFill = new ConstantProperty(true);
  21. var defaultOutline = new ConstantProperty(false);
  22. var defaultOutlineColor = new ConstantProperty(Color.BLACK);
  23. var defaultShadows = new ConstantProperty(ShadowMode.DISABLED);
  24. var defaultDistanceDisplayCondition = new ConstantProperty(new DistanceDisplayCondition());
  25. var defaultClassificationType = new ConstantProperty(ClassificationType.BOTH);
  26. /**
  27. * An abstract class for updating geometry entities.
  28. * @alias GeometryUpdater
  29. * @constructor
  30. *
  31. * @param {Object} options An object with the following properties:
  32. * @param {Entity} options.entity The entity containing the geometry to be visualized.
  33. * @param {Scene} options.scene The scene where visualization is taking place.
  34. * @param {Object} options.geometryOptions Options for the geometry
  35. * @param {String} options.geometryPropertyName The geometry property name
  36. * @param {String[]} options.observedPropertyNames The entity properties this geometry cares about
  37. */
  38. function GeometryUpdater(options) {
  39. //>>includeStart('debug', pragmas.debug);
  40. Check.defined('options.entity', options.entity);
  41. Check.defined('options.scene', options.scene);
  42. Check.defined('options.geometryOptions', options.geometryOptions);
  43. Check.defined('options.geometryPropertyName', options.geometryPropertyName);
  44. Check.defined('options.observedPropertyNames', options.observedPropertyNames);
  45. //>>includeEnd('debug');
  46. var entity = options.entity;
  47. var geometryPropertyName = options.geometryPropertyName;
  48. this._entity = entity;
  49. this._scene = options.scene;
  50. this._fillEnabled = false;
  51. this._isClosed = false;
  52. this._onTerrain = false;
  53. this._dynamic = false;
  54. this._outlineEnabled = false;
  55. this._geometryChanged = new Event();
  56. this._showProperty = undefined;
  57. this._materialProperty = undefined;
  58. this._showOutlineProperty = undefined;
  59. this._outlineColorProperty = undefined;
  60. this._outlineWidth = 1.0;
  61. this._shadowsProperty = undefined;
  62. this._distanceDisplayConditionProperty = undefined;
  63. this._classificationTypeProperty = undefined;
  64. this._options = options.geometryOptions;
  65. this._geometryPropertyName = geometryPropertyName;
  66. this._id = geometryPropertyName + '-' + entity.id;
  67. this._observedPropertyNames = options.observedPropertyNames;
  68. this._supportsMaterialsforEntitiesOnTerrain = Entity.supportsMaterialsforEntitiesOnTerrain(options.scene);
  69. }
  70. defineProperties(GeometryUpdater.prototype, {
  71. /**
  72. * Gets the unique ID associated with this updater
  73. * @memberof GeometryUpdater.prototype
  74. * @type {String}
  75. * @readonly
  76. */
  77. id : {
  78. get : function() {
  79. return this._id;
  80. }
  81. },
  82. /**
  83. * Gets the entity associated with this geometry.
  84. * @memberof GeometryUpdater.prototype
  85. *
  86. * @type {Entity}
  87. * @readonly
  88. */
  89. entity : {
  90. get : function() {
  91. return this._entity;
  92. }
  93. },
  94. /**
  95. * Gets a value indicating if the geometry has a fill component.
  96. * @memberof GeometryUpdater.prototype
  97. *
  98. * @type {Boolean}
  99. * @readonly
  100. */
  101. fillEnabled : {
  102. get : function() {
  103. return this._fillEnabled;
  104. }
  105. },
  106. /**
  107. * Gets a value indicating if fill visibility varies with simulation time.
  108. * @memberof GeometryUpdater.prototype
  109. *
  110. * @type {Boolean}
  111. * @readonly
  112. */
  113. hasConstantFill : {
  114. get : function() {
  115. return !this._fillEnabled ||
  116. (!defined(this._entity.availability) &&
  117. Property.isConstant(this._showProperty) &&
  118. Property.isConstant(this._fillProperty));
  119. }
  120. },
  121. /**
  122. * Gets the material property used to fill the geometry.
  123. * @memberof GeometryUpdater.prototype
  124. *
  125. * @type {MaterialProperty}
  126. * @readonly
  127. */
  128. fillMaterialProperty : {
  129. get : function() {
  130. return this._materialProperty;
  131. }
  132. },
  133. /**
  134. * Gets a value indicating if the geometry has an outline component.
  135. * @memberof GeometryUpdater.prototype
  136. *
  137. * @type {Boolean}
  138. * @readonly
  139. */
  140. outlineEnabled : {
  141. get : function() {
  142. return this._outlineEnabled;
  143. }
  144. },
  145. /**
  146. * Gets a value indicating if the geometry has an outline component.
  147. * @memberof GeometryUpdater.prototype
  148. *
  149. * @type {Boolean}
  150. * @readonly
  151. */
  152. hasConstantOutline : {
  153. get : function() {
  154. return !this._outlineEnabled ||
  155. (!defined(this._entity.availability) &&
  156. Property.isConstant(this._showProperty) &&
  157. Property.isConstant(this._showOutlineProperty));
  158. }
  159. },
  160. /**
  161. * Gets the {@link Color} property for the geometry outline.
  162. * @memberof GeometryUpdater.prototype
  163. *
  164. * @type {Property}
  165. * @readonly
  166. */
  167. outlineColorProperty : {
  168. get : function() {
  169. return this._outlineColorProperty;
  170. }
  171. },
  172. /**
  173. * Gets the constant with of the geometry outline, in pixels.
  174. * This value is only valid if isDynamic is false.
  175. * @memberof GeometryUpdater.prototype
  176. *
  177. * @type {Number}
  178. * @readonly
  179. */
  180. outlineWidth : {
  181. get : function() {
  182. return this._outlineWidth;
  183. }
  184. },
  185. /**
  186. * Gets the property specifying whether the geometry
  187. * casts or receives shadows from each light source.
  188. * @memberof GeometryUpdater.prototype
  189. *
  190. * @type {Property}
  191. * @readonly
  192. */
  193. shadowsProperty : {
  194. get : function() {
  195. return this._shadowsProperty;
  196. }
  197. },
  198. /**
  199. * Gets or sets the {@link DistanceDisplayCondition} Property specifying at what distance from the camera that this geometry will be displayed.
  200. * @memberof GeometryUpdater.prototype
  201. *
  202. * @type {Property}
  203. * @readonly
  204. */
  205. distanceDisplayConditionProperty : {
  206. get : function() {
  207. return this._distanceDisplayConditionProperty;
  208. }
  209. },
  210. /**
  211. * Gets or sets the {@link ClassificationType} Property specifying if this geometry will classify terrain, 3D Tiles, or both when on the ground.
  212. * @memberof GeometryUpdater.prototype
  213. *
  214. * @type {Property}
  215. * @readonly
  216. */
  217. classificationTypeProperty : {
  218. get : function() {
  219. return this._classificationTypeProperty;
  220. }
  221. },
  222. /**
  223. * Gets a value indicating if the geometry is time-varying.
  224. * If true, all visualization is delegated to a DynamicGeometryUpdater
  225. * returned by GeometryUpdater#createDynamicUpdater.
  226. * @memberof GeometryUpdater.prototype
  227. *
  228. * @type {Boolean}
  229. * @readonly
  230. */
  231. isDynamic : {
  232. get : function() {
  233. return this._dynamic;
  234. }
  235. },
  236. /**
  237. * Gets a value indicating if the geometry is closed.
  238. * This property is only valid for static geometry.
  239. * @memberof GeometryUpdater.prototype
  240. *
  241. * @type {Boolean}
  242. * @readonly
  243. */
  244. isClosed : {
  245. get : function() {
  246. return this._isClosed;
  247. }
  248. },
  249. /**
  250. * Gets a value indicating if the geometry should be drawn on terrain.
  251. * @memberof EllipseGeometryUpdater.prototype
  252. *
  253. * @type {Boolean}
  254. * @readonly
  255. */
  256. onTerrain : {
  257. get : function() {
  258. return this._onTerrain;
  259. }
  260. },
  261. /**
  262. * Gets an event that is raised whenever the public properties
  263. * of this updater change.
  264. * @memberof GeometryUpdater.prototype
  265. *
  266. * @type {Boolean}
  267. * @readonly
  268. */
  269. geometryChanged : {
  270. get : function() {
  271. return this._geometryChanged;
  272. }
  273. }
  274. });
  275. /**
  276. * Checks if the geometry is outlined at the provided time.
  277. *
  278. * @param {JulianDate} time The time for which to retrieve visibility.
  279. * @returns {Boolean} true if geometry is outlined at the provided time, false otherwise.
  280. */
  281. GeometryUpdater.prototype.isOutlineVisible = function(time) {
  282. var entity = this._entity;
  283. var visible = this._outlineEnabled && entity.isAvailable(time) && this._showProperty.getValue(time) && this._showOutlineProperty.getValue(time);
  284. return defaultValue(visible, false);
  285. };
  286. /**
  287. * Checks if the geometry is filled at the provided time.
  288. *
  289. * @param {JulianDate} time The time for which to retrieve visibility.
  290. * @returns {Boolean} true if geometry is filled at the provided time, false otherwise.
  291. */
  292. GeometryUpdater.prototype.isFilled = function(time) {
  293. var entity = this._entity;
  294. var visible = this._fillEnabled && entity.isAvailable(time) && this._showProperty.getValue(time) && this._fillProperty.getValue(time);
  295. return defaultValue(visible, false);
  296. };
  297. /**
  298. * Creates the geometry instance which represents the fill of the geometry.
  299. *
  300. * @function
  301. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  302. * @returns {GeometryInstance} The geometry instance representing the filled portion of the geometry.
  303. *
  304. * @exception {DeveloperError} This instance does not represent a filled geometry.
  305. */
  306. GeometryUpdater.prototype.createFillGeometryInstance = DeveloperError.throwInstantiationError;
  307. /**
  308. * Creates the geometry instance which represents the outline of the geometry.
  309. *
  310. * @function
  311. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  312. * @returns {GeometryInstance} The geometry instance representing the outline portion of the geometry.
  313. *
  314. * @exception {DeveloperError} This instance does not represent an outlined geometry.
  315. */
  316. GeometryUpdater.prototype.createOutlineGeometryInstance = DeveloperError.throwInstantiationError;
  317. /**
  318. * Returns true if this object was destroyed; otherwise, false.
  319. *
  320. * @returns {Boolean} True if this object was destroyed; otherwise, false.
  321. */
  322. GeometryUpdater.prototype.isDestroyed = function() {
  323. return false;
  324. };
  325. /**
  326. * Destroys and resources used by the object. Once an object is destroyed, it should not be used.
  327. *
  328. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  329. */
  330. GeometryUpdater.prototype.destroy = function() {
  331. destroyObject(this);
  332. };
  333. /**
  334. * @param {Entity} entity
  335. * @param {Object} geometry
  336. * @private
  337. */
  338. GeometryUpdater.prototype._isHidden = function(entity, geometry) {
  339. var show = geometry.show;
  340. return defined(show) && show.isConstant && !show.getValue(Iso8601.MINIMUM_VALUE);
  341. };
  342. /**
  343. * @param {Entity} entity
  344. * @param {Object} geometry
  345. * @private
  346. */
  347. GeometryUpdater.prototype._isOnTerrain = function(entity, geometry) {
  348. return false;
  349. };
  350. /**
  351. * @param {GeometryOptions} options
  352. * @private
  353. */
  354. GeometryUpdater.prototype._getIsClosed = function(options) {
  355. return true;
  356. };
  357. /**
  358. * @param {Entity} entity
  359. * @param {Object} geometry
  360. * @private
  361. */
  362. GeometryUpdater.prototype._isDynamic = DeveloperError.throwInstantiationError;
  363. /**
  364. * @param {Entity} entity
  365. * @param {Object} geometry
  366. * @private
  367. */
  368. GeometryUpdater.prototype._setStaticOptions = DeveloperError.throwInstantiationError;
  369. /**
  370. * @param {Entity} entity
  371. * @param {String} propertyName
  372. * @param {*} newValue
  373. * @param {*} oldValue
  374. * @private
  375. */
  376. GeometryUpdater.prototype._onEntityPropertyChanged = function(entity, propertyName, newValue, oldValue) {
  377. if (this._observedPropertyNames.indexOf(propertyName) === -1) {
  378. return;
  379. }
  380. var geometry = this._entity[this._geometryPropertyName];
  381. if (!defined(geometry)) {
  382. if (this._fillEnabled || this._outlineEnabled) {
  383. this._fillEnabled = false;
  384. this._outlineEnabled = false;
  385. this._geometryChanged.raiseEvent(this);
  386. }
  387. return;
  388. }
  389. var fillProperty = geometry.fill;
  390. var fillEnabled = defined(fillProperty) && fillProperty.isConstant ? fillProperty.getValue(Iso8601.MINIMUM_VALUE) : true;
  391. var outlineProperty = geometry.outline;
  392. var outlineEnabled = defined(outlineProperty);
  393. if (outlineEnabled && outlineProperty.isConstant) {
  394. outlineEnabled = outlineProperty.getValue(Iso8601.MINIMUM_VALUE);
  395. }
  396. if (!fillEnabled && !outlineEnabled) {
  397. if (this._fillEnabled || this._outlineEnabled) {
  398. this._fillEnabled = false;
  399. this._outlineEnabled = false;
  400. this._geometryChanged.raiseEvent(this);
  401. }
  402. return;
  403. }
  404. var show = geometry.show;
  405. if (this._isHidden(entity, geometry)) {
  406. if (this._fillEnabled || this._outlineEnabled) {
  407. this._fillEnabled = false;
  408. this._outlineEnabled = false;
  409. this._geometryChanged.raiseEvent(this);
  410. }
  411. return;
  412. }
  413. this._materialProperty = defaultValue(geometry.material, defaultMaterial);
  414. this._fillProperty = defaultValue(fillProperty, defaultFill);
  415. this._showProperty = defaultValue(show, defaultShow);
  416. this._showOutlineProperty = defaultValue(geometry.outline, defaultOutline);
  417. this._outlineColorProperty = outlineEnabled ? defaultValue(geometry.outlineColor, defaultOutlineColor) : undefined;
  418. this._shadowsProperty = defaultValue(geometry.shadows, defaultShadows);
  419. this._distanceDisplayConditionProperty = defaultValue(geometry.distanceDisplayCondition, defaultDistanceDisplayCondition);
  420. this._classificationTypeProperty = defaultValue(geometry.classificationType, defaultClassificationType);
  421. this._fillEnabled = fillEnabled;
  422. var onTerrain = this._isOnTerrain(entity, geometry) &&
  423. (this._supportsMaterialsforEntitiesOnTerrain || this._materialProperty instanceof ColorMaterialProperty);
  424. if (outlineEnabled && onTerrain) {
  425. oneTimeWarning(oneTimeWarning.geometryOutlines);
  426. outlineEnabled = false;
  427. }
  428. this._onTerrain = onTerrain;
  429. this._outlineEnabled = outlineEnabled;
  430. if (this._isDynamic(entity, geometry)) {
  431. if (!this._dynamic) {
  432. this._dynamic = true;
  433. this._geometryChanged.raiseEvent(this);
  434. }
  435. } else {
  436. this._setStaticOptions(entity, geometry);
  437. this._isClosed = this._getIsClosed(this._options);
  438. var outlineWidth = geometry.outlineWidth;
  439. this._outlineWidth = defined(outlineWidth) ? outlineWidth.getValue(Iso8601.MINIMUM_VALUE) : 1.0;
  440. this._dynamic = false;
  441. this._geometryChanged.raiseEvent(this);
  442. }
  443. };
  444. /**
  445. * Creates the dynamic updater to be used when GeometryUpdater#isDynamic is true.
  446. *
  447. * @param {PrimitiveCollection} primitives The primitive collection to use.
  448. * @param {PrimitiveCollection} [groundPrimitives] The primitive collection to use for ground primitives.
  449. *
  450. * @returns {DynamicGeometryUpdater} The dynamic updater used to update the geometry each frame.
  451. *
  452. * @exception {DeveloperError} This instance does not represent dynamic geometry.
  453. */
  454. GeometryUpdater.prototype.createDynamicUpdater = function(primitives, groundPrimitives) {
  455. //>>includeStart('debug', pragmas.debug);
  456. Check.defined('primitives', primitives);
  457. Check.defined('groundPrimitives', groundPrimitives);
  458. if (!this._dynamic) {
  459. throw new DeveloperError('This instance does not represent dynamic geometry.');
  460. }
  461. //>>includeEnd('debug');
  462. return new this.constructor.DynamicGeometryUpdater(this, primitives, groundPrimitives);
  463. };
  464. export default GeometryUpdater;