Commit 2e69c4bf by limin

fix: 百度热力缓冲按默认 max=100 归一化导致一片红,构造时传真实上限并调整提交顺序

1 parent a41eb563
......@@ -65,7 +65,19 @@ export class BaiduHeatLayer implements HeatLayerLike {
});
}
private ensure(): BaiduLayerLike {
/**
* ⚠️ max 必须在**构造时**就传对,不能只靠 setOptions 后补。
*
* 百度 4.0 脚本里 fz(Heatmap) 的 max 默认是 100;setData 触发 _rebuild 生成 GPU 缓冲,
* 归一化就在那一刻按当时的 _style.max 算;而 _onOptionsChanged 只在 gradient /
* weightField 变化时重建,**改 max 不重建**。所以旧的「先 setData(max=100)→ 再
* setOptions({max:真实值})」会让缓冲一直按 100 归一化:count 上百的桶全 clamp 到 1,
* 整片饱和成红色(2026-11-24 实测)。
*
* 两处一起保证:构造传入首帧 max;后续换 max 时先 setOptions 再 setData,
* 让重建缓冲时读到的已经是新上限。
*/
private ensure(max: number): BaiduLayerLike {
if (!this.heat) {
const Ctor = this.basemap.gl.Heatmap;
if (!Ctor) {
......@@ -74,6 +86,8 @@ export class BaiduHeatLayer implements HeatLayerLike {
this.heat = new Ctor({
size: this.style.radius,
unit: 'px',
max,
min: 0,
minOpacity: HEAT_MIN_OPACITY,
maxOpacity: this.style.opacity,
gradient: HEAT_GRADIENT,
......@@ -95,7 +109,6 @@ export class BaiduHeatLayer implements HeatLayerLike {
count: Int32Array,
keep?: Uint8Array | null,
): void {
const heat = this.ensure();
const n = lng.length;
const size = this.basemap.getSize();
const center = this.basemap.getCenter();
......@@ -137,22 +150,25 @@ export class BaiduHeatLayer implements HeatLayerLike {
// 数据与归一化上限都没变 → 只管显隐,不重传 GPU 数据
if (sameSource && this.lastMax === this.max) {
heat.setVisible?.(this.visible);
this.heat?.setVisible?.(this.visible);
return;
}
this.lastMax = this.max;
const heat = this.ensure(this.max);
this.geo.features = data.map((p) => ({
type: 'Feature',
geometry: { type: 'Point', coordinates: [p.lng, p.lat] },
properties: { count: p.count },
}));
heat.setData(this.geo);
// 顺序要紧:先把新 max 写进 _style,再 setData 重建缓冲。
// 反过来(旧代码)缓冲会按上一次的 max 归一化,就是"一片红"的来源。
heat.setOptions?.({
max: this.max,
size: this.style.radius,
minOpacity: HEAT_MIN_OPACITY,
maxOpacity: this.visible ? this.style.opacity : 0,
});
heat.setData(this.geo);
heat.setVisible?.(this.visible);
}
......@@ -173,7 +189,7 @@ export class BaiduHeatLayer implements HeatLayerLike {
setVisible(visible: boolean) {
this.visible = visible;
if (visible) this.ensure().setVisible?.(true);
if (visible) this.ensure(Math.max(this.max, 1)).setVisible?.(true);
else this.heat?.setVisible?.(false);
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!