bruce-cesium 7.9.5 → 7.9.6

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.
@@ -1551,6 +1551,13 @@ void main()
1551
1551
  }
1552
1552
  }
1553
1553
  CameraControls.SetGroundTileset = SetGroundTileset;
1554
+ /**
1555
+ * Whether a tileset was marked as standing in for the globe.
1556
+ */
1557
+ function IsGroundTileset(tileset) {
1558
+ return Boolean(tileset === null || tileset === void 0 ? void 0 : tileset[GROUND_TILESET_KEY]);
1559
+ }
1560
+ CameraControls.IsGroundTileset = IsGroundTileset;
1554
1561
  /**
1555
1562
  * Says whether the scene has data underground, which makes the ground easier to push through; null scans for it.
1556
1563
  */
@@ -57722,12 +57729,6 @@ void main()
57722
57729
  const SKIRT_FRACTION = 0.015;
57723
57730
  // Alpha below which a texel counts as no-data and is not drawn at all.
57724
57731
  const COVERAGE_CUTOFF = 0.35;
57725
- // Terrain is sampled on this grid and interpolated between samples.
57726
- // Per-texel sampling would be hundreds of thousands of lookups for a surface whose own data is far coarser than that.
57727
- const GROUND_SAMPLES_PER_SIDE = 64;
57728
- // Terrain detail level the samples are taken at.
57729
- // Most-detailed would fetch every tile under a parcel that can be a hundred kilometres across.
57730
- const GROUND_SAMPLE_LEVEL = 11;
57731
57732
  // Height window the packed ground texture covers, wide enough for any terrain on Earth.
57732
57733
  // Ground samples across the clearance regions rather than the whole extent.
57733
57734
  const MASK_GROUND_SAMPLES_PER_SIDE = 128;
@@ -57735,9 +57736,23 @@ void main()
57735
57736
  const MASK_GROUND_LEVEL_MAX = 13;
57736
57737
  const MASK_GROUND_LEVEL_MIN = 8;
57737
57738
  const MASK_GROUND_TILE_BUDGET = 64;
57739
+ // Finest terrain level a ground block is read at, and how many terrain tiles one block may pull.
57740
+ const FINE_GROUND_LEVEL_MAX = 18;
57741
+ const FINE_GROUND_TILE_BUDGET = 16;
57738
57742
  // Longest side of the rasterised region mask. The mask decides whether a texel is held up, so it
57739
57743
  // wants to be finer than the value raster it is compared against.
57740
57744
  const MASK_TEXTURE_MAX_SIDE = 1024;
57745
+ const DEFAULT_DRAPE_LIFT_METRES = 4;
57746
+ const DEFAULT_DRAPE_BAND_METRES = 3;
57747
+ // A ground tileset is measured against the ground raster at this many points around the camera, this far apart.
57748
+ const OFFSET_PROBES_PER_SIDE = 3;
57749
+ const OFFSET_PROBE_SPACING_METRES = 250;
57750
+ // The camera has to move this far before a ground tileset is measured again.
57751
+ const OFFSET_REPROBE_METRES = 1500;
57752
+ // Low percentile of tileset minus ground, so roofs and canopy over a probe do not lift the water.
57753
+ const OFFSET_PERCENTILE = 0.25;
57754
+ const WGS84_A = 6378137.0;
57755
+ const WGS84_E2 = 0.00669437999014;
57741
57756
  // Stands in for "no floor" and "no clearance", far below anything the sheet can reach.
57742
57757
  const NO_LIMIT_METRES = -1e6;
57743
57758
  // Ripple layers to cycle across the field's own repeat.
@@ -57782,6 +57797,32 @@ void main()
57782
57797
  const GROUND_MAX_METRES = 9000;
57783
57798
  const DEFAULT_LOW_COLOR = { red: 255, green: 255, blue: 255, alpha: 0.12 };
57784
57799
  const DEFAULT_HIGH_COLOR = { red: 21, green: 96, blue: 196, alpha: 0.92 };
57800
+ // Decodes the packed ground raster, shared by both shaders.
57801
+ const GROUND_GLSL = `
57802
+ float decodeGround(vec2 uv) {
57803
+ // Channels arrive normalised to 0..1, so they go back to bytes before being reassembled.
57804
+ vec3 bytes = texture(u_groundTexture, uv).rgb * 255.0;
57805
+ float normalised = dot(bytes, vec3(65536.0, 256.0, 1.0)) / 16777215.0;
57806
+ return u_groundRange.x + normalised * (u_groundRange.y - u_groundRange.x);
57807
+ }
57808
+
57809
+ /*
57810
+ * Bilinear by hand, because the height is packed across three channels and hardware filtering would
57811
+ * blend the packing rather than the heights, turning a smooth slope into noise at every texel edge.
57812
+ */
57813
+ float sampleGround(vec2 uv) {
57814
+ vec2 texel = 1.0 / u_groundSize;
57815
+ vec2 coord = uv * u_groundSize - 0.5;
57816
+ vec2 base = floor(coord);
57817
+ vec2 f = coord - base;
57818
+ vec2 origin = (base + 0.5) * texel;
57819
+ float h00 = decodeGround(origin);
57820
+ float h10 = decodeGround(origin + vec2(texel.x, 0.0));
57821
+ float h01 = decodeGround(origin + vec2(0.0, texel.y));
57822
+ float h11 = decodeGround(origin + texel);
57823
+ return mix(mix(h00, h10, f.x), mix(h01, h11, f.x), f.y);
57824
+ }
57825
+ `;
57785
57826
  const VERTEX_SHADER_GLSL300 = `
57786
57827
  in vec3 position3DHigh;
57787
57828
  in vec3 position3DLow;
@@ -57816,6 +57857,9 @@ uniform vec2 u_maskGroundSize;
57816
57857
  uniform vec4 u_extentDegrees;
57817
57858
  uniform vec4 u_maskGroundDegrees;
57818
57859
  uniform float u_coverageCutoffVertex;
57860
+ uniform float u_drape;
57861
+ uniform float u_drapeLift;
57862
+ uniform float u_groundOffset;
57819
57863
  //#water
57820
57864
  uniform vec2 u_extentMetres;
57821
57865
  uniform vec2 u_waveOrigin;
@@ -57824,6 +57868,7 @@ uniform vec2 u_waveOrigin;
57824
57868
  out vec2 v_st;
57825
57869
  out float v_value;
57826
57870
  out float v_coverage;
57871
+ out vec3 v_eyeEC;
57827
57872
  //#water
57828
57873
  out vec3 v_positionEC;
57829
57874
  out vec3 v_eastEC;
@@ -57832,30 +57877,7 @@ out vec3 v_upEC;
57832
57877
  out vec2 v_waveMetres;
57833
57878
  //#endwater
57834
57879
 
57835
- float decodeGround(vec2 uv) {
57836
- // Channels arrive normalised to 0..1, so they go back to bytes before being reassembled.
57837
- vec3 packed = texture(u_groundTexture, uv).rgb * 255.0;
57838
- float normalised = dot(packed, vec3(65536.0, 256.0, 1.0)) / 16777215.0;
57839
- return u_groundRange.x + normalised * (u_groundRange.y - u_groundRange.x);
57840
- }
57841
-
57842
- /*
57843
- * Bilinear by hand, because the height is packed across three channels and hardware filtering would
57844
- * blend the packing rather than the heights, turning a smooth slope into noise at every texel edge.
57845
- */
57846
- float sampleGround(vec2 uv) {
57847
- vec2 texel = 1.0 / u_groundSize;
57848
- vec2 coord = uv * u_groundSize - 0.5;
57849
- vec2 base = floor(coord);
57850
- vec2 f = coord - base;
57851
- vec2 origin = (base + 0.5) * texel;
57852
- float h00 = decodeGround(origin);
57853
- float h10 = decodeGround(origin + vec2(texel.x, 0.0));
57854
- float h01 = decodeGround(origin + vec2(0.0, texel.y));
57855
- float h11 = decodeGround(origin + texel);
57856
- return mix(mix(h00, h10, f.x), mix(h01, h11, f.x), f.y);
57857
- }
57858
-
57880
+ ${GROUND_GLSL}
57859
57881
  /*
57860
57882
  * Height of the terrain under a point of the surface, read from the region's own ground raster.
57861
57883
  *
@@ -57937,12 +57959,13 @@ void main() {
57937
57959
 
57938
57960
  // Value zero sits at the polygon's own altitude, plus the terrain underneath when the polygon
57939
57961
  // is following the ground rather than absolutely positioned.
57940
- float ground = sampleGround(st) * u_groundWeight;
57962
+ float ground = (sampleGround(st) + u_groundOffset) * u_groundWeight;
57941
57963
 
57942
57964
  // Coverage scales the displacement only where it is measured from the terrain, because there a
57943
57965
  // zero means the surface sits on the ground.
57944
57966
  float coverageScale = mix(1.0, v_coverage, u_groundWeight);
57945
- float placed = u_baseHeight + ground + metres * coverageScale;
57967
+ // Draped, the sheet only has to stand above what is drawn; the fragment paints the drawn surface behind it.
57968
+ float placed = u_baseHeight + ground + metres * coverageScale + u_drapeLift * u_drape;
57946
57969
 
57947
57970
  // Held above whichever is higher, the region's own floor or the terrain under it plus a margin.
57948
57971
  // Only inside the region: lifting the whole sheet would raise it over dry land as well.
@@ -57966,6 +57989,7 @@ void main() {
57966
57989
  v_waveMetres = st * u_extentMetres - u_waveOrigin;
57967
57990
  //#endwater
57968
57991
 
57992
+ v_eyeEC = (czm_modelViewRelativeToEye * p).xyz;
57969
57993
  gl_Position = czm_modelViewProjectionRelativeToEye * p;
57970
57994
  }
57971
57995
  `;
@@ -57980,7 +58004,32 @@ uniform float u_valuePacked;
57980
58004
  uniform float u_valueRange;
57981
58005
  uniform float u_exaggeration;
57982
58006
  uniform float u_metresPerTexel;
58007
+ uniform float u_valueMin;
58008
+ uniform sampler2D u_groundTexture;
58009
+ uniform vec2 u_groundRange;
58010
+ uniform vec2 u_groundSize;
58011
+ uniform float u_groundWeight;
58012
+ uniform float u_groundOffset;
58013
+ uniform float u_drape;
58014
+ // Metres a drawn surface may stand above the water level and still be painted, below zero to paint whatever is drawn.
58015
+ uniform float u_drapeBand;
58016
+ // The extent centre on the ellipsoid in eye coordinates, with its east, north and up axes.
58017
+ uniform vec3 u_drapeCentreEC;
58018
+ uniform vec3 u_drapeEastEC;
58019
+ uniform vec3 u_drapeNorthEC;
58020
+ uniform vec3 u_drapeUpEC;
58021
+ // Centre longitude and latitude in radians, then the meridian and prime vertical radii of curvature there.
58022
+ uniform vec4 u_drapeGeo;
58023
+ // West, East, North, South in radians.
58024
+ uniform vec4 u_extentRadians;
58025
+ uniform sampler2D u_maskTexture;
58026
+ uniform float u_maskWeight;
58027
+ uniform float u_maskFill;
58028
+ uniform float u_maskFillValue;
58029
+ uniform float u_maskFloor;
57983
58030
  //#water
58031
+ uniform vec2 u_extentMetres;
58032
+ uniform vec2 u_waveOrigin;
57984
58033
  uniform float u_waterWavelength;
57985
58034
  uniform float u_waterAmplitude;
57986
58035
  uniform float u_waterReflectivity;
@@ -57997,6 +58046,7 @@ uniform float u_wavePeriod;
57997
58046
  in vec2 v_st;
57998
58047
  in float v_value;
57999
58048
  in float v_coverage;
58049
+ in vec3 v_eyeEC;
58000
58050
  //#water
58001
58051
  in vec3 v_positionEC;
58002
58052
  in vec3 v_eastEC;
@@ -58018,6 +58068,72 @@ float valueAt(vec2 uv) {
58018
58068
  return mix(decodeValue(texture(u_valueTextureFrom, uv)), decodeValue(texture(u_valueTexture, uv)), u_valueFade);
58019
58069
  }
58020
58070
 
58071
+ ${GROUND_GLSL}
58072
+
58073
+ /*
58074
+ * The opaque surface drawn behind this pixel, or false when none, outside the extent, or above the band.
58075
+ */
58076
+ bool drawnBehind(out vec2 uv, out float value, out float coverage, out vec3 positionEC) {
58077
+ float depth = czm_unpackDepth(texture(czm_globeDepthTexture, gl_FragCoord.xy / czm_viewport.zw));
58078
+ if (depth <= 0.0 || depth >= 1.0) {
58079
+ return false;
58080
+ }
58081
+ vec4 eye = czm_windowToEyeCoordinates(gl_FragCoord.xy, depth);
58082
+ positionEC = eye.xyz / eye.w;
58083
+
58084
+ // East, north and up metres from the extent centre, small numbers that keep their precision.
58085
+ vec3 offset = positionEC - u_drapeCentreEC;
58086
+ float east = dot(offset, u_drapeEastEC);
58087
+ float north = dot(offset, u_drapeNorthEC);
58088
+ float up = dot(offset, u_drapeUpEC);
58089
+ float latitude = u_drapeGeo.y + north / u_drapeGeo.z;
58090
+ float longitude = u_drapeGeo.x + east / (u_drapeGeo.w * cos(latitude));
58091
+ uv = vec2((longitude - u_extentRadians.x) / (u_extentRadians.y - u_extentRadians.x),
58092
+ (u_extentRadians.z - latitude) / (u_extentRadians.z - u_extentRadians.w));
58093
+ if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) {
58094
+ return false;
58095
+ }
58096
+
58097
+ // Bilinear by hand between texel centres, so cell edges on the ground read as a shoreline, not squares.
58098
+ vec2 coord = uv / u_texelSize - 0.5;
58099
+ vec2 base = floor(coord);
58100
+ vec2 f = coord - base;
58101
+ vec2 origin = (base + 0.5) * u_texelSize;
58102
+ vec2 values = vec2(0.0);
58103
+ vec2 weights = vec2(0.0);
58104
+ for (int i = 0; i < 4; i++) {
58105
+ vec2 step01 = vec2(float(i - (i / 2) * 2), float(i / 2));
58106
+ vec2 at = origin + step01 * u_texelSize;
58107
+ float w = mix(1.0 - f.x, f.x, step01.x) * mix(1.0 - f.y, f.y, step01.y);
58108
+ vec4 texelTo = texture(u_valueTexture, at);
58109
+ vec4 texelFrom = texture(u_valueTextureFrom, at);
58110
+ float a = mix(texelFrom.a, texelTo.a, u_valueFade);
58111
+ // Weighted by coverage too, so a no-data neighbour fades the edge instead of dragging the value to zero.
58112
+ values += vec2(mix(decodeValue(texelFrom), decodeValue(texelTo), u_valueFade) * a, a) * w;
58113
+ weights += vec2(a, 1.0) * w;
58114
+ }
58115
+ value = values.x / max(values.y, 1e-6);
58116
+ coverage = values.y;
58117
+
58118
+ // A region known to be water paints its gaps here too, the same way the vertex shader fills them.
58119
+ float inMask = u_maskWeight * texture(u_maskTexture, uv).a;
58120
+ float hasData = step(u_coverageCutoff, coverage);
58121
+ value = mix(value, mix(u_maskFillValue, max(value, u_maskFillValue), hasData), inMask * u_maskFill);
58122
+ coverage = max(coverage, inMask * u_maskFill);
58123
+
58124
+ if (u_drapeBand >= 0.0) {
58125
+ // The tangent plane falls away from the ellipsoid by distance squared over twice the radius.
58126
+ float height = up + (east * east + north * north) / (2.0 * u_drapeGeo.w);
58127
+ float level = (sampleGround(uv) + u_groundOffset) * u_groundWeight + u_valueMin + value * u_valueRange * u_exaggeration;
58128
+ // Inside a region the water is never below its known floor, so a deck or bank under that floor is still wet.
58129
+ level = mix(level, max(level, u_maskFloor), step(0.5, inMask));
58130
+ if (height - level > u_drapeBand) {
58131
+ return false;
58132
+ }
58133
+ }
58134
+ return true;
58135
+ }
58136
+
58021
58137
  //#water
58022
58138
  /*
58023
58139
  * Slope of the ripple field at a point, in metres of rise per metre travelled.
@@ -58049,17 +58165,17 @@ ${WATER_WAVES.map((wave, index) => {
58049
58165
  * The sheet shaded as a water surface: a flat body colour seen through a sun glint and a grazing
58050
58166
  * sky reflection, with the ripple field standing in for a geometry it is far too fine to carry.
58051
58167
  */
58052
- vec4 water(vec3 relief, vec4 color) {
58053
- float metresPerPixel = czm_metersPerPixel(vec4(v_positionEC, 1.0));
58168
+ vec4 water(vec3 relief, vec4 color, vec3 positionEC, vec2 waveMetres, float coverage) {
58169
+ float metresPerPixel = czm_metersPerPixel(vec4(positionEC, 1.0));
58054
58170
  float detail;
58055
- vec2 slope = rippleSlope(v_waveMetres, metresPerPixel, detail);
58171
+ vec2 slope = rippleSlope(waveMetres, metresPerPixel, detail);
58056
58172
 
58057
58173
  // relief arrives normalised, so its horizontal part over its vertical part is the slope it means.
58058
58174
  vec2 reliefSlope = relief.xy / max(relief.z, 1e-3);
58059
58175
  vec3 local = normalize(vec3(-(slope + reliefSlope), 1.0));
58060
58176
  vec3 normalEC = normalize(local.x * v_eastEC + local.y * v_northEC + local.z * v_upEC);
58061
58177
 
58062
- vec3 view = normalize(-v_positionEC);
58178
+ vec3 view = normalize(-positionEC);
58063
58179
  // Drawn with culling off, so the underside of the sheet arrives with its normal pointing away.
58064
58180
  normalEC = dot(normalEC, view) < 0.0 ? -normalEC : normalEC;
58065
58181
  vec3 sun = normalize(czm_sunDirectionEC);
@@ -58086,46 +58202,54 @@ vec4 water(vec3 relief, vec4 color) {
58086
58202
  /*
58087
58203
  * The same fog the globe under it is already drawing with.
58088
58204
  */
58089
- lit = czm_fog(length(v_positionEC), lit, u_waterSky);
58205
+ lit = czm_fog(length(positionEC), lit, u_waterSky);
58090
58206
 
58091
58207
  // A reflection seen through a half-transparent sheet is not a reflection, so the surface is
58092
58208
  // allowed to close up exactly where it is being reflective.
58093
- float alpha = color.a * v_coverage;
58209
+ float alpha = color.a * coverage;
58094
58210
  alpha = clamp(alpha + (1.0 - alpha) * (fresnel * 0.55 + glint), 0.0, 1.0);
58095
58211
  return vec4(lit, alpha);
58096
58212
  }
58097
58213
  //#endwater
58098
58214
 
58099
58215
  void main() {
58100
- if (v_coverage < u_coverageCutoff) {
58216
+ vec2 uv = v_st;
58217
+ float value = v_value;
58218
+ float coverage = v_coverage;
58219
+ vec3 positionEC = v_eyeEC;
58220
+ // Clamped: paint what is drawn behind this pixel, so the water sits on it however far off the sheet is.
58221
+ if (u_drape > 0.5 && !drawnBehind(uv, value, coverage, positionEC)) {
58222
+ discard;
58223
+ }
58224
+ if (coverage < u_coverageCutoff) {
58101
58225
  discard;
58102
58226
  }
58103
58227
  // The ramp is baked to a lookup rather than lerped between two colours, so a banded style with a
58104
58228
  // hidden floor reads the same here as it does on the flat drape.
58105
58229
  // Without it this surface painted water the style hides, and the cell grid then correctly refused to outline any of it.
58106
- vec4 color = texture(u_rampTexture, vec2(clamp(v_value, 0.0, 1.0), 0.5));
58230
+ vec4 color = texture(u_rampTexture, vec2(clamp(value, 0.0, 1.0), 0.5));
58107
58231
  if (color.a <= 0.0) {
58108
58232
  discard;
58109
58233
  }
58110
58234
 
58111
58235
  // Relief shading from the value gradient, so the displacement reads at a distance without
58112
58236
  // recomputing vertex normals every time the texture changes.
58113
- float left = valueAt(v_st - vec2(u_texelSize.x, 0.0));
58114
- float right = valueAt(v_st + vec2(u_texelSize.x, 0.0));
58115
- float down = valueAt(v_st - vec2(0.0, u_texelSize.y));
58116
- float up = valueAt(v_st + vec2(0.0, u_texelSize.y));
58237
+ float left = valueAt(uv - vec2(u_texelSize.x, 0.0));
58238
+ float right = valueAt(uv + vec2(u_texelSize.x, 0.0));
58239
+ float down = valueAt(uv - vec2(0.0, u_texelSize.y));
58240
+ float up = valueAt(uv + vec2(0.0, u_texelSize.y));
58117
58241
  float scale = u_valueRange * u_exaggeration;
58118
58242
  vec3 n = normalize(vec3((left - right) * scale, (down - up) * scale, 2.0 * u_metresPerTexel));
58119
58243
 
58120
58244
  //#water
58121
- out_FragColor = water(n, color);
58245
+ out_FragColor = water(n, color, positionEC, mix(v_waveMetres, uv * u_extentMetres - u_waveOrigin, u_drape), coverage);
58122
58246
  return;
58123
58247
  //#endwater
58124
58248
 
58125
58249
  float lambert = clamp(dot(n, normalize(vec3(-0.5, -0.6, 0.62))), 0.0, 1.0);
58126
58250
  color.rgb *= 0.45 + 0.9 * lambert;
58127
58251
 
58128
- out_FragColor = vec4(color.rgb, color.a * v_coverage);
58252
+ out_FragColor = vec4(color.rgb, color.a * coverage);
58129
58253
  }
58130
58254
  `;
58131
58255
  /*
@@ -58171,9 +58295,25 @@ void main() {
58171
58295
  // No probe means no evidence the host rewrites anything, so trust the context alone.
58172
58296
  modernizes = false;
58173
58297
  }
58174
- _glsl300Support = Boolean(context.webgl2) && !modernizes;
58298
+ _glsl300Support = context.webgl2 ? !modernizes : demotes(context);
58175
58299
  return _glsl300Support;
58176
58300
  }
58301
+ /*
58302
+ * True when the host takes GLSL 300 and rewrites it down itself on a WebGL1 context, as Cesium 1.102 onwards does.
58303
+ */
58304
+ function demotes(context) {
58305
+ try {
58306
+ const probe = new Cesium.ShaderSource({
58307
+ sources: ["in float a_probe;\nout float v_probe;\nvoid main() { v_probe = a_probe; gl_Position = vec4(0.0); }"],
58308
+ includeBuiltIns: false
58309
+ });
58310
+ const combined = probe.createCombinedVertexShader(context);
58311
+ return combined.indexOf("attribute") >= 0;
58312
+ }
58313
+ catch (e) {
58314
+ return false;
58315
+ }
58316
+ }
58177
58317
  /*
58178
58318
  * Places grid node (fx, fy) on the ellipsoid, stepping the way the raster was sampled.
58179
58319
  * A raster sampled at lon/lat has to be walked in degrees; one sampled linearly across a projected CRS has to be walked in tangent-plane metres.
@@ -58455,9 +58595,334 @@ void main() {
58455
58595
  }
58456
58596
  return MASK_GROUND_LEVEL_MIN;
58457
58597
  }
58598
+ // Ground under a clamped value surface: coarse everywhere first, then refined block by block nearest the camera.
58599
+ let GroundRaster;
58600
+ (function (GroundRaster) {
58601
+ // Height window the packed raster covers, wide enough for any terrain on Earth.
58602
+ GroundRaster.MIN_METRES = -1000;
58603
+ GroundRaster.MAX_METRES = 9000;
58604
+ const PACKED_MAX = 16777215;
58605
+ // Samples per side of the first pass over the whole extent.
58606
+ const COARSE_SAMPLES = 64;
58607
+ // Samples per side of one refined block.
58608
+ GroundRaster.BLOCK_SAMPLES = 64;
58609
+ const MAX_SIDE = 2048;
58610
+ // Blocks being sampled at once, so a fast camera does not queue the whole model.
58611
+ const MAX_IN_FLIGHT = 2;
58612
+ // A block is refined once the camera is within this many block widths of it.
58613
+ const REFINE_BLOCK_WIDTHS = 12;
58614
+ /**
58615
+ * Raster side for a value source of the given size: a whole number of blocks, at least one, at most the cap.
58616
+ */
58617
+ function SideFor(width, height) {
58618
+ const wanted = Math.max(1, Math.round(Math.max(width || 0, height || 0)));
58619
+ const blocks = Math.ceil(wanted / GroundRaster.BLOCK_SAMPLES);
58620
+ return Math.min(MAX_SIDE, Math.max(1, blocks) * GroundRaster.BLOCK_SAMPLES);
58621
+ }
58622
+ GroundRaster.SideFor = SideFor;
58623
+ /**
58624
+ * Packs a height into three bytes the way the vertex shader unpacks it.
58625
+ */
58626
+ function Pack(height, into, at) {
58627
+ const known = typeof height === "number" && isFinite(height);
58628
+ const clamped = Math.min(GroundRaster.MAX_METRES, Math.max(GroundRaster.MIN_METRES, known ? height : 0));
58629
+ const packed = Math.round(((clamped - GroundRaster.MIN_METRES) / (GroundRaster.MAX_METRES - GroundRaster.MIN_METRES)) * PACKED_MAX);
58630
+ into[at] = (packed >> 16) & 255;
58631
+ into[at + 1] = (packed >> 8) & 255;
58632
+ into[at + 2] = packed & 255;
58633
+ into[at + 3] = known ? 255 : 0;
58634
+ }
58635
+ GroundRaster.Pack = Pack;
58636
+ /**
58637
+ * Bilinear between texel centres, the way the shader reads it.
58638
+ */
58639
+ function Bilinear(heights, side, u, v) {
58640
+ const read = (x, y) => heights[Math.min(side - 1, Math.max(0, y)) * side + Math.min(side - 1, Math.max(0, x))];
58641
+ const x = u * side - 0.5;
58642
+ const y = v * side - 0.5;
58643
+ const x0 = Math.floor(x);
58644
+ const y0 = Math.floor(y);
58645
+ const fx = x - x0;
58646
+ const fy = y - y0;
58647
+ const top = read(x0, y0) + (read(x0 + 1, y0) - read(x0, y0)) * fx;
58648
+ const bottom = read(x0, y0 + 1) + (read(x0 + 1, y0 + 1) - read(x0, y0 + 1)) * fx;
58649
+ return top + (bottom - top) * fy;
58650
+ }
58651
+ GroundRaster.Bilinear = Bilinear;
58652
+ class Raster {
58653
+ constructor(extent, side, sampler) {
58654
+ this.extent = extent;
58655
+ this.sampler = sampler;
58656
+ this.inFlight = new Set();
58657
+ this.dirty = [];
58658
+ this.coarseDone = false;
58659
+ this.coarseStarted = false;
58660
+ this.disposed = false;
58661
+ this.version = 0;
58662
+ this.side = Math.max(GroundRaster.BLOCK_SAMPLES, Math.round(side / GroundRaster.BLOCK_SAMPLES) * GroundRaster.BLOCK_SAMPLES);
58663
+ this.blocksPerSide = this.side / GroundRaster.BLOCK_SAMPLES;
58664
+ this.pixels = new Uint8Array(this.side * this.side * 4);
58665
+ this.heights = new Float32Array(this.side * this.side);
58666
+ this.refined = new Uint8Array(this.blocksPerSide * this.blocksPerSide);
58667
+ }
58668
+ Dispose() {
58669
+ this.disposed = true;
58670
+ }
58671
+ HasCoarse() {
58672
+ return this.coarseDone;
58673
+ }
58674
+ /**
58675
+ * Changes whenever any height changes.
58676
+ */
58677
+ GetVersion() {
58678
+ return this.version;
58679
+ }
58680
+ RefinedCount() {
58681
+ return this.refined.reduce((sum, value) => sum + value, 0);
58682
+ }
58683
+ InFlightCount() {
58684
+ return this.inFlight.size;
58685
+ }
58686
+ /**
58687
+ * Metres above the ellipsoid at a normalised point, north at v zero.
58688
+ */
58689
+ HeightAt(u, v) {
58690
+ return Bilinear(this.heights, this.side, u, v);
58691
+ }
58692
+ /**
58693
+ * The regions changed since the last call.
58694
+ */
58695
+ TakeDirty() {
58696
+ const dirty = this.dirty;
58697
+ this.dirty = [];
58698
+ return dirty;
58699
+ }
58700
+ /**
58701
+ * Samples the whole extent once, at the cheap level.
58702
+ */
58703
+ StartCoarse() {
58704
+ if (this.coarseStarted) {
58705
+ return Promise.resolve();
58706
+ }
58707
+ this.coarseStarted = true;
58708
+ const positions = this.grid(0, 0, 1, 1, COARSE_SAMPLES);
58709
+ return this.sampler.coarse(positions)
58710
+ .then((heights) => {
58711
+ if (this.disposed) {
58712
+ return;
58713
+ }
58714
+ const coarse = new Float32Array(COARSE_SAMPLES * COARSE_SAMPLES);
58715
+ for (let i = 0; i < coarse.length; i++) {
58716
+ const h = heights[i];
58717
+ coarse[i] = typeof h === "number" && isFinite(h) ? h : 0;
58718
+ }
58719
+ for (let y = 0; y < this.side; y++) {
58720
+ for (let x = 0; x < this.side; x++) {
58721
+ this.write(x, y, Bilinear(coarse, COARSE_SAMPLES, (x + 0.5) / this.side, (y + 0.5) / this.side));
58722
+ }
58723
+ }
58724
+ this.coarseDone = true;
58725
+ this.markDirty(0, 0, this.side, this.side);
58726
+ })
58727
+ .catch(() => {
58728
+ // Terrain that cannot be sampled leaves the sheet at its base height.
58729
+ });
58730
+ }
58731
+ /*
58732
+ * Starts sampling the unrefined visible blocks nearest the eye (world coordinates), up to the cap.
58733
+ */
58734
+ Refine(eye, isVisible) {
58735
+ if (!this.coarseDone || this.disposed || this.inFlight.size >= MAX_IN_FLIGHT) {
58736
+ return 0;
58737
+ }
58738
+ const candidates = [];
58739
+ const blockMetres = this.blockMetres();
58740
+ for (let by = 0; by < this.blocksPerSide; by++) {
58741
+ for (let bx = 0; bx < this.blocksPerSide; bx++) {
58742
+ const index = by * this.blocksPerSide + bx;
58743
+ if (this.refined[index] || this.inFlight.has(index)) {
58744
+ continue;
58745
+ }
58746
+ const sphere = this.blockSphere(bx, by, blockMetres);
58747
+ const distance = Math.max(0, Cesium.Cartesian3.distance(eye, sphere.center) - sphere.radius);
58748
+ if (distance > blockMetres * REFINE_BLOCK_WIDTHS) {
58749
+ continue;
58750
+ }
58751
+ if (isVisible && !isVisible(sphere)) {
58752
+ continue;
58753
+ }
58754
+ candidates.push({ index, distance });
58755
+ }
58756
+ }
58757
+ candidates.sort((a, b) => a.distance - b.distance);
58758
+ let started = 0;
58759
+ for (const candidate of candidates) {
58760
+ if (this.inFlight.size >= MAX_IN_FLIGHT) {
58761
+ break;
58762
+ }
58763
+ this.refineBlock(candidate.index);
58764
+ started++;
58765
+ }
58766
+ return started;
58767
+ }
58768
+ refineBlock(index) {
58769
+ this.inFlight.add(index);
58770
+ const bx = index % this.blocksPerSide;
58771
+ const by = Math.floor(index / this.blocksPerSide);
58772
+ const u0 = bx / this.blocksPerSide;
58773
+ const v0 = by / this.blocksPerSide;
58774
+ const span = 1 / this.blocksPerSide;
58775
+ const positions = this.grid(u0, v0, span, span, GroundRaster.BLOCK_SAMPLES);
58776
+ const e = this.extent;
58777
+ const box = {
58778
+ West: Cesium.Math.lerp(e.West, e.East, u0),
58779
+ East: Cesium.Math.lerp(e.West, e.East, u0 + span),
58780
+ North: Cesium.Math.lerp(e.North, e.South, v0),
58781
+ South: Cesium.Math.lerp(e.North, e.South, v0 + span)
58782
+ };
58783
+ this.sampler.fine(positions, box)
58784
+ .then((heights) => {
58785
+ if (this.disposed) {
58786
+ return;
58787
+ }
58788
+ for (let row = 0; row < GroundRaster.BLOCK_SAMPLES; row++) {
58789
+ for (let col = 0; col < GroundRaster.BLOCK_SAMPLES; col++) {
58790
+ const h = heights[row * GroundRaster.BLOCK_SAMPLES + col];
58791
+ // Keeping the coarse height where the source has nothing finer.
58792
+ if (typeof h === "number" && isFinite(h)) {
58793
+ this.write(bx * GroundRaster.BLOCK_SAMPLES + col, by * GroundRaster.BLOCK_SAMPLES + row, h);
58794
+ }
58795
+ }
58796
+ }
58797
+ this.markDirty(bx * GroundRaster.BLOCK_SAMPLES, by * GroundRaster.BLOCK_SAMPLES, GroundRaster.BLOCK_SAMPLES, GroundRaster.BLOCK_SAMPLES);
58798
+ })
58799
+ .catch(() => {
58800
+ // A block that cannot be sampled keeps its coarse heights.
58801
+ })
58802
+ .then(() => {
58803
+ this.refined[index] = 1;
58804
+ this.inFlight.delete(index);
58805
+ });
58806
+ }
58807
+ /*
58808
+ * Positions at texel centres of a sub-box, given in normalised extent coordinates, north at v zero.
58809
+ */
58810
+ grid(u0, v0, uSpan, vSpan, samples) {
58811
+ const positions = [];
58812
+ const e = this.extent;
58813
+ for (let row = 0; row < samples; row++) {
58814
+ const v = v0 + vSpan * (row + 0.5) / samples;
58815
+ const latitude = Cesium.Math.lerp(e.North, e.South, v);
58816
+ for (let col = 0; col < samples; col++) {
58817
+ const u = u0 + uSpan * (col + 0.5) / samples;
58818
+ positions.push(Cesium.Cartographic.fromDegrees(Cesium.Math.lerp(e.West, e.East, u), latitude));
58819
+ }
58820
+ }
58821
+ return positions;
58822
+ }
58823
+ write(x, y, height) {
58824
+ const at = y * this.side + x;
58825
+ this.heights[at] = height;
58826
+ Pack(height, this.pixels, at * 4);
58827
+ }
58828
+ markDirty(x, y, width, height) {
58829
+ this.dirty.push({ x, y, width, height });
58830
+ this.version++;
58831
+ }
58832
+ blockMetres() {
58833
+ const e = this.extent;
58834
+ const midLat = (e.North + e.South) / 2;
58835
+ const west = Cesium.Cartesian3.fromDegrees(e.West, midLat);
58836
+ const east = Cesium.Cartesian3.fromDegrees(e.East, midLat);
58837
+ const south = Cesium.Cartesian3.fromDegrees(e.West, e.South);
58838
+ const north = Cesium.Cartesian3.fromDegrees(e.West, e.North);
58839
+ const across = Math.max(Cesium.Cartesian3.distance(west, east), Cesium.Cartesian3.distance(south, north));
58840
+ return across / this.blocksPerSide;
58841
+ }
58842
+ blockSphere(bx, by, blockMetres) {
58843
+ const e = this.extent;
58844
+ const u = (bx + 0.5) / this.blocksPerSide;
58845
+ const v = (by + 0.5) / this.blocksPerSide;
58846
+ const centre = Cesium.Cartesian3.fromDegrees(Cesium.Math.lerp(e.West, e.East, u), Cesium.Math.lerp(e.North, e.South, v), this.HeightAt(u, v));
58847
+ return new Cesium.BoundingSphere(centre, blockMetres * Math.SQRT1_2);
58848
+ }
58849
+ }
58850
+ GroundRaster.Raster = Raster;
58851
+ })(GroundRaster = DisplacedSurfacePrimitive.GroundRaster || (DisplacedSurfacePrimitive.GroundRaster = {}));
58852
+ /**
58853
+ * Drawn ground minus sampled ground: a low percentile, so roofs and canopy over a probe are ignored.
58854
+ */
58855
+ function GroundOffsetFrom(differences) {
58856
+ const known = differences.filter((d) => typeof d === "number" && isFinite(d)).sort((a, b) => a - b);
58857
+ if (!known.length) {
58858
+ return null;
58859
+ }
58860
+ return known[Math.floor(OFFSET_PERCENTILE * (known.length - 1))];
58861
+ }
58862
+ DisplacedSurfacePrimitive.GroundOffsetFrom = GroundOffsetFrom;
58863
+ /*
58864
+ * The first shown tileset standing in for the globe, such as Google's photorealistic tiles.
58865
+ */
58866
+ function findGroundTileset(collection) {
58867
+ if (!collection) {
58868
+ return null;
58869
+ }
58870
+ for (let i = 0; i < collection.length; i++) {
58871
+ const primitive = collection.get(i);
58872
+ if (!primitive || primitive.show === false) {
58873
+ continue;
58874
+ }
58875
+ if (typeof primitive.length === "number" && typeof primitive.get === "function") {
58876
+ const nested = findGroundTileset(primitive);
58877
+ if (nested) {
58878
+ return nested;
58879
+ }
58880
+ }
58881
+ else if (exports.CameraControls.IsGroundTileset(primitive)) {
58882
+ return primitive;
58883
+ }
58884
+ }
58885
+ return null;
58886
+ }
58887
+ /*
58888
+ * The finest level a ground block can be sampled at within a few tiles, and no finer than the terrain has there.
58889
+ */
58890
+ function fineLevelFor(box, provider) {
58891
+ const scheme = provider && provider.tilingScheme;
58892
+ let available = FINE_GROUND_LEVEL_MAX;
58893
+ const availability = provider && provider.availability;
58894
+ if (availability && typeof availability.computeMaximumLevelAtPosition === "function") {
58895
+ const centre = Cesium.Cartographic.fromDegrees((box.West + box.East) / 2, (box.North + box.South) / 2);
58896
+ available = Math.min(available, availability.computeMaximumLevelAtPosition(centre));
58897
+ }
58898
+ for (let level = available; level > MASK_GROUND_LEVEL_MIN; level--) {
58899
+ const across = scheme && typeof scheme.getNumberOfXTilesAtLevel === "function"
58900
+ ? scheme.getNumberOfXTilesAtLevel(level)
58901
+ : Math.pow(2, level + 1);
58902
+ const down = scheme && typeof scheme.getNumberOfYTilesAtLevel === "function"
58903
+ ? scheme.getNumberOfYTilesAtLevel(level)
58904
+ : Math.pow(2, level);
58905
+ const tiles = Math.ceil((box.East - box.West) / (360 / across) + 1)
58906
+ * Math.ceil((box.North - box.South) / (180 / down) + 1);
58907
+ if (tiles <= FINE_GROUND_TILE_BUDGET) {
58908
+ return level;
58909
+ }
58910
+ }
58911
+ return MASK_GROUND_LEVEL_MIN;
58912
+ }
58913
+ /*
58914
+ * Heights for a ground raster from a terrain provider: a cheap level first, then the finest in budget per block.
58915
+ */
58916
+ function terrainSampler(provider, extent) {
58917
+ const heightsOf = (sampled) => (sampled || []).map((c) => (c && isFinite(c.height) ? c.height : undefined));
58918
+ return {
58919
+ coarse: (positions) => Promise.resolve(Cesium.sampleTerrain(provider, groundLevelFor(extent, provider), positions)).then(heightsOf),
58920
+ fine: (positions, box) => Promise.resolve(Cesium.sampleTerrain(provider, fineLevelFor(box, provider), positions)).then(heightsOf)
58921
+ };
58922
+ }
58458
58923
  class Surface {
58459
58924
  constructor(options, hasCoverage) {
58460
- var _a, _b, _c, _d;
58925
+ var _a, _b, _c, _d, _e, _f;
58461
58926
  this.show = true;
58462
58927
  this.valueSource = null;
58463
58928
  this.lastPresentVersion = -1;
@@ -58478,9 +58943,22 @@ void main() {
58478
58943
  this.lastEyePosition = null;
58479
58944
  this.lastSelectionMs = -Infinity;
58480
58945
  this.selectionDirty = true;
58946
+ this.groundRaster = null;
58947
+ this.groundTextureSide = 0;
58948
+ // Draping reads globe coordinates back from depth, true only in 3D; 2D and Columbus keep the raised sheet.
58949
+ this.drapeActive = false;
58950
+ // Drawn ground minus the ground raster, measured where a ground tileset such as Google's is shown.
58951
+ this.groundOffset = 0;
58952
+ this.offsetProbe = { at: null, busy: false };
58953
+ this.drapeFrame = {
58954
+ centreEC: new Cesium.Cartesian3(),
58955
+ eastEC: new Cesium.Cartesian3(),
58956
+ northEC: new Cesium.Cartesian3(),
58957
+ upEC: new Cesium.Cartesian3(),
58958
+ geo: new Cesium.Cartesian4(),
58959
+ extent: new Cesium.Cartesian4()
58960
+ };
58481
58961
  this.groundTexture = null;
58482
- this.groundPixels = null;
58483
- this.groundRequested = false;
58484
58962
  this.groundClearance = null;
58485
58963
  this.maskTexture = null;
58486
58964
  this.maskPixels = null;
@@ -58497,13 +58975,18 @@ void main() {
58497
58975
  this.wavePhaseScratch = [new Cesium.Cartesian4(), new Cesium.Cartesian4()];
58498
58976
  this.waterSkyScratch = new Cesium.Cartesian3();
58499
58977
  this.followGround = Boolean(options.followGround);
58978
+ this.groundRasterSide = options.groundRasterSide;
58979
+ this.scene = options.scene || null;
58980
+ this.drape = Boolean(options.followGround) && options.drape !== false;
58981
+ this.drapeLift = (_a = options.drapeLift) !== null && _a !== void 0 ? _a : DEFAULT_DRAPE_LIFT_METRES;
58982
+ this.drapeBand = (_b = options.drapeBand) !== null && _b !== void 0 ? _b : DEFAULT_DRAPE_BAND_METRES;
58500
58983
  this.terrainProvider = options.terrainProvider || null;
58501
58984
  this.extent = options.extent;
58502
58985
  this.source = options.source || null;
58503
- this.valueMin = (_a = options.valueMin) !== null && _a !== void 0 ? _a : 0;
58504
- this.valueMax = (_b = options.valueMax) !== null && _b !== void 0 ? _b : 1;
58505
- this.exaggeration = (_c = options.exaggeration) !== null && _c !== void 0 ? _c : 1;
58506
- this.baseHeight = (_d = options.baseHeight) !== null && _d !== void 0 ? _d : 0;
58986
+ this.valueMin = (_c = options.valueMin) !== null && _c !== void 0 ? _c : 0;
58987
+ this.valueMax = (_d = options.valueMax) !== null && _d !== void 0 ? _d : 1;
58988
+ this.exaggeration = (_e = options.exaggeration) !== null && _e !== void 0 ? _e : 1;
58989
+ this.baseHeight = (_f = options.baseHeight) !== null && _f !== void 0 ? _f : 0;
58507
58990
  this.packedValue = Boolean(options.packedValue);
58508
58991
  this.pixelPlacement = options.pixelPlacement;
58509
58992
  this.tileSkirts = Boolean(options.tileSkirts);
@@ -58608,8 +59091,9 @@ void main() {
58608
59091
  return;
58609
59092
  }
58610
59093
  this.terrainProvider = provider;
58611
- this.groundPixels = null;
58612
- this.groundRequested = false;
59094
+ this.dropGroundRaster();
59095
+ this.groundOffset = 0;
59096
+ this.offsetProbe = { at: null, busy: false };
58613
59097
  this.maskGroundPixels = null;
58614
59098
  this.maskGroundRequested = false;
58615
59099
  this.maskGroundLevel = 0;
@@ -58663,15 +59147,15 @@ void main() {
58663
59147
  ? (pixels[at] * 256 + pixels[at + 1]) / 65535
58664
59148
  : pixels[at] / 255;
58665
59149
  const metres = this.valueMin + normalised * (this.valueMax - this.valueMin) * this.exaggeration;
58666
- const following = this.followGround && Boolean(this.groundPixels);
58667
- const ground = following ? this.groundAt(fx, fy) : 0;
59150
+ const following = this.hasGround();
59151
+ const ground = following ? this.groundRaster.HeightAt(fx, fy) + this.groundOffset : 0;
58668
59152
  return this.baseHeight + ground + metres * (following ? coverage : 1);
58669
59153
  }
58670
59154
  /**
58671
59155
  * Changes whenever HeightAt would answer differently for the same frame.
58672
59156
  */
58673
59157
  GetPlacementKey() {
58674
- return [this.groundPixels ? 1 : 0, this.baseHeight, this.exaggeration, this.followGround ? 1 : 0].join("|");
59158
+ return [this.groundRaster ? this.groundRaster.GetVersion() : 0, this.baseHeight, this.exaggeration, this.followGround ? 1 : 0].join("|");
58675
59159
  }
58676
59160
  /**
58677
59161
  * Repaints the sheet with new colours, keeping its geometry and the frames it is reading.
@@ -58697,25 +59181,19 @@ void main() {
58697
59181
  return this.rampPixels;
58698
59182
  }
58699
59183
  /*
58700
- * The packed ground raster read the way sampleGround reads it, bilinear between texel centres.
58701
- */
58702
- groundAt(u, v) {
58703
- const size = GROUND_SAMPLES_PER_SIDE;
58704
- const pixels = this.groundPixels;
58705
- const read = (x, y) => {
58706
- const i = (Math.min(size - 1, Math.max(0, y)) * size + Math.min(size - 1, Math.max(0, x))) * 4;
58707
- const packed = (pixels[i] << 16) + (pixels[i + 1] << 8) + pixels[i + 2];
58708
- return GROUND_MIN_METRES + (packed / 16777215) * (GROUND_MAX_METRES - GROUND_MIN_METRES);
58709
- };
58710
- const x = u * size - 0.5;
58711
- const y = v * size - 0.5;
58712
- const x0 = Math.floor(x);
58713
- const y0 = Math.floor(y);
58714
- const fx = x - x0;
58715
- const fy = y - y0;
58716
- const top = read(x0, y0) + (read(x0 + 1, y0) - read(x0, y0)) * fx;
58717
- const bottom = read(x0, y0 + 1) + (read(x0 + 1, y0 + 1) - read(x0, y0 + 1)) * fx;
58718
- return top + (bottom - top) * fy;
59184
+ * Whether a clamped sheet has ground to stand on yet.
59185
+ */
59186
+ hasGround() {
59187
+ return this.followGround && Boolean(this.groundRaster) && this.groundRaster.HasCoarse();
59188
+ }
59189
+ /*
59190
+ * Lets go of the ground raster, so samples still in flight land nowhere.
59191
+ */
59192
+ dropGroundRaster() {
59193
+ if (this.groundRaster) {
59194
+ this.groundRaster.Dispose();
59195
+ this.groundRaster = null;
59196
+ }
58719
59197
  }
58720
59198
  /**
58721
59199
  * Feeds the surface from an animated frame series.
@@ -58769,8 +59247,13 @@ void main() {
58769
59247
  this.syncTexture(context);
58770
59248
  }
58771
59249
  this.syncRampTexture(context);
58772
- this.requestGround();
59250
+ this.requestGround(frameState);
58773
59251
  this.syncGroundTexture(context);
59252
+ this.drapeActive = this.drape && frameState.mode === Cesium.SceneMode.SCENE3D;
59253
+ if (this.drapeActive) {
59254
+ this.probeGroundOffset(frameState);
59255
+ this.updateDrapeFrame(frameState);
59256
+ }
58774
59257
  this.requestMaskGround();
58775
59258
  this.syncMaskTextures(context);
58776
59259
  // A tile needs one mesh before it has a bounding sphere to measure against.
@@ -58827,6 +59310,7 @@ void main() {
58827
59310
  this.groundTexture.destroy();
58828
59311
  this.groundTexture = null;
58829
59312
  }
59313
+ this.dropGroundRaster();
58830
59314
  if (this.maskTexture) {
58831
59315
  this.maskTexture.destroy();
58832
59316
  this.maskTexture = null;
@@ -58838,42 +59322,108 @@ void main() {
58838
59322
  return undefined;
58839
59323
  }
58840
59324
  /*
58841
- * Samples terrain across the extent once and packs it into a texture the vertex shader reads.
58842
- *
58843
- * A texture rather than heights baked into the meshes, because the samples arrive
58844
- * asynchronously and every cached tile mesh would otherwise have to be rebuilt when they land.
59325
+ * The extent centre's frame in eye coordinates, in double precision each frame, for the draped fragment shader.
59326
+ */
59327
+ updateDrapeFrame(frameState) {
59328
+ const e = this.extent;
59329
+ const lon = Cesium.Math.toRadians((e.West + e.East) / 2);
59330
+ const lat = Cesium.Math.toRadians((e.North + e.South) / 2);
59331
+ const centre = Cesium.Cartesian3.fromRadians(lon, lat, 0);
59332
+ const enu = Cesium.Transforms.eastNorthUpToFixedFrame(centre);
59333
+ const view = frameState.camera.viewMatrix;
59334
+ const f = this.drapeFrame;
59335
+ Cesium.Matrix4.multiplyByPoint(view, centre, f.centreEC);
59336
+ const axis = new Cesium.Cartesian3();
59337
+ const columns = [f.eastEC, f.northEC, f.upEC];
59338
+ for (let i = 0; i < 3; i++) {
59339
+ Cesium.Matrix4.getColumn(enu, i, axis);
59340
+ Cesium.Matrix4.multiplyByPointAsVector(view, axis, columns[i]);
59341
+ Cesium.Cartesian3.normalize(columns[i], columns[i]);
59342
+ }
59343
+ const sin = Math.sin(lat);
59344
+ const w = 1 - WGS84_E2 * sin * sin;
59345
+ f.geo = new Cesium.Cartesian4(lon, lat, WGS84_A * (1 - WGS84_E2) / Math.pow(w, 1.5), WGS84_A / Math.sqrt(w));
59346
+ f.extent = new Cesium.Cartesian4(Cesium.Math.toRadians(e.West), Cesium.Math.toRadians(e.East), Cesium.Math.toRadians(e.North), Cesium.Math.toRadians(e.South));
59347
+ }
59348
+ /*
59349
+ * Measures a shown ground tileset such as Google's against the ground raster near the camera.
58845
59350
  */
58846
- requestGround() {
58847
- if (this.groundRequested || !this.followGround) {
59351
+ probeGroundOffset(frameState) {
59352
+ const raster = this.groundRaster;
59353
+ const scene = this.scene;
59354
+ if (!raster || !raster.HasCoarse() || !scene || this.offsetProbe.busy || typeof scene.sampleHeightMostDetailed !== "function") {
58848
59355
  return;
58849
59356
  }
58850
- this.groundRequested = true;
58851
- const sampler = Cesium.sampleTerrain;
58852
- if (!this.terrainProvider || typeof sampler !== "function") {
59357
+ const eye = frameState.camera.positionWC;
59358
+ if (this.offsetProbe.at && Cesium.Cartesian3.distance(this.offsetProbe.at, eye) < OFFSET_REPROBE_METRES) {
58853
59359
  return;
58854
59360
  }
58855
- const positions = [];
58856
- for (let row = 0; row < GROUND_SAMPLES_PER_SIDE; row++) {
58857
- const fy = row / (GROUND_SAMPLES_PER_SIDE - 1);
58858
- const latitude = Cesium.Math.lerp(this.extent.North, this.extent.South, fy);
58859
- for (let col = 0; col < GROUND_SAMPLES_PER_SIDE; col++) {
58860
- const fx = col / (GROUND_SAMPLES_PER_SIDE - 1);
58861
- const longitude = Cesium.Math.lerp(this.extent.West, this.extent.East, fx);
58862
- positions.push(Cesium.Cartographic.fromDegrees(longitude, latitude));
59361
+ this.offsetProbe.at = Cesium.Cartesian3.clone(eye);
59362
+ const tileset = findGroundTileset(scene.primitives);
59363
+ if (!tileset || !scene.sampleHeightSupported) {
59364
+ this.groundOffset = 0;
59365
+ return;
59366
+ }
59367
+ const e = this.extent;
59368
+ const camera = Cesium.Cartographic.fromCartesian(eye);
59369
+ if (!camera) {
59370
+ return;
59371
+ }
59372
+ const toDegrees = 180 / (Math.PI * WGS84_A);
59373
+ const latStep = OFFSET_PROBE_SPACING_METRES * toDegrees;
59374
+ const lonStep = latStep / Math.max(0.1, Math.cos(camera.latitude));
59375
+ const lon0 = Math.min(e.East, Math.max(e.West, Cesium.Math.toDegrees(camera.longitude)));
59376
+ const lat0 = Math.min(e.North, Math.max(e.South, Cesium.Math.toDegrees(camera.latitude)));
59377
+ const probes = [];
59378
+ const half = (OFFSET_PROBES_PER_SIDE - 1) / 2;
59379
+ for (let row = 0; row < OFFSET_PROBES_PER_SIDE; row++) {
59380
+ for (let col = 0; col < OFFSET_PROBES_PER_SIDE; col++) {
59381
+ const lon = Math.min(e.East, Math.max(e.West, lon0 + (col - half) * lonStep));
59382
+ const lat = Math.min(e.North, Math.max(e.South, lat0 + (row - half) * latStep));
59383
+ probes.push(Cesium.Cartographic.fromDegrees(lon, lat));
58863
59384
  }
58864
59385
  }
58865
- Promise.resolve(sampler(this.terrainProvider, GROUND_SAMPLE_LEVEL, positions))
58866
- .then((sampled) => {
58867
- if (this.destroyed || !sampled) {
59386
+ this.offsetProbe.busy = true;
59387
+ Promise.resolve(scene.sampleHeightMostDetailed(probes, [this]))
59388
+ .then((drawn) => {
59389
+ if (this.destroyed || this.groundRaster !== raster) {
58868
59390
  return;
58869
59391
  }
58870
- this.groundPixels = packHeights(sampled);
59392
+ const differences = (drawn || []).map((c) => {
59393
+ const u = (Cesium.Math.toDegrees(c.longitude) - e.West) / (e.East - e.West);
59394
+ const v = (e.North - Cesium.Math.toDegrees(c.latitude)) / (e.North - e.South);
59395
+ return c && isFinite(c.height) ? c.height - raster.HeightAt(u, v) : NaN;
59396
+ });
59397
+ const offset = GroundOffsetFrom(differences);
59398
+ if (offset !== null) {
59399
+ this.groundOffset = offset;
59400
+ }
58871
59401
  })
58872
59402
  .catch(() => {
58873
- // Terrain that cannot be sampled leaves the surface on the ellipsoid, which is
58874
- // where a clamped polygon sat before this existed.
59403
+ // Unmeasured, the sheet keeps its last offset.
59404
+ })
59405
+ .then(() => {
59406
+ this.offsetProbe.busy = false;
58875
59407
  });
58876
59408
  }
59409
+ /*
59410
+ * Starts the ground raster a clamped sheet stands on, and refines it near the camera each frame.
59411
+ */
59412
+ requestGround(frameState) {
59413
+ if (!this.followGround || !this.terrainProvider || !this.sourceDims) {
59414
+ return;
59415
+ }
59416
+ if (!this.groundRaster) {
59417
+ const side = this.groundRasterSide || GroundRaster.SideFor(this.sourceDims.width, this.sourceDims.height);
59418
+ this.groundRaster = new GroundRaster.Raster(this.extent, side, terrainSampler(this.terrainProvider, this.extent));
59419
+ this.groundRaster.StartCoarse();
59420
+ return;
59421
+ }
59422
+ const culling = frameState.cullingVolume;
59423
+ this.groundRaster.Refine(frameState.camera.positionWC, culling
59424
+ ? (sphere) => culling.computeVisibility(sphere) !== Cesium.Intersect.OUTSIDE
59425
+ : undefined);
59426
+ }
58877
59427
  /*
58878
59428
  * Samples the terrain under the clearance regions.
58879
59429
  *
@@ -58902,10 +59452,10 @@ void main() {
58902
59452
  // does not cover are never asked for at all.
58903
59453
  const slots = [];
58904
59454
  for (let row = 0; row < MASK_GROUND_SAMPLES_PER_SIDE; row++) {
58905
- const fy = row / (MASK_GROUND_SAMPLES_PER_SIDE - 1);
59455
+ const fy = (row + 0.5) / MASK_GROUND_SAMPLES_PER_SIDE;
58906
59456
  const latitude = Cesium.Math.lerp(box.North, box.South, fy);
58907
59457
  for (let col = 0; col < MASK_GROUND_SAMPLES_PER_SIDE; col++) {
58908
- const fx = col / (MASK_GROUND_SAMPLES_PER_SIDE - 1);
59458
+ const fx = (col + 0.5) / MASK_GROUND_SAMPLES_PER_SIDE;
58909
59459
  const at = Cesium.Cartographic.fromDegrees(Cesium.Math.lerp(box.West, box.East, fx), latitude);
58910
59460
  // An imported terrain covers a survey area, so asking outside it answers 404 and
58911
59461
  // fills the console with failures for points that were never going to have a height.
@@ -58989,27 +59539,48 @@ void main() {
58989
59539
  * shader always has a texture bound whether or not this surface follows the ground.
58990
59540
  */
58991
59541
  syncGroundTexture(context) {
58992
- if (this.groundTexture && (!this.groundPixels || this.groundTexture.width === GROUND_SAMPLES_PER_SIDE)) {
59542
+ const raster = this.hasGround() ? this.groundRaster : null;
59543
+ const side = raster ? raster.side : 1;
59544
+ if (!this.groundTexture || this.groundTextureSide !== side) {
59545
+ if (this.groundTexture) {
59546
+ this.groundTexture.destroy();
59547
+ }
59548
+ const stand = new Uint8Array(4);
59549
+ GroundRaster.Pack(0, stand, 0);
59550
+ this.groundTexture = new Cesium.Texture({
59551
+ context,
59552
+ pixelFormat: Cesium.PixelFormat.RGBA,
59553
+ source: { width: side, height: side, arrayBufferView: raster ? raster.pixels : stand },
59554
+ flipY: false,
59555
+ sampler: new Cesium.Sampler({
59556
+ wrapS: Cesium.TextureWrap.CLAMP_TO_EDGE,
59557
+ wrapT: Cesium.TextureWrap.CLAMP_TO_EDGE,
59558
+ // Nearest, because the shader interpolates decoded heights itself.
59559
+ minificationFilter: Cesium.TextureMinificationFilter.NEAREST,
59560
+ magnificationFilter: Cesium.TextureMagnificationFilter.NEAREST
59561
+ })
59562
+ });
59563
+ this.groundTextureSide = side;
59564
+ if (raster) {
59565
+ raster.TakeDirty();
59566
+ }
58993
59567
  return;
58994
59568
  }
58995
- if (this.groundTexture) {
58996
- this.groundTexture.destroy();
59569
+ if (!raster) {
59570
+ return;
59571
+ }
59572
+ for (const region of raster.TakeDirty()) {
59573
+ const block = new Uint8Array(region.width * region.height * 4);
59574
+ for (let row = 0; row < region.height; row++) {
59575
+ const from = ((region.y + row) * side + region.x) * 4;
59576
+ block.set(raster.pixels.subarray(from, from + region.width * 4), row * region.width * 4);
59577
+ }
59578
+ this.groundTexture.copyFrom({
59579
+ source: { width: region.width, height: region.height, arrayBufferView: block },
59580
+ xOffset: region.x,
59581
+ yOffset: region.y
59582
+ });
58997
59583
  }
58998
- const size = this.groundPixels ? GROUND_SAMPLES_PER_SIDE : 1;
58999
- const arrayBufferView = this.groundPixels || packHeights([{ height: 0 }]);
59000
- this.groundTexture = new Cesium.Texture({
59001
- context,
59002
- pixelFormat: Cesium.PixelFormat.RGBA,
59003
- source: { width: size, height: size, arrayBufferView },
59004
- flipY: false,
59005
- sampler: new Cesium.Sampler({
59006
- wrapS: Cesium.TextureWrap.CLAMP_TO_EDGE,
59007
- wrapT: Cesium.TextureWrap.CLAMP_TO_EDGE,
59008
- // Nearest, because the shader interpolates decoded heights itself.
59009
- minificationFilter: Cesium.TextureMinificationFilter.NEAREST,
59010
- magnificationFilter: Cesium.TextureMagnificationFilter.NEAREST
59011
- })
59012
- });
59013
59584
  }
59014
59585
  /*
59015
59586
  * Metres the extent spans west to east and north to south, which is what turns the shader's
@@ -59330,9 +59901,9 @@ void main() {
59330
59901
  u_valueFade: () => (self.fade && self.fade.from ? self.fade.mix : 1),
59331
59902
  u_groundTexture: () => self.groundTexture,
59332
59903
  u_valuePacked: () => (self.packedValue ? 1 : 0),
59333
- u_groundWeight: () => (self.followGround && self.groundPixels ? 1 : 0),
59904
+ u_groundWeight: () => (self.hasGround() ? 1 : 0),
59334
59905
  u_groundRange: () => new Cesium.Cartesian2(GROUND_MIN_METRES, GROUND_MAX_METRES),
59335
- u_groundSize: () => new Cesium.Cartesian2(self.groundPixels ? GROUND_SAMPLES_PER_SIDE : 1, self.groundPixels ? GROUND_SAMPLES_PER_SIDE : 1),
59906
+ u_groundSize: () => new Cesium.Cartesian2(self.groundTextureSide || 1, self.groundTextureSide || 1),
59336
59907
  u_maskTexture: () => self.maskTexture,
59337
59908
  u_maskGroundTexture: () => self.maskGroundTexture,
59338
59909
  u_maskWeight: () => (self.groundClearance && self.maskPixels ? 1 : 0),
@@ -59391,7 +59962,17 @@ void main() {
59391
59962
  u_wavePhaseB: () => self.wavePhase(4),
59392
59963
  u_wavePeriod: () => self.wavePeriod(),
59393
59964
  u_waveOrigin: () => self.waveOrigin,
59394
- u_extentMetres: () => self.extentMetres()
59965
+ u_extentMetres: () => self.extentMetres(),
59966
+ u_drape: () => (self.drapeActive ? 1 : 0),
59967
+ u_drapeLift: () => self.drapeLift,
59968
+ u_drapeBand: () => self.drapeBand,
59969
+ u_groundOffset: () => self.groundOffset,
59970
+ u_drapeCentreEC: () => self.drapeFrame.centreEC,
59971
+ u_drapeEastEC: () => self.drapeFrame.eastEC,
59972
+ u_drapeNorthEC: () => self.drapeFrame.northEC,
59973
+ u_drapeUpEC: () => self.drapeFrame.upEC,
59974
+ u_drapeGeo: () => self.drapeFrame.geo,
59975
+ u_extentRadians: () => self.drapeFrame.extent
59395
59976
  }
59396
59977
  });
59397
59978
  }
@@ -60320,6 +60901,7 @@ void main() {
60320
60901
  baseHeight: placement.baseHeight,
60321
60902
  followGround: placement.followGround,
60322
60903
  terrainProvider: viewer && viewer.terrainProvider,
60904
+ scene: viewer && viewer.scene,
60323
60905
  valueMin: extrusionArchive.metadata.ValueMin,
60324
60906
  valueMax: extrusionArchive.metadata.ValueMax,
60325
60907
  packedValue: exports.TextureFrameSeriesAnimator.IsPackedValue(extrusionArchive.metadata),
@@ -62314,7 +62896,7 @@ void main() {
62314
62896
  StyleUtils.ApplyTypeStyle = ApplyTypeStyle;
62315
62897
  })(exports.StyleUtils || (exports.StyleUtils = {}));
62316
62898
 
62317
- const VERSION = "7.9.5";
62899
+ const VERSION = "7.9.6";
62318
62900
  /**
62319
62901
  * Updates the environment instance used by bruce-cesium to one specified.
62320
62902
  * This can be used to ensure that the instance a parent is referencing is shared between bruce-cesium, bruce-models, and the parent app.