123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- import ApproximateTerrainHeights from "../Core/ApproximateTerrainHeights.js";
- import BoundingSphere from "../Core/BoundingSphere.js";
- import Check from "../Core/Check.js";
- import defaultValue from "../Core/defaultValue.js";
- import defined from "../Core/defined.js";
- import destroyObject from "../Core/destroyObject.js";
- import EventHelper from "../Core/EventHelper.js";
- import GroundPolylinePrimitive from "../Scene/GroundPolylinePrimitive.js";
- import GroundPrimitive from "../Scene/GroundPrimitive.js";
- import OrderedGroundPrimitiveCollection from "../Scene/OrderedGroundPrimitiveCollection.js";
- import PrimitiveCollection from "../Scene/PrimitiveCollection.js";
- import BillboardVisualizer from "./BillboardVisualizer.js";
- import BoundingSphereState from "./BoundingSphereState.js";
- import CustomDataSource from "./CustomDataSource.js";
- import GeometryVisualizer from "./GeometryVisualizer.js";
- import LabelVisualizer from "./LabelVisualizer.js";
- import ModelVisualizer from "./ModelVisualizer.js";
- import Cesium3DTilesetVisualizer from "./Cesium3DTilesetVisualizer.js";
- import PathVisualizer from "./PathVisualizer.js";
- import PointVisualizer from "./PointVisualizer.js";
- import PolylineVisualizer from "./PolylineVisualizer.js";
- function DataSourceDisplay(options) {
-
- Check.typeOf.object("options", options);
- Check.typeOf.object("options.scene", options.scene);
- Check.typeOf.object(
- "options.dataSourceCollection",
- options.dataSourceCollection
- );
-
- GroundPrimitive.initializeTerrainHeights();
- GroundPolylinePrimitive.initializeTerrainHeights();
- var scene = options.scene;
- var dataSourceCollection = options.dataSourceCollection;
- this._eventHelper = new EventHelper();
- this._eventHelper.add(
- dataSourceCollection.dataSourceAdded,
- this._onDataSourceAdded,
- this
- );
- this._eventHelper.add(
- dataSourceCollection.dataSourceRemoved,
- this._onDataSourceRemoved,
- this
- );
- this._eventHelper.add(
- dataSourceCollection.dataSourceMoved,
- this._onDataSourceMoved,
- this
- );
- this._eventHelper.add(scene.postRender, this._postRender, this);
- this._dataSourceCollection = dataSourceCollection;
- this._scene = scene;
- this._visualizersCallback = defaultValue(
- options.visualizersCallback,
- DataSourceDisplay.defaultVisualizersCallback
- );
- var primitivesAdded = false;
- var primitives = new PrimitiveCollection();
- var groundPrimitives = new PrimitiveCollection();
- if (dataSourceCollection.length > 0) {
- scene.primitives.add(primitives);
- scene.groundPrimitives.add(groundPrimitives);
- primitivesAdded = true;
- }
- this._primitives = primitives;
- this._groundPrimitives = groundPrimitives;
- for (var i = 0, len = dataSourceCollection.length; i < len; i++) {
- this._onDataSourceAdded(dataSourceCollection, dataSourceCollection.get(i));
- }
- var defaultDataSource = new CustomDataSource();
- this._onDataSourceAdded(undefined, defaultDataSource);
- this._defaultDataSource = defaultDataSource;
- var removeDefaultDataSourceListener;
- var removeDataSourceCollectionListener;
- if (!primitivesAdded) {
- var that = this;
- var addPrimitives = function () {
- scene.primitives.add(primitives);
- scene.groundPrimitives.add(groundPrimitives);
- removeDefaultDataSourceListener();
- removeDataSourceCollectionListener();
- that._removeDefaultDataSourceListener = undefined;
- that._removeDataSourceCollectionListener = undefined;
- };
- removeDefaultDataSourceListener = defaultDataSource.entities.collectionChanged.addEventListener(
- addPrimitives
- );
- removeDataSourceCollectionListener = dataSourceCollection.dataSourceAdded.addEventListener(
- addPrimitives
- );
- }
- this._removeDefaultDataSourceListener = removeDefaultDataSourceListener;
- this._removeDataSourceCollectionListener = removeDataSourceCollectionListener;
- this._ready = false;
- }
- DataSourceDisplay.defaultVisualizersCallback = function (
- scene,
- entityCluster,
- dataSource
- ) {
- var entities = dataSource.entities;
- return [
- new BillboardVisualizer(entityCluster, entities),
- new GeometryVisualizer(
- scene,
- entities,
- dataSource._primitives,
- dataSource._groundPrimitives
- ),
- new LabelVisualizer(entityCluster, entities),
- new ModelVisualizer(scene, entities),
- new Cesium3DTilesetVisualizer(scene, entities),
- new PointVisualizer(entityCluster, entities),
- new PathVisualizer(scene, entities),
- new PolylineVisualizer(
- scene,
- entities,
- dataSource._primitives,
- dataSource._groundPrimitives
- ),
- ];
- };
- Object.defineProperties(DataSourceDisplay.prototype, {
-
- scene: {
- get: function () {
- return this._scene;
- },
- },
-
- dataSources: {
- get: function () {
- return this._dataSourceCollection;
- },
- },
-
- defaultDataSource: {
- get: function () {
- return this._defaultDataSource;
- },
- },
-
- ready: {
- get: function () {
- return this._ready;
- },
- },
- });
- DataSourceDisplay.prototype.isDestroyed = function () {
- return false;
- };
- DataSourceDisplay.prototype.destroy = function () {
- this._eventHelper.removeAll();
- var dataSourceCollection = this._dataSourceCollection;
- for (var i = 0, length = dataSourceCollection.length; i < length; ++i) {
- this._onDataSourceRemoved(
- this._dataSourceCollection,
- dataSourceCollection.get(i)
- );
- }
- this._onDataSourceRemoved(undefined, this._defaultDataSource);
- if (defined(this._removeDefaultDataSourceListener)) {
- this._removeDefaultDataSourceListener();
- this._removeDataSourceCollectionListener();
- } else {
- this._scene.primitives.remove(this._primitives);
- this._scene.groundPrimitives.remove(this._groundPrimitives);
- }
- return destroyObject(this);
- };
- DataSourceDisplay.prototype.update = function (time) {
-
- Check.defined("time", time);
-
- if (!ApproximateTerrainHeights.initialized) {
- this._ready = false;
- return false;
- }
- var result = true;
- var i;
- var x;
- var visualizers;
- var vLength;
- var dataSources = this._dataSourceCollection;
- var length = dataSources.length;
- for (i = 0; i < length; i++) {
- var dataSource = dataSources.get(i);
- if (defined(dataSource.update)) {
- result = dataSource.update(time) && result;
- }
- visualizers = dataSource._visualizers;
- vLength = visualizers.length;
- for (x = 0; x < vLength; x++) {
- result = visualizers[x].update(time) && result;
- }
- }
- visualizers = this._defaultDataSource._visualizers;
- vLength = visualizers.length;
- for (x = 0; x < vLength; x++) {
- result = visualizers[x].update(time) && result;
- }
- this._ready = result;
- return result;
- };
- DataSourceDisplay.prototype._postRender = function () {
-
- var frameState = this._scene.frameState;
- var dataSources = this._dataSourceCollection;
- var length = dataSources.length;
- for (var i = 0; i < length; i++) {
- var dataSource = dataSources.get(i);
- var credit = dataSource.credit;
- if (defined(credit)) {
- frameState.creditDisplay.addCredit(credit);
- }
-
- var credits = dataSource._resourceCredits;
- if (defined(credits)) {
- var creditCount = credits.length;
- for (var c = 0; c < creditCount; c++) {
- frameState.creditDisplay.addCredit(credits[c]);
- }
- }
- }
- };
- var getBoundingSphereArrayScratch = [];
- var getBoundingSphereBoundingSphereScratch = new BoundingSphere();
- DataSourceDisplay.prototype.getBoundingSphere = function (
- entity,
- allowPartial,
- result
- ) {
-
- Check.defined("entity", entity);
- Check.typeOf.bool("allowPartial", allowPartial);
- Check.defined("result", result);
-
- if (!this._ready) {
- return BoundingSphereState.PENDING;
- }
- var i;
- var length;
- var dataSource = this._defaultDataSource;
- if (!dataSource.entities.contains(entity)) {
- dataSource = undefined;
- var dataSources = this._dataSourceCollection;
- length = dataSources.length;
- for (i = 0; i < length; i++) {
- var d = dataSources.get(i);
- if (d.entities.contains(entity)) {
- dataSource = d;
- break;
- }
- }
- }
- if (!defined(dataSource)) {
- return BoundingSphereState.FAILED;
- }
- var boundingSpheres = getBoundingSphereArrayScratch;
- var tmp = getBoundingSphereBoundingSphereScratch;
- var count = 0;
- var state = BoundingSphereState.DONE;
- var visualizers = dataSource._visualizers;
- var visualizersLength = visualizers.length;
- for (i = 0; i < visualizersLength; i++) {
- var visualizer = visualizers[i];
- if (defined(visualizer.getBoundingSphere)) {
- state = visualizers[i].getBoundingSphere(entity, tmp);
- if (!allowPartial && state === BoundingSphereState.PENDING) {
- return BoundingSphereState.PENDING;
- } else if (state === BoundingSphereState.DONE) {
- boundingSpheres[count] = BoundingSphere.clone(
- tmp,
- boundingSpheres[count]
- );
- count++;
- }
- }
- }
- if (count === 0) {
- return BoundingSphereState.FAILED;
- }
- boundingSpheres.length = count;
- BoundingSphere.fromBoundingSpheres(boundingSpheres, result);
- return BoundingSphereState.DONE;
- };
- DataSourceDisplay.prototype._onDataSourceAdded = function (
- dataSourceCollection,
- dataSource
- ) {
- var scene = this._scene;
- var displayPrimitives = this._primitives;
- var displayGroundPrimitives = this._groundPrimitives;
- var primitives = displayPrimitives.add(new PrimitiveCollection());
- var groundPrimitives = displayGroundPrimitives.add(
- new OrderedGroundPrimitiveCollection()
- );
- dataSource._primitives = primitives;
- dataSource._groundPrimitives = groundPrimitives;
- var entityCluster = dataSource.clustering;
- entityCluster._initialize(scene);
- primitives.add(entityCluster);
- dataSource._visualizers = this._visualizersCallback(
- scene,
- entityCluster,
- dataSource
- );
- };
- DataSourceDisplay.prototype._onDataSourceRemoved = function (
- dataSourceCollection,
- dataSource
- ) {
- var displayPrimitives = this._primitives;
- var displayGroundPrimitives = this._groundPrimitives;
- var primitives = dataSource._primitives;
- var groundPrimitives = dataSource._groundPrimitives;
- var entityCluster = dataSource.clustering;
- primitives.remove(entityCluster);
- var visualizers = dataSource._visualizers;
- var length = visualizers.length;
- for (var i = 0; i < length; i++) {
- visualizers[i].destroy();
- }
- displayPrimitives.remove(primitives);
- displayGroundPrimitives.remove(groundPrimitives);
- dataSource._visualizers = undefined;
- };
- DataSourceDisplay.prototype._onDataSourceMoved = function (
- dataSource,
- newIndex,
- oldIndex
- ) {
- var displayPrimitives = this._primitives;
- var displayGroundPrimitives = this._groundPrimitives;
- var primitives = dataSource._primitives;
- var groundPrimitives = dataSource._groundPrimitives;
- if (newIndex === oldIndex + 1) {
- displayPrimitives.raise(primitives);
- displayGroundPrimitives.raise(groundPrimitives);
- } else if (newIndex === oldIndex - 1) {
- displayPrimitives.lower(primitives);
- displayGroundPrimitives.lower(groundPrimitives);
- } else if (newIndex === 0) {
- displayPrimitives.lowerToBottom(primitives);
- displayGroundPrimitives.lowerToBottom(groundPrimitives);
- displayPrimitives.raise(primitives);
- displayGroundPrimitives.raise(groundPrimitives);
- } else {
- displayPrimitives.raiseToTop(primitives);
- displayGroundPrimitives.raiseToTop(groundPrimitives);
- }
- };
- export default DataSourceDisplay;
|