CorridorGeometryUpdater.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import ApproximateTerrainHeights from "../Core/ApproximateTerrainHeights.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Check from "../Core/Check.js";
  4. import Color from "../Core/Color.js";
  5. import ColorGeometryInstanceAttribute from "../Core/ColorGeometryInstanceAttribute.js";
  6. import CorridorGeometry from "../Core/CorridorGeometry.js";
  7. import CorridorOutlineGeometry from "../Core/CorridorOutlineGeometry.js";
  8. import defined from "../Core/defined.js";
  9. import DeveloperError from "../Core/DeveloperError.js";
  10. import DistanceDisplayConditionGeometryInstanceAttribute from "../Core/DistanceDisplayConditionGeometryInstanceAttribute.js";
  11. import GeometryInstance from "../Core/GeometryInstance.js";
  12. import Iso8601 from "../Core/Iso8601.js";
  13. import OffsetGeometryInstanceAttribute from "../Core/OffsetGeometryInstanceAttribute.js";
  14. import Rectangle from "../Core/Rectangle.js";
  15. import ShowGeometryInstanceAttribute from "../Core/ShowGeometryInstanceAttribute.js";
  16. import HeightReference from "../Scene/HeightReference.js";
  17. import MaterialAppearance from "../Scene/MaterialAppearance.js";
  18. import PerInstanceColorAppearance from "../Scene/PerInstanceColorAppearance.js";
  19. import ColorMaterialProperty from "./ColorMaterialProperty.js";
  20. import DynamicGeometryUpdater from "./DynamicGeometryUpdater.js";
  21. import GeometryUpdater from "./GeometryUpdater.js";
  22. import GroundGeometryUpdater from "./GroundGeometryUpdater.js";
  23. import Property from "./Property.js";
  24. const scratchColor = new Color();
  25. const defaultOffset = Cartesian3.ZERO;
  26. const offsetScratch = new Cartesian3();
  27. const scratchRectangle = new Rectangle();
  28. function CorridorGeometryOptions(entity) {
  29. this.id = entity;
  30. this.vertexFormat = undefined;
  31. this.positions = undefined;
  32. this.width = undefined;
  33. this.cornerType = undefined;
  34. this.height = undefined;
  35. this.extrudedHeight = undefined;
  36. this.granularity = undefined;
  37. this.offsetAttribute = undefined;
  38. }
  39. /**
  40. * A {@link GeometryUpdater} for corridors.
  41. * Clients do not normally create this class directly, but instead rely on {@link DataSourceDisplay}.
  42. * @alias CorridorGeometryUpdater
  43. * @constructor
  44. *
  45. * @param {Entity} entity The entity containing the geometry to be visualized.
  46. * @param {Scene} scene The scene where visualization is taking place.
  47. */
  48. function CorridorGeometryUpdater(entity, scene) {
  49. GroundGeometryUpdater.call(this, {
  50. entity: entity,
  51. scene: scene,
  52. geometryOptions: new CorridorGeometryOptions(entity),
  53. geometryPropertyName: "corridor",
  54. observedPropertyNames: ["availability", "corridor"],
  55. });
  56. this._onEntityPropertyChanged(entity, "corridor", entity.corridor, undefined);
  57. }
  58. if (defined(Object.create)) {
  59. CorridorGeometryUpdater.prototype = Object.create(
  60. GroundGeometryUpdater.prototype
  61. );
  62. CorridorGeometryUpdater.prototype.constructor = CorridorGeometryUpdater;
  63. }
  64. /**
  65. * Creates the geometry instance which represents the fill of the geometry.
  66. *
  67. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  68. * @returns {GeometryInstance} The geometry instance representing the filled portion of the geometry.
  69. *
  70. * @exception {DeveloperError} This instance does not represent a filled geometry.
  71. */
  72. CorridorGeometryUpdater.prototype.createFillGeometryInstance = function (time) {
  73. //>>includeStart('debug', pragmas.debug);
  74. Check.defined("time", time);
  75. if (!this._fillEnabled) {
  76. throw new DeveloperError(
  77. "This instance does not represent a filled geometry."
  78. );
  79. }
  80. //>>includeEnd('debug');
  81. const entity = this._entity;
  82. const isAvailable = entity.isAvailable(time);
  83. const attributes = {
  84. show: new ShowGeometryInstanceAttribute(
  85. isAvailable &&
  86. entity.isShowing &&
  87. this._showProperty.getValue(time) &&
  88. this._fillProperty.getValue(time)
  89. ),
  90. distanceDisplayCondition: DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(
  91. this._distanceDisplayConditionProperty.getValue(time)
  92. ),
  93. offset: undefined,
  94. color: undefined,
  95. };
  96. if (this._materialProperty instanceof ColorMaterialProperty) {
  97. let currentColor;
  98. if (
  99. defined(this._materialProperty.color) &&
  100. (this._materialProperty.color.isConstant || isAvailable)
  101. ) {
  102. currentColor = this._materialProperty.color.getValue(time, scratchColor);
  103. }
  104. if (!defined(currentColor)) {
  105. currentColor = Color.WHITE;
  106. }
  107. attributes.color = ColorGeometryInstanceAttribute.fromColor(currentColor);
  108. }
  109. if (defined(this._options.offsetAttribute)) {
  110. attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(
  111. Property.getValueOrDefault(
  112. this._terrainOffsetProperty,
  113. time,
  114. defaultOffset,
  115. offsetScratch
  116. )
  117. );
  118. }
  119. return new GeometryInstance({
  120. id: entity,
  121. geometry: new CorridorGeometry(this._options),
  122. attributes: attributes,
  123. });
  124. };
  125. /**
  126. * Creates the geometry instance which represents the outline of the geometry.
  127. *
  128. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  129. * @returns {GeometryInstance} The geometry instance representing the outline portion of the geometry.
  130. *
  131. * @exception {DeveloperError} This instance does not represent an outlined geometry.
  132. */
  133. CorridorGeometryUpdater.prototype.createOutlineGeometryInstance = function (
  134. time
  135. ) {
  136. //>>includeStart('debug', pragmas.debug);
  137. Check.defined("time", time);
  138. if (!this._outlineEnabled) {
  139. throw new DeveloperError(
  140. "This instance does not represent an outlined geometry."
  141. );
  142. }
  143. //>>includeEnd('debug');
  144. const entity = this._entity;
  145. const isAvailable = entity.isAvailable(time);
  146. const outlineColor = Property.getValueOrDefault(
  147. this._outlineColorProperty,
  148. time,
  149. Color.BLACK,
  150. scratchColor
  151. );
  152. const attributes = {
  153. show: new ShowGeometryInstanceAttribute(
  154. isAvailable &&
  155. entity.isShowing &&
  156. this._showProperty.getValue(time) &&
  157. this._showOutlineProperty.getValue(time)
  158. ),
  159. color: ColorGeometryInstanceAttribute.fromColor(outlineColor),
  160. distanceDisplayCondition: DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(
  161. this._distanceDisplayConditionProperty.getValue(time)
  162. ),
  163. offset: undefined,
  164. };
  165. if (defined(this._options.offsetAttribute)) {
  166. attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(
  167. Property.getValueOrDefault(
  168. this._terrainOffsetProperty,
  169. time,
  170. defaultOffset,
  171. offsetScratch
  172. )
  173. );
  174. }
  175. return new GeometryInstance({
  176. id: entity,
  177. geometry: new CorridorOutlineGeometry(this._options),
  178. attributes: attributes,
  179. });
  180. };
  181. CorridorGeometryUpdater.prototype._computeCenter = function (time, result) {
  182. const positions = Property.getValueOrUndefined(
  183. this._entity.corridor.positions,
  184. time
  185. );
  186. if (!defined(positions) || positions.length === 0) {
  187. return;
  188. }
  189. return Cartesian3.clone(
  190. positions[Math.floor(positions.length / 2.0)],
  191. result
  192. );
  193. };
  194. CorridorGeometryUpdater.prototype._isHidden = function (entity, corridor) {
  195. return (
  196. !defined(corridor.positions) ||
  197. !defined(corridor.width) ||
  198. GeometryUpdater.prototype._isHidden.call(this, entity, corridor)
  199. );
  200. };
  201. CorridorGeometryUpdater.prototype._isDynamic = function (entity, corridor) {
  202. return (
  203. !corridor.positions.isConstant || //
  204. !Property.isConstant(corridor.height) || //
  205. !Property.isConstant(corridor.extrudedHeight) || //
  206. !Property.isConstant(corridor.granularity) || //
  207. !Property.isConstant(corridor.width) || //
  208. !Property.isConstant(corridor.outlineWidth) || //
  209. !Property.isConstant(corridor.cornerType) || //
  210. !Property.isConstant(corridor.zIndex) || //
  211. (this._onTerrain &&
  212. !Property.isConstant(this._materialProperty) &&
  213. !(this._materialProperty instanceof ColorMaterialProperty))
  214. );
  215. };
  216. CorridorGeometryUpdater.prototype._setStaticOptions = function (
  217. entity,
  218. corridor
  219. ) {
  220. let heightValue = Property.getValueOrUndefined(
  221. corridor.height,
  222. Iso8601.MINIMUM_VALUE
  223. );
  224. const heightReferenceValue = Property.getValueOrDefault(
  225. corridor.heightReference,
  226. Iso8601.MINIMUM_VALUE,
  227. HeightReference.NONE
  228. );
  229. let extrudedHeightValue = Property.getValueOrUndefined(
  230. corridor.extrudedHeight,
  231. Iso8601.MINIMUM_VALUE
  232. );
  233. const extrudedHeightReferenceValue = Property.getValueOrDefault(
  234. corridor.extrudedHeightReference,
  235. Iso8601.MINIMUM_VALUE,
  236. HeightReference.NONE
  237. );
  238. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  239. heightValue = 0;
  240. }
  241. const options = this._options;
  242. options.vertexFormat =
  243. this._materialProperty instanceof ColorMaterialProperty
  244. ? PerInstanceColorAppearance.VERTEX_FORMAT
  245. : MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat;
  246. options.positions = corridor.positions.getValue(
  247. Iso8601.MINIMUM_VALUE,
  248. options.positions
  249. );
  250. options.width = corridor.width.getValue(Iso8601.MINIMUM_VALUE);
  251. options.granularity = Property.getValueOrUndefined(
  252. corridor.granularity,
  253. Iso8601.MINIMUM_VALUE
  254. );
  255. options.cornerType = Property.getValueOrUndefined(
  256. corridor.cornerType,
  257. Iso8601.MINIMUM_VALUE
  258. );
  259. options.offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(
  260. heightValue,
  261. heightReferenceValue,
  262. extrudedHeightValue,
  263. extrudedHeightReferenceValue
  264. );
  265. options.height = GroundGeometryUpdater.getGeometryHeight(
  266. heightValue,
  267. heightReferenceValue
  268. );
  269. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(
  270. extrudedHeightValue,
  271. extrudedHeightReferenceValue
  272. );
  273. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  274. extrudedHeightValue = ApproximateTerrainHeights.getMinimumMaximumHeights(
  275. CorridorGeometry.computeRectangle(options, scratchRectangle)
  276. ).minimumTerrainHeight;
  277. }
  278. options.extrudedHeight = extrudedHeightValue;
  279. };
  280. CorridorGeometryUpdater.DynamicGeometryUpdater = DynamicCorridorGeometryUpdater;
  281. /**
  282. * @private
  283. */
  284. function DynamicCorridorGeometryUpdater(
  285. geometryUpdater,
  286. primitives,
  287. groundPrimitives
  288. ) {
  289. DynamicGeometryUpdater.call(
  290. this,
  291. geometryUpdater,
  292. primitives,
  293. groundPrimitives
  294. );
  295. }
  296. if (defined(Object.create)) {
  297. DynamicCorridorGeometryUpdater.prototype = Object.create(
  298. DynamicGeometryUpdater.prototype
  299. );
  300. DynamicCorridorGeometryUpdater.prototype.constructor = DynamicCorridorGeometryUpdater;
  301. }
  302. DynamicCorridorGeometryUpdater.prototype._isHidden = function (
  303. entity,
  304. corridor,
  305. time
  306. ) {
  307. const options = this._options;
  308. return (
  309. !defined(options.positions) ||
  310. !defined(options.width) ||
  311. DynamicGeometryUpdater.prototype._isHidden.call(
  312. this,
  313. entity,
  314. corridor,
  315. time
  316. )
  317. );
  318. };
  319. DynamicCorridorGeometryUpdater.prototype._setOptions = function (
  320. entity,
  321. corridor,
  322. time
  323. ) {
  324. const options = this._options;
  325. let heightValue = Property.getValueOrUndefined(corridor.height, time);
  326. const heightReferenceValue = Property.getValueOrDefault(
  327. corridor.heightReference,
  328. time,
  329. HeightReference.NONE
  330. );
  331. let extrudedHeightValue = Property.getValueOrUndefined(
  332. corridor.extrudedHeight,
  333. time
  334. );
  335. const extrudedHeightReferenceValue = Property.getValueOrDefault(
  336. corridor.extrudedHeightReference,
  337. time,
  338. HeightReference.NONE
  339. );
  340. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  341. heightValue = 0;
  342. }
  343. options.positions = Property.getValueOrUndefined(corridor.positions, time);
  344. options.width = Property.getValueOrUndefined(corridor.width, time);
  345. options.granularity = Property.getValueOrUndefined(
  346. corridor.granularity,
  347. time
  348. );
  349. options.cornerType = Property.getValueOrUndefined(corridor.cornerType, time);
  350. options.offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(
  351. heightValue,
  352. heightReferenceValue,
  353. extrudedHeightValue,
  354. extrudedHeightReferenceValue
  355. );
  356. options.height = GroundGeometryUpdater.getGeometryHeight(
  357. heightValue,
  358. heightReferenceValue
  359. );
  360. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(
  361. extrudedHeightValue,
  362. extrudedHeightReferenceValue
  363. );
  364. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  365. extrudedHeightValue = ApproximateTerrainHeights.getMinimumMaximumHeights(
  366. CorridorGeometry.computeRectangle(options, scratchRectangle)
  367. ).minimumTerrainHeight;
  368. }
  369. options.extrudedHeight = extrudedHeightValue;
  370. };
  371. export default CorridorGeometryUpdater;