bruce-cesium 2.9.6 → 2.9.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5008,6 +5008,267 @@
5008
5008
  EntityLabel.GetLabel = GetLabel;
5009
5009
  })(exports.EntityLabel || (exports.EntityLabel = {}));
5010
5010
 
5011
+ // Area in m^2 for 3D model visibility check.
5012
+ var MODEL_SIZE_TOLERANCE = 1000;
5013
+ function getValue$2(viewer, obj) {
5014
+ if (obj === null || obj === void 0 ? void 0 : obj.getValue) {
5015
+ return obj.getValue(viewer.scene.lastRenderTime);
5016
+ }
5017
+ return obj;
5018
+ }
5019
+ var boundingSphereCache = {};
5020
+ function getPositionsFromEntity(viewer, entity) {
5021
+ if (entity.billboard) {
5022
+ var pos3d = getValue$2(viewer, entity.position);
5023
+ if (!(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
5024
+ return null;
5025
+ }
5026
+ return [pos3d];
5027
+ }
5028
+ else if (entity.polyline) {
5029
+ return getValue$2(viewer, entity.polyline.positions);
5030
+ }
5031
+ else if (entity.polygon) {
5032
+ var hierarchy = getValue$2(viewer, entity.polygon.hierarchy);
5033
+ return hierarchy.positions;
5034
+ }
5035
+ else if (entity.corridor) {
5036
+ return getValue$2(viewer, entity.corridor.positions);
5037
+ }
5038
+ else if (entity.ellipse) {
5039
+ var position = getValue$2(viewer, entity.position);
5040
+ if (!(position === null || position === void 0 ? void 0 : position.x)) {
5041
+ return null;
5042
+ }
5043
+ var semiMajorAxis = getValue$2(viewer, entity.ellipse.semiMajorAxis);
5044
+ var semiMinorAxis = getValue$2(viewer, entity.ellipse.semiMinorAxis);
5045
+ var rotation = getValue$2(viewer, entity.ellipse.rotation);
5046
+ // More subdivisions means a more accurate ellipse but worse performance.
5047
+ var numberOfSubdivisions = 5;
5048
+ var positions = [];
5049
+ for (var i = 0; i < numberOfSubdivisions; i++) {
5050
+ var theta = i * (2 * Math.PI / numberOfSubdivisions);
5051
+ var x = semiMajorAxis * Math.cos(theta);
5052
+ var y = semiMinorAxis * Math.sin(theta);
5053
+ // Apply rotation.
5054
+ var rotatedX = x * Math.cos(rotation) - y * Math.sin(rotation);
5055
+ var rotatedY = x * Math.sin(rotation) + y * Math.cos(rotation);
5056
+ // Translate by the ellipse's position.
5057
+ var point = new Cesium.Cartesian3(position.x + rotatedX, position.y + rotatedY, position.z);
5058
+ positions.push(point);
5059
+ }
5060
+ return positions;
5061
+ }
5062
+ else if (entity.model) {
5063
+ var pos3d = getValue$2(viewer, entity.position);
5064
+ if (!(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
5065
+ return null;
5066
+ }
5067
+ return [pos3d];
5068
+ }
5069
+ else if (entity.point) {
5070
+ var pos3d = getValue$2(viewer, entity.position);
5071
+ if (!(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
5072
+ return null;
5073
+ }
5074
+ return [pos3d];
5075
+ }
5076
+ return null;
5077
+ }
5078
+ function computeBoundingSphereFromPositions(positions) {
5079
+ return Cesium.BoundingSphere.fromPoints(positions);
5080
+ }
5081
+ function shouldCullEntity(viewer, cEntity) {
5082
+ var camera = viewer.scene.camera;
5083
+ var boundingSphere;
5084
+ if (boundingSphereCache[cEntity.id]) {
5085
+ boundingSphere = boundingSphereCache[cEntity.id];
5086
+ }
5087
+ else {
5088
+ var positions = getPositionsFromEntity(viewer, cEntity);
5089
+ if (positions) {
5090
+ boundingSphere = computeBoundingSphereFromPositions(positions);
5091
+ if (cEntity.model) {
5092
+ // We don't know how large models are so we'll just add a tolerance.
5093
+ boundingSphere.radius += Math.sqrt(MODEL_SIZE_TOLERANCE);
5094
+ }
5095
+ boundingSphereCache[cEntity.id] = boundingSphere;
5096
+ }
5097
+ }
5098
+ if (!boundingSphere) {
5099
+ return false;
5100
+ }
5101
+ var cullingVolume = camera.frustum.computeCullingVolume(camera.position, camera.direction, camera.up);
5102
+ var visibility = cullingVolume.computeVisibility(boundingSphere);
5103
+ return visibility !== Cesium.Intersect.INSIDE && visibility !== Cesium.Intersect.INTERSECTING;
5104
+ }
5105
+ // Amount of entities to check per interval.
5106
+ var CHECK_BATCH = 500;
5107
+ var checkInterval = null;
5108
+ var checkWaiting = false;
5109
+ // We will store the last recheck data on the camera.
5110
+ // This will allow us to check if the camera has moved enough to warrant a recheck.
5111
+ var LAST_RECHECK_KEY = Symbol("LAST_RECHECK_KEY");
5112
+ /**
5113
+ * We will recheck if the camera has turned or moved enough.
5114
+ * @param viewer
5115
+ * @returns
5116
+ */
5117
+ function shouldRecheck(viewer) {
5118
+ var _a;
5119
+ var camera = viewer.scene.camera;
5120
+ if (!((_a = camera === null || camera === void 0 ? void 0 : camera.position) === null || _a === void 0 ? void 0 : _a.x)) {
5121
+ return false;
5122
+ }
5123
+ var lastRecheck = camera[LAST_RECHECK_KEY];
5124
+ if (!lastRecheck) {
5125
+ camera[LAST_RECHECK_KEY] = {
5126
+ position: camera.position.clone(),
5127
+ direction: camera.direction.clone(),
5128
+ };
5129
+ return true;
5130
+ }
5131
+ var _b = lastRecheck, position = _b.position, direction = _b.direction;
5132
+ var posDiff = Cesium.Cartesian3.magnitude(Cesium.Cartesian3.subtract(position, camera.position, new Cesium.Cartesian3()));
5133
+ var dirDiff = Cesium.Cartesian3.magnitude(Cesium.Cartesian3.subtract(direction, camera.direction, new Cesium.Cartesian3()));
5134
+ var recheck = posDiff > 100 || dirDiff > 0.01;
5135
+ if (recheck) {
5136
+ camera[LAST_RECHECK_KEY] = {
5137
+ position: camera.position.clone(),
5138
+ direction: camera.direction.clone(),
5139
+ };
5140
+ }
5141
+ return recheck;
5142
+ }
5143
+ /**
5144
+ * Runs through all entities in the register and culls them if they are out of the viewport.
5145
+ * This will work in batches.
5146
+ * @param register
5147
+ */
5148
+ function runCullChecker(register) {
5149
+ if (checkInterval != null) {
5150
+ checkWaiting = true;
5151
+ return;
5152
+ }
5153
+ var viewer = register.Viewer;
5154
+ var entityIds = register.GetEntityIds();
5155
+ checkInterval = setInterval(function () {
5156
+ if (viewer.isDestroyed() || !viewer.scene) {
5157
+ clearInterval(checkInterval);
5158
+ checkInterval = null;
5159
+ return;
5160
+ }
5161
+ var slice = entityIds.splice(0, CHECK_BATCH);
5162
+ for (var i = 0; i < slice.length; i++) {
5163
+ var entityId = slice[i];
5164
+ var rego = register.GetRego({
5165
+ entityId: entityId
5166
+ });
5167
+ if (!rego || !rego.visual || !(rego.visual instanceof Cesium.Entity)) {
5168
+ continue;
5169
+ }
5170
+ var parts = exports.EntityUtils.GatherEntity({
5171
+ entity: rego.visual
5172
+ });
5173
+ for (var i_1 = 0; i_1 < parts.length; i_1++) {
5174
+ var part = parts[i_1];
5175
+ var shouldCull = shouldCullEntity(viewer, part);
5176
+ if (shouldCull) {
5177
+ part[VisualRegisterCuller.VISUAL_CULL_KEY] = true;
5178
+ if (viewer.entities.contains(part)) {
5179
+ viewer.entities.remove(part);
5180
+ }
5181
+ }
5182
+ else {
5183
+ delete part[VisualRegisterCuller.VISUAL_CULL_KEY];
5184
+ if (!rego.suppressShow && rego.best && !viewer.entities.contains(part)) {
5185
+ viewer.entities.add(part);
5186
+ }
5187
+ }
5188
+ }
5189
+ }
5190
+ if (entityIds.length <= 0) {
5191
+ clearInterval(checkInterval);
5192
+ checkInterval = null;
5193
+ if (checkWaiting) {
5194
+ checkWaiting = false;
5195
+ runCullChecker(register);
5196
+ }
5197
+ }
5198
+ }, 200);
5199
+ }
5200
+ var VisualRegisterCuller;
5201
+ (function (VisualRegisterCuller) {
5202
+ VisualRegisterCuller.VISUAL_CULL_KEY = Symbol("VISUAL_CULL_KEY");
5203
+ /**
5204
+ * Will monitor the visuals within a visual register and cull ones that are out of the viewport.
5205
+ * @param params
5206
+ * @returns a dispose function.
5207
+ */
5208
+ function Monitor(params) {
5209
+ var _a;
5210
+ var register = params.register;
5211
+ if (!((_a = register === null || register === void 0 ? void 0 : register.Viewer) === null || _a === void 0 ? void 0 : _a.scene) || register.Viewer.isDestroyed()) {
5212
+ console.warn("Cannot monitor a visual register that is not attached to a viewer.");
5213
+ return function () { };
5214
+ }
5215
+ var lastCullCheck = null;
5216
+ var checkQueue = new bruceModels.DelayQueue(function () {
5217
+ if (register.Viewer == null || register.Viewer.isDestroyed() || !shouldRecheck(register.Viewer)) {
5218
+ return;
5219
+ }
5220
+ runCullChecker(register);
5221
+ lastCullCheck = new Date();
5222
+ }, 1000);
5223
+ var moveStartRemoval = register.Viewer.camera.moveStart.addEventListener(function () {
5224
+ var _a;
5225
+ (_a = checkQueue.Call) === null || _a === void 0 ? void 0 : _a.call(checkQueue);
5226
+ });
5227
+ var moveEndRemoval = register.Viewer.camera.moveEnd.addEventListener(function () {
5228
+ var _a;
5229
+ (_a = checkQueue.Call) === null || _a === void 0 ? void 0 : _a.call(checkQueue);
5230
+ });
5231
+ var RENDER_SECS_PASSED = 5;
5232
+ var updateRemoval = register.Viewer.scene.postUpdate.addEventListener(function () {
5233
+ var _a;
5234
+ var now = new Date();
5235
+ if (lastCullCheck == null || (now.getTime() - lastCullCheck.getTime()) > RENDER_SECS_PASSED * 1000) {
5236
+ (_a = checkQueue.Call) === null || _a === void 0 ? void 0 : _a.call(checkQueue);
5237
+ }
5238
+ });
5239
+ checkQueue.Call();
5240
+ return function (params) {
5241
+ var uncull = (params || {}).uncull;
5242
+ moveStartRemoval === null || moveStartRemoval === void 0 ? void 0 : moveStartRemoval();
5243
+ moveStartRemoval = null;
5244
+ moveEndRemoval === null || moveEndRemoval === void 0 ? void 0 : moveEndRemoval();
5245
+ moveEndRemoval = null;
5246
+ updateRemoval === null || updateRemoval === void 0 ? void 0 : updateRemoval();
5247
+ updateRemoval = null;
5248
+ checkInterval === null || checkInterval === void 0 ? void 0 : checkInterval();
5249
+ checkInterval = null;
5250
+ checkWaiting = false;
5251
+ checkQueue === null || checkQueue === void 0 ? void 0 : checkQueue.Dispose();
5252
+ checkQueue = null;
5253
+ };
5254
+ }
5255
+ VisualRegisterCuller.Monitor = Monitor;
5256
+ function IsCulled(viewer, visual) {
5257
+ if (!visual) {
5258
+ return false;
5259
+ }
5260
+ if (visual instanceof Cesium.Entity) {
5261
+ var status_1 = visual[VisualRegisterCuller.VISUAL_CULL_KEY];
5262
+ if (status_1 == null) {
5263
+ status_1 = visual[VisualRegisterCuller.VISUAL_CULL_KEY] = shouldCullEntity(viewer, visual);
5264
+ }
5265
+ return status_1;
5266
+ }
5267
+ return false;
5268
+ }
5269
+ VisualRegisterCuller.IsCulled = IsCulled;
5270
+ })(VisualRegisterCuller || (VisualRegisterCuller = {}));
5271
+
5011
5272
  /**
5012
5273
  * Returns if a given visual is alive and in the scene.
5013
5274
  * @param viewer
@@ -5062,6 +5323,13 @@
5062
5323
  }
5063
5324
  }
5064
5325
  function updateCEntityShow(viewer, visual, show, ignoreParent) {
5326
+ if (show) {
5327
+ // Culling is controlled by "visual-register-culler.ts".
5328
+ // When an object is unculled then the 'updateEntityShow' function is re-called to reveal it and related objects.
5329
+ // A sub-object can be culled while the siblings are not.
5330
+ var isCulled = show ? VisualRegisterCuller.IsCulled(viewer, visual) : true;
5331
+ show = !isCulled;
5332
+ }
5065
5333
  if (visual._parentEntity && !ignoreParent) {
5066
5334
  updateCEntityShow(viewer, visual._parentEntity, show, false);
5067
5335
  }
@@ -5343,6 +5611,9 @@
5343
5611
  this.labelledEntityIds = [];
5344
5612
  this.viewer = params.viewer;
5345
5613
  this.apiGetters = params.apiGetters;
5614
+ this.cameraCullerDispose = VisualRegisterCuller.Monitor({
5615
+ register: this
5616
+ });
5346
5617
  }
5347
5618
  Object.defineProperty(Register.prototype, "Id", {
5348
5619
  get: function () {
@@ -5375,6 +5646,13 @@
5375
5646
  enumerable: false,
5376
5647
  configurable: true
5377
5648
  });
5649
+ Register.prototype.Dispose = function () {
5650
+ var _a;
5651
+ (_a = this.cameraCullerDispose) === null || _a === void 0 ? void 0 : _a.call(this, {
5652
+ uncull: false
5653
+ });
5654
+ this.cameraCullerDispose = null;
5655
+ };
5378
5656
  Register.prototype.ForceUpdate = function (params) {
5379
5657
  var entityIds = params.entityIds;
5380
5658
  for (var i = 0; i < entityIds.length; i++) {
@@ -11440,6 +11718,27 @@
11440
11718
  enumerable: false,
11441
11719
  configurable: true
11442
11720
  });
11721
+ /**
11722
+ * Disposes the menu item manager.
11723
+ * This will dispose all render managers and unregister all menu items.
11724
+ */
11725
+ Manager.prototype.Dispose = function (params) {
11726
+ var _a;
11727
+ var disposeRegister = (params !== null && params !== void 0 ? params : {}).disposeRegister;
11728
+ for (var i = 0; i < this.items.length; i++) {
11729
+ var item = this.items[i];
11730
+ try {
11731
+ (_a = item.renderManager) === null || _a === void 0 ? void 0 : _a.Dispose();
11732
+ }
11733
+ catch (e) {
11734
+ console.error(e);
11735
+ }
11736
+ }
11737
+ this.items = [];
11738
+ if (this.visualsRegister && disposeRegister != false) {
11739
+ this.visualsRegister.Dispose();
11740
+ }
11741
+ };
11443
11742
  /**
11444
11743
  * Renders a given menu item and all its children.
11445
11744
  * Will return the enabled item id.
@@ -17211,7 +17510,7 @@
17211
17510
  ViewerUtils.CreateWidgets = CreateWidgets;
17212
17511
  })(exports.ViewerUtils || (exports.ViewerUtils = {}));
17213
17512
 
17214
- var VERSION$1 = "2.9.6";
17513
+ var VERSION$1 = "2.9.8";
17215
17514
 
17216
17515
  exports.VERSION = VERSION$1;
17217
17516
  exports.CesiumViewMonitor = CesiumViewMonitor;