代码
var CENTER = new BMap.Point(116.404, 39.915);
var map = new BMap.Map('map');
map.centerAndZoom(CENTER, 13);
map.enableScrollWheelZoom(true);
// ---- 着色器(u_ 打头多为内置 uniform,着色器里声明了就自动注入)----
var VS_POINT = `
attribute vec2 a_pos;
attribute vec4 a_color;
uniform mat4 u_mvpMatrix;
uniform float u_pixelRatio;
uniform float u_size;
varying vec4 v_color;
void main() {
v_color = a_color;
gl_Position = u_mvpMatrix * vec4(a_pos, 0.0, 1.0);
gl_PointSize = u_size * u_pixelRatio;
}
`;
var FS_GLOW = `
precision mediump float;
uniform float u_opacity;
uniform float u_time;
varying vec4 v_color;
void main() {
float d = distance(gl_PointCoord, vec2(0.5)) * 2.0;
if (d > 1.0) { discard; }
float breath = 0.5 + 0.5 * sin(u_time * 2.0);
float core = smoothstep(1.0, 0.0, d);
float alpha = mix(0.35, 1.0, breath) * core * core;
gl_FragColor = vec4(v_color.rgb, v_color.a * alpha * u_opacity);
}
`;
var VS_VCOLOR = `
attribute vec2 a_pos;
attribute vec4 a_color;
uniform mat4 u_mvpMatrix;
varying vec4 v_color;
void main() {
v_color = a_color;
gl_Position = u_mvpMatrix * vec4(a_pos, 0.0, 1.0);
}
`;
var FS_VCOLOR = `
precision mediump float;
uniform float u_opacity;
varying vec4 v_color;
void main() {
gl_FragColor = vec4(v_color.rgb, v_color.a * u_opacity);
}
`;
// ---- 数据 ----
var POIS = [
{name: '景山公园', color: '#52c41a', coordinates: [116.40282, 39.93227]},
{name: '天安门', color: '#ff4d4f', coordinates: [116.40396, 39.91512]},
{name: '北海公园', color: '#13c2c2', coordinates: [116.38909, 39.92451]},
{name: '什刹海', color: '#2f9bff', coordinates: [116.3838, 39.94033]},
{name: '王府井', color: '#faad14', coordinates: [116.41727, 39.91466]},
{name: '国家大剧院', color: '#eb2f96', coordinates: [116.38609, 39.90583]},
{name: '天坛公园', color: '#722ed1', coordinates: [116.41725, 39.88824]},
];
function hexToRgb(hex) {
var n = parseInt(hex.slice(1), 16);
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
}
// 换 demo 时先移除旧图层;extras 放附属层(发光叠加用的深色底衬)
var layer = null;
var extras = [];
function removeAll() {
if (layer) {
map.removeLayer(layer);
layer = null;
}
extras.forEach(function (l) {
map.removeLayer(l);
});
extras = [];
}
// 1. 呼吸光点:声明式 attributes + 逐顶点色 + 内置 u_time 动画
function demoGlow() {
removeAll();
var pos = POIS.map(function (p) {
return p.coordinates;
});
var col = [];
POIS.forEach(function (p) {
var c = hexToRgb(p.color);
col.push(c[0], c[1], c[2], 255);
});
layer = new BMap.WebGLCustomLayer({
referCenter: CENTER,
animation: true,
vertexShader: VS_POINT,
fragmentShader: FS_GLOW,
attributes: {
// type:'lnglat' 语法糖:框架自动把经纬度投影成图层局部坐标,size 不用写
a_pos: {data: pos, type: 'lnglat'},
// Uint8Array + normalized:着色器里读到 0~1,省 3/4 显存
a_color: {data: new Uint8Array(col), size: 4, normalized: true},
},
uniforms: {u_size: 56},
draw: {mode: 'POINTS'},
});
map.addLayer(layer);
}
// 2. 渐变覆盖面:给了 indices 就自动走 drawElements,四角各一色插值出渐变
function demoGradient() {
removeAll();
var corners = [
[116.36, 39.9],
[116.45, 39.9],
[116.45, 39.94],
[116.36, 39.94],
];
layer = new BMap.WebGLCustomLayer({
referCenter: CENTER,
vertexShader: VS_VCOLOR,
fragmentShader: FS_VCOLOR,
attributes: {
a_pos: {data: corners, type: 'lnglat'},
a_color: {
data: new Uint8Array([
79, 140, 255, 170, 107, 225, 255, 170, 124, 255, 178, 170, 255, 209, 102, 170,
]),
size: 4,
normalized: true,
},
},
indices: [0, 1, 2, 0, 2, 3],
draw: {mode: 'TRIANGLES'},
});
map.addLayer(layer);
}
// 3. 雷达扫描:命令式 onRender 每帧算几何,getUnitsPerPixel 保持屏幕尺寸恒定
function demoRadar() {
removeAll();
layer = new BMap.WebGLCustomLayer({
referCenter: CENTER,
animation: true,
vertexShader: VS_VCOLOR,
fragmentShader: FS_VCOLOR,
onRender: function (ctx) {
var radius = 150 * ctx.getUnitsPerPixel(); // 屏幕恒定 150px
var sweep = ctx.getTime() * 1.4; // 随时间旋转
var n = 64;
var pos = [0, 0]; // 扇心
var col = [80, 230, 170, 0]; // 扇心透明
for (var i = 0; i <= n; i++) {
var a = sweep - (i / n) * 1.2; // 约 69° 拖尾
pos.push(Math.cos(a) * radius, Math.sin(a) * radius);
col.push(80, 230, 170, Math.round(210 * (1 - i / n)));
}
ctx.setAttribute('a_pos', {data: new Float32Array(pos), size: 2});
ctx.setAttribute('a_color', {data: new Uint8Array(col), size: 4, normalized: true});
ctx.draw({mode: 'TRIANGLE_FAN'});
},
});
map.addLayer(layer);
}
// 4. 发光叠加:renderState additive 混合 + zIndex 分层
function demoCluster() {
removeAll();
// additive 需深色背景才看得出叠加,先铺一层深色底衬(zIndex 小,先画)
var quad = [
[116.34, 39.86],
[116.47, 39.86],
[116.47, 39.97],
[116.34, 39.97],
];
var dark = new Uint8Array([10, 14, 24, 200, 10, 14, 24, 200, 10, 14, 24, 200, 10, 14, 24, 200]);
var backdrop = new BMap.WebGLCustomLayer({
referCenter: CENTER,
zIndex: 1,
vertexShader: VS_VCOLOR,
fragmentShader: FS_VCOLOR,
attributes: {
a_pos: {data: quad, type: 'lnglat'},
a_color: {data: dark, size: 4, normalized: true},
},
indices: [0, 1, 2, 0, 2, 3],
renderState: {blend: 'normal'},
draw: {mode: 'TRIANGLES'},
});
map.addLayer(backdrop);
extras.push(backdrop);
// 一圈互相重叠的发光点(additive,zIndex 大,后画)
var palette = ['#6be1ff', '#8f7bff', '#ff6ea6', '#7CFFB2', '#ffd166'];
var spots = [],
col = [];
for (var i = 0; i < 9; i++) {
var a = (i / 9) * Math.PI * 2;
spots.push([116.404 + Math.cos(a) * 0.013, 39.915 + Math.sin(a) * 0.01]);
var c = hexToRgb(palette[i % palette.length]);
col.push(c[0], c[1], c[2], 255);
}
layer = new BMap.WebGLCustomLayer({
referCenter: CENTER,
zIndex: 2,
animation: true,
vertexShader: VS_POINT,
fragmentShader: FS_GLOW,
attributes: {
a_pos: {data: spots, type: 'lnglat'},
a_color: {data: new Uint8Array(col), size: 4, normalized: true},
},
uniforms: {u_size: 130},
renderState: {blend: 'additive'},
draw: {mode: 'POINTS'},
});
map.addLayer(layer);
}
// 显示 / 隐藏:封装基类的 setVisible,与「使用 DOM 元素」示例的 show/hide 一致
function show() {
if (layer) {
layer.setVisible(true);
}
extras.forEach(function (l) {
l.setVisible(true);
});
}
function hide() {
if (layer) {
layer.setVisible(false);
}
extras.forEach(function (l) {
l.setVisible(false);
});
}
demoGlow();
预览