Entity.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. import Cartesian3 from "../Core/Cartesian3.js";
  2. import Cartographic from "../Core/Cartographic.js";
  3. import Check from "../Core/Check.js";
  4. import createGuid from "../Core/createGuid.js";
  5. import defaultValue from "../Core/defaultValue.js";
  6. import defined from "../Core/defined.js";
  7. import DeveloperError from "../Core/DeveloperError.js";
  8. import Event from "../Core/Event.js";
  9. import CesiumMath from "../Core/Math.js";
  10. import Matrix3 from "../Core/Matrix3.js";
  11. import Matrix4 from "../Core/Matrix4.js";
  12. import Quaternion from "../Core/Quaternion.js";
  13. import Transforms from "../Core/Transforms.js";
  14. import GroundPolylinePrimitive from "../Scene/GroundPolylinePrimitive.js";
  15. import GroundPrimitive from "../Scene/GroundPrimitive.js";
  16. import HeightReference from "../Scene/HeightReference.js";
  17. import BillboardGraphics from "./BillboardGraphics.js";
  18. import BoxGraphics from "./BoxGraphics.js";
  19. import ConstantPositionProperty from "./ConstantPositionProperty.js";
  20. import CorridorGraphics from "./CorridorGraphics.js";
  21. import createPropertyDescriptor from "./createPropertyDescriptor.js";
  22. import createRawPropertyDescriptor from "./createRawPropertyDescriptor.js";
  23. import CylinderGraphics from "./CylinderGraphics.js";
  24. import EllipseGraphics from "./EllipseGraphics.js";
  25. import EllipsoidGraphics from "./EllipsoidGraphics.js";
  26. import LabelGraphics from "./LabelGraphics.js";
  27. import ModelGraphics from "./ModelGraphics.js";
  28. import Cesium3DTilesetGraphics from "./Cesium3DTilesetGraphics.js";
  29. import PathGraphics from "./PathGraphics.js";
  30. import PlaneGraphics from "./PlaneGraphics.js";
  31. import PointGraphics from "./PointGraphics.js";
  32. import PolygonGraphics from "./PolygonGraphics.js";
  33. import PolylineGraphics from "./PolylineGraphics.js";
  34. import PolylineVolumeGraphics from "./PolylineVolumeGraphics.js";
  35. import Property from "./Property.js";
  36. import PropertyBag from "./PropertyBag.js";
  37. import RectangleGraphics from "./RectangleGraphics.js";
  38. import WallGraphics from "./WallGraphics.js";
  39. const cartoScratch = new Cartographic();
  40. function createConstantPositionProperty(value) {
  41. return new ConstantPositionProperty(value);
  42. }
  43. function createPositionPropertyDescriptor(name) {
  44. return createPropertyDescriptor(
  45. name,
  46. undefined,
  47. createConstantPositionProperty
  48. );
  49. }
  50. function createPropertyTypeDescriptor(name, Type) {
  51. return createPropertyDescriptor(name, undefined, function (value) {
  52. if (value instanceof Type) {
  53. return value;
  54. }
  55. return new Type(value);
  56. });
  57. }
  58. /**
  59. * @typedef {object} Entity.ConstructorOptions
  60. *
  61. * Initialization options for the Entity constructor
  62. *
  63. * @property {string} [id] A unique identifier for this object. If none is provided, a GUID is generated.
  64. * @property {string} [name] A human readable name to display to users. It does not have to be unique.
  65. * @property {TimeIntervalCollection} [availability] The availability, if any, associated with this object.
  66. * @property {boolean} [show] A boolean value indicating if the entity and its children are displayed.
  67. * @property {Property | string} [description] A string Property specifying an HTML description for this entity.
  68. * @property {PositionProperty | Cartesian3} [position] A Property specifying the entity position.
  69. * @property {Property} [orientation] A Property specifying the entity orientation.
  70. * @property {Property} [viewFrom] A suggested initial offset for viewing this object.
  71. * @property {Entity} [parent] A parent entity to associate with this entity.
  72. * @property {BillboardGraphics | BillboardGraphics.ConstructorOptions} [billboard] A billboard to associate with this entity.
  73. * @property {BoxGraphics | BoxGraphics.ConstructorOptions} [box] A box to associate with this entity.
  74. * @property {CorridorGraphics | CorridorGraphics.ConstructorOptions} [corridor] A corridor to associate with this entity.
  75. * @property {CylinderGraphics | CylinderGraphics.ConstructorOptions} [cylinder] A cylinder to associate with this entity.
  76. * @property {EllipseGraphics | EllipseGraphics.ConstructorOptions} [ellipse] A ellipse to associate with this entity.
  77. * @property {EllipsoidGraphics | EllipsoidGraphics.ConstructorOptions} [ellipsoid] A ellipsoid to associate with this entity.
  78. * @property {LabelGraphics | LabelGraphics.ConstructorOptions} [label] A options.label to associate with this entity.
  79. * @property {ModelGraphics | ModelGraphics.ConstructorOptions} [model] A model to associate with this entity.
  80. * @property {Cesium3DTilesetGraphics | Cesium3DTilesetGraphics.ConstructorOptions} [tileset] A 3D Tiles tileset to associate with this entity.
  81. * @property {PathGraphics | PathGraphics.ConstructorOptions} [path] A path to associate with this entity.
  82. * @property {PlaneGraphics | PlaneGraphics.ConstructorOptions} [plane] A plane to associate with this entity.
  83. * @property {PointGraphics | PointGraphics.ConstructorOptions} [point] A point to associate with this entity.
  84. * @property {PolygonGraphics | PolygonGraphics.ConstructorOptions} [polygon] A polygon to associate with this entity.
  85. * @property {PolylineGraphics | PolylineGraphics.ConstructorOptions} [polyline] A polyline to associate with this entity.
  86. * @property {PropertyBag | Object<string,*>} [properties] Arbitrary properties to associate with this entity.
  87. * @property {PolylineVolumeGraphics | PolylineVolumeGraphics.ConstructorOptions} [polylineVolume] A polylineVolume to associate with this entity.
  88. * @property {RectangleGraphics | RectangleGraphics.ConstructorOptions} [rectangle] A rectangle to associate with this entity.
  89. * @property {WallGraphics | WallGraphics.ConstructorOptions} [wall] A wall to associate with this entity.
  90. */
  91. /**
  92. * Entity instances aggregate multiple forms of visualization into a single high-level object.
  93. * They can be created manually and added to {@link Viewer#entities} or be produced by
  94. * data sources, such as {@link CzmlDataSource} and {@link GeoJsonDataSource}.
  95. * @alias Entity
  96. * @constructor
  97. *
  98. * @param {Entity.ConstructorOptions} [options] Object describing initialization options
  99. *
  100. * @see {@link https://cesium.com/learn/cesiumjs-learn/cesiumjs-creating-entities/|Creating Entities}
  101. */
  102. function Entity(options) {
  103. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  104. let id = options.id;
  105. if (!defined(id)) {
  106. id = createGuid();
  107. }
  108. this._availability = undefined;
  109. this._id = id;
  110. this._definitionChanged = new Event();
  111. this._name = options.name;
  112. this._show = defaultValue(options.show, true);
  113. this._parent = undefined;
  114. this._propertyNames = [
  115. "billboard",
  116. "box",
  117. "corridor",
  118. "cylinder",
  119. "description",
  120. "ellipse", //
  121. "ellipsoid",
  122. "label",
  123. "model",
  124. "tileset",
  125. "orientation",
  126. "path",
  127. "plane",
  128. "point",
  129. "polygon", //
  130. "polyline",
  131. "polylineVolume",
  132. "position",
  133. "properties",
  134. "rectangle",
  135. "viewFrom",
  136. "wall",
  137. ];
  138. this._billboard = undefined;
  139. this._billboardSubscription = undefined;
  140. this._box = undefined;
  141. this._boxSubscription = undefined;
  142. this._corridor = undefined;
  143. this._corridorSubscription = undefined;
  144. this._cylinder = undefined;
  145. this._cylinderSubscription = undefined;
  146. this._description = undefined;
  147. this._descriptionSubscription = undefined;
  148. this._ellipse = undefined;
  149. this._ellipseSubscription = undefined;
  150. this._ellipsoid = undefined;
  151. this._ellipsoidSubscription = undefined;
  152. this._label = undefined;
  153. this._labelSubscription = undefined;
  154. this._model = undefined;
  155. this._modelSubscription = undefined;
  156. this._tileset = undefined;
  157. this._tilesetSubscription = undefined;
  158. this._orientation = undefined;
  159. this._orientationSubscription = undefined;
  160. this._path = undefined;
  161. this._pathSubscription = undefined;
  162. this._plane = undefined;
  163. this._planeSubscription = undefined;
  164. this._point = undefined;
  165. this._pointSubscription = undefined;
  166. this._polygon = undefined;
  167. this._polygonSubscription = undefined;
  168. this._polyline = undefined;
  169. this._polylineSubscription = undefined;
  170. this._polylineVolume = undefined;
  171. this._polylineVolumeSubscription = undefined;
  172. this._position = undefined;
  173. this._positionSubscription = undefined;
  174. this._properties = undefined;
  175. this._propertiesSubscription = undefined;
  176. this._rectangle = undefined;
  177. this._rectangleSubscription = undefined;
  178. this._viewFrom = undefined;
  179. this._viewFromSubscription = undefined;
  180. this._wall = undefined;
  181. this._wallSubscription = undefined;
  182. this._children = [];
  183. /**
  184. * Gets or sets the entity collection that this entity belongs to.
  185. * @type {EntityCollection}
  186. */
  187. this.entityCollection = undefined;
  188. this.parent = options.parent;
  189. this.merge(options);
  190. }
  191. function updateShow(entity, children, isShowing) {
  192. const length = children.length;
  193. for (let i = 0; i < length; i++) {
  194. const child = children[i];
  195. const childShow = child._show;
  196. const oldValue = !isShowing && childShow;
  197. const newValue = isShowing && childShow;
  198. if (oldValue !== newValue) {
  199. updateShow(child, child._children, isShowing);
  200. }
  201. }
  202. entity._definitionChanged.raiseEvent(
  203. entity,
  204. "isShowing",
  205. isShowing,
  206. !isShowing
  207. );
  208. }
  209. Object.defineProperties(Entity.prototype, {
  210. /**
  211. * The availability, if any, associated with this object.
  212. * If availability is undefined, it is assumed that this object's
  213. * other properties will return valid data for any provided time.
  214. * If availability exists, the objects other properties will only
  215. * provide valid data if queried within the given interval.
  216. * @memberof Entity.prototype
  217. * @type {TimeIntervalCollection|undefined}
  218. */
  219. availability: createRawPropertyDescriptor("availability"),
  220. /**
  221. * Gets the unique ID associated with this object.
  222. * @memberof Entity.prototype
  223. * @type {string}
  224. */
  225. id: {
  226. get: function () {
  227. return this._id;
  228. },
  229. },
  230. /**
  231. * Gets the event that is raised whenever a property or sub-property is changed or modified.
  232. * @memberof Entity.prototype
  233. *
  234. * @type {Event}
  235. * @readonly
  236. */
  237. definitionChanged: {
  238. get: function () {
  239. return this._definitionChanged;
  240. },
  241. },
  242. /**
  243. * Gets or sets the name of the object. The name is intended for end-user
  244. * consumption and does not need to be unique.
  245. * @memberof Entity.prototype
  246. * @type {string|undefined}
  247. */
  248. name: createRawPropertyDescriptor("name"),
  249. /**
  250. * Gets or sets whether this entity should be displayed. When set to true,
  251. * the entity is only displayed if the parent entity's show property is also true.
  252. * @memberof Entity.prototype
  253. * @type {boolean}
  254. */
  255. show: {
  256. get: function () {
  257. return this._show;
  258. },
  259. set: function (value) {
  260. //>>includeStart('debug', pragmas.debug);
  261. if (!defined(value)) {
  262. throw new DeveloperError("value is required.");
  263. }
  264. //>>includeEnd('debug');
  265. if (value === this._show) {
  266. return;
  267. }
  268. const wasShowing = this.isShowing;
  269. this._show = value;
  270. const isShowing = this.isShowing;
  271. if (wasShowing !== isShowing) {
  272. updateShow(this, this._children, isShowing);
  273. }
  274. this._definitionChanged.raiseEvent(this, "show", value, !value);
  275. },
  276. },
  277. /**
  278. * Gets whether this entity is being displayed, taking into account
  279. * the visibility of any ancestor entities.
  280. * @memberof Entity.prototype
  281. * @type {boolean}
  282. */
  283. isShowing: {
  284. get: function () {
  285. return (
  286. this._show &&
  287. (!defined(this.entityCollection) || this.entityCollection.show) &&
  288. (!defined(this._parent) || this._parent.isShowing)
  289. );
  290. },
  291. },
  292. /**
  293. * Gets or sets the parent object.
  294. * @memberof Entity.prototype
  295. * @type {Entity|undefined}
  296. */
  297. parent: {
  298. get: function () {
  299. return this._parent;
  300. },
  301. set: function (value) {
  302. const oldValue = this._parent;
  303. if (oldValue === value) {
  304. return;
  305. }
  306. const wasShowing = this.isShowing;
  307. if (defined(oldValue)) {
  308. const index = oldValue._children.indexOf(this);
  309. oldValue._children.splice(index, 1);
  310. }
  311. this._parent = value;
  312. if (defined(value)) {
  313. value._children.push(this);
  314. }
  315. const isShowing = this.isShowing;
  316. if (wasShowing !== isShowing) {
  317. updateShow(this, this._children, isShowing);
  318. }
  319. this._definitionChanged.raiseEvent(this, "parent", value, oldValue);
  320. },
  321. },
  322. /**
  323. * Gets the names of all properties registered on this instance.
  324. * @memberof Entity.prototype
  325. * @type {string[]}
  326. */
  327. propertyNames: {
  328. get: function () {
  329. return this._propertyNames;
  330. },
  331. },
  332. /**
  333. * Gets or sets the billboard.
  334. * @memberof Entity.prototype
  335. * @type {BillboardGraphics|undefined}
  336. */
  337. billboard: createPropertyTypeDescriptor("billboard", BillboardGraphics),
  338. /**
  339. * Gets or sets the box.
  340. * @memberof Entity.prototype
  341. * @type {BoxGraphics|undefined}
  342. */
  343. box: createPropertyTypeDescriptor("box", BoxGraphics),
  344. /**
  345. * Gets or sets the corridor.
  346. * @memberof Entity.prototype
  347. * @type {CorridorGraphics|undefined}
  348. */
  349. corridor: createPropertyTypeDescriptor("corridor", CorridorGraphics),
  350. /**
  351. * Gets or sets the cylinder.
  352. * @memberof Entity.prototype
  353. * @type {CylinderGraphics|undefined}
  354. */
  355. cylinder: createPropertyTypeDescriptor("cylinder", CylinderGraphics),
  356. /**
  357. * Gets or sets the description.
  358. * @memberof Entity.prototype
  359. * @type {Property|undefined}
  360. */
  361. description: createPropertyDescriptor("description"),
  362. /**
  363. * Gets or sets the ellipse.
  364. * @memberof Entity.prototype
  365. * @type {EllipseGraphics|undefined}
  366. */
  367. ellipse: createPropertyTypeDescriptor("ellipse", EllipseGraphics),
  368. /**
  369. * Gets or sets the ellipsoid.
  370. * @memberof Entity.prototype
  371. * @type {EllipsoidGraphics|undefined}
  372. */
  373. ellipsoid: createPropertyTypeDescriptor("ellipsoid", EllipsoidGraphics),
  374. /**
  375. * Gets or sets the label.
  376. * @memberof Entity.prototype
  377. * @type {LabelGraphics|undefined}
  378. */
  379. label: createPropertyTypeDescriptor("label", LabelGraphics),
  380. /**
  381. * Gets or sets the model.
  382. * @memberof Entity.prototype
  383. * @type {ModelGraphics|undefined}
  384. */
  385. model: createPropertyTypeDescriptor("model", ModelGraphics),
  386. /**
  387. * Gets or sets the tileset.
  388. * @memberof Entity.prototype
  389. * @type {Cesium3DTilesetGraphics|undefined}
  390. */
  391. tileset: createPropertyTypeDescriptor("tileset", Cesium3DTilesetGraphics),
  392. /**
  393. * Gets or sets the orientation.
  394. * @memberof Entity.prototype
  395. * @type {Property|undefined}
  396. */
  397. orientation: createPropertyDescriptor("orientation"),
  398. /**
  399. * Gets or sets the path.
  400. * @memberof Entity.prototype
  401. * @type {PathGraphics|undefined}
  402. */
  403. path: createPropertyTypeDescriptor("path", PathGraphics),
  404. /**
  405. * Gets or sets the plane.
  406. * @memberof Entity.prototype
  407. * @type {PlaneGraphics|undefined}
  408. */
  409. plane: createPropertyTypeDescriptor("plane", PlaneGraphics),
  410. /**
  411. * Gets or sets the point graphic.
  412. * @memberof Entity.prototype
  413. * @type {PointGraphics|undefined}
  414. */
  415. point: createPropertyTypeDescriptor("point", PointGraphics),
  416. /**
  417. * Gets or sets the polygon.
  418. * @memberof Entity.prototype
  419. * @type {PolygonGraphics|undefined}
  420. */
  421. polygon: createPropertyTypeDescriptor("polygon", PolygonGraphics),
  422. /**
  423. * Gets or sets the polyline.
  424. * @memberof Entity.prototype
  425. * @type {PolylineGraphics|undefined}
  426. */
  427. polyline: createPropertyTypeDescriptor("polyline", PolylineGraphics),
  428. /**
  429. * Gets or sets the polyline volume.
  430. * @memberof Entity.prototype
  431. * @type {PolylineVolumeGraphics|undefined}
  432. */
  433. polylineVolume: createPropertyTypeDescriptor(
  434. "polylineVolume",
  435. PolylineVolumeGraphics
  436. ),
  437. /**
  438. * Gets or sets the bag of arbitrary properties associated with this entity.
  439. * @memberof Entity.prototype
  440. * @type {PropertyBag|undefined}
  441. */
  442. properties: createPropertyTypeDescriptor("properties", PropertyBag),
  443. /**
  444. * Gets or sets the position.
  445. * @memberof Entity.prototype
  446. * @type {PositionProperty|undefined}
  447. */
  448. position: createPositionPropertyDescriptor("position"),
  449. /**
  450. * Gets or sets the rectangle.
  451. * @memberof Entity.prototype
  452. * @type {RectangleGraphics|undefined}
  453. */
  454. rectangle: createPropertyTypeDescriptor("rectangle", RectangleGraphics),
  455. /**
  456. * Gets or sets the suggested initial offset when tracking this object.
  457. * The offset is typically defined in the east-north-up reference frame,
  458. * but may be another frame depending on the object's velocity.
  459. * @memberof Entity.prototype
  460. * @type {Property|undefined}
  461. */
  462. viewFrom: createPropertyDescriptor("viewFrom"),
  463. /**
  464. * Gets or sets the wall.
  465. * @memberof Entity.prototype
  466. * @type {WallGraphics|undefined}
  467. */
  468. wall: createPropertyTypeDescriptor("wall", WallGraphics),
  469. });
  470. /**
  471. * Given a time, returns true if this object should have data during that time.
  472. *
  473. * @param {JulianDate} time The time to check availability for.
  474. * @returns {boolean} true if the object should have data during the provided time, false otherwise.
  475. */
  476. Entity.prototype.isAvailable = function (time) {
  477. //>>includeStart('debug', pragmas.debug);
  478. if (!defined(time)) {
  479. throw new DeveloperError("time is required.");
  480. }
  481. //>>includeEnd('debug');
  482. const availability = this._availability;
  483. return !defined(availability) || availability.contains(time);
  484. };
  485. /**
  486. * Adds a property to this object. Once a property is added, it can be
  487. * observed with {@link Entity#definitionChanged} and composited
  488. * with {@link CompositeEntityCollection}
  489. *
  490. * @param {string} propertyName The name of the property to add.
  491. *
  492. * @exception {DeveloperError} "propertyName" is a reserved property name.
  493. * @exception {DeveloperError} "propertyName" is already a registered property.
  494. */
  495. Entity.prototype.addProperty = function (propertyName) {
  496. const propertyNames = this._propertyNames;
  497. //>>includeStart('debug', pragmas.debug);
  498. if (!defined(propertyName)) {
  499. throw new DeveloperError("propertyName is required.");
  500. }
  501. if (propertyNames.indexOf(propertyName) !== -1) {
  502. throw new DeveloperError(
  503. `${propertyName} is already a registered property.`
  504. );
  505. }
  506. if (propertyName in this) {
  507. throw new DeveloperError(`${propertyName} is a reserved property name.`);
  508. }
  509. //>>includeEnd('debug');
  510. propertyNames.push(propertyName);
  511. Object.defineProperty(
  512. this,
  513. propertyName,
  514. createRawPropertyDescriptor(propertyName, true)
  515. );
  516. };
  517. /**
  518. * Removed a property previously added with addProperty.
  519. *
  520. * @param {string} propertyName The name of the property to remove.
  521. *
  522. * @exception {DeveloperError} "propertyName" is a reserved property name.
  523. * @exception {DeveloperError} "propertyName" is not a registered property.
  524. */
  525. Entity.prototype.removeProperty = function (propertyName) {
  526. const propertyNames = this._propertyNames;
  527. const index = propertyNames.indexOf(propertyName);
  528. //>>includeStart('debug', pragmas.debug);
  529. if (!defined(propertyName)) {
  530. throw new DeveloperError("propertyName is required.");
  531. }
  532. if (index === -1) {
  533. throw new DeveloperError(`${propertyName} is not a registered property.`);
  534. }
  535. //>>includeEnd('debug');
  536. this._propertyNames.splice(index, 1);
  537. delete this[propertyName];
  538. };
  539. /**
  540. * Assigns each unassigned property on this object to the value
  541. * of the same property on the provided source object.
  542. *
  543. * @param {Entity} source The object to be merged into this object.
  544. */
  545. Entity.prototype.merge = function (source) {
  546. //>>includeStart('debug', pragmas.debug);
  547. if (!defined(source)) {
  548. throw new DeveloperError("source is required.");
  549. }
  550. //>>includeEnd('debug');
  551. //Name, show, and availability are not Property objects and are currently handled differently.
  552. //source.show is intentionally ignored because this.show always has a value.
  553. this.name = defaultValue(this.name, source.name);
  554. this.availability = defaultValue(this.availability, source.availability);
  555. const propertyNames = this._propertyNames;
  556. const sourcePropertyNames = defined(source._propertyNames)
  557. ? source._propertyNames
  558. : Object.keys(source);
  559. const propertyNamesLength = sourcePropertyNames.length;
  560. for (let i = 0; i < propertyNamesLength; i++) {
  561. const name = sourcePropertyNames[i];
  562. //While source is required by the API to be an Entity, we internally call this method from the
  563. //constructor with an options object to configure initial custom properties.
  564. //So we need to ignore reserved-non-property.
  565. if (
  566. name === "parent" ||
  567. name === "name" ||
  568. name === "availability" ||
  569. name === "children"
  570. ) {
  571. continue;
  572. }
  573. const targetProperty = this[name];
  574. const sourceProperty = source[name];
  575. //Custom properties that are registered on the source entity must also
  576. //get registered on this entity.
  577. if (!defined(targetProperty) && propertyNames.indexOf(name) === -1) {
  578. this.addProperty(name);
  579. }
  580. if (defined(sourceProperty)) {
  581. if (defined(targetProperty)) {
  582. if (defined(targetProperty.merge)) {
  583. targetProperty.merge(sourceProperty);
  584. }
  585. } else if (
  586. defined(sourceProperty.merge) &&
  587. defined(sourceProperty.clone)
  588. ) {
  589. this[name] = sourceProperty.clone();
  590. } else {
  591. this[name] = sourceProperty;
  592. }
  593. }
  594. }
  595. };
  596. const matrix3Scratch = new Matrix3();
  597. const positionScratch = new Cartesian3();
  598. const orientationScratch = new Quaternion();
  599. /**
  600. * Computes the model matrix for the entity's transform at specified time. Returns undefined if orientation or position
  601. * are undefined.
  602. *
  603. * @param {JulianDate} time The time to retrieve model matrix for.
  604. * @param {Matrix4} [result] The object onto which to store the result.
  605. *
  606. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. Result is undefined if position or orientation are undefined.
  607. */
  608. Entity.prototype.computeModelMatrix = function (time, result) {
  609. //>>includeStart('debug', pragmas.debug);
  610. Check.typeOf.object("time", time);
  611. //>>includeEnd('debug');
  612. const position = Property.getValueOrUndefined(
  613. this._position,
  614. time,
  615. positionScratch
  616. );
  617. if (!defined(position)) {
  618. return undefined;
  619. }
  620. const orientation = Property.getValueOrUndefined(
  621. this._orientation,
  622. time,
  623. orientationScratch
  624. );
  625. if (!defined(orientation)) {
  626. result = Transforms.eastNorthUpToFixedFrame(position, undefined, result);
  627. } else {
  628. result = Matrix4.fromRotationTranslation(
  629. Matrix3.fromQuaternion(orientation, matrix3Scratch),
  630. position,
  631. result
  632. );
  633. }
  634. return result;
  635. };
  636. /**
  637. * @private
  638. */
  639. Entity.prototype.computeModelMatrixForHeightReference = function (
  640. time,
  641. heightReferenceProperty,
  642. heightOffset,
  643. ellipsoid,
  644. result
  645. ) {
  646. //>>includeStart('debug', pragmas.debug);
  647. Check.typeOf.object("time", time);
  648. //>>includeEnd('debug');
  649. const heightReference = Property.getValueOrDefault(
  650. heightReferenceProperty,
  651. time,
  652. HeightReference.NONE
  653. );
  654. let position = Property.getValueOrUndefined(
  655. this._position,
  656. time,
  657. positionScratch
  658. );
  659. if (
  660. heightReference === HeightReference.NONE ||
  661. !defined(position) ||
  662. Cartesian3.equalsEpsilon(position, Cartesian3.ZERO, CesiumMath.EPSILON8)
  663. ) {
  664. return this.computeModelMatrix(time, result);
  665. }
  666. const carto = ellipsoid.cartesianToCartographic(position, cartoScratch);
  667. if (heightReference === HeightReference.CLAMP_TO_GROUND) {
  668. carto.height = heightOffset;
  669. } else {
  670. carto.height += heightOffset;
  671. }
  672. position = ellipsoid.cartographicToCartesian(carto, position);
  673. const orientation = Property.getValueOrUndefined(
  674. this._orientation,
  675. time,
  676. orientationScratch
  677. );
  678. if (!defined(orientation)) {
  679. result = Transforms.eastNorthUpToFixedFrame(position, undefined, result);
  680. } else {
  681. result = Matrix4.fromRotationTranslation(
  682. Matrix3.fromQuaternion(orientation, matrix3Scratch),
  683. position,
  684. result
  685. );
  686. }
  687. return result;
  688. };
  689. /**
  690. * Checks if the given Scene supports materials besides Color on Entities draped on terrain or 3D Tiles.
  691. * If this feature is not supported, Entities with non-color materials but no `height` will
  692. * instead be rendered as if height is 0.
  693. *
  694. * @param {Scene} scene The current scene.
  695. * @returns {boolean} Whether or not the current scene supports materials for entities on terrain.
  696. */
  697. Entity.supportsMaterialsforEntitiesOnTerrain = function (scene) {
  698. return GroundPrimitive.supportsMaterials(scene);
  699. };
  700. /**
  701. * Checks if the given Scene supports polylines clamped to terrain or 3D Tiles.
  702. * If this feature is not supported, Entities with PolylineGraphics will be rendered with vertices at
  703. * the provided heights and using the `arcType` parameter instead of clamped to the ground.
  704. *
  705. * @param {Scene} scene The current scene.
  706. * @returns {boolean} Whether or not the current scene supports polylines on terrain or 3D TIles.
  707. */
  708. Entity.supportsPolylinesOnTerrain = function (scene) {
  709. return GroundPolylinePrimitive.isSupported(scene);
  710. };
  711. export default Entity;