123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752 |
- import Cartesian3 from "../Core/Cartesian3.js";
- import Cartographic from "../Core/Cartographic.js";
- import Check from "../Core/Check.js";
- import createGuid from "../Core/createGuid.js";
- import defaultValue from "../Core/defaultValue.js";
- import defined from "../Core/defined.js";
- import DeveloperError from "../Core/DeveloperError.js";
- import Event from "../Core/Event.js";
- import CesiumMath from "../Core/Math.js";
- import Matrix3 from "../Core/Matrix3.js";
- import Matrix4 from "../Core/Matrix4.js";
- import Quaternion from "../Core/Quaternion.js";
- import Transforms from "../Core/Transforms.js";
- import GroundPolylinePrimitive from "../Scene/GroundPolylinePrimitive.js";
- import GroundPrimitive from "../Scene/GroundPrimitive.js";
- import HeightReference from "../Scene/HeightReference.js";
- import BillboardGraphics from "./BillboardGraphics.js";
- import BoxGraphics from "./BoxGraphics.js";
- import ConstantPositionProperty from "./ConstantPositionProperty.js";
- import CorridorGraphics from "./CorridorGraphics.js";
- import createPropertyDescriptor from "./createPropertyDescriptor.js";
- import createRawPropertyDescriptor from "./createRawPropertyDescriptor.js";
- import CylinderGraphics from "./CylinderGraphics.js";
- import EllipseGraphics from "./EllipseGraphics.js";
- import EllipsoidGraphics from "./EllipsoidGraphics.js";
- import LabelGraphics from "./LabelGraphics.js";
- import ModelGraphics from "./ModelGraphics.js";
- import Cesium3DTilesetGraphics from "./Cesium3DTilesetGraphics.js";
- import PathGraphics from "./PathGraphics.js";
- import PlaneGraphics from "./PlaneGraphics.js";
- import PointGraphics from "./PointGraphics.js";
- import PolygonGraphics from "./PolygonGraphics.js";
- import PolylineGraphics from "./PolylineGraphics.js";
- import PolylineVolumeGraphics from "./PolylineVolumeGraphics.js";
- import Property from "./Property.js";
- import PropertyBag from "./PropertyBag.js";
- import RectangleGraphics from "./RectangleGraphics.js";
- import WallGraphics from "./WallGraphics.js";
- var cartoScratch = new Cartographic();
- function createConstantPositionProperty(value) {
- return new ConstantPositionProperty(value);
- }
- function createPositionPropertyDescriptor(name) {
- return createPropertyDescriptor(
- name,
- undefined,
- createConstantPositionProperty
- );
- }
- function createPropertyTypeDescriptor(name, Type) {
- return createPropertyDescriptor(name, undefined, function (value) {
- if (value instanceof Type) {
- return value;
- }
- return new Type(value);
- });
- }
- function Entity(options) {
- options = defaultValue(options, defaultValue.EMPTY_OBJECT);
- var id = options.id;
- if (!defined(id)) {
- id = createGuid();
- }
- this._availability = undefined;
- this._id = id;
- this._definitionChanged = new Event();
- this._name = options.name;
- this._show = defaultValue(options.show, true);
- this._parent = undefined;
- this._propertyNames = [
- "billboard",
- "box",
- "corridor",
- "cylinder",
- "description",
- "ellipse",
- "ellipsoid",
- "label",
- "model",
- "tileset",
- "orientation",
- "path",
- "plane",
- "point",
- "polygon",
- "polyline",
- "polylineVolume",
- "position",
- "properties",
- "rectangle",
- "viewFrom",
- "wall",
- ];
- this._billboard = undefined;
- this._billboardSubscription = undefined;
- this._box = undefined;
- this._boxSubscription = undefined;
- this._corridor = undefined;
- this._corridorSubscription = undefined;
- this._cylinder = undefined;
- this._cylinderSubscription = undefined;
- this._description = undefined;
- this._descriptionSubscription = undefined;
- this._ellipse = undefined;
- this._ellipseSubscription = undefined;
- this._ellipsoid = undefined;
- this._ellipsoidSubscription = undefined;
- this._label = undefined;
- this._labelSubscription = undefined;
- this._model = undefined;
- this._modelSubscription = undefined;
- this._tileset = undefined;
- this._tilesetSubscription = undefined;
- this._orientation = undefined;
- this._orientationSubscription = undefined;
- this._path = undefined;
- this._pathSubscription = undefined;
- this._plane = undefined;
- this._planeSubscription = undefined;
- this._point = undefined;
- this._pointSubscription = undefined;
- this._polygon = undefined;
- this._polygonSubscription = undefined;
- this._polyline = undefined;
- this._polylineSubscription = undefined;
- this._polylineVolume = undefined;
- this._polylineVolumeSubscription = undefined;
- this._position = undefined;
- this._positionSubscription = undefined;
- this._properties = undefined;
- this._propertiesSubscription = undefined;
- this._rectangle = undefined;
- this._rectangleSubscription = undefined;
- this._viewFrom = undefined;
- this._viewFromSubscription = undefined;
- this._wall = undefined;
- this._wallSubscription = undefined;
- this._children = [];
-
- this.entityCollection = undefined;
- this.parent = options.parent;
- this.merge(options);
- }
- function updateShow(entity, children, isShowing) {
- var length = children.length;
- for (var i = 0; i < length; i++) {
- var child = children[i];
- var childShow = child._show;
- var oldValue = !isShowing && childShow;
- var newValue = isShowing && childShow;
- if (oldValue !== newValue) {
- updateShow(child, child._children, isShowing);
- }
- }
- entity._definitionChanged.raiseEvent(
- entity,
- "isShowing",
- isShowing,
- !isShowing
- );
- }
- Object.defineProperties(Entity.prototype, {
-
- availability: createRawPropertyDescriptor("availability"),
-
- id: {
- get: function () {
- return this._id;
- },
- },
-
- definitionChanged: {
- get: function () {
- return this._definitionChanged;
- },
- },
-
- name: createRawPropertyDescriptor("name"),
-
- show: {
- get: function () {
- return this._show;
- },
- set: function (value) {
-
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
-
- if (value === this._show) {
- return;
- }
- var wasShowing = this.isShowing;
- this._show = value;
- var isShowing = this.isShowing;
- if (wasShowing !== isShowing) {
- updateShow(this, this._children, isShowing);
- }
- this._definitionChanged.raiseEvent(this, "show", value, !value);
- },
- },
-
- isShowing: {
- get: function () {
- return (
- this._show &&
- (!defined(this.entityCollection) || this.entityCollection.show) &&
- (!defined(this._parent) || this._parent.isShowing)
- );
- },
- },
-
- parent: {
- get: function () {
- return this._parent;
- },
- set: function (value) {
- var oldValue = this._parent;
- if (oldValue === value) {
- return;
- }
- var wasShowing = this.isShowing;
- if (defined(oldValue)) {
- var index = oldValue._children.indexOf(this);
- oldValue._children.splice(index, 1);
- }
- this._parent = value;
- if (defined(value)) {
- value._children.push(this);
- }
- var isShowing = this.isShowing;
- if (wasShowing !== isShowing) {
- updateShow(this, this._children, isShowing);
- }
- this._definitionChanged.raiseEvent(this, "parent", value, oldValue);
- },
- },
-
- propertyNames: {
- get: function () {
- return this._propertyNames;
- },
- },
-
- billboard: createPropertyTypeDescriptor("billboard", BillboardGraphics),
-
- box: createPropertyTypeDescriptor("box", BoxGraphics),
-
- corridor: createPropertyTypeDescriptor("corridor", CorridorGraphics),
-
- cylinder: createPropertyTypeDescriptor("cylinder", CylinderGraphics),
-
- description: createPropertyDescriptor("description"),
-
- ellipse: createPropertyTypeDescriptor("ellipse", EllipseGraphics),
-
- ellipsoid: createPropertyTypeDescriptor("ellipsoid", EllipsoidGraphics),
-
- label: createPropertyTypeDescriptor("label", LabelGraphics),
-
- model: createPropertyTypeDescriptor("model", ModelGraphics),
-
- tileset: createPropertyTypeDescriptor("tileset", Cesium3DTilesetGraphics),
-
- orientation: createPropertyDescriptor("orientation"),
-
- path: createPropertyTypeDescriptor("path", PathGraphics),
-
- plane: createPropertyTypeDescriptor("plane", PlaneGraphics),
-
- point: createPropertyTypeDescriptor("point", PointGraphics),
-
- polygon: createPropertyTypeDescriptor("polygon", PolygonGraphics),
-
- polyline: createPropertyTypeDescriptor("polyline", PolylineGraphics),
-
- polylineVolume: createPropertyTypeDescriptor(
- "polylineVolume",
- PolylineVolumeGraphics
- ),
-
- properties: createPropertyTypeDescriptor("properties", PropertyBag),
-
- position: createPositionPropertyDescriptor("position"),
-
- rectangle: createPropertyTypeDescriptor("rectangle", RectangleGraphics),
-
- viewFrom: createPropertyDescriptor("viewFrom"),
-
- wall: createPropertyTypeDescriptor("wall", WallGraphics),
- });
- Entity.prototype.isAvailable = function (time) {
-
- if (!defined(time)) {
- throw new DeveloperError("time is required.");
- }
-
- var availability = this._availability;
- return !defined(availability) || availability.contains(time);
- };
- Entity.prototype.addProperty = function (propertyName) {
- var propertyNames = this._propertyNames;
-
- if (!defined(propertyName)) {
- throw new DeveloperError("propertyName is required.");
- }
- if (propertyNames.indexOf(propertyName) !== -1) {
- throw new DeveloperError(
- propertyName + " is already a registered property."
- );
- }
- if (propertyName in this) {
- throw new DeveloperError(propertyName + " is a reserved property name.");
- }
-
- propertyNames.push(propertyName);
- Object.defineProperty(
- this,
- propertyName,
- createRawPropertyDescriptor(propertyName, true)
- );
- };
- Entity.prototype.removeProperty = function (propertyName) {
- var propertyNames = this._propertyNames;
- var index = propertyNames.indexOf(propertyName);
-
- if (!defined(propertyName)) {
- throw new DeveloperError("propertyName is required.");
- }
- if (index === -1) {
- throw new DeveloperError(propertyName + " is not a registered property.");
- }
-
- this._propertyNames.splice(index, 1);
- delete this[propertyName];
- };
- Entity.prototype.merge = function (source) {
-
- if (!defined(source)) {
- throw new DeveloperError("source is required.");
- }
-
-
-
- this.name = defaultValue(this.name, source.name);
- this.availability = defaultValue(this.availability, source.availability);
- var propertyNames = this._propertyNames;
- var sourcePropertyNames = defined(source._propertyNames)
- ? source._propertyNames
- : Object.keys(source);
- var propertyNamesLength = sourcePropertyNames.length;
- for (var i = 0; i < propertyNamesLength; i++) {
- var name = sourcePropertyNames[i];
-
-
-
- if (name === "parent" || name === "name" || name === "availability") {
- continue;
- }
- var targetProperty = this[name];
- var sourceProperty = source[name];
-
-
- if (!defined(targetProperty) && propertyNames.indexOf(name) === -1) {
- this.addProperty(name);
- }
- if (defined(sourceProperty)) {
- if (defined(targetProperty)) {
- if (defined(targetProperty.merge)) {
- targetProperty.merge(sourceProperty);
- }
- } else if (
- defined(sourceProperty.merge) &&
- defined(sourceProperty.clone)
- ) {
- this[name] = sourceProperty.clone();
- } else {
- this[name] = sourceProperty;
- }
- }
- }
- };
- var matrix3Scratch = new Matrix3();
- var positionScratch = new Cartesian3();
- var orientationScratch = new Quaternion();
- Entity.prototype.computeModelMatrix = function (time, result) {
-
- Check.typeOf.object("time", time);
-
- var position = Property.getValueOrUndefined(
- this._position,
- time,
- positionScratch
- );
- if (!defined(position)) {
- return undefined;
- }
- var orientation = Property.getValueOrUndefined(
- this._orientation,
- time,
- orientationScratch
- );
- if (!defined(orientation)) {
- result = Transforms.eastNorthUpToFixedFrame(position, undefined, result);
- } else {
- result = Matrix4.fromRotationTranslation(
- Matrix3.fromQuaternion(orientation, matrix3Scratch),
- position,
- result
- );
- }
- return result;
- };
- Entity.prototype.computeModelMatrixForHeightReference = function (
- time,
- heightReferenceProperty,
- heightOffset,
- ellipsoid,
- result
- ) {
-
- Check.typeOf.object("time", time);
-
- var heightReference = Property.getValueOrDefault(
- heightReferenceProperty,
- time,
- HeightReference.NONE
- );
- var position = Property.getValueOrUndefined(
- this._position,
- time,
- positionScratch
- );
- if (
- heightReference === HeightReference.NONE ||
- !defined(position) ||
- Cartesian3.equalsEpsilon(position, Cartesian3.ZERO, CesiumMath.EPSILON8)
- ) {
- return this.computeModelMatrix(time, result);
- }
- var carto = ellipsoid.cartesianToCartographic(position, cartoScratch);
- if (heightReference === HeightReference.CLAMP_TO_GROUND) {
- carto.height = heightOffset;
- } else {
- carto.height += heightOffset;
- }
- position = ellipsoid.cartographicToCartesian(carto, position);
- var orientation = Property.getValueOrUndefined(
- this._orientation,
- time,
- orientationScratch
- );
- if (!defined(orientation)) {
- result = Transforms.eastNorthUpToFixedFrame(position, undefined, result);
- } else {
- result = Matrix4.fromRotationTranslation(
- Matrix3.fromQuaternion(orientation, matrix3Scratch),
- position,
- result
- );
- }
- return result;
- };
- Entity.supportsMaterialsforEntitiesOnTerrain = function (scene) {
- return GroundPrimitive.supportsMaterials(scene);
- };
- Entity.supportsPolylinesOnTerrain = function (scene) {
- return GroundPolylinePrimitive.isSupported(scene);
- };
- export default Entity;
|