123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- import AssociativeArray from "../Core/AssociativeArray.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 ClassificationType from "../Scene/ClassificationType.js";
- import PolylineColorAppearance from "../Scene/PolylineColorAppearance.js";
- import PolylineMaterialAppearance from "../Scene/PolylineMaterialAppearance.js";
- import ShadowMode from "../Scene/ShadowMode.js";
- import BoundingSphereState from "./BoundingSphereState.js";
- import ColorMaterialProperty from "./ColorMaterialProperty.js";
- import DynamicGeometryBatch from "./DynamicGeometryBatch.js";
- import PolylineGeometryUpdater from "./PolylineGeometryUpdater.js";
- import StaticGeometryColorBatch from "./StaticGeometryColorBatch.js";
- import StaticGeometryPerMaterialBatch from "./StaticGeometryPerMaterialBatch.js";
- import StaticGroundPolylinePerMaterialBatch from "./StaticGroundPolylinePerMaterialBatch.js";
- var emptyArray = [];
- function removeUpdater(that, updater) {
-
- var batches = that._batches;
- var length = batches.length;
- for (var i = 0; i < length; i++) {
- batches[i].remove(updater);
- }
- }
- function insertUpdaterIntoBatch(that, time, updater) {
- if (updater.isDynamic) {
- that._dynamicBatch.add(time, updater);
- return;
- }
- if (updater.clampToGround && updater.fillEnabled) {
-
- var classificationType = updater.classificationTypeProperty.getValue(time);
- that._groundBatches[classificationType].add(time, updater);
- return;
- }
- var shadows;
- if (updater.fillEnabled) {
- shadows = updater.shadowsProperty.getValue(time);
- }
- var multiplier = 0;
- if (defined(updater.depthFailMaterialProperty)) {
- multiplier =
- updater.depthFailMaterialProperty instanceof ColorMaterialProperty
- ? 1
- : 2;
- }
- var index;
- if (defined(shadows)) {
- index = shadows + multiplier * ShadowMode.NUMBER_OF_SHADOW_MODES;
- }
- if (updater.fillEnabled) {
- if (updater.fillMaterialProperty instanceof ColorMaterialProperty) {
- that._colorBatches[index].add(time, updater);
- } else {
- that._materialBatches[index].add(time, updater);
- }
- }
- }
- function PolylineVisualizer(
- scene,
- entityCollection,
- primitives,
- groundPrimitives
- ) {
-
- Check.defined("scene", scene);
- Check.defined("entityCollection", entityCollection);
-
- groundPrimitives = defaultValue(groundPrimitives, scene.groundPrimitives);
- primitives = defaultValue(primitives, scene.primitives);
- this._scene = scene;
- this._primitives = primitives;
- this._entityCollection = undefined;
- this._addedObjects = new AssociativeArray();
- this._removedObjects = new AssociativeArray();
- this._changedObjects = new AssociativeArray();
- var i;
- var numberOfShadowModes = ShadowMode.NUMBER_OF_SHADOW_MODES;
- this._colorBatches = new Array(numberOfShadowModes * 3);
- this._materialBatches = new Array(numberOfShadowModes * 3);
- for (i = 0; i < numberOfShadowModes; ++i) {
- this._colorBatches[i] = new StaticGeometryColorBatch(
- primitives,
- PolylineColorAppearance,
- undefined,
- false,
- i
- );
- this._materialBatches[i] = new StaticGeometryPerMaterialBatch(
- primitives,
- PolylineMaterialAppearance,
- undefined,
- false,
- i
- );
- this._colorBatches[i + numberOfShadowModes] = new StaticGeometryColorBatch(
- primitives,
- PolylineColorAppearance,
- PolylineColorAppearance,
- false,
- i
- );
- this._materialBatches[
- i + numberOfShadowModes
- ] = new StaticGeometryPerMaterialBatch(
- primitives,
- PolylineMaterialAppearance,
- PolylineColorAppearance,
- false,
- i
- );
- this._colorBatches[
- i + numberOfShadowModes * 2
- ] = new StaticGeometryColorBatch(
- primitives,
- PolylineColorAppearance,
- PolylineMaterialAppearance,
- false,
- i
- );
- this._materialBatches[
- i + numberOfShadowModes * 2
- ] = new StaticGeometryPerMaterialBatch(
- primitives,
- PolylineMaterialAppearance,
- PolylineMaterialAppearance,
- false,
- i
- );
- }
- this._dynamicBatch = new DynamicGeometryBatch(primitives, groundPrimitives);
- var numberOfClassificationTypes =
- ClassificationType.NUMBER_OF_CLASSIFICATION_TYPES;
- this._groundBatches = new Array(numberOfClassificationTypes);
- for (i = 0; i < numberOfClassificationTypes; ++i) {
- this._groundBatches[i] = new StaticGroundPolylinePerMaterialBatch(
- groundPrimitives,
- i
- );
- }
- this._batches = this._colorBatches.concat(
- this._materialBatches,
- this._dynamicBatch,
- this._groundBatches
- );
- this._subscriptions = new AssociativeArray();
- this._updaters = new AssociativeArray();
- this._entityCollection = entityCollection;
- entityCollection.collectionChanged.addEventListener(
- PolylineVisualizer.prototype._onCollectionChanged,
- this
- );
- this._onCollectionChanged(
- entityCollection,
- entityCollection.values,
- emptyArray
- );
- }
- PolylineVisualizer.prototype.update = function (time) {
-
- Check.defined("time", time);
-
- var addedObjects = this._addedObjects;
- var added = addedObjects.values;
- var removedObjects = this._removedObjects;
- var removed = removedObjects.values;
- var changedObjects = this._changedObjects;
- var changed = changedObjects.values;
- var i;
- var entity;
- var id;
- var updater;
- for (i = changed.length - 1; i > -1; i--) {
- entity = changed[i];
- id = entity.id;
- updater = this._updaters.get(id);
-
-
-
-
-
- if (updater.entity === entity) {
- removeUpdater(this, updater);
- insertUpdaterIntoBatch(this, time, updater);
- } else {
- removed.push(entity);
- added.push(entity);
- }
- }
- for (i = removed.length - 1; i > -1; i--) {
- entity = removed[i];
- id = entity.id;
- updater = this._updaters.get(id);
- removeUpdater(this, updater);
- updater.destroy();
- this._updaters.remove(id);
- this._subscriptions.get(id)();
- this._subscriptions.remove(id);
- }
- for (i = added.length - 1; i > -1; i--) {
- entity = added[i];
- id = entity.id;
- updater = new PolylineGeometryUpdater(entity, this._scene);
- this._updaters.set(id, updater);
- insertUpdaterIntoBatch(this, time, updater);
- this._subscriptions.set(
- id,
- updater.geometryChanged.addEventListener(
- PolylineVisualizer._onGeometryChanged,
- this
- )
- );
- }
- addedObjects.removeAll();
- removedObjects.removeAll();
- changedObjects.removeAll();
- var isUpdated = true;
- var batches = this._batches;
- var length = batches.length;
- for (i = 0; i < length; i++) {
- isUpdated = batches[i].update(time) && isUpdated;
- }
- return isUpdated;
- };
- var getBoundingSphereArrayScratch = [];
- var getBoundingSphereBoundingSphereScratch = new BoundingSphere();
- PolylineVisualizer.prototype.getBoundingSphere = function (entity, result) {
-
- Check.defined("entity", entity);
- Check.defined("result", result);
-
- var boundingSpheres = getBoundingSphereArrayScratch;
- var tmp = getBoundingSphereBoundingSphereScratch;
- var count = 0;
- var state = BoundingSphereState.DONE;
- var batches = this._batches;
- var batchesLength = batches.length;
- var updater = this._updaters.get(entity.id);
- for (var i = 0; i < batchesLength; i++) {
- state = batches[i].getBoundingSphere(updater, tmp);
- if (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;
- };
- PolylineVisualizer.prototype.isDestroyed = function () {
- return false;
- };
- PolylineVisualizer.prototype.destroy = function () {
- this._entityCollection.collectionChanged.removeEventListener(
- PolylineVisualizer.prototype._onCollectionChanged,
- this
- );
- this._addedObjects.removeAll();
- this._removedObjects.removeAll();
- var i;
- var batches = this._batches;
- var length = batches.length;
- for (i = 0; i < length; i++) {
- batches[i].removeAllPrimitives();
- }
- var subscriptions = this._subscriptions.values;
- length = subscriptions.length;
- for (i = 0; i < length; i++) {
- subscriptions[i]();
- }
- this._subscriptions.removeAll();
- return destroyObject(this);
- };
- PolylineVisualizer._onGeometryChanged = function (updater) {
- var removedObjects = this._removedObjects;
- var changedObjects = this._changedObjects;
- var entity = updater.entity;
- var id = entity.id;
- if (!defined(removedObjects.get(id)) && !defined(changedObjects.get(id))) {
- changedObjects.set(id, entity);
- }
- };
- PolylineVisualizer.prototype._onCollectionChanged = function (
- entityCollection,
- added,
- removed
- ) {
- var addedObjects = this._addedObjects;
- var removedObjects = this._removedObjects;
- var changedObjects = this._changedObjects;
- var i;
- var id;
- var entity;
- for (i = removed.length - 1; i > -1; i--) {
- entity = removed[i];
- id = entity.id;
- if (!addedObjects.remove(id)) {
- removedObjects.set(id, entity);
- changedObjects.remove(id);
- }
- }
- for (i = added.length - 1; i > -1; i--) {
- entity = added[i];
- id = entity.id;
- if (removedObjects.remove(id)) {
- changedObjects.set(id, entity);
- } else {
- addedObjects.set(id, entity);
- }
- }
- };
- export default PolylineVisualizer;
|