代码
function $(id) {
  return document.getElementById(id);
}

// ================= 地图 =================
var center = new BMap.Point(116.4175, 39.9125);
var map = new BMap.Map('map');
map.centerAndZoom(center, 18.5);
map.enableScrollWheelZoom(true);
// 关掉底图自带的楼块 / POI / 室内图,舞台留给自定义层
map.setDisplayOptions({poi: false, building: false, indoor: false});
map.setMapStyleV2({
  styleJson: [
    {featureType: 'land', elementType: 'geometry', stylers: {color: '#050b16ff'}},
    {featureType: 'water', elementType: 'geometry', stylers: {color: '#03101fff'}},
    {featureType: 'green', elementType: 'geometry', stylers: {color: '#06141cff'}},
    {featureType: 'building', elementType: 'geometry', stylers: {visibility: 'off'}},
    // 底图道路整体隐藏:B2 楼板贴地(z=0),保留道路会压到最底层上
    {featureType: 'road', elementType: 'all', stylers: {visibility: 'off'}},
    {featureType: 'highway', elementType: 'all', stylers: {visibility: 'off'}},
    {featureType: 'arterial', elementType: 'all', stylers: {visibility: 'off'}},
    {featureType: 'local', elementType: 'all', stylers: {visibility: 'off'}},
    {featureType: 'railway', elementType: 'all', stylers: {visibility: 'off'}},
    {featureType: 'subway', elementType: 'all', stylers: {visibility: 'off'}},
    {featureType: 'poilabel', elementType: 'all', stylers: {visibility: 'off'}},
    {featureType: 'districtlabel', elementType: 'labels', stylers: {visibility: 'off'}},
  ],
});
map.setTilt(58, {noAnimation: true});
map.setHeading(-22, {noAnimation: true});

// ================= 工具 =================
// 固定种子的伪随机:每次打开看到的车位占用、商铺排布都一样,便于截图对比
var seed = 20261112;
function rand() {
  seed = (seed + 0x6d2b79f5) | 0;
  var t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
  t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
  return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
function hex(h, a) {
  var n = parseInt(h.slice(1), 16);
  return [
    ((n >> 16) & 255) / 255,
    ((n >> 8) & 255) / 255,
    (n & 255) / 255,
    a === undefined ? 1 : a,
  ];
}

/**
 * 非索引三角网格的累加器。坐标是图层局部坐标(相对 referCenter 的墨卡托米),
 * z 是「楼层内」的高度 —— 楼层自身的抬升由着色器按 a_level 和 u_spread 算,
 * 所以展开 / 合拢 / 聚焦都不用重传顶点。
 */
function Mesh() {
  this.p = [];
  this.n = [];
  this.c = [];
  this.l = [];
  this.e = [];
}
Mesh.prototype.vert = function (p, n, c, lv, e) {
  this.p.push(p[0], p[1], p[2]);
  this.n.push(n[0], n[1], n[2]);
  this.c.push(c[0], c[1], c[2], c[3]);
  this.l.push(lv);
  this.e.push(e[0], e[1], e[2], e[3]);
};
// q: 4 个角点;e: 4 个角各自的 extra,或者一个共用的
// lv 也可以是 4 个角各自的楼层(坡道、扶梯这种跨层的面)
Mesh.prototype.quad = function (q, c, lv, e, n) {
  if (!n) {
    var ux = q[1][0] - q[0][0],
      uy = q[1][1] - q[0][1],
      uz = q[1][2] - q[0][2];
    var vx = q[3][0] - q[0][0],
      vy = q[3][1] - q[0][1],
      vz = q[3][2] - q[0][2];
    n = [uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx];
    var len = Math.sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]) || 1;
    var s = n[2] < 0 ? -1 : 1;
    n = [(n[0] / len) * s, (n[1] / len) * s, (n[2] / len) * s];
  }
  var order = [0, 1, 2, 0, 2, 3];
  for (var k = 0; k < 6; k++) {
    var i = order[k];
    this.vert(q[i], n, c, typeof lv === 'number' ? lv : lv[i], typeof e[0] === 'number' ? e : e[i]);
  }
};
/**
 * 盒子(四壁 + 顶,不要底)。ex(face, cornerU, cornerV) 返回每个角的 extra:
 * face 0~3 是四壁(u 沿墙、v 自下而上),4 是顶(u / v 是顶面的归一化坐标)。
 */
Mesh.prototype.box = function (x0, y0, x1, y1, z0, z1, c, lv, ex) {
  var walls = [
    [
      [x0, y0],
      [x1, y0],
      [0, -1],
    ],
    [
      [x1, y0],
      [x1, y1],
      [1, 0],
    ],
    [
      [x1, y1],
      [x0, y1],
      [0, 1],
    ],
    [
      [x0, y1],
      [x0, y0],
      [-1, 0],
    ],
  ];
  for (var f = 0; f < 4; f++) {
    var a = walls[f][0],
      b = walls[f][1],
      nn = walls[f][2];
    this.quad(
      [
        [a[0], a[1], z0],
        [b[0], b[1], z0],
        [b[0], b[1], z1],
        [a[0], a[1], z1],
      ],
      c,
      lv,
      [ex(f, 0, 0), ex(f, 1, 0), ex(f, 1, 1), ex(f, 0, 1)],
      [nn[0], nn[1], 0],
    );
  }
  this.quad(
    [
      [x0, y0, z1],
      [x1, y0, z1],
      [x1, y1, z1],
      [x0, y1, z1],
    ],
    c,
    lv,
    [ex(4, 0, 0), ex(4, 1, 0), ex(4, 1, 1), ex(4, 0, 1)],
    [0, 0, 1],
  );
};
Mesh.prototype.count = function () {
  return this.l.length;
};
function f32(a) {
  return new Float32Array(a);
}

// ================= 着色器 =================
var FS_HEAD = [
  '#ifdef GL_FRAGMENT_PRECISION_HIGH',
  'precision highp float;',
  '#else',
  'precision mediump float;',
  '#endif',
].join('\n');

// 楼层栈的公共逻辑:所有「属于某一层」的图层都 include 这一段。
// u_spread 楼层间距(展开 / 合拢);u_focus 聚焦的楼层,-1 表示全部;
// u_mix 聚焦动画进度:聚焦层之上的楼层被整体抬走,其他楼层变暗
var STACK = [
  'uniform float u_spread;',
  'uniform float u_focus;',
  'uniform float u_mix;',
  // 导航模式:0~1,开启时整栋楼压暗,把画面让给导航路线
  'uniform float u_nav;',
  'float floorOf(float lv) { return floor(lv + 0.001); }',
  'float navDim() { return 1.0 - u_nav * 0.65; }',
  'float levelZ(float lv) {',
  '    float focusing = step(-0.5, u_focus);',
  '    float above = focusing * step(u_focus + 0.5, floorOf(lv));',
  // 整栋楼整体抬高 3m:B2 不再与底图(道路)共面,避免被底图压住
  '    return 3.0 + lv * u_spread + above * u_mix * 110.0;',
  '}',
  'float focusOn(float lv) {',
  '    return step(-0.5, u_focus) * (1.0 - step(0.5, abs(floorOf(lv) - u_focus))) * u_mix;',
  '}',
  'float levelDim(float lv) {',
  '    float other = step(-0.5, u_focus) * step(0.5, abs(floorOf(lv) - u_focus));',
  '    return 1.0 - other * u_mix * 0.85;',
  '}',
  // 地库环形车道:周长参数 s -> 位置与行驶方向。行驶车辆和车灯都靠它在 GPU 上跑,CPU 不用每帧算
  'vec2 loopPos(float s, vec2 h, out vec2 dir) {',
  '    float ex = 2.0 * h.x;',
  '    float ey = 2.0 * h.y;',
  '    s = mod(s, 2.0 * (ex + ey));',
  '    if (s < ex) { dir = vec2(1.0, 0.0); return vec2(-h.x + s, -h.y); }',
  '    s -= ex;',
  '    if (s < ey) { dir = vec2(0.0, 1.0); return vec2(h.x, -h.y + s); }',
  '    s -= ey;',
  '    if (s < ex) { dir = vec2(-1.0, 0.0); return vec2(h.x - s, h.y); }',
  '    s -= ex;',
  '    dir = vec2(0.0, -1.0);',
  '    return vec2(-h.x, h.y - s);',
  '}',
  'vec2 rot(vec2 p, vec2 d) { return vec2(p.x * d.x - p.y * d.y, p.x * d.y + p.y * d.x); }',
].join('\n');
// 车道环的半宽 / 半高,与下面造车位时的布局一致
var LANE = [78, 31];
var LANE_GLSL = 'vec2(78.0, 31.0)';

// ---- ground:深色舞台、网格、雷达扫描 ----
var VS_GROUND = [
  'attribute vec2 a_pos;',
  'uniform mat4 u_mvpMatrix;',
  'varying vec2 v_p;',
  'void main() {',
  '    v_p = a_pos;',
  '    gl_Position = u_mvpMatrix * vec4(a_pos, -3.0, 1.0);',
  '}',
].join('\n');
var FS_GROUND = [
  FS_HEAD,
  'uniform float u_time;',
  'uniform float u_opacity;',
  'varying vec2 v_p;',
  'float gridLine(vec2 p, float step, float w) {',
  '    vec2 d = (0.5 - abs(fract(p / step) - 0.5)) * step;',
  '    return 1.0 - smoothstep(0.0, w, min(d.x, d.y));',
  '}',
  'void main() {',
  '    float r = length(v_p);',
  '    vec3 col = mix(vec3(0.02, 0.06, 0.12), vec3(0.005, 0.01, 0.03), smoothstep(80.0, 900.0, r));',
  '    float g = gridLine(v_p, 20.0, 0.5) * 0.10 + gridLine(v_p, 100.0, 0.9) * 0.22;',
  '    col += vec3(0.1, 0.55, 0.9) * g * (1.0 - smoothstep(150.0, 800.0, r));',
  // 同心环
  '    float ring = 1.0 - smoothstep(0.0, 0.8, abs(fract(r / 60.0) - 0.5) * 60.0 - 29.0);',
  '    col += vec3(0.1, 0.6, 1.0) * ring * 0.12 * (1.0 - smoothstep(120.0, 600.0, r));',
  // 雷达扫描:角度差越小越亮,尾迹指数衰减
  '    float ang = atan(v_p.y, v_p.x);',
  '    float sweep = mod(u_time * 0.7, 6.2831853);',
  '    float diff = mod(sweep - ang, 6.2831853);',
  '    col += vec3(0.0, 0.8, 1.0) * exp(-diff * 2.5) * 0.28 * (1.0 - smoothstep(120.0, 700.0, r));',
  // 地面上的楼体投影光晕
  '    vec2 q = v_p / vec2(130.0, 95.0);',
  '    col += vec3(0.0, 0.45, 0.9) * exp(-dot(q, q) * 1.6) * 0.35;',
  '    float a = 1.0 - smoothstep(700.0, 1500.0, r);',
  '    gl_FragColor = vec4(col, a * 0.96 * u_opacity);',
  '}',
].join('\n');

// ---- slab:楼板。顶面带网格、边缘霓虹、扫描环,F2/F3 中庭镂空 ----
var VS_SLAB = [
  'attribute vec3 a_pos;',
  'attribute float a_level;',
  'attribute vec4 a_extra;',
  'uniform mat4 u_mvpMatrix;',
  STACK,
  'varying vec3 v_p;',
  'varying vec4 v_e;',
  'varying float v_lv;',
  'varying float v_dim;',
  'varying float v_focus;',
  'void main() {',
  '    v_p = a_pos;',
  '    v_e = a_extra;',
  '    v_lv = a_level;',
  '    v_dim = levelDim(a_level) * navDim();',
  '    v_focus = focusOn(a_level);',
  '    gl_Position = u_mvpMatrix * vec4(a_pos.xy, a_pos.z + levelZ(a_level), 1.0);',
  '}',
].join('\n');
var FS_SLAB = [
  FS_HEAD,
  'uniform float u_time;',
  'uniform float u_opacity;',
  'varying vec3 v_p;',
  'varying vec4 v_e;',
  'varying float v_lv;',
  'varying float v_dim;',
  'varying float v_focus;',
  'void main() {',
  '    vec2 p = v_p.xy;',
  '    float mall = step(1.5, v_lv);',
  '    vec3 edgeCol = mix(vec3(0.15, 0.85, 1.0), vec3(0.75, 0.4, 1.0), mall);',
  '    if (v_e.x > 0.5) {',
  // 侧面:一条亮边 + 向下渐隐
  '        float t = clamp(v_e.y, 0.0, 1.0);',
  '        float a = 0.25 + 0.75 * smoothstep(0.6, 1.0, t);',
  '        gl_FragColor = vec4(edgeCol * (1.2 + v_focus), a * v_dim * u_opacity);',
  '        return;',
  '    }',
  '    vec2 e = p / vec2(34.0, 20.0);',
  '    float ell = dot(e, e);',
  // F2 / F3 中庭镂空,北侧留一条通往观光电梯的玻璃连廊
  '    if (v_lv > 2.5 && ell < 1.0 && !(abs(p.x) < 4.5 && p.y > 0.0)) { discard; }',
  '    vec3 base = mix(vec3(0.03, 0.08, 0.15), vec3(0.07, 0.05, 0.17), mall);',
  '    vec2 gd = (0.5 - abs(fract(p / 8.0) - 0.5)) * 8.0;',
  '    float grid = 1.0 - smoothstep(0.0, 0.3, min(gd.x, gd.y));',
  '    vec3 col = base + edgeCol * grid * 0.10;',
  '    float de = min(100.0 - abs(p.x), 65.0 - abs(p.y));',
  '    col += edgeCol * exp(-de / 2.2) * (0.9 + v_focus * 1.2);',
  // 中庭边缘光环(F1 是喷泉的水波纹)
  '    float ringA = exp(-abs(ell - 1.0) * 14.0) * step(2.5, v_lv);',
  '    float ripple = step(1.5, v_lv) * (1.0 - step(2.5, v_lv)) * (1.0 - smoothstep(0.6, 1.0, ell))',
  '        * (0.5 + 0.5 * sin(sqrt(ell) * 26.0 - u_time * 3.0));',
  '    col += vec3(0.9, 0.55, 1.0) * ringA * 0.9 + vec3(0.2, 0.7, 1.0) * ripple * 0.35;',
  // 从中心向外扩散的扫描环,每层错开相位
  '    float wave = fract(u_time * 0.22 + v_lv * 0.17) * 150.0;',
  '    float r = length(p * vec2(1.0, 1.4));',
  '    col += edgeCol * exp(-abs(r - wave) / 2.5) * (1.0 - wave / 150.0) * 0.8;',
  '    float a = 0.62 + 0.25 * v_focus;',
  '    gl_FragColor = vec4(col, a * v_dim * u_opacity);',
  '}',
].join('\n');

// ---- solid:不透明实体,写深度。a_extra.x = 1 的是在环形车道上行驶的车 ----
var VS_SOLID = [
  'attribute vec3 a_pos;',
  'attribute vec3 a_normal;',
  'attribute vec4 a_color;',
  'attribute float a_level;',
  'attribute vec4 a_extra;',
  'uniform mat4 u_mvpMatrix;',
  'uniform float u_time;',
  STACK,
  'varying vec4 v_color;',
  'varying vec3 v_n;',
  'varying vec3 v_p;',
  'varying float v_dim;',
  'void main() {',
  '    vec3 p = a_pos;',
  '    vec3 n = a_normal;',
  '    if (a_extra.x > 0.5) {',
  '        vec2 d;',
  '        vec2 c = loopPos(a_extra.y + u_time * a_extra.z, ' + LANE_GLSL + ', d);',
  '        p.xy = c + rot(p.xy, d);',
  '        n.xy = rot(n.xy, d);',
  '    }',
  '    v_color = a_color;',
  '    v_n = n;',
  '    v_p = p;',
  '    v_dim = levelDim(a_level) * navDim();',
  '    gl_Position = u_mvpMatrix * vec4(p.xy, p.z + levelZ(a_level), 1.0);',
  '}',
].join('\n');
var FS_SOLID = [
  FS_HEAD,
  'uniform float u_time;',
  'uniform float u_opacity;',
  'varying vec4 v_color;',
  'varying vec3 v_n;',
  'varying vec3 v_p;',
  'varying float v_dim;',
  'void main() {',
  '    vec3 n = normalize(v_n);',
  '    float lam = max(dot(n, normalize(vec3(-0.35, -0.55, 0.76))), 0.0);',
  '    vec3 col = v_color.rgb * (0.30 + 0.75 * lam);',
  // 顶面高光,侧面越高越亮,体积感更强
  '    col += v_color.rgb * 0.25 * smoothstep(0.0, 5.0, v_p.z);',
  // 斜向扫过整栋楼的光带
  '    float sweep = mod(u_time * 55.0, 360.0) - 180.0;',
  '    float band = exp(-pow((v_p.x + v_p.y * 0.4 - sweep) / 4.0, 2.0));',
  '    col += vec3(0.2, 0.85, 1.0) * band * 0.7;',
  '    gl_FragColor = vec4(col * (0.2 + 0.8 * v_dim), v_color.a * u_opacity);',
  '}',
].join('\n');

// ---- holo:全息体与各种发光线,additive,不写深度所以无需排序 ----
// a_extra = (kind, a, b, c)
//   0 商铺墙   (0, v, seed, u)      1 商铺顶   (1, u, seed, v)
//   2 车位线   (2, isFree, seed, 0) 3 流光虚线 (3, 0, seed, dist)
//   4 坡道扶梯 (4, across, seed, dist)  5 电梯光柱 (5, v, seed, u)
var VS_HOLO = [
  'attribute vec3 a_pos;',
  'attribute vec4 a_color;',
  'attribute float a_level;',
  'attribute vec4 a_extra;',
  'uniform mat4 u_mvpMatrix;',
  STACK,
  'varying vec4 v_color;',
  'varying vec4 v_e;',
  'varying float v_dim;',
  'varying float v_focus;',
  'void main() {',
  '    v_color = a_color;',
  '    v_e = a_extra;',
  '    v_dim = levelDim(a_level) * navDim();',
  '    v_focus = focusOn(a_level);',
  '    gl_Position = u_mvpMatrix * vec4(a_pos.xy, a_pos.z + levelZ(a_level), 1.0);',
  '}',
].join('\n');
var FS_HOLO = [
  FS_HEAD,
  'uniform float u_time;',
  'uniform float u_opacity;',
  'uniform float u_hlFree;',
  'varying vec4 v_color;',
  'varying vec4 v_e;',
  'varying float v_dim;',
  'varying float v_focus;',
  'float edge(float x, float w) { return 1.0 - smoothstep(0.0, w, min(x, 1.0 - x)); }',
  'void main() {',
  '    float k = v_e.x;',
  '    float seed = v_e.z;',
  '    float a = 0.0;',
  '    if (k < 0.5) {',
  '        float v = v_e.y;',
  '        float scan = smoothstep(0.88, 1.0, fract(v * 1.2 - u_time * 0.45 + seed));',
  '        a = 0.10 + 0.32 * (1.0 - v) + edge(v_e.w, 0.025) * 0.85 + smoothstep(0.93, 1.0, v) * 1.1',
  '            + (1.0 - smoothstep(0.0, 0.1, v)) * 0.6 + scan * 0.55;',
  '        a *= 0.85 + 0.15 * sin(u_time * 2.0 + seed * 40.0);',
  '    } else if (k < 1.5) {',
  '        a = 0.08 + max(edge(v_e.y, 0.03), edge(v_e.w, 0.03)) * 0.9;',
  '    } else if (k < 2.5) {',
  '        float blink = v_e.y * u_hlFree * (1.2 + 1.2 * sin(u_time * 7.0 + seed * 9.0));',
  '        a = 0.75 + blink;',
  '    } else if (k < 3.5) {',
  '        a = step(0.55, fract(v_e.w / 5.0 - u_time * 0.9)) * 0.9;',
  '    } else if (k < 4.5) {',
  '        float along = fract(v_e.w / 5.0 - u_time * 0.8);',
  '        float c = along - abs(v_e.y) * 0.35;',
  '        float chev = smoothstep(0.0, 0.08, c) * (1.0 - smoothstep(0.22, 0.30, c));',
  '        a = 0.18 + chev * 0.8 + smoothstep(0.8, 1.0, abs(v_e.y)) * 0.8;',
  '    } else {',
  '        float v = v_e.y;',
  '        float stripes = smoothstep(0.7, 1.0, fract(v * 14.0 - u_time * 1.6));',
  '        a = (0.12 + stripes * 0.35 + edge(v_e.w, 0.08) * 0.5) * (1.0 - v * 0.6);',
  '    }',
  '    a *= v_dim * (1.0 + v_focus * 0.6) * u_opacity;',
  '    gl_FragColor = vec4(v_color.rgb, clamp(a * v_color.a, 0.0, 1.0));',
  '}',
].join('\n');

// ---- route:导航路线。a_extra = (across -1~1, 沿线距离),u_progress 控制揭示进度 ----
var VS_ROUTE = [
  'attribute vec3 a_pos;',
  'attribute float a_level;',
  'attribute vec2 a_extra;',
  'uniform mat4 u_mvpMatrix;',
  STACK,
  'varying vec2 v_e;',
  'varying float v_dim;',
  'void main() {',
  '    v_e = a_extra;',
  // 路线自己不被导航模式压暗,反而稍微提亮
  '    v_dim = (0.35 + 0.65 * levelDim(a_level)) * (1.0 + u_nav * 0.15);',
  '    gl_Position = u_mvpMatrix * vec4(a_pos.xy, a_pos.z + levelZ(a_level), 1.0);',
  '}',
].join('\n');
var FS_ROUTE = [
  FS_HEAD,
  'uniform float u_time;',
  'uniform float u_opacity;',
  'uniform float u_progress;',
  'uniform float u_total;',
  'varying vec2 v_e;',
  'varying float v_dim;',
  'void main() {',
  '    float d = v_e.y;',
  '    float head = u_progress * u_total;',
  '    if (d > head) { discard; }',
  '    float across = abs(v_e.x);',
  '    vec3 col = mix(vec3(0.1, 1.0, 0.6), vec3(1.0, 0.3, 0.9), clamp(d / u_total, 0.0, 1.0));',
  // normal 混合画在最上层:深色带身 + 亮色描边 + 白色流动箭头,压在任何发光物上都分得清
  '    float along = fract(d / 9.0 - u_time * 1.3);',
  '    float c = along - across * 0.3;',
  '    float chev = smoothstep(0.0, 0.05, c) * (1.0 - smoothstep(0.18, 0.24, c))',
  '        * (1.0 - smoothstep(0.72, 0.8, across));',
  '    float rim = smoothstep(0.7, 0.85, across);',
  '    vec3 outc = mix(col * 0.55, min(col * 1.4, vec3(1.0)), rim);',
  '    outc = mix(outc, vec3(1.0), chev);',
  // 揭示过程中的路线头部高亮
  '    outc += vec3(1.0) * exp(-(head - d) / 4.0) * step(u_progress, 0.999);',
  '    gl_FragColor = vec4(outc, 0.9 * v_dim * u_opacity);',
  '}',
].join('\n');

// ---- points:各种点精灵。a_extra = (kind, size, seed, param) ----
//   0 空车位涟漪  size 为世界尺寸     1 商铺标记  size 为像素
//   3 中庭粒子    param 为环绕半径     4 电梯轿厢  size 为世界尺寸
//   5 车灯        size 像素,seed=相位,param=速度,a_pos.xy 为车身局部偏移
var VS_POINTS = [
  'attribute vec3 a_pos;',
  'attribute vec4 a_color;',
  'attribute float a_level;',
  'attribute vec4 a_extra;',
  'uniform mat4 u_mvpMatrix;',
  'uniform float u_time;',
  'uniform float u_pixelRatio;',
  'uniform float u_unitsPerPixel;',
  'uniform float u_hlFree;',
  STACK,
  'varying vec4 v_color;',
  'varying float v_kind;',
  'varying float v_seed;',
  'void main() {',
  '    float k = a_extra.x;',
  '    float size = a_extra.y;',
  '    float seed = a_extra.z;',
  '    vec3 p = a_pos;',
  '    float lv = a_level;',
  '    float px = size;',
  '    float fade = 1.0;',
  '    if (k < 0.5) {',
  '        px = size * (1.0 + u_hlFree * 0.6) / u_unitsPerPixel;',
  '    } else if (k < 1.5) {',
  '        p.z += 11.0 + sin(u_time * 2.2 + seed * 30.0) * 1.2;',
  '    } else if (k < 3.5) {',
  '        float f = fract(seed * 7.31 + u_time * (0.03 + seed * 0.03));',
  '        float ang = seed * 6.2831853 + u_time * (0.25 + seed * 0.2);',
  '        p.xy = vec2(cos(ang) * a_extra.w * 1.5, sin(ang) * a_extra.w * 0.85);',
  '        lv = 2.0 + f * 2.9;',
  '        fade = sin(f * 3.1415926);',
  '    } else if (k < 4.5) {',
  '        lv = 2.0 - 2.0 * cos(u_time * 0.32);',
  '        p.z = 2.0;',
  '        px = size / u_unitsPerPixel;',
  '    } else {',
  '        vec2 d;',
  '        vec2 c = loopPos(seed + u_time * a_extra.w, ' + LANE_GLSL + ', d);',
  '        p.xy = c + rot(p.xy, d);',
  '    }',
  '    v_color = vec4(a_color.rgb, a_color.a * fade * levelDim(lv) * navDim());',
  '    v_kind = k;',
  '    v_seed = seed;',
  '    gl_PointSize = clamp(px, 2.0, 220.0) * u_pixelRatio;',
  '    gl_Position = u_mvpMatrix * vec4(p.xy, p.z + levelZ(lv), 1.0);',
  '}',
].join('\n');
var FS_POINTS = [
  FS_HEAD,
  'uniform float u_time;',
  'uniform float u_opacity;',
  'varying vec4 v_color;',
  'varying float v_kind;',
  'varying float v_seed;',
  'void main() {',
  '    vec2 q = gl_PointCoord * 2.0 - 1.0;',
  '    float r = length(q);',
  '    if (r > 1.0) { discard; }',
  '    float a;',
  '    if (v_kind < 0.5) {',
  '        float w = fract(r - u_time * 0.7 + v_seed);',
  '        a = smoothstep(0.0, 0.12, w) * (1.0 - smoothstep(0.12, 0.3, w)) * (1.0 - r) * 1.4',
  '            + exp(-r * 9.0) * 0.9;',
  '    } else if (v_kind < 1.5) {',
  '        float dm = abs(q.x) + abs(q.y);',
  '        a = (1.0 - smoothstep(0.5, 0.58, dm)) * smoothstep(0.22, 0.3, dm) + exp(-dm * 3.0) * 0.55;',
  '    } else if (v_kind < 4.5 && v_kind > 3.5) {',
  '        vec2 b = abs(q);',
  '        float m = max(b.x, b.y);',
  '        a = (1.0 - smoothstep(0.62, 0.7, m)) * 0.35 + (1.0 - smoothstep(0.04, 0.1, abs(m - 0.62))) * 1.2',
  '            + exp(-r * 2.0) * 0.4;',
  '    } else {',
  '        a = exp(-r * r * 5.0);',
  '    }',
  '    gl_FragColor = vec4(v_color.rgb, clamp(a * v_color.a * u_opacity, 0.0, 1.0));',
  '}',
].join('\n');

// ---- labels:canvas 图集里的文字,锚点投影后在屏幕空间展开成矩形,大小恒定 ----
// a_corner: 矩形角 (0~1, 0~1);a_extra = (宽 px, 高 px, 仅聚焦时显示, 上移 px)
var VS_LABEL = [
  'attribute vec3 a_pos;',
  'attribute float a_level;',
  'attribute vec2 a_corner;',
  'attribute vec2 a_uv;',
  'attribute vec4 a_extra;',
  'uniform mat4 u_mvpMatrix;',
  'uniform vec2 u_resolution;',
  STACK,
  'varying vec2 v_uv;',
  'varying float v_a;',
  'void main() {',
  '    vec4 c = u_mvpMatrix * vec4(a_pos.xy, a_pos.z + levelZ(a_level), 1.0);',
  '    vec2 off = vec2((a_corner.x - 0.5) * a_extra.x, (1.0 - a_corner.y) * a_extra.y + a_extra.w);',
  '    c.xy += off / u_resolution * 2.0 * c.w;',
  '    v_uv = a_uv;',
  // 标签只轻微压暗:导航时「我的车」「目标商铺」还要看得清
  '    v_a = levelDim(a_level) * mix(1.0, focusOn(a_level), a_extra.z) * (1.0 - u_nav * 0.3);',
  '    gl_Position = c;',
  '}',
].join('\n');
var FS_LABEL = [
  FS_HEAD,
  'uniform sampler2D u_tex;',
  'uniform float u_opacity;',
  'varying vec2 v_uv;',
  'varying float v_a;',
  'void main() {',
  '    gl_FragColor = texture2D(u_tex, v_uv) * v_a * u_opacity;',
  '}',
].join('\n');

// ================= 场景数据(假数据,程序生成) =================
// 楼层:0=B2 1=B1 2=F1 3=F2 4=F3。平面范围 x∈[-100,100],y∈[-65,65]
var FLOORS = [
  {code: 'B2', name: '停车场'},
  {code: 'B1', name: '停车场'},
  {code: 'F1', name: '精品 · 珠宝'},
  {code: 'F2', name: '潮流 · 服饰'},
  {code: 'F3', name: '餐饮 · 影院'},
];
var HX = 100,
  HY = 65;
var solid = new Mesh(),
  slab = new Mesh(),
  holo = new Mesh();
var pts = {p: [], c: [], l: [], e: []};
var labels = [];
var stats = {total: [0, 0], free: [0, 0], ev: 0, evFree: 0};
var MY_CAR = {lv: 0, x: 33, y: 55, id: 'B2-086'};

function point(p, c, lv, e) {
  pts.p.push(p[0], p[1], p[2]);
  pts.c.push(c[0], c[1], c[2], c[3]);
  pts.l.push(lv);
  pts.e.push(e[0], e[1], e[2], e[3]);
}
function label(text, x, y, z, lv, color, opt) {
  opt = opt || {};
  labels.push({
    text: text,
    x: x,
    y: y,
    z: z,
    lv: lv,
    color: color,
    focusOnly: opt.focusOnly ? 1 : 0,
    lift: opt.lift === undefined ? 14 : opt.lift,
    big: !!opt.big,
  });
}
function staticEx() {
  return [0, 0, 0, 0];
}
// 平铺在楼板上的细线(车位线、车道边线),宽 w
function flatLine(x0, y0, x1, y1, w, z, c, lv, ex) {
  var dx = x1 - x0,
    dy = y1 - y0;
  var len = Math.sqrt(dx * dx + dy * dy) || 1;
  var nx = ((-dy / len) * w) / 2,
    ny = ((dx / len) * w) / 2;
  var e0 = ex(0),
    e1 = ex(len);
  holo.quad(
    [
      [x0 + nx, y0 + ny, z],
      [x1 + nx, y1 + ny, z],
      [x1 - nx, y1 - ny, z],
      [x0 - nx, y0 - ny, z],
    ],
    c,
    lv,
    [e0, e1, e1, e0],
    [0, 0, 1],
  );
}
// 一辆车:车身 + 车厢,沿 +y 方向(停车位里)或 +x 方向(行驶中,由着色器旋转)
function car(cx, cy, alongX, color, lv, ex) {
  var L = 4.7,
    W = 2.2;
  var hx = alongX ? L : W,
    hy = alongX ? W : L;
  solid.box(cx - hx, cy - hy, cx + hx, cy + hy, 0.4, 2.4, color, lv, ex);
  var cl = alongX ? 2.6 : 1.9,
    cw = alongX ? 1.9 : 2.6;
  var off = alongX ? -0.4 : 0;
  solid.box(
    cx - cl + off,
    cy - cw,
    cx + cl + off,
    cy + cw,
    2.4,
    4.0,
    [color[0] * 0.55, color[1] * 0.55, color[2] * 0.6 + 0.08, 1],
    lv,
    ex,
  );
}

var CAR_COLORS = [
  '#e9eef5',
  '#1d2430',
  '#c8ccd4',
  '#b3263a',
  '#2f5fb8',
  '#6b7480',
  '#f2f2f2',
  '#121722',
];

// 一个车位:outline + 状态(busy 停车 / free 涟漪 / ev 充电)
function spot(lv, x0, y0, x1, y1, ev) {
  var cx = (x0 + x1) / 2,
    cy = (y0 + y1) / 2;
  var mine = lv === MY_CAR.lv && Math.abs(cx - MY_CAR.x) < 0.1 && Math.abs(cy - MY_CAR.y) < 0.1;
  var busy = mine || rand() < (ev ? 0.45 : 0.64);
  stats.total[lv]++;
  if (ev) {
    stats.ev++;
  }
  if (!busy) {
    stats.free[lv]++;
    if (ev) {
      stats.evFree++;
    }
  }
  var c = mine ? hex('#ff4fd8') : ev ? hex('#38c8ff') : busy ? hex('#ff4b6e', 0.7) : hex('#4dffb0');
  var s = rand();
  var ex = function () {
    return [2, busy ? 0 : 1, s, 0];
  };
  // 车位画成 U 形,开口朝向车道(车道在 y = ±LANE[1])
  var openAtY1 = (cy > 0 && cy < LANE[1]) || cy < -LANE[1];
  var yb = openAtY1 ? y0 : y1;
  flatLine(x0, y0, x0, y1, 0.35, 0.25, c, lv, ex);
  flatLine(x1, y0, x1, y1, 0.35, 0.25, c, lv, ex);
  flatLine(x0, yb, x1, yb, 0.35, 0.25, c, lv, ex);
  if (busy) {
    var color = mine ? hex('#ff4fd8') : hex(CAR_COLORS[Math.floor(rand() * CAR_COLORS.length)]);
    car(cx, cy, false, color, lv, staticEx);
    if (mine) {
      point([cx, cy, 6], hex('#ff4fd8', 1), lv, [1, 30, s, 0]);
      label('我的车 ' + MY_CAR.id, cx, cy, 6, lv, '#ff4fd8', {lift: 30});
    } else if (ev) {
      point([cx, cy, 5], hex('#38c8ff', 0.9), lv, [1, 16, s, 0]);
    }
  } else {
    point([cx, cy, 0.6], ev ? hex('#38c8ff') : hex('#4dffb0'), lv, [0, 9, s, 0]);
  }
}

function buildParking(lv) {
  var i, x;
  // 南北外侧两排:x∈[-90,90],每格宽 6
  for (i = 0; i < 30; i++) {
    x = -90 + i * 6;
    spot(lv, x, 48, x + 6, 62, false);
    spot(lv, x, -62, x + 6, -48, lv === 1 && i < 6);
  }
  // 中岛两排,中间 x∈[-8,8] 留给观光电梯厅
  for (i = 0; i < 8; i++) {
    x = -56 + i * 6;
    spot(lv, x, 0, x + 6, 14, lv === 0 && i < 4);
    spot(lv, x, -14, x + 6, 0, false);
    x = 8 + i * 6;
    spot(lv, x, 0, x + 6, 14, false);
    spot(lv, x, -14, x + 6, 0, false);
  }
  // 立柱
  var pillar = hex('#2a3d58');
  for (x = -84; x <= 84; x += 28) {
    solid.box(x - 1.3, 45.5, x + 1.3, 48.1, 0, 8, pillar, lv, staticEx);
    solid.box(x - 1.3, -48.1, x + 1.3, -45.5, 0, 8, pillar, lv, staticEx);
  }
  solid.box(-62, -2, -58.5, 2, 0, 8, pillar, lv, staticEx);
  solid.box(58.5, -2, 62, 2, 0, 8, pillar, lv, staticEx);
  // 环形车道:两侧边线 + 中心流光虚线(虚线的 dist 让光沿行车方向流动)
  var lane = hex('#ffcc33', 0.8),
    edgeC = hex('#2f8fff', 0.7);
  var corners = [
    [-LANE[0], -LANE[1]],
    [LANE[0], -LANE[1]],
    [LANE[0], LANE[1]],
    [-LANE[0], LANE[1]],
  ];
  var dist = 0;
  for (i = 0; i < 4; i++) {
    var a = corners[i],
      b = corners[(i + 1) % 4];
    var segLen = Math.abs(b[0] - a[0]) + Math.abs(b[1] - a[1]);
    (function (d0) {
      flatLine(a[0], a[1], b[0], b[1], 0.5, 0.3, lane, lv, function (t) {
        return [3, 0, 0, d0 + t];
      });
    })(dist);
    dist += segLen;
  }
  flatLine(-90, 40, 90, 40, 0.3, 0.2, edgeC, lv, function () {
    return [2, 0, 0, 0];
  });
  flatLine(-90, -40, 90, -40, 0.3, 0.2, edgeC, lv, function () {
    return [2, 0, 0, 0];
  });
  flatLine(-66, 22, 66, 22, 0.3, 0.2, edgeC, lv, function () {
    return [2, 0, 0, 0];
  });
  flatLine(-66, -22, 66, -22, 0.3, 0.2, edgeC, lv, function () {
    return [2, 0, 0, 0];
  });
  // 在车道上跑的车:局部坐标沿 +x,extra = (1, 相位, 速度, 0)
  var n = lv === 0 ? 3 : 2;
  for (i = 0; i < n; i++) {
    // 环形车道周长 4 * (hx + hy),几辆车均匀错开
    var phase = (i / n) * 4 * (LANE[0] + LANE[1]) + lv * 37;
    var speed = 9 + i * 1.5;
    var color = hex(['#ffd23f', '#e9eef5', '#ff6b3d'][i % 3]);
    car(
      0,
      0,
      true,
      color,
      lv,
      (function (ph, sp) {
        return function () {
          return [1, ph, sp, 0];
        };
      })(phase, speed),
    );
    point([4.9, 1.4, 1.4], hex('#fff6c8'), lv, [5, 9, phase, speed]);
    point([4.9, -1.4, 1.4], hex('#fff6c8'), lv, [5, 9, phase, speed]);
    point([-4.9, 1.4, 1.4], hex('#ff2a3a'), lv, [5, 6, phase, speed]);
    point([-4.9, -1.4, 1.4], hex('#ff2a3a'), lv, [5, 6, phase, speed]);
  }
}
buildParking(0);
buildParking(1);

// ---- 商场楼层:外圈商铺 + 走廊里的中岛 / 绿植 ----
var SHOP_PALETTE = {
  2: ['#ff4fd8', '#ffc850', '#9fe8ff', '#ff8fb8'],
  3: ['#22e8ff', '#8a5cff', '#6dff9e', '#3d8bff'],
  4: ['#ff7a30', '#ff3b5c', '#ffd23f', '#ff9f6b'],
};
var SHOP_NAMES = {
  2: ['LUMIÈRE 珠宝', '星辰美妆', '极简腕表', 'AURA 香氛', '月光咖啡'],
  3: ['极光运动', '赛博女装', '量子数码', '漫游者户外', '像素潮玩'],
  4: ['赤焰火锅', '深海料理', '银河甜品', '霓虹烤肉', '宇宙电竞'],
};
var TARGET_SHOP = {lv: 3, x: -38, name: 'NEON 潮牌旗舰店'};

function shop(lv, x0, y0, x1, y1, h, color, name, opt) {
  var s = rand();
  var c = hex(color);
  holo.box(x0 + 0.8, y0 + 0.8, x1 - 0.8, y1 - 0.8, 0.2, h, c, lv, function (face, u, v) {
    return face < 4 ? [0, v, s, u] : [1, u, s, v];
  });
  var cx = (x0 + x1) / 2,
    cy = (y0 + y1) / 2;
  point([cx, cy, h], c, lv, [1, opt && opt.target ? 30 : 20, s, 0]);
  if (name) {
    label(name, cx, cy, h + 12, lv, color, {focusOnly: !(opt && opt.target), lift: 12});
  }
}

function shopRow(lv, y0, y1, names, skipTo) {
  var pal = SHOP_PALETTE[lv];
  var x = skipTo || -96;
  while (x < 94) {
    var w = Math.min(18 + Math.floor(rand() * 4) * 5, 96 - x);
    if (96 - x - w < 12) {
      w = 96 - x;
    }
    var isTarget = lv === TARGET_SHOP.lv && y0 > 0 && x <= TARGET_SHOP.x && x + w > TARGET_SHOP.x;
    var name = isTarget ? TARGET_SHOP.name : rand() < 0.55 ? names.shift() : null;
    shop(
      lv,
      x,
      y0,
      x + w,
      y1,
      8.5,
      isTarget ? '#ff4fd8' : pal[Math.floor(rand() * pal.length)],
      name,
      {target: isTarget},
    );
    x += w;
  }
}

function buildMall(lv) {
  var names = SHOP_NAMES[lv].slice();
  var pal = SHOP_PALETTE[lv];
  shopRow(lv, 38, 62, names);
  if (lv === 4) {
    // F3 南侧整块是影城
    shop(lv, -96, -62, -20, -38, 10.5, '#ff3b5c', '宇宙影城 IMAX');
    shopRow(lv, -62, -38, names, -20);
  } else {
    shopRow(lv, -62, -38, names);
  }
  shop(lv, -96, -34, -72, 0, 8.5, pal[0], names.shift());
  shop(lv, -96, 0, -72, 34, 8.5, pal[1], null);
  shop(lv, 72, -34, 96, 0, 8.5, pal[2], names.shift());
  shop(lv, 72, 0, 96, 34, 8.5, pal[3], null);
  // 走廊里的中岛柜台和绿植
  var kiosk = hex('#d9e6f2'),
    leaf = hex('#2bd47a'),
    pot = hex('#24304a');
  [
    [-56, 27],
    [56, -27],
    [52, 27],
    [-52, -33],
  ].forEach(function (k) {
    solid.box(k[0] - 4, k[1] - 1.6, k[0] + 4, k[1] + 1.6, 0, 2.2, kiosk, lv, staticEx);
  });
  [
    [-66, 30],
    [66, 30],
    [-66, -30],
    [66, -30],
    [-24, 35],
    [30, -32],
  ].forEach(function (k) {
    solid.box(k[0] - 1.2, k[1] - 1.2, k[0] + 1.2, k[1] + 1.2, 0, 1.4, pot, lv, staticEx);
    solid.box(k[0] - 1.6, k[1] - 1.6, k[0] + 1.6, k[1] + 1.6, 1.4, 4.2, leaf, lv, staticEx);
  });
}
buildMall(2);
buildMall(3);
buildMall(4);

// ---- 楼板 ----
for (var lv = 0; lv < 5; lv++) {
  slab.box(-HX, -HY, HX, HY, -1.6, 0, [0, 0, 0, 1], lv, function (face, u, v) {
    return face < 4 ? [1, v, 0, u] : [0, 0, 0, 0];
  });
  var fl = FLOORS[lv];
  var text = fl.code + ' ' + fl.name + (lv < 2 ? ' · 空位 ' + stats.free[lv] : '');
  // 锚在楼板西侧边中点:锚在角上的话,倾斜视角下标签会落到相邻楼层的位置,容易看错层
  label(text, -HX + 2, 0, 0, lv, lv < 2 ? '#4dffb0' : '#59f3ff', {lift: 6, big: true});
}

// ---- 跨层的斜面:车库坡道、扶梯。四个角各自给楼层,z 由着色器按楼层插值 ----
function ramp(xa, ya, xb, yb, w, lvA, lvB, color, alongX) {
  // a -> b 是行进方向,w 是宽度
  var len = Math.sqrt((xb - xa) * (xb - xa) + (yb - ya) * (yb - ya));
  var nx = alongX ? 0 : w / 2,
    ny = alongX ? w / 2 : 0;
  var dist = len + Math.abs(lvB - lvA) * 20;
  var s = rand();
  holo.quad(
    [
      [xa - nx, ya - ny, 0.4],
      [xb - nx, yb - ny, 0.4],
      [xb + nx, yb + ny, 0.4],
      [xa + nx, ya + ny, 0.4],
    ],
    hex(color),
    [lvA, lvB, lvB, lvA],
    [
      [4, -1, s, 0],
      [4, -1, s, dist],
      [4, 1, s, dist],
      [4, 1, s, 0],
    ],
  );
}
ramp(-92, -24, -92, 24, 12, 0, 1, '#ffb43f', false);
ramp(92, 24, 92, -24, 12, 1, 2, '#ffb43f', false);
ramp(-40, -25, -16, -25, 4, 2, 3, '#59f3ff', true);
ramp(-16, -31, -40, -31, 4, 3, 2, '#8a5cff', true);
ramp(16, 25, 40, 25, 4, 3, 4, '#59f3ff', true);
ramp(40, 31, 16, 31, 4, 4, 3, '#8a5cff', true);
label('入口坡道', -92, -24, 1, 0, '#ffb43f', {lift: 8});

// ---- 观光电梯:贯穿五层的玻璃光柱,轿厢是 points 层里上下跑的方块 ----
(function () {
  var R = 4,
    N = 16,
    gold = hex('#ffd36b');
  for (var i = 0; i < N; i++) {
    var a0 = (i / N) * Math.PI * 2,
      a1 = ((i + 1) / N) * Math.PI * 2;
    var p0 = [Math.cos(a0) * R, Math.sin(a0) * R],
      p1 = [Math.cos(a1) * R, Math.sin(a1) * R];
    holo.quad(
      [
        [p0[0], p0[1], 0],
        [p1[0], p1[1], 0],
        [p1[0], p1[1], 14],
        [p0[0], p0[1], 14],
      ],
      gold,
      [0, 0, 4, 4],
      [
        [5, 0, 0, i / N],
        [5, 0, 0, (i + 1) / N],
        [5, 1, 0, (i + 1) / N],
        [5, 1, 0, i / N],
      ],
      [Math.cos(a0), Math.sin(a0), 0],
    );
  }
  point([0, 0, 0], gold, 0, [4, 9, 0, 0]);
  label('全景观光电梯', 0, 0, 16, 4, '#ffd36b', {lift: 10});
})();

// ---- 中庭里盘旋上升的粒子 ----
for (var pi = 0; pi < 260; pi++) {
  var ps = rand();
  point([0, 0, 0], pi % 3 ? hex('#7fe9ff') : hex('#ff8be8'), 2, [
    3,
    2 + rand() * 4,
    ps,
    6 + rand() * 20,
  ]);
}

// ---- 导航路线:我的车 -> 车道 -> 观光电梯 -> F2 -> 目标商铺 ----
// 每个点 [x, y, 楼层];楼层变化的那段是电梯里的竖直段
var ROUTE = [
  [MY_CAR.x, 48, 0],
  [MY_CAR.x, LANE[1], 0],
  [0, LANE[1], 0],
  [0, 5, 0],
  [0, 5, 3],
  [0, 24, 3],
  [0, 31, 3],
  [TARGET_SHOP.x, 31, 3],
  [TARGET_SHOP.x, 38, 3],
];
var route = {p: [], l: [], e: [], total: 0};
(function () {
  var W = 3,
    Z = 1.2,
    NOMINAL_SPREAD = 40;
  function v(x, y, lv, across, d) {
    route.p.push(x, y, Z);
    route.l.push(lv);
    route.e.push(across, d);
  }
  for (var i = 0; i < ROUTE.length - 1; i++) {
    var a = ROUTE[i],
      b = ROUTE[i + 1];
    var d0 = route.total,
      len,
      nx,
      ny;
    if (a[2] !== b[2]) {
      // 竖直段:带子立在 xz 平面里,倾斜视角下才看得见
      len = Math.abs(b[2] - a[2]) * NOMINAL_SPREAD;
      nx = W;
      ny = 0;
    } else {
      len = Math.sqrt((b[0] - a[0]) * (b[0] - a[0]) + (b[1] - a[1]) * (b[1] - a[1]));
      nx = (-(b[1] - a[1]) / len) * W;
      ny = ((b[0] - a[0]) / len) * W;
    }
    var d1 = d0 + len;
    v(a[0] + nx, a[1] + ny, a[2], 1, d0);
    v(b[0] + nx, b[1] + ny, b[2], 1, d1);
    v(b[0] - nx, b[1] - ny, b[2], -1, d1);
    v(a[0] + nx, a[1] + ny, a[2], 1, d0);
    v(b[0] - nx, b[1] - ny, b[2], -1, d1);
    v(a[0] - nx, a[1] - ny, a[2], -1, d0);
    route.total = d1;
  }
})();

// ---- 文字图集:每个标签一行,画成发光胶囊;屏幕上按 1/2 尺寸显示,高分屏也清晰 ----
var ROW_H = 64,
  ATLAS_W = 1024;
var atlas = document.createElement('canvas');
atlas.width = ATLAS_W;
atlas.height = 2048;
var lab = {p: [], l: [], corner: [], uv: [], e: []};
(function () {
  var g = atlas.getContext('2d');
  function pill(x, y, w, h, r) {
    g.beginPath();
    g.moveTo(x + r, y);
    g.lineTo(x + w - r, y);
    g.arc(x + w - r, y + r, r, -Math.PI / 2, Math.PI / 2);
    g.lineTo(x + r, y + h);
    g.arc(x + r, y + r, r, Math.PI / 2, Math.PI * 1.5);
    g.closePath();
  }
  var rows = Math.min(labels.length, atlas.height / ROW_H);
  for (var i = 0; i < rows; i++) {
    var L = labels[i];
    var y = i * ROW_H;
    g.font =
      (L.big ? '600 30px' : '500 26px') +
      ' -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif';
    var w = Math.min(ATLAS_W - 8, Math.ceil(g.measureText(L.text).width) + 44);
    pill(4, y + 8, w, ROW_H - 16, (ROW_H - 16) / 2);
    g.fillStyle = 'rgba(4, 14, 30, 0.82)';
    g.fill();
    g.lineWidth = 2.5;
    g.strokeStyle = L.color;
    g.shadowColor = L.color;
    g.shadowBlur = 8;
    g.stroke();
    g.shadowBlur = 0;
    g.fillStyle = L.color;
    g.beginPath();
    g.arc(24, y + ROW_H / 2, 5, 0, Math.PI * 2);
    g.fill();
    g.fillStyle = '#ffffff';
    g.textBaseline = 'middle';
    g.fillText(L.text, 36, y + ROW_H / 2 + 1);
    // 两个三角形,6 个顶点共享锚点,靠 a_corner 在屏幕空间展开
    var u1 = (w + 8) / ATLAS_W,
      v0 = y / atlas.height,
      v1 = (y + ROW_H) / atlas.height;
    var cs = [
      [0, 0],
      [1, 0],
      [1, 1],
      [0, 0],
      [1, 1],
      [0, 1],
    ];
    for (var k = 0; k < 6; k++) {
      lab.p.push(L.x, L.y, L.z);
      lab.l.push(L.lv);
      lab.corner.push(cs[k][0], cs[k][1]);
      lab.uv.push(cs[k][0] * u1, v0 + cs[k][1] * (v1 - v0));
      lab.e.push((w + 8) / 2, ROW_H / 2, L.focusOnly, L.lift);
    }
  }
})();

// ================= 图层 =================
// 驱动整栋楼形态的共享 uniform:一开始楼层叠在一起(spread 0),入场动画把它们升起来
var U = {u_spread: 0, u_focus: -1, u_mix: 0, u_nav: 0};
var EXTRA = {u_hlFree: 0, u_progress: 0};
var stackLayers = [];
var layers = {};

function showError(msg) {
  $('err').style.display = 'block';
  $('err').innerHTML = msg;
}
function attr(data, size) {
  return {data: f32(data), size: size};
}
function addLayer(name, opt, inStack) {
  opt.referCenter = center;
  opt.onError = function (err) {
    showError('图层 ' + name + ' 出错 [' + err.type + ']<br>' + String(err.message).split('\n')[0]);
  };
  var layer = new BMap.WebGLCustomLayer(opt);
  map.addLayer(layer);
  layers[name] = layer;
  if (inStack) {
    stackLayers.push(layer);
  }
  return layer;
}
var DEPTH_ON = {depthTest: true, depthWrite: true, depthFunc: 'lequal', blend: 'normal'};
var DEPTH_READ = {depthTest: true, depthWrite: false, depthFunc: 'lequal'};
function assign(a, b) {
  var o = {};
  for (var k in a) {
    o[k] = a[k];
  }
  for (k in b) {
    o[k] = b[k];
  }
  return o;
}

function buildLayers() {
  var G = 1500;
  addLayer('ground', {
    zIndex: 1,
    animation: true,
    vertexShader: VS_GROUND,
    fragmentShader: FS_GROUND,
    attributes: {a_pos: attr([-G, -G, G, -G, G, G, -G, G], 2)},
    indices: [0, 1, 2, 0, 2, 3],
    // 舞台不做深度测试,直接盖住底图(含道路)
    renderState: {blend: 'normal', depthTest: false, depthWrite: false},
    draw: {mode: 'TRIANGLES'},
  });
  addLayer(
    'solid',
    {
      zIndex: 2,
      animation: true,
      vertexShader: VS_SOLID,
      fragmentShader: FS_SOLID,
      attributes: {
        a_pos: attr(solid.p, 3),
        a_normal: attr(solid.n, 3),
        a_color: attr(solid.c, 4),
        a_level: attr(solid.l, 1),
        a_extra: attr(solid.e, 4),
      },
      uniforms: U,
      renderState: DEPTH_ON,
      draw: {mode: 'TRIANGLES'},
    },
    true,
  );
  addLayer(
    'slab',
    {
      zIndex: 3,
      animation: true,
      vertexShader: VS_SLAB,
      fragmentShader: FS_SLAB,
      attributes: {a_pos: attr(slab.p, 3), a_level: attr(slab.l, 1), a_extra: attr(slab.e, 4)},
      uniforms: U,
      renderState: assign(DEPTH_READ, {blend: 'normal'}),
      draw: {mode: 'TRIANGLES'},
    },
    true,
  );
  addLayer(
    'holo',
    {
      zIndex: 4,
      animation: true,
      vertexShader: VS_HOLO,
      fragmentShader: FS_HOLO,
      attributes: {
        a_pos: attr(holo.p, 3),
        a_color: attr(holo.c, 4),
        a_level: attr(holo.l, 1),
        a_extra: attr(holo.e, 4),
      },
      uniforms: assign(U, {u_hlFree: 0}),
      renderState: assign(DEPTH_READ, {blend: 'additive'}),
      draw: {mode: 'TRIANGLES'},
    },
    true,
  );
  addLayer(
    'route',
    {
      zIndex: 5,
      animation: true,
      vertexShader: VS_ROUTE,
      fragmentShader: FS_ROUTE,
      attributes: {a_pos: attr(route.p, 3), a_level: attr(route.l, 1), a_extra: attr(route.e, 2)},
      uniforms: assign(U, {u_progress: 0, u_total: route.total}),
      // 导航线不参与深度、画在所有楼层之上:它和车道流光、电梯光柱走的是同一条路,被遮住就看不出来了
      renderState: {blend: 'normal'},
      draw: {mode: 'TRIANGLES'},
    },
    true,
  );
  addLayer(
    'points',
    {
      zIndex: 6,
      animation: true,
      vertexShader: VS_POINTS,
      fragmentShader: FS_POINTS,
      attributes: {
        a_pos: attr(pts.p, 3),
        a_color: attr(pts.c, 4),
        a_level: attr(pts.l, 1),
        a_extra: attr(pts.e, 4),
      },
      uniforms: assign(U, {u_hlFree: 0}),
      renderState: assign(DEPTH_READ, {blend: 'additive'}),
      draw: {mode: 'POINTS'},
    },
    true,
  );
  addLayer(
    'labels',
    {
      zIndex: 7,
      vertexShader: VS_LABEL,
      fragmentShader: FS_LABEL,
      attributes: {
        a_pos: attr(lab.p, 3),
        a_level: attr(lab.l, 1),
        a_corner: attr(lab.corner, 2),
        a_uv: attr(lab.uv, 2),
        a_extra: attr(lab.e, 4),
      },
      uniforms: U,
      textures: {u_tex: {source: atlas, flipY: false, premultiplyAlpha: true}},
      renderState: {blend: 'premultiplied'},
      draw: {mode: 'TRIANGLES'},
    },
    true,
  );
}

if (!BMap.WebGLCustomLayer) {
  showError('当前环境未提供 WebGLCustomLayer 图层能力,无法渲染室内数字孪生。');
} else {
  buildLayers();
}

// ================= 动画:补间几个 uniform,顶点数据一个字节都不重传 =================
var tweens = {};
function ease(t) {
  return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
}
// key 在 U(所有楼层图层共享)或 EXTRA(个别图层专用)里
function tween(key, to, dur, done) {
  var bag = key in U ? U : EXTRA;
  tweens[key] = {bag: bag, from: bag[key], to: to, t0: performance.now(), dur: dur, done: done};
}
function pushUniforms(changed) {
  var i;
  if (changed.stack) {
    for (i = 0; i < stackLayers.length; i++) {
      stackLayers[i].setUniforms(U);
    }
  }
  if (changed.u_hlFree && layers.holo) {
    layers.holo.setUniform('u_hlFree', EXTRA.u_hlFree);
    layers.points.setUniform('u_hlFree', EXTRA.u_hlFree);
  }
  if (changed.u_progress && layers.route) {
    layers.route.setUniform('u_progress', EXTRA.u_progress);
  }
}
function tick(now) {
  var changed = {};
  for (var key in tweens) {
    var tw = tweens[key];
    // rAF 的时间戳是帧开始时刻,可能早于 tween() 里记的 performance.now(),不夹住会出现负进度
    var t = Math.max(0, Math.min(1, (now - tw.t0) / tw.dur));
    tw.bag[key] = tw.from + (tw.to - tw.from) * ease(t);
    changed[tw.bag === U ? 'stack' : key] = true;
    if (t >= 1) {
      delete tweens[key];
      if (tw.done) {
        tw.done();
      }
    }
  }
  pushUniforms(changed);
  if (touring) {
    heading += 0.06;
    map.setHeading(heading, {noAnimation: true});
  }
  requestAnimationFrame(tick);
}

function tip(text) {
  var el = $('tip');
  el.innerHTML = text;
  el.className = 'im-glass im-show';
  clearTimeout(tip.timer);
  tip.timer = setTimeout(function () {
    el.className = 'im-glass';
  }, 2600);
}

// ---- 楼层聚焦:先把上次抬起的楼层放回去,再切换焦点抬起新的 ----
var focus = -1;
function setFocus(lv) {
  if (lv === focus) {
    return;
  }
  focus = lv;
  var btns = $('floors').getElementsByTagName('button');
  for (var i = 0; i < btns.length; i++) {
    btns[i].className = Number(btns[i].getAttribute('data-lv')) === lv ? 'im-on' : '';
  }
  tween('u_mix', 0, U.u_focus < 0 ? 1 : 320, function () {
    U.u_focus = lv;
    pushUniforms({stack: true});
    if (lv >= 0) {
      tween('u_mix', 1, 700);
      var fl = FLOORS[lv];
      tip(
        '<b>' +
          fl.code +
          '</b> ' +
          fl.name +
          (lv < 2
            ? ' · 空位 <b style="color:#4dffb0">' + stats.free[lv] + '</b> / ' + stats.total[lv]
            : ''),
      );
    }
  });
}
$('floors').onclick = function (e) {
  var btn = e.target.closest && e.target.closest('button');
  if (btn) {
    stopTour();
    setFocus(Number(btn.getAttribute('data-lv')));
  }
};

var exploded = true;
$('b-explode').onclick = function () {
  exploded = !exploded;
  this.className = exploded ? 'im-on' : '';
  this.innerHTML = exploded ? '楼层展开' : '楼层合拢';
  tween('u_spread', exploded ? 40 : 13, 1100);
};

// 寻车导航是开关:开启时压暗整栋楼并逐段揭示路线,再点一次收起
var navOn = false;
function playRoute() {
  stopTour();
  setFocus(-1);
  if (!exploded) {
    $('b-explode').onclick();
  }
  navOn = true;
  EXTRA.u_progress = 0;
  pushUniforms({u_progress: true});
  $('b-route').className = 'im-on';
  tip(
    '导航:<b style="color:#ff4fd8">我的车 ' +
      MY_CAR.id +
      '</b> → 观光电梯 → F2 ' +
      '<b style="color:#ff4fd8">' +
      TARGET_SHOP.name +
      '</b>',
  );
  tween('u_nav', 1, 600);
  tween('u_progress', 1, 4200);
}
function stopRoute() {
  navOn = false;
  $('b-route').className = '';
  tween('u_nav', 0, 600);
  tween('u_progress', 0, 600);
}
$('b-route').onclick = function () {
  if (navOn) {
    stopRoute();
  } else {
    playRoute();
  }
};

var hlFree = false;
$('b-free').onclick = function () {
  hlFree = !hlFree;
  this.className = hlFree ? 'im-on' : '';
  tween('u_hlFree', hlFree ? 1 : 0, 500);
  if (hlFree) {
    tip(
      '空闲车位 <b style="color:#4dffb0">' +
        (stats.free[0] + stats.free[1]) +
        '</b> 个,' +
        '充电桩空闲 <b style="color:#38c8ff">' +
        stats.evFree +
        '</b> 个',
    );
  }
};

// ---- 自动漫游:镜头缓慢环绕,楼层轮流聚焦 ----
var touring = false,
  heading = -22,
  tourTimer = null,
  tourIdx = 0;
var TOUR = [-1, 0, 1, 2, 3, 4];
function stopTour() {
  touring = false;
  clearInterval(tourTimer);
  $('b-tour').className = '';
}
$('b-tour').onclick = function () {
  if (touring) {
    stopTour();
    return;
  }
  touring = true;
  heading = map.getHeading ? map.getHeading() : heading;
  this.className = 'im-on';
  tourTimer = setInterval(function () {
    tourIdx = (tourIdx + 1) % TOUR.length;
    setFocus(TOUR[tourIdx]);
  }, 3600);
};

// ---- HUD:统计、各层负荷、时钟、跳动的客流 ----
(function () {
  var free = stats.free[0] + stats.free[1],
    total = stats.total[0] + stats.total[1];
  $('s-free').innerHTML = free + '<small>/' + total + '</small>';
  $('s-ev').innerHTML = stats.evFree + '<small>/' + stats.ev + '</small>';
  var loads = [
    1 - stats.free[0] / stats.total[0],
    1 - stats.free[1] / stats.total[1],
    0.72,
    0.58,
    0.86,
  ];
  var colors = ['#4dffb0', '#4dffb0', '#ff4fd8', '#22e8ff', '#ff7a30'];
  var html = '';
  for (var i = 4; i >= 0; i--) {
    html +=
      '<div class="im-bar"><b>' +
      FLOORS[i].code +
      '</b><div class="im-track"><div class="im-fill" id="bar' +
      i +
      '" style="width:0;background:' +
      colors[i] +
      ';box-shadow:0 0 8px ' +
      colors[i] +
      '"></div></div>' +
      '<span class="im-num" id="num' +
      i +
      '">' +
      Math.round(loads[i] * 100) +
      '%</span></div>';
  }
  $('bars').innerHTML = html;
  setTimeout(function () {
    for (var j = 0; j < 5; j++) {
      $('bar' + j).style.width = Math.round(loads[j] * 100) + '%';
    }
  }, 300);
  var flow = 12846;
  function pad(n) {
    return n < 10 ? '0' + n : String(n);
  }
  function update() {
    var d = new Date();
    $('clock').innerHTML =
      pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds());
    flow += Math.floor(Math.random() * 9) - 2;
    $('s-flow').innerHTML = flow.toLocaleString();
    // 商场楼层的负荷做点小幅波动,看起来像实时数据
    for (var k = 2; k < 5; k++) {
      loads[k] = Math.max(0.3, Math.min(0.97, loads[k] + (Math.random() - 0.5) * 0.04));
      $('bar' + k).style.width = Math.round(loads[k] * 100) + '%';
      $('num' + k).innerHTML = Math.round(loads[k] * 100) + '%';
    }
  }
  update();
  setInterval(update, 1000);
})();

// ---- 入场:楼层从地面依次升起;寻车导航等用户点按钮再开 ----
if (BMap.WebGLCustomLayer) {
  requestAnimationFrame(tick);
  setTimeout(function () {
    tween('u_spread', 40, 2400);
  }, 500);
}
预览