@caoguo/maplibre 0.0.5 → 0.0.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.
package/dist/index.cjs CHANGED
@@ -866,39 +866,54 @@ function projectSimple(lng, lat) {
866
866
  }
867
867
  function buildGlowGeometry(lines, opts = {}) {
868
868
  const passes = glowPasses(opts.passes ?? 4, opts.baseWidth ?? 3);
869
- const stride = 5;
869
+ const stride = 6;
870
870
  const verts = [];
871
871
  const passRanges = [];
872
- for (const pass of passes) {
872
+ const renderGroups = [];
873
+ const groups = [];
874
+ for (const line of lines) {
875
+ const g = line.group ?? "default";
876
+ if (!groups.includes(g)) groups.push(g);
877
+ }
878
+ for (let p = 0; p < passes.length; p++) {
879
+ passes[p];
873
880
  const start = verts.length / stride;
874
- for (const line of lines) {
875
- const pts = line.coordinates.map(([lng, lat]) => projectSimple(lng, lat));
876
- if (pts.length < 2) continue;
877
- for (let i = 0; i < pts.length - 1; i++) {
878
- const a = pts[i];
879
- const b = pts[i + 1];
880
- const dx = b[0] - a[0];
881
- const dy = b[1] - a[1];
882
- const quad = [
883
- [a[0], a[1], dx, dy, 1],
884
- [a[0], a[1], dx, dy, -1],
885
- [b[0], b[1], dx, dy, 1],
886
- [b[0], b[1], dx, dy, -1]
887
- ];
888
- const idx = [0, 1, 2, 2, 1, 3];
889
- for (const k of idx) {
890
- const v = quad[k];
891
- verts.push(v[0], v[1], v[2], v[3], v[4]);
881
+ for (const group of groups) {
882
+ const groupStart = verts.length / stride;
883
+ const groupIndex = groups.indexOf(group);
884
+ for (const line of lines) {
885
+ if ((line.group ?? "default") !== group) continue;
886
+ const pts = line.coordinates.map(([lng, lat]) => projectSimple(lng, lat));
887
+ if (pts.length < 2) continue;
888
+ for (let i = 0; i < pts.length - 1; i++) {
889
+ const a = pts[i];
890
+ const b = pts[i + 1];
891
+ const dx = b[0] - a[0];
892
+ const dy = b[1] - a[1];
893
+ const quad = [
894
+ [a[0], a[1], dx, dy, 1, groupIndex],
895
+ [a[0], a[1], dx, dy, -1, groupIndex],
896
+ [b[0], b[1], dx, dy, 1, groupIndex],
897
+ [b[0], b[1], dx, dy, -1, groupIndex]
898
+ ];
899
+ const idx = [0, 1, 2, 2, 1, 3];
900
+ for (const k of idx) {
901
+ const v = quad[k];
902
+ verts.push(v[0], v[1], v[2], v[3], v[4], v[5]);
903
+ }
892
904
  }
893
905
  }
906
+ const count2 = verts.length / stride - groupStart;
907
+ if (count2 > 0) renderGroups.push({ passIndex: p, group, start: groupStart, count: count2 });
894
908
  }
895
909
  const count = verts.length / stride - start;
896
910
  passRanges.push({ start, count });
897
911
  }
898
- return { vertices: new Float32Array(verts), passRanges, passes, stride };
912
+ return { vertices: new Float32Array(verts), passRanges, passes, stride, groups, renderGroups };
899
913
  }
900
914
 
901
915
  // src/shaders/CustomLineLayer.ts
916
+ var FALLBACK_COLOR = [0.6, 0.7, 0.9];
902
917
  var DEFAULT_COLORS = {
903
918
  pipe: [0.2, 0.85, 1],
904
919
  // 青蓝:管线
@@ -952,9 +967,13 @@ var CustomLineLayer = class {
952
967
  this.colors = { ...DEFAULT_COLORS, ...opts.colors };
953
968
  this.baseWidth = opts.baseWidth ?? 3;
954
969
  this.geometry = buildGlowGeometry(this.lines, { passes: opts.passes ?? 4, baseWidth: this.baseWidth });
970
+ for (const g of this.geometry.groups) {
971
+ if (!(g in this.colors)) this.colors[g] = FALLBACK_COLOR;
972
+ }
955
973
  }
956
974
  onAdd(map, gl) {
957
975
  this.map = map;
976
+ this.gl = gl;
958
977
  const vs = compile(gl, gl.VERTEX_SHADER, VERT);
959
978
  const fs = compile(gl, gl.FRAGMENT_SHADER, FRAG);
960
979
  const prog = gl.createProgram();
@@ -978,12 +997,17 @@ var CustomLineLayer = class {
978
997
  const aPos = gl.getAttribLocation(this.program, "aPos");
979
998
  const aDir = gl.getAttribLocation(this.program, "aDir");
980
999
  const aSide = gl.getAttribLocation(this.program, "aSide");
1000
+ const aGroup = gl.getAttribLocation(this.program, "aGroup");
981
1001
  gl.enableVertexAttribArray(aPos);
982
1002
  gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, stride, 0);
983
1003
  gl.enableVertexAttribArray(aDir);
984
1004
  gl.vertexAttribPointer(aDir, 2, gl.FLOAT, false, stride, 8);
985
1005
  gl.enableVertexAttribArray(aSide);
986
1006
  gl.vertexAttribPointer(aSide, 1, gl.FLOAT, false, stride, 16);
1007
+ if (aGroup >= 0) {
1008
+ gl.enableVertexAttribArray(aGroup);
1009
+ gl.vertexAttribPointer(aGroup, 1, gl.FLOAT, false, stride, 20);
1010
+ }
987
1011
  const uMatrix = gl.getUniformLocation(this.program, "uMatrix");
988
1012
  gl.uniformMatrix4fv(uMatrix, false, new Float32Array(matrix));
989
1013
  const uWidth = gl.getUniformLocation(this.program, "uWidth");
@@ -994,15 +1018,31 @@ var CustomLineLayer = class {
994
1018
  gl.uniform2f(uRes, canvas?.width ?? 1024, canvas?.height ?? 768);
995
1019
  gl.enable(gl.BLEND);
996
1020
  gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
997
- const color = this.colors[this.lines[0]?.group ?? "pipe"] ?? this.colors.pipe;
998
- for (let p = 0; p < this.geometry.passes.length; p++) {
999
- const pass = this.geometry.passes[p];
1000
- const range = this.geometry.passRanges[p];
1001
- if (range.count <= 0) continue;
1021
+ for (const rg of this.geometry.renderGroups) {
1022
+ const pass = this.geometry.passes[rg.passIndex];
1023
+ const color = this.colors[rg.group] ?? FALLBACK_COLOR;
1002
1024
  gl.uniform1f(uWidth, pass.width);
1003
1025
  gl.uniform3f(uColor, color[0], color[1], color[2]);
1004
1026
  gl.uniform1f(uOpacity, pass.opacity);
1005
- gl.drawArrays(gl.TRIANGLES, range.start, range.count);
1027
+ gl.drawArrays(gl.TRIANGLES, rg.start, rg.count);
1028
+ }
1029
+ }
1030
+ /**
1031
+ * 动态更新线集合(高亮选中、切换数据等无需重建图层)。
1032
+ * 浏览器环境会刷新 GPU 缓冲;纯逻辑/测试环境仅更新几何与数据。
1033
+ */
1034
+ setLines(lines) {
1035
+ this.lines = lines;
1036
+ this.geometry = buildGlowGeometry(this.lines, {
1037
+ passes: this.geometry.passes.length,
1038
+ baseWidth: this.baseWidth
1039
+ });
1040
+ for (const g of this.geometry.groups) {
1041
+ if (!(g in this.colors)) this.colors[g] = FALLBACK_COLOR;
1042
+ }
1043
+ if (this.buffer && this.gl) {
1044
+ this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffer);
1045
+ this.gl.bufferData(this.gl.ARRAY_BUFFER, this.geometry.vertices, this.gl.STATIC_DRAW);
1006
1046
  }
1007
1047
  }
1008
1048
  onRemove(_map, gl) {
@@ -1321,7 +1361,10 @@ var Map2 = class {
1321
1361
  addGlowLayer(opts) {
1322
1362
  const layer = new CustomLineLayer(opts);
1323
1363
  this._map.addLayer(layer);
1324
- return layer.id;
1364
+ return {
1365
+ id: layer.id,
1366
+ setLines: (lines) => layer.setLines(lines)
1367
+ };
1325
1368
  }
1326
1369
  /**
1327
1370
  * 挂载 LOD 控制器(T7 / F-1.7)。