signalk-livedepth-plugin 0.3.4 → 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.
@@ -163,6 +163,7 @@ uniform float uShadeLimit;
163
163
  uniform int uLevels;
164
164
  uniform float uInterval;
165
165
  uniform float uLineWidth;
166
+ uniform float uIsobars; // 1 = per-fragment isobars, 0 = vector contours drawn by the runtime
166
167
  bool uncov(float v) { return v < 0.0; } // -1: no coverage
167
168
  float sampleDepth(vec2 uv) {
168
169
  vec2 fc = uv / uTexel - 0.5;
@@ -193,16 +194,87 @@ void main() {
193
194
  float tCenter = (band + 0.5) / float(uLevels);
194
195
  fill = texture(uLut, vec2(clamp(tCenter, 0.0, 1.0), 0.5)).rgb;
195
196
  }
196
- // Isobars at every absolute multiple of the interval, over ALL data.
197
- float ci = d / uInterval;
198
- float dv = min(fract(ci), 1.0 - fract(ci));
199
- float px = dv / max(fwidth(ci), 1e-6);
200
- 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
+ }
201
206
  vec3 col = mix(fill, vec3(0.0), line);
202
207
  float alpha = max(line, shaded ? 1.0 : 0.0);
203
208
  fragColor = vec4(col, alpha);
204
209
  }`;
205
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
+
206
278
  // src/web/shared/live-depth.ts
207
279
  var DEFAULT_SHADE_LIMIT_M = 10;
208
280
  var EARTH_RADIUS2 = 6378137;
@@ -222,11 +294,11 @@ function latOf(y) {
222
294
  return (2 * Math.atan(Math.exp(y / EARTH_RADIUS2)) - Math.PI / 2) / RAD2;
223
295
  }
224
296
  var SHADE_RAMP = [
225
- [0, 26, 132, 216],
226
- [0.3, 96, 178, 232],
227
- [0.55, 156, 209, 240],
228
- [0.78, 205, 232, 248],
229
- [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]
230
302
  ];
231
303
  var SHADE_CONTOUR_LEVELS = 6;
232
304
  function shadeColor(t) {
@@ -417,11 +489,18 @@ var liveSamples = [];
417
489
  var settings = null;
418
490
  var builder = null;
419
491
  var fieldDirty = true;
492
+ var fieldVersion = 0;
493
+ var surfaceCache = null;
494
+ var overlayCache = null;
495
+ var overlayCanvas = null;
496
+ var overlayCtx = null;
420
497
  var vessel = { position: null };
421
498
  var depthUnit = "m";
422
499
  var samplesVersion = 0;
423
500
  var shoalCache = null;
424
501
  var clusterCache = null;
502
+ var contours = { major: [], minor: [] };
503
+ var gpuMsaa = true;
425
504
  var AUTO_FRAME_BUDGET_MS = 10;
426
505
  var AUTO_ADJUST_EVERY = 30;
427
506
  var PERF_REPORT_EVERY = 20;
@@ -430,6 +509,7 @@ var frameAvgMs = 0;
430
509
  var framesSinceAdjust = 0;
431
510
  var perfFrames = 0;
432
511
  function renderScale() {
512
+ if (settings?.fullResolution) return 1;
433
513
  const quality = settings?.quality ?? "auto";
434
514
  return quality === "auto" ? autoScale : QUALITY_SCALE[quality];
435
515
  }
@@ -452,6 +532,7 @@ var uShadeLimit = null;
452
532
  var uLevels = null;
453
533
  var uInterval = null;
454
534
  var uLineWidth = null;
535
+ var uIsobars = null;
455
536
  function compile(ctx, type, src) {
456
537
  const shader = ctx.createShader(type);
457
538
  if (!shader) return null;
@@ -466,11 +547,14 @@ function compile(ctx, type, src) {
466
547
  function initWebgl() {
467
548
  if (gl) return true;
468
549
  glCanvas = new OffscreenCanvas(1, 1);
550
+ const msaa = settings?.msaa ?? true;
469
551
  const ctx = glCanvas.getContext("webgl2", {
470
552
  alpha: true,
471
- premultipliedAlpha: true
553
+ premultipliedAlpha: true,
554
+ antialias: msaa
472
555
  });
473
556
  if (!ctx) return false;
557
+ gpuMsaa = msaa;
474
558
  const vs = compile(ctx, ctx.VERTEX_SHADER, DEPTH_VERT_SRC);
475
559
  const fs = compile(ctx, ctx.FRAGMENT_SHADER, DEPTH_FRAG_SRC);
476
560
  const prog = ctx.createProgram();
@@ -491,6 +575,7 @@ function initWebgl() {
491
575
  uLevels = ctx.getUniformLocation(prog, "uLevels");
492
576
  uInterval = ctx.getUniformLocation(prog, "uInterval");
493
577
  uLineWidth = ctx.getUniformLocation(prog, "uLineWidth");
578
+ uIsobars = ctx.getUniformLocation(prog, "uIsobars");
494
579
  quad = ctx.createBuffer();
495
580
  ctx.bindBuffer(ctx.ARRAY_BUFFER, quad);
496
581
  ctx.bufferData(
@@ -541,6 +626,12 @@ function releaseGpu() {
541
626
  depthTexture = null;
542
627
  lutTexture = null;
543
628
  glCanvas = null;
629
+ surfaceCache?.image.close();
630
+ surfaceCache = null;
631
+ overlayCache?.image.close();
632
+ overlayCache = null;
633
+ overlayCanvas = null;
634
+ overlayCtx = null;
544
635
  }
545
636
  function uploadField(f) {
546
637
  if (!gl || !depthTexture) return;
@@ -562,6 +653,7 @@ function uploadField(f) {
562
653
  gl.FLOAT,
563
654
  gpu
564
655
  );
656
+ fieldVersion++;
565
657
  }
566
658
  function uploadRegion(f, [x0, y0, x1, y1]) {
567
659
  if (!gl || !depthTexture) return;
@@ -580,6 +672,7 @@ function uploadRegion(f, [x0, y0, x1, y1]) {
580
672
  gl.activeTexture(gl.TEXTURE0);
581
673
  gl.bindTexture(gl.TEXTURE_2D, depthTexture);
582
674
  gl.texSubImage2D(gl.TEXTURE_2D, 0, x0, y0, w, h, gl.RED, gl.FLOAT, gpu);
675
+ fieldVersion++;
583
676
  }
584
677
  function rebuildField() {
585
678
  builder = null;
@@ -610,9 +703,14 @@ function rebuildField() {
610
703
  const tiles = settings.source === "tiles";
611
704
  const gap = tiles ? 0 : medianConsecutiveGap(source);
612
705
  const minCover = tiles ? 0 : DEFAULT_COVERAGE_MIN_M;
613
- 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);
614
711
  const next = createDepthFieldBuilder(extent, {
615
712
  cellMetres: cell,
713
+ maxCells,
616
714
  // `gap` bridges the along-track gaps, the fill radius widens a sounding
617
715
  // into empty water, and a sounding always covers its own cell.
618
716
  coverageMetres: Math.max(cell * 1.5, gap * 1.5, settings.fillMetres),
@@ -623,6 +721,12 @@ function rebuildField() {
623
721
  if (liveSamples.length) next.add(liveSamples);
624
722
  builder = next;
625
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: [] };
626
730
  }
627
731
  function addLive(newSamples) {
628
732
  liveSamples.push(...newSamples);
@@ -669,11 +773,45 @@ function renderGpu(box, canvasW, canvasH, ratio) {
669
773
  gl.uniform1i(uLevels, SHADE_CONTOUR_LEVELS);
670
774
  gl.uniform1f(uInterval, Math.max(0.01, settings?.contourIntervalMetres ?? 1));
671
775
  gl.uniform1f(uLineWidth, Math.max(1, ratio));
776
+ gl.uniform1f(uIsobars, settings?.vectorContours ? 0 : 1);
672
777
  gl.bindBuffer(gl.ARRAY_BUFFER, quad);
673
778
  gl.enableVertexAttribArray(aPos);
674
779
  gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);
675
780
  gl.drawArrays(gl.TRIANGLES, 0, 6);
676
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
+ }
677
815
  function drawOverlay(ctx, box, canvasW, canvasH, resolution, ratio, zoom) {
678
816
  if (!settings) return;
679
817
  const boxW = box[2] - box[0];
@@ -707,6 +845,7 @@ function drawOverlay(ctx, box, canvasW, canvasH, resolution, ratio, zoom) {
707
845
  ctx.fillStyle = "rgba(0,0,0,0.95)";
708
846
  ctx.fillText(text, p[0], p[1]);
709
847
  };
848
+ drawContours(ctx, box, canvasW, canvasH, ratio);
710
849
  const shoalKey = `${samplesVersion}:${settings.safetyDepthMetres}`;
711
850
  if (!shoalCache || shoalCache.key !== shoalKey) {
712
851
  shoalCache = {
@@ -731,7 +870,6 @@ function drawOverlay(ctx, box, canvasW, canvasH, resolution, ratio, zoom) {
731
870
  const clusters = clusterCache.value;
732
871
  for (const c of clusters) {
733
872
  const p = toPixel(c.lon, c.lat);
734
- dot(p, c.depth);
735
873
  const radiusPx = c.radiusMetres / resolution * ratio;
736
874
  if (radiusPx >= LABEL_MIN_RADIUS_PX) {
737
875
  const fontCss = Math.max(
@@ -739,6 +877,8 @@ function drawOverlay(ctx, box, canvasW, canvasH, resolution, ratio, zoom) {
739
877
  Math.min(LABEL_FONT_MAX, (zoom - 8) * 1.5 + LABEL_FONT_MIN)
740
878
  );
741
879
  label(formatDepth(c.depth), p, fontCss);
880
+ } else {
881
+ dot(p, c.depth);
742
882
  }
743
883
  }
744
884
  const v = vessel;
@@ -800,10 +940,30 @@ function render(req) {
800
940
  return;
801
941
  }
802
942
  ctx.clearRect(0, 0, width, height);
803
- if (builder) {
804
- 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
+ }
805
965
  ctx.drawImage(
806
- glCanvas,
966
+ surfaceCache.image,
807
967
  0,
808
968
  0,
809
969
  surfaceW,
@@ -814,10 +974,56 @@ function render(req) {
814
974
  height
815
975
  );
816
976
  }
817
- 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);
818
1024
  const image = out.transferToImageBitmap();
819
1025
  const frameMs = performance.now() - started;
820
- if ((settings?.quality ?? "auto") === "auto") {
1026
+ if (redrew && (settings?.quality ?? "auto") === "auto") {
821
1027
  frameAvgMs = frameAvgMs === 0 ? frameMs : frameAvgMs * 0.8 + frameMs * 0.2;
822
1028
  if (++framesSinceAdjust >= AUTO_ADJUST_EVERY) {
823
1029
  framesSinceAdjust = 0;
@@ -854,10 +1060,12 @@ self.onmessage = (e) => {
854
1060
  case "vessel":
855
1061
  vessel = msg;
856
1062
  break;
857
- case "settings":
1063
+ case "settings": {
1064
+ if (msg.settings.msaa !== gpuMsaa) releaseGpu();
858
1065
  settings = msg.settings;
859
1066
  fieldDirty = true;
860
1067
  break;
1068
+ }
861
1069
  case "units":
862
1070
  depthUnit = msg.depth;
863
1071
  break;