bruce-cesium 2.9.6 → 2.9.7

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,260 @@
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 = 100;
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
+ // Expand the bounding sphere for models due to our 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
+ var CHECK_BATCH = 500;
5106
+ var LAST_RECHECK_KEY = Symbol("LAST_RECHECK_KEY");
5107
+ var checkInterval = null;
5108
+ var checkWaiting = false;
5109
+ /**
5110
+ * We will recheck if the camera has turned or moved enough.
5111
+ * @param viewer
5112
+ * @returns
5113
+ */
5114
+ function shouldRecheck(viewer) {
5115
+ var camera = viewer.scene.camera;
5116
+ var lastRecheck = camera[LAST_RECHECK_KEY];
5117
+ if (!lastRecheck) {
5118
+ camera[LAST_RECHECK_KEY] = {
5119
+ position: camera.position.clone(),
5120
+ direction: camera.direction.clone(),
5121
+ };
5122
+ return true;
5123
+ }
5124
+ var _a = lastRecheck, position = _a.position, direction = _a.direction;
5125
+ var posDiff = Cesium.Cartesian3.magnitude(Cesium.Cartesian3.subtract(position, camera.position, new Cesium.Cartesian3()));
5126
+ var dirDiff = Cesium.Cartesian3.magnitude(Cesium.Cartesian3.subtract(direction, camera.direction, new Cesium.Cartesian3()));
5127
+ var recheck = posDiff > 100 || dirDiff > 0.01;
5128
+ if (recheck) {
5129
+ camera[LAST_RECHECK_KEY] = {
5130
+ position: camera.position.clone(),
5131
+ direction: camera.direction.clone(),
5132
+ };
5133
+ }
5134
+ return recheck;
5135
+ }
5136
+ /**
5137
+ * Runs through all entities in the register and culls them if they are out of the viewport.
5138
+ * This will work in batches.
5139
+ * @param register
5140
+ */
5141
+ function runCullChecker(register) {
5142
+ if (checkInterval != null) {
5143
+ checkWaiting = true;
5144
+ return;
5145
+ }
5146
+ var viewer = register.Viewer;
5147
+ var entityIds = register.GetEntityIds();
5148
+ checkInterval = setInterval(function () {
5149
+ if (viewer.isDestroyed() || !viewer.scene) {
5150
+ clearInterval(checkInterval);
5151
+ checkInterval = null;
5152
+ return;
5153
+ }
5154
+ var slice = entityIds.splice(0, CHECK_BATCH);
5155
+ for (var i = 0; i < slice.length; i++) {
5156
+ var entityId = slice[i];
5157
+ var rego = register.GetRego({
5158
+ entityId: entityId
5159
+ });
5160
+ if (!rego || !rego.visual || !(rego.visual instanceof Cesium.Entity)) {
5161
+ continue;
5162
+ }
5163
+ var parts = exports.EntityUtils.GatherEntity({
5164
+ entity: rego.visual
5165
+ });
5166
+ for (var i_1 = 0; i_1 < parts.length; i_1++) {
5167
+ var part = parts[i_1];
5168
+ var shouldCull = shouldCullEntity(viewer, part);
5169
+ if (shouldCull) {
5170
+ part[VisualRegisterCuller.VISUAL_CULL_KEY] = true;
5171
+ if (viewer.entities.contains(part)) {
5172
+ viewer.entities.remove(part);
5173
+ }
5174
+ }
5175
+ else {
5176
+ delete part[VisualRegisterCuller.VISUAL_CULL_KEY];
5177
+ if (!rego.suppressShow && rego.best && !viewer.entities.contains(part)) {
5178
+ viewer.entities.add(part);
5179
+ }
5180
+ }
5181
+ }
5182
+ }
5183
+ if (entityIds.length <= 0) {
5184
+ clearInterval(checkInterval);
5185
+ checkInterval = null;
5186
+ if (checkWaiting) {
5187
+ checkWaiting = false;
5188
+ runCullChecker(register);
5189
+ }
5190
+ }
5191
+ }, 200);
5192
+ }
5193
+ var VisualRegisterCuller;
5194
+ (function (VisualRegisterCuller) {
5195
+ VisualRegisterCuller.VISUAL_CULL_KEY = Symbol("VISUAL_CULL_KEY");
5196
+ /**
5197
+ * Will monitor the visuals within a visual register and cull ones that are out of the viewport.
5198
+ * @param params
5199
+ * @returns a dispose function.
5200
+ */
5201
+ function Monitor(params) {
5202
+ var _a;
5203
+ var register = params.register;
5204
+ if (!((_a = register === null || register === void 0 ? void 0 : register.Viewer) === null || _a === void 0 ? void 0 : _a.scene) || register.Viewer.isDestroyed()) {
5205
+ console.warn("Cannot monitor a visual register that is not attached to a viewer.");
5206
+ return function () { };
5207
+ }
5208
+ var lastCullCheck = null;
5209
+ var checkQueue = new bruceModels.DelayQueue(function () {
5210
+ if (register.Viewer == null || register.Viewer.isDestroyed() || !shouldRecheck(register.Viewer)) {
5211
+ return;
5212
+ }
5213
+ runCullChecker(register);
5214
+ lastCullCheck = new Date();
5215
+ }, 1000);
5216
+ var moveStartRemoval = register.Viewer.camera.moveStart.addEventListener(function () {
5217
+ var _a;
5218
+ (_a = checkQueue.Call) === null || _a === void 0 ? void 0 : _a.call(checkQueue);
5219
+ });
5220
+ var moveEndRemoval = register.Viewer.camera.moveEnd.addEventListener(function () {
5221
+ var _a;
5222
+ (_a = checkQueue.Call) === null || _a === void 0 ? void 0 : _a.call(checkQueue);
5223
+ });
5224
+ var RENDER_SECS_PASSED = 5;
5225
+ var updateRemoval = register.Viewer.scene.postRender.addEventListener(function () {
5226
+ var _a;
5227
+ var now = new Date();
5228
+ if (lastCullCheck == null || (now.getTime() - lastCullCheck.getTime()) > RENDER_SECS_PASSED * 1000) {
5229
+ (_a = checkQueue.Call) === null || _a === void 0 ? void 0 : _a.call(checkQueue);
5230
+ }
5231
+ });
5232
+ checkQueue.Call();
5233
+ return function (params) {
5234
+ var uncull = (params || {}).uncull;
5235
+ moveStartRemoval === null || moveStartRemoval === void 0 ? void 0 : moveStartRemoval();
5236
+ moveStartRemoval = null;
5237
+ moveEndRemoval === null || moveEndRemoval === void 0 ? void 0 : moveEndRemoval();
5238
+ moveEndRemoval = null;
5239
+ updateRemoval === null || updateRemoval === void 0 ? void 0 : updateRemoval();
5240
+ updateRemoval = null;
5241
+ checkInterval === null || checkInterval === void 0 ? void 0 : checkInterval();
5242
+ checkInterval = null;
5243
+ checkWaiting = false;
5244
+ checkQueue === null || checkQueue === void 0 ? void 0 : checkQueue.Dispose();
5245
+ checkQueue = null;
5246
+ };
5247
+ }
5248
+ VisualRegisterCuller.Monitor = Monitor;
5249
+ function IsCulled(viewer, visual) {
5250
+ if (!visual) {
5251
+ return false;
5252
+ }
5253
+ if (visual instanceof Cesium.Entity) {
5254
+ var status_1 = visual[VisualRegisterCuller.VISUAL_CULL_KEY];
5255
+ if (status_1 == null) {
5256
+ status_1 = visual[VisualRegisterCuller.VISUAL_CULL_KEY] = shouldCullEntity(viewer, visual);
5257
+ }
5258
+ return status_1;
5259
+ }
5260
+ return false;
5261
+ }
5262
+ VisualRegisterCuller.IsCulled = IsCulled;
5263
+ })(VisualRegisterCuller || (VisualRegisterCuller = {}));
5264
+
5011
5265
  /**
5012
5266
  * Returns if a given visual is alive and in the scene.
5013
5267
  * @param viewer
@@ -5062,6 +5316,13 @@
5062
5316
  }
5063
5317
  }
5064
5318
  function updateCEntityShow(viewer, visual, show, ignoreParent) {
5319
+ if (show) {
5320
+ // Culling is controlled by "visual-register-culler.ts".
5321
+ // When an object is unculled then the 'updateEntityShow' function is re-called to reveal it and related objects.
5322
+ // A sub-object can be culled while the siblings are not.
5323
+ var isCulled = show ? VisualRegisterCuller.IsCulled(viewer, visual) : true;
5324
+ show = !isCulled;
5325
+ }
5065
5326
  if (visual._parentEntity && !ignoreParent) {
5066
5327
  updateCEntityShow(viewer, visual._parentEntity, show, false);
5067
5328
  }
@@ -5343,6 +5604,9 @@
5343
5604
  this.labelledEntityIds = [];
5344
5605
  this.viewer = params.viewer;
5345
5606
  this.apiGetters = params.apiGetters;
5607
+ this.cameraCullerDispose = VisualRegisterCuller.Monitor({
5608
+ register: this
5609
+ });
5346
5610
  }
5347
5611
  Object.defineProperty(Register.prototype, "Id", {
5348
5612
  get: function () {
@@ -5375,6 +5639,13 @@
5375
5639
  enumerable: false,
5376
5640
  configurable: true
5377
5641
  });
5642
+ Register.prototype.Dispose = function () {
5643
+ var _a;
5644
+ (_a = this.cameraCullerDispose) === null || _a === void 0 ? void 0 : _a.call(this, {
5645
+ uncull: false
5646
+ });
5647
+ this.cameraCullerDispose = null;
5648
+ };
5378
5649
  Register.prototype.ForceUpdate = function (params) {
5379
5650
  var entityIds = params.entityIds;
5380
5651
  for (var i = 0; i < entityIds.length; i++) {
@@ -11440,6 +11711,27 @@
11440
11711
  enumerable: false,
11441
11712
  configurable: true
11442
11713
  });
11714
+ /**
11715
+ * Disposes the menu item manager.
11716
+ * This will dispose all render managers and unregister all menu items.
11717
+ */
11718
+ Manager.prototype.Dispose = function (params) {
11719
+ var _a;
11720
+ var disposeRegister = (params !== null && params !== void 0 ? params : {}).disposeRegister;
11721
+ for (var i = 0; i < this.items.length; i++) {
11722
+ var item = this.items[i];
11723
+ try {
11724
+ (_a = item.renderManager) === null || _a === void 0 ? void 0 : _a.Dispose();
11725
+ }
11726
+ catch (e) {
11727
+ console.error(e);
11728
+ }
11729
+ }
11730
+ this.items = [];
11731
+ if (this.visualsRegister && disposeRegister != false) {
11732
+ this.visualsRegister.Dispose();
11733
+ }
11734
+ };
11443
11735
  /**
11444
11736
  * Renders a given menu item and all its children.
11445
11737
  * Will return the enabled item id.
@@ -17211,7 +17503,7 @@
17211
17503
  ViewerUtils.CreateWidgets = CreateWidgets;
17212
17504
  })(exports.ViewerUtils || (exports.ViewerUtils = {}));
17213
17505
 
17214
- var VERSION$1 = "2.9.6";
17506
+ var VERSION$1 = "2.9.7";
17215
17507
 
17216
17508
  exports.VERSION = VERSION$1;
17217
17509
  exports.CesiumViewMonitor = CesiumViewMonitor;