代码
var map = new BMap.Map('map', {enableAdaptiveMinZoom: false, backgroundColor: [6, 19, 32, 255]});
map.centerAndZoom(new BMap.Point(105.348145, 37.976416), 5);
map.enableScrollWheelZoom(true);
// 深色底图,关闭所有文字、POI 图标和路牌
map.setMapStyle({
styleJson: [
{featureType: 'land', elementType: 'geometry', stylers: {color: '#1d2433ff'}},
{featureType: 'water', elementType: 'geometry', stylers: {color: '#0f1520ff'}},
{featureType: 'green', elementType: 'geometry', stylers: {color: '#1d2433ff'}},
{featureType: 'building', elementType: 'geometry', stylers: {visibility: 'off'}},
{featureType: 'manmade', elementType: 'geometry', stylers: {visibility: 'off'}},
{featureType: 'road', elementType: 'geometry', stylers: {visibility: 'off'}},
{featureType: 'railway', elementType: 'geometry', stylers: {visibility: 'off'}},
{featureType: 'subway', elementType: 'geometry', stylers: {visibility: 'off'}},
{featureType: 'boundary', elementType: 'geometry', stylers: {color: '#5b6b85ff'}},
{featureType: 'all', elementType: 'labels', stylers: {visibility: 'off'}},
{featureType: 'all', elementType: 'labels.icon', stylers: {visibility: 'off'}},
{featureType: 'road', elementType: 'labels', stylers: {visibility: 'off'}},
{featureType: 'road', elementType: 'labels.icon', stylers: {visibility: 'off'}},
{featureType: 'poilabel', elementType: 'all', stylers: {visibility: 'off'}},
],
});
// 三档点:数据里越靠后的组越稀,画得越大越亮。
// 第一档另外随机闪烁 —— 每个点在一轮里有一个随机时刻,到点亮起、再线性淡出。
// 全部点放在一个缓冲里、一次 drawArrays 画完,闪烁只靠 u_head 一个 uniform 推进
var VS = [
'attribute vec2 a_pos;',
'attribute vec2 a_info;', // x:档位 0 / 1 / 2,y:本轮亮起时刻 0~1,小于 0 不闪
'uniform mat4 u_mvpMatrix;',
'uniform float u_pixelRatio;',
'uniform vec4 u_color0;',
'uniform vec4 u_color1;',
'uniform vec4 u_color2;',
'uniform vec3 u_sizes;', // 三档点径,CSS 像素
'uniform vec4 u_flashColor;',
'uniform float u_flashSize;',
'uniform float u_head;', // 本轮进度 0~1
'uniform float u_trail;', // 亮起后淡出用掉的进度
'varying vec4 v_color;',
'void main() {',
' vec4 color = u_color2;',
' float size = u_sizes.z;',
' if (a_info.x < 0.5) {',
' color = u_color0;',
' size = u_sizes.x;',
' } else if (a_info.x < 1.5) {',
' color = u_color1;',
' size = u_sizes.y;',
' }',
' float flash = 0.0;',
' if (a_info.y >= 0.0) {',
// fract 让一轮首尾相接,亮起时刻靠近 1 的点会跨轮淡出,不会被截断
' float age = fract(u_head - a_info.y);',
' flash = age < u_trail ? 1.0 - age / u_trail : 0.0;',
' }',
// 加性混合下「底点 + 闪光」各画一次与合成一次等价,这里直接按 rgb * a 相加,alpha 给 1
' v_color = vec4(color.rgb * color.a + u_flashColor.rgb * u_flashColor.a * flash, 1.0);',
' gl_Position = u_mvpMatrix * vec4(a_pos, 0.0, 1.0);',
' gl_PointSize = max(size, flash > 0.0 ? u_flashSize : 0.0) * u_pixelRatio;',
'}',
].join('\n');
var FS = [
'precision mediump float;',
'uniform float u_opacity;',
'varying vec4 v_color;',
'void main() {',
// 点只有一两个像素,硬裁成圆会整颗裁没,只把边缘淡掉
' float d = length(gl_PointCoord - 0.5) * 2.0;',
' float edge = 1.0 - smoothstep(0.8, 1.0, d);',
' gl_FragColor = vec4(v_color.rgb, v_color.a * edge * u_opacity);',
'}',
].join('\n');
/**
* 把三组 {geoCoord: [lng, lat]} 拼成一个缓冲。
*
* @param {Object} layer 用它的 projectToLayer 换算局部坐标
* @param {Array<Array>} groups chinalocation.json 的三组数据
* @return {Object} attributes
*/
function buildPoints(layer, groups) {
var total = 0;
for (var g = 0; g < groups.length; g++) {
total += groups[g].length;
}
var pos = new Float32Array(total * 2);
var info = new Float32Array(total * 2);
var k = 0;
for (g = 0; g < groups.length; g++) {
for (var i = 0; i < groups[g].length; i++, k += 2) {
var xy = layer.projectToLayer(groups[g][i].geoCoord);
pos[k] = xy[0];
pos[k + 1] = xy[1];
info[k] = g;
info[k + 1] = g === 0 ? Math.random() : -1;
}
}
return {
a_pos: {data: pos, size: 2},
a_info: {data: info, size: 2},
};
}
var DURATION = 16; // 一轮的秒数,每个闪烁点一轮亮一次
var layer = new BMap.WebGLCustomLayer({
// referCenter 取中国中部:顶点是 float32,离原点几千公里也还有亚米级精度
referCenter: new BMap.Point(105, 35),
vertexShader: VS,
fragmentShader: FS,
uniforms: {
u_color0: [1, 0.3, 0.3, 0.8],
u_color1: [1, 0.8, 0, 0.6],
u_color2: [1, 1, 0, 0.6],
u_sizes: [1, 1.5, 2],
u_flashColor: [1, 1, 0.6, 0.5],
u_flashSize: 1.5,
u_trail: 0.1,
},
// 点越密越亮
renderState: {blend: 'additive'},
animation: true,
onRender: function (ctx) {
ctx.setUniform('u_head', (ctx.getTime() / DURATION) % 1);
ctx.draw({mode: 'POINTS'});
},
});
map.addLayer(layer);
// 数据共三组,约 11.8 万个点,放在允许跨域的 bos 上
fetch('https://jsapi-demo.bj.bcebos.com/demo_data/chinalocation.json')
.then(function (rs) {
return rs.json();
})
.then(function (groups) {
layer.setAttributes(buildPoints(layer, groups));
});
使用 WebGLCustomLayer 实现全国闪烁点
预览