signalk-livedepth-plugin 0.3.3 → 0.3.11

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.
@@ -1,8 +1,9 @@
1
1
  // src/web/shared/bathymetry.ts
2
2
  var DEFAULT_COVERAGE_CELLS = 2;
3
3
  var DEFAULT_COVERAGE_MIN_M = 2;
4
- var DEFAULT_IDW_POWER = 2;
4
+ var DEFAULT_IDW_POWER = 1;
5
5
  var DEFAULT_SHALLOW_BIAS = 0.5;
6
+ var DEFAULT_CONTOUR_M = 2;
6
7
  var DEFAULT_MAX_CELLS = 1024;
7
8
  var EARTH_RADIUS = 6378137;
8
9
  var MERCATOR_MAX_LAT = 85.05112878;
@@ -38,6 +39,7 @@ function createDepthFieldBuilder(extent, options) {
38
39
  0,
39
40
  Math.min(1, options.shallowBias ?? DEFAULT_SHALLOW_BIAS)
40
41
  );
42
+ const contourMetres = options.contourMetres ?? DEFAULT_CONTOUR_M;
41
43
  const maxCells = options.maxCells ?? DEFAULT_MAX_CELLS;
42
44
  const width = Math.min(
43
45
  maxCells,
@@ -82,10 +84,12 @@ function createDepthFieldBuilder(extent, options) {
82
84
  }
83
85
  const idw = wdsum[i] / w;
84
86
  const shallowest = minSeen[i];
85
- depth[i] = (1 - shallowBias) * idw + shallowBias * Math.min(idw, shallowest);
87
+ const blended = (1 - shallowBias) * idw + shallowBias * Math.min(idw, shallowest);
88
+ depth[i] = Math.min(blended, shallowest + contourMetres);
86
89
  }
87
90
  }
88
91
  }
92
+ const isUsable = (s) => Number.isFinite(s.lon) && Number.isFinite(s.lat) && Number.isFinite(s.depth) && s.depth >= 0;
89
93
  function add(samples2) {
90
94
  let rect = null;
91
95
  const grow = (gx, gy) => {
@@ -99,7 +103,7 @@ function createDepthFieldBuilder(extent, options) {
99
103
  if (gy + 1 > rect[3]) rect[3] = gy + 1;
100
104
  };
101
105
  for (const s of samples2) {
102
- if (!Number.isFinite(s.lon) || !Number.isFinite(s.lat) || !Number.isFinite(s.depth) || s.depth < 0) {
106
+ if (!isUsable(s)) {
103
107
  continue;
104
108
  }
105
109
  const sx = mercX(s.lon);
@@ -125,7 +129,7 @@ function createDepthFieldBuilder(extent, options) {
125
129
  continue;
126
130
  }
127
131
  const idx = gy * width + gx;
128
- const w = 1 / (Math.pow(dist2, power / 2) + 1e-6);
132
+ const w = Math.pow(1 - Math.sqrt(dist2) / coverageRadius, power);
129
133
  wsum[idx] += w;
130
134
  wdsum[idx] += w * s.depth;
131
135
  if (s.depth < minSeen[idx]) {
@@ -159,6 +163,7 @@ uniform float uShadeLimit;
159
163
  uniform int uLevels;
160
164
  uniform float uInterval;
161
165
  uniform float uLineWidth;
166
+ uniform float uIsobars; // 1 = per-fragment isobars, 0 = vector contours drawn by the runtime
162
167
  bool uncov(float v) { return v < 0.0; } // -1: no coverage
163
168
  float sampleDepth(vec2 uv) {
164
169
  vec2 fc = uv / uTexel - 0.5;
@@ -189,16 +194,87 @@ void main() {
189
194
  float tCenter = (band + 0.5) / float(uLevels);
190
195
  fill = texture(uLut, vec2(clamp(tCenter, 0.0, 1.0), 0.5)).rgb;
191
196
  }
192
- // Isobars at every absolute multiple of the interval, over ALL data.
193
- float ci = d / uInterval;
194
- float dv = min(fract(ci), 1.0 - fract(ci));
195
- float px = dv / max(fwidth(ci), 1e-6);
196
- float line = 1.0 - smoothstep(uLineWidth, uLineWidth + 1.5, px);
197
+ // Isobars at every absolute multiple of the interval, over ALL data. The
198
+ // runtime can disable these and draw vector contours instead.
199
+ float line = 0.0;
200
+ if (uIsobars > 0.5) {
201
+ float ci = d / uInterval;
202
+ float dv = min(fract(ci), 1.0 - fract(ci));
203
+ float px = dv / max(fwidth(ci), 1e-6);
204
+ line = 1.0 - smoothstep(uLineWidth, uLineWidth + 1.5, px);
205
+ }
197
206
  vec3 col = mix(fill, vec3(0.0), line);
198
207
  float alpha = max(line, shaded ? 1.0 : 0.0);
199
208
  fragColor = vec4(col, alpha);
200
209
  }`;
201
210
 
211
+ // src/web/shared/contours.ts
212
+ function onGrid(level, step) {
213
+ const n = level / step;
214
+ return Math.abs(n - Math.round(n)) < 1e-6;
215
+ }
216
+ function extractContours(field, minorStep, majorStep, maxLevel) {
217
+ const { width, height, depth, extent } = field;
218
+ const result = { major: [], minor: [] };
219
+ const minor = minorStep > 0 ? minorStep : 1;
220
+ const major = majorStep > 0 ? majorStep : minor;
221
+ if (width < 2 || height < 2) return result;
222
+ const cell = (extent[2] - extent[0]) / width;
223
+ const at = (gx, gy) => depth[gy * width + gx];
224
+ const ox = [0, 1, 1, 0];
225
+ const oy = [0, 0, 1, 1];
226
+ const step = Math.min(minor, major);
227
+ for (let gy = 0; gy < height - 1; gy++) {
228
+ for (let gx = 0; gx < width - 1; gx++) {
229
+ const d = [
230
+ at(gx, gy),
231
+ at(gx + 1, gy),
232
+ at(gx + 1, gy + 1),
233
+ at(gx, gy + 1)
234
+ ];
235
+ if (!Number.isFinite(d[0]) || !Number.isFinite(d[1]) || !Number.isFinite(d[2]) || !Number.isFinite(d[3])) {
236
+ continue;
237
+ }
238
+ const lo = Math.min(d[0], d[1], d[2], d[3]);
239
+ const hi = Math.max(d[0], d[1], d[2], d[3]);
240
+ if (hi <= lo) continue;
241
+ const first = Math.floor(lo / step) * step + step;
242
+ for (let level = first; level < hi && level <= maxLevel; level += step) {
243
+ if (level <= lo) continue;
244
+ const out = onGrid(level, major) ? result.major : result.minor;
245
+ const cross = [null, null, null, null];
246
+ const crossed = [];
247
+ for (let i = 0; i < 4; i++) {
248
+ const j = (i + 1) % 4;
249
+ if (d[i] >= level === d[j] >= level) {
250
+ continue;
251
+ }
252
+ const t = (level - d[i]) / (d[j] - d[i]);
253
+ cross[i] = [
254
+ extent[0] + (gx + ox[i] + (ox[j] - ox[i]) * t) * cell,
255
+ extent[1] + (gy + oy[i] + (oy[j] - oy[i]) * t) * cell
256
+ ];
257
+ crossed.push(i);
258
+ }
259
+ if (crossed.length === 2) {
260
+ const [a, b] = crossed;
261
+ out.push([...cross[a], ...cross[b]]);
262
+ } else if (crossed.length === 4) {
263
+ const centre = (d[0] + d[1] + d[2] + d[3]) / 4;
264
+ if (centre >= level) {
265
+ out.push([...cross[0], ...cross[1]]);
266
+ out.push([...cross[2], ...cross[3]]);
267
+ } else {
268
+ out.push([...cross[0], ...cross[3]]);
269
+ out.push([...cross[1], ...cross[2]]);
270
+ }
271
+ }
272
+ }
273
+ }
274
+ }
275
+ return result;
276
+ }
277
+
202
278
  // src/web/shared/live-depth.ts
203
279
  var DEFAULT_SHADE_LIMIT_M = 10;
204
280
  var EARTH_RADIUS2 = 6378137;
@@ -218,11 +294,11 @@ function latOf(y) {
218
294
  return (2 * Math.atan(Math.exp(y / EARTH_RADIUS2)) - Math.PI / 2) / RAD2;
219
295
  }
220
296
  var SHADE_RAMP = [
221
- [0, 26, 132, 216],
222
- [0.3, 96, 178, 232],
223
- [0.55, 156, 209, 240],
224
- [0.78, 205, 232, 248],
225
- [1, 255, 255, 255]
297
+ [0, 37, 175, 248],
298
+ [0.25, 143, 215, 248],
299
+ [0.5, 167, 223, 248],
300
+ [0.75, 183, 223, 248],
301
+ [1, 248, 248, 248]
226
302
  ];
227
303
  var SHADE_CONTOUR_LEVELS = 6;
228
304
  function shadeColor(t) {
@@ -413,11 +489,18 @@ var liveSamples = [];
413
489
  var settings = null;
414
490
  var builder = null;
415
491
  var fieldDirty = true;
492
+ var fieldVersion = 0;
493
+ var surfaceCache = null;
494
+ var overlayCache = null;
495
+ var overlayCanvas = null;
496
+ var overlayCtx = null;
416
497
  var vessel = { position: null };
417
498
  var depthUnit = "m";
418
499
  var samplesVersion = 0;
419
500
  var shoalCache = null;
420
501
  var clusterCache = null;
502
+ var contours = { major: [], minor: [] };
503
+ var gpuMsaa = true;
421
504
  var AUTO_FRAME_BUDGET_MS = 10;
422
505
  var AUTO_ADJUST_EVERY = 30;
423
506
  var PERF_REPORT_EVERY = 20;
@@ -426,6 +509,7 @@ var frameAvgMs = 0;
426
509
  var framesSinceAdjust = 0;
427
510
  var perfFrames = 0;
428
511
  function renderScale() {
512
+ if (settings?.fullResolution) return 1;
429
513
  const quality = settings?.quality ?? "auto";
430
514
  return quality === "auto" ? autoScale : QUALITY_SCALE[quality];
431
515
  }
@@ -448,6 +532,7 @@ var uShadeLimit = null;
448
532
  var uLevels = null;
449
533
  var uInterval = null;
450
534
  var uLineWidth = null;
535
+ var uIsobars = null;
451
536
  function compile(ctx, type, src) {
452
537
  const shader = ctx.createShader(type);
453
538
  if (!shader) return null;
@@ -462,11 +547,14 @@ function compile(ctx, type, src) {
462
547
  function initWebgl() {
463
548
  if (gl) return true;
464
549
  glCanvas = new OffscreenCanvas(1, 1);
550
+ const msaa = settings?.msaa ?? true;
465
551
  const ctx = glCanvas.getContext("webgl2", {
466
552
  alpha: true,
467
- premultipliedAlpha: true
553
+ premultipliedAlpha: true,
554
+ antialias: msaa
468
555
  });
469
556
  if (!ctx) return false;
557
+ gpuMsaa = msaa;
470
558
  const vs = compile(ctx, ctx.VERTEX_SHADER, DEPTH_VERT_SRC);
471
559
  const fs = compile(ctx, ctx.FRAGMENT_SHADER, DEPTH_FRAG_SRC);
472
560
  const prog = ctx.createProgram();
@@ -487,6 +575,7 @@ function initWebgl() {
487
575
  uLevels = ctx.getUniformLocation(prog, "uLevels");
488
576
  uInterval = ctx.getUniformLocation(prog, "uInterval");
489
577
  uLineWidth = ctx.getUniformLocation(prog, "uLineWidth");
578
+ uIsobars = ctx.getUniformLocation(prog, "uIsobars");
490
579
  quad = ctx.createBuffer();
491
580
  ctx.bindBuffer(ctx.ARRAY_BUFFER, quad);
492
581
  ctx.bufferData(
@@ -537,6 +626,12 @@ function releaseGpu() {
537
626
  depthTexture = null;
538
627
  lutTexture = null;
539
628
  glCanvas = null;
629
+ surfaceCache?.image.close();
630
+ surfaceCache = null;
631
+ overlayCache?.image.close();
632
+ overlayCache = null;
633
+ overlayCanvas = null;
634
+ overlayCtx = null;
540
635
  }
541
636
  function uploadField(f) {
542
637
  if (!gl || !depthTexture) return;
@@ -558,6 +653,7 @@ function uploadField(f) {
558
653
  gl.FLOAT,
559
654
  gpu
560
655
  );
656
+ fieldVersion++;
561
657
  }
562
658
  function uploadRegion(f, [x0, y0, x1, y1]) {
563
659
  if (!gl || !depthTexture) return;
@@ -576,6 +672,7 @@ function uploadRegion(f, [x0, y0, x1, y1]) {
576
672
  gl.activeTexture(gl.TEXTURE0);
577
673
  gl.bindTexture(gl.TEXTURE_2D, depthTexture);
578
674
  gl.texSubImage2D(gl.TEXTURE_2D, 0, x0, y0, w, h, gl.RED, gl.FLOAT, gpu);
675
+ fieldVersion++;
579
676
  }
580
677
  function rebuildField() {
581
678
  builder = null;
@@ -606,10 +703,17 @@ function rebuildField() {
606
703
  const tiles = settings.source === "tiles";
607
704
  const gap = tiles ? 0 : medianConsecutiveGap(source);
608
705
  const minCover = tiles ? 0 : DEFAULT_COVERAGE_MIN_M;
609
- const cell = Math.max(gap, extMax / MAX_FIELD_CELLS, minCover / 8, 0.01);
706
+ const maxCells = Math.max(
707
+ 64,
708
+ Math.round(settings.fieldCells) || MAX_FIELD_CELLS
709
+ );
710
+ const cell = Math.max(gap, extMax / maxCells, minCover / 8, 0.01);
610
711
  const next = createDepthFieldBuilder(extent, {
611
712
  cellMetres: cell,
612
- coverageMetres: Math.max(cell * 1.5, gap * 1.5),
713
+ maxCells,
714
+ // `gap` bridges the along-track gaps, the fill radius widens a sounding
715
+ // into empty water, and a sounding always covers its own cell.
716
+ coverageMetres: Math.max(cell * 1.5, gap * 1.5, settings.fillMetres),
613
717
  coverageMinMetres: minCover
614
718
  });
615
719
  if (!next) return;
@@ -617,6 +721,12 @@ function rebuildField() {
617
721
  if (liveSamples.length) next.add(liveSamples);
618
722
  builder = next;
619
723
  uploadField(next.field);
724
+ contours = settings.vectorContours ? extractContours(
725
+ next.field,
726
+ Math.max(0.01, settings.minorContourMetres),
727
+ Math.max(0.01, settings.contourIntervalMetres),
728
+ Math.max(0, settings.maxDepthMetres)
729
+ ) : { major: [], minor: [] };
620
730
  }
621
731
  function addLive(newSamples) {
622
732
  liveSamples.push(...newSamples);
@@ -663,11 +773,45 @@ function renderGpu(box, canvasW, canvasH, ratio) {
663
773
  gl.uniform1i(uLevels, SHADE_CONTOUR_LEVELS);
664
774
  gl.uniform1f(uInterval, Math.max(0.01, settings?.contourIntervalMetres ?? 1));
665
775
  gl.uniform1f(uLineWidth, Math.max(1, ratio));
776
+ gl.uniform1f(uIsobars, settings?.vectorContours ? 0 : 1);
666
777
  gl.bindBuffer(gl.ARRAY_BUFFER, quad);
667
778
  gl.enableVertexAttribArray(aPos);
668
779
  gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);
669
780
  gl.drawArrays(gl.TRIANGLES, 0, 6);
670
781
  }
782
+ function drawContours(ctx, box, canvasW, canvasH, ratio) {
783
+ if (!settings?.vectorContours) return;
784
+ const f = builder?.field;
785
+ if (!f || contours.major.length === 0 && contours.minor.length === 0) {
786
+ return;
787
+ }
788
+ const boxW = box[2] - box[0];
789
+ const boxH = box[3] - box[1];
790
+ if (!(boxW > 0) || !(boxH > 0)) return;
791
+ const ext = f.extent;
792
+ const shift = Math.round(((ext[0] + ext[2]) / 2 - (box[0] + box[2]) / 2) / WORLD_WIDTH) * WORLD_WIDTH;
793
+ const px = (x) => (x + shift - box[0]) / boxW * canvasW;
794
+ const py = (y) => (box[3] - y) / boxH * canvasH;
795
+ const stroke = (segments, color, widthPx) => {
796
+ if (segments.length === 0) return;
797
+ ctx.beginPath();
798
+ for (const [x0, y0, x1, y1] of segments) {
799
+ ctx.moveTo(px(x0), py(y0));
800
+ ctx.lineTo(px(x1), py(y1));
801
+ }
802
+ ctx.lineWidth = Math.max(0.5, widthPx * ratio);
803
+ ctx.lineCap = "round";
804
+ ctx.lineJoin = "round";
805
+ ctx.strokeStyle = color;
806
+ ctx.stroke();
807
+ };
808
+ stroke(
809
+ contours.minor,
810
+ settings.minorContourColor,
811
+ settings.minorContourWidthPx
812
+ );
813
+ stroke(contours.major, settings.majorContourColor, settings.contourWidthPx);
814
+ }
671
815
  function drawOverlay(ctx, box, canvasW, canvasH, resolution, ratio, zoom) {
672
816
  if (!settings) return;
673
817
  const boxW = box[2] - box[0];
@@ -701,6 +845,7 @@ function drawOverlay(ctx, box, canvasW, canvasH, resolution, ratio, zoom) {
701
845
  ctx.fillStyle = "rgba(0,0,0,0.95)";
702
846
  ctx.fillText(text, p[0], p[1]);
703
847
  };
848
+ drawContours(ctx, box, canvasW, canvasH, ratio);
704
849
  const shoalKey = `${samplesVersion}:${settings.safetyDepthMetres}`;
705
850
  if (!shoalCache || shoalCache.key !== shoalKey) {
706
851
  shoalCache = {
@@ -725,7 +870,6 @@ function drawOverlay(ctx, box, canvasW, canvasH, resolution, ratio, zoom) {
725
870
  const clusters = clusterCache.value;
726
871
  for (const c of clusters) {
727
872
  const p = toPixel(c.lon, c.lat);
728
- dot(p, c.depth);
729
873
  const radiusPx = c.radiusMetres / resolution * ratio;
730
874
  if (radiusPx >= LABEL_MIN_RADIUS_PX) {
731
875
  const fontCss = Math.max(
@@ -733,6 +877,8 @@ function drawOverlay(ctx, box, canvasW, canvasH, resolution, ratio, zoom) {
733
877
  Math.min(LABEL_FONT_MAX, (zoom - 8) * 1.5 + LABEL_FONT_MIN)
734
878
  );
735
879
  label(formatDepth(c.depth), p, fontCss);
880
+ } else {
881
+ dot(p, c.depth);
736
882
  }
737
883
  }
738
884
  const v = vessel;
@@ -794,10 +940,30 @@ function render(req) {
794
940
  return;
795
941
  }
796
942
  ctx.clearRect(0, 0, width, height);
797
- if (builder) {
798
- renderGpu(box, surfaceW, surfaceH, pixelRatio * scale);
943
+ let redrew = false;
944
+ if (builder && glCanvas) {
945
+ const surfaceKey = [
946
+ fieldVersion,
947
+ box.join(","),
948
+ surfaceW,
949
+ surfaceH,
950
+ pixelRatio,
951
+ settings?.shadeLimitMetres,
952
+ settings?.contourIntervalMetres,
953
+ settings?.vectorContours,
954
+ settings?.msaa
955
+ ].join("|");
956
+ if (!surfaceCache || surfaceCache.key !== surfaceKey) {
957
+ renderGpu(box, surfaceW, surfaceH, pixelRatio * scale);
958
+ surfaceCache?.image.close();
959
+ surfaceCache = {
960
+ key: surfaceKey,
961
+ image: glCanvas.transferToImageBitmap()
962
+ };
963
+ redrew = true;
964
+ }
799
965
  ctx.drawImage(
800
- glCanvas,
966
+ surfaceCache.image,
801
967
  0,
802
968
  0,
803
969
  surfaceW,
@@ -808,10 +974,56 @@ function render(req) {
808
974
  height
809
975
  );
810
976
  }
811
- drawOverlay(ctx, box, width, height, resolution, pixelRatio, zoom);
977
+ const overlayKey = [
978
+ fieldVersion,
979
+ samplesVersion,
980
+ box.join(","),
981
+ width,
982
+ height,
983
+ resolution,
984
+ pixelRatio,
985
+ zoom,
986
+ settings?.vectorContours,
987
+ settings?.contourIntervalMetres,
988
+ settings?.minorContourMetres,
989
+ settings?.contourWidthPx,
990
+ settings?.minorContourWidthPx,
991
+ settings?.majorContourColor,
992
+ settings?.minorContourColor,
993
+ settings?.safetyDepthMetres,
994
+ settings?.cellPx,
995
+ settings?.mergeTolerance,
996
+ settings?.maxDepthMetres,
997
+ settings?.shadeLimitMetres
998
+ ].join("|");
999
+ if (!overlayCache || overlayCache.key !== overlayKey) {
1000
+ if (!overlayCanvas || !overlayCtx || overlayCanvas.width !== width || overlayCanvas.height !== height) {
1001
+ overlayCanvas = new OffscreenCanvas(width, height);
1002
+ overlayCtx = overlayCanvas.getContext("2d");
1003
+ }
1004
+ if (overlayCtx) {
1005
+ overlayCtx.clearRect(0, 0, width, height);
1006
+ drawOverlay(
1007
+ overlayCtx,
1008
+ box,
1009
+ width,
1010
+ height,
1011
+ resolution,
1012
+ pixelRatio,
1013
+ zoom
1014
+ );
1015
+ overlayCache?.image.close();
1016
+ overlayCache = {
1017
+ key: overlayKey,
1018
+ image: overlayCanvas.transferToImageBitmap()
1019
+ };
1020
+ redrew = true;
1021
+ }
1022
+ }
1023
+ if (overlayCache) ctx.drawImage(overlayCache.image, 0, 0);
812
1024
  const image = out.transferToImageBitmap();
813
1025
  const frameMs = performance.now() - started;
814
- if ((settings?.quality ?? "auto") === "auto") {
1026
+ if (redrew && (settings?.quality ?? "auto") === "auto") {
815
1027
  frameAvgMs = frameAvgMs === 0 ? frameMs : frameAvgMs * 0.8 + frameMs * 0.2;
816
1028
  if (++framesSinceAdjust >= AUTO_ADJUST_EVERY) {
817
1029
  framesSinceAdjust = 0;
@@ -848,10 +1060,12 @@ self.onmessage = (e) => {
848
1060
  case "vessel":
849
1061
  vessel = msg;
850
1062
  break;
851
- case "settings":
1063
+ case "settings": {
1064
+ if (msg.settings.msaa !== gpuMsaa) releaseGpu();
852
1065
  settings = msg.settings;
853
1066
  fieldDirty = true;
854
1067
  break;
1068
+ }
855
1069
  case "units":
856
1070
  depthUnit = msg.depth;
857
1071
  break;