fellwing / osm.js
CyberMax-tools's picture
Deploy fellwing
8f3ff28 verified
Raw History Blame Contribute Delete
15.6 kB
// Fellwing 3D buildings: OpenStreetMap building footprints and heights from OpenFreeMap's free vector tiles (OpenMapTiles
// schema, no key, no usage limits, commercial use allowed with the attribution "OpenFreeMap © OpenMapTiles Data from
// OpenStreetMap"). A tiny Mapbox Vector Tile decoder reads the `building` layer of each zoom-14 tile near the aircraft and
// extrudes every footprint (render_height / render_min_height) into one merged mesh per tile, sat on the real ground.
// (c) 2026 CyberMax. All rights reserved.
import * as THREE from 'three';
import { ATMOS_GLSL } from './atmos.js';
export const TILEJSON = 'https://tiles.openfreemap.org/planet';
export const CREDIT = 'OpenFreeMap © OpenMapTiles · Data © OpenStreetMap contributors (ODbL)';
const BZ = 14;
// ---------------------------------------------------------------- Mapbox Vector Tile (protobuf) decoder, pure
class PBF {
constructor(buf) { this.b = buf; this.p = 0; this.end = buf.length; }
varint() { let r = 0, s = 0, c; do { c = this.b[this.p++]; if (s < 28) r |= (c & 0x7f) << s; else r += (c & 0x7f) * 2 ** s; s += 7; } while (c >= 0x80); return r; }
skip(t) { if (t === 0) this.varint(); else if (t === 1) this.p += 8; else if (t === 2) this.p += this.varint(); else if (t === 5) this.p += 4; else throw new Error('pbf wire type ' + t); }
bytes() { const n = this.varint(), s = this.p; this.p += n; return this.b.subarray(s, s + n); }
str() { return new TextDecoder().decode(this.bytes()); }
}
const zz = (n) => (n >>> 1) ^ -(n & 1);
function value(b) {
const r = new PBF(b); let v = null;
while (r.p < r.end) {
const k = r.varint(), f = k >> 3, t = k & 7;
if (f === 1) v = r.str();
else if (f === 2) { v = new DataView(r.b.buffer, r.b.byteOffset + r.p, 4).getFloat32(0, true); r.p += 4; }
else if (f === 3) { v = new DataView(r.b.buffer, r.b.byteOffset + r.p, 8).getFloat64(0, true); r.p += 8; }
else if (f === 4 || f === 5) v = r.varint();
else if (f === 6) v = zz(r.varint());
else if (f === 7) v = !!r.varint();
else r.skip(t);
}
return v;
}
/** Decode the named layers of a vector tile: { name: { extent, features: [{ type, tags: {..}, geom: [[x, y, ...], ...] }] } } */
export function decodeMVT(buf, want = null) {
const r = new PBF(buf instanceof Uint8Array ? buf : new Uint8Array(buf)); const out = {};
while (r.p < r.end) {
const k = r.varint();
if (k >> 3 !== 3 || (k & 7) !== 2) { r.skip(k & 7); continue; }
const L = new PBF(r.bytes());
let name = '', extent = 4096; const keys = [], vals = [], feats = [];
while (L.p < L.end) {
const kk = L.varint(), f = kk >> 3, t = kk & 7;
if (f === 1) name = L.str(); else if (f === 2) feats.push(L.bytes()); else if (f === 3) keys.push(L.str()); else if (f === 4) vals.push(value(L.bytes())); else if (f === 5) extent = L.varint(); else L.skip(t);
}
if (want && !want.includes(name)) continue;
const features = feats.map((fb) => {
const F = new PBF(fb); let type = 0, tags = [], geo = [];
while (F.p < F.end) {
const kk = F.varint(), f = kk >> 3, t = kk & 7;
if (f === 2) { const e = F.varint() + F.p; while (F.p < e) tags.push(F.varint()); }
else if (f === 3) type = F.varint();
else if (f === 4) { const e = F.varint() + F.p; while (F.p < e) geo.push(F.varint()); }
else F.skip(t);
}
const tg = {}; for (let i = 0; i + 1 < tags.length; i += 2) tg[keys[tags[i]]] = vals[tags[i + 1]];
// geometry commands -> rings / lines of flat [x, y, x, y...]
const rings = []; let x = 0, y = 0, cur = null;
for (let i = 0; i < geo.length;) {
const c = geo[i++], id = c & 7, n = c >> 3;
if (id === 7) { if (cur) rings.push(cur); cur = null; continue; }
for (let j = 0; j < n; j++) {
x += zz(geo[i++]); y += zz(geo[i++]);
if (id === 1) { if (cur) rings.push(cur); cur = [x, y]; } else cur.push(x, y);
}
}
if (cur && type !== 3) rings.push(cur);
return { type, tags: tg, geom: rings };
});
out[name] = { extent, features };
}
return out;
}
/** Signed area of a flat ring in tile coordinates (y down): > 0 = exterior ring (MVT spec 2.1). */
export function ringArea(r) { let a = 0; for (let i = 0, n = r.length; i < n; i += 2) { const j = (i + 2) % n; a += r[i] * r[j + 1] - r[j] * r[i + 1]; } return a / 2; }
/** Group rings into polygons [outer, ...holes]. */
export function polygons(rings) {
const out = [];
for (const r of rings) { if (r.length < 6) continue; const a = ringArea(r); if (a > 0) out.push([r]); else if (out.length && a < 0) out[out.length - 1].push(r); }
return out;
}
// ---------------------------------------------------------------- building shader
const VERT = /* glsl */`
attribute vec3 aCol; attribute vec2 aWin;
uniform vec3 uCam;
varying vec3 vW; varying vec3 vN; varying vec3 vCol; varying vec2 vWin;
${ATMOS_GLSL.split('vec3 skyColor')[0]}
#include <common>
#include <logdepthbuf_pars_vertex>
void main() {
vec4 w = modelMatrix * vec4(position, 1.0);
vW = w.xyz; vN = normal; vCol = aCol; vWin = aWin;
vec2 d = w.xz - uCam.xz; w.y -= dot(d, d) / (2.0 * R_EARTH);
gl_Position = projectionMatrix * viewMatrix * w;
#include <logdepthbuf_vertex>
}`;
const FRAG = /* glsl */`
uniform vec3 uCam; uniform vec3 uAmbSky; uniform vec3 uAmbGnd;
varying vec3 vW; varying vec3 vN; varying vec3 vCol; varying vec2 vWin;
${ATMOS_GLSL}
float h21(vec2 p) { p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 45.32); return fract(p.x * p.y); }
#include <logdepthbuf_pars_fragment>
void main() {
#include <logdepthbuf_fragment>
vec3 n = normalize(vN);
vec3 col = vCol;
float dist = length(vW - uCam);
float wall = 1.0 - step(0.5, abs(n.y));
// windows: floors every 3.3 m, bays every 2.6 m along the facade; fade to an average tone far away
vec2 cell = vec2(vWin.x / 2.6, vWin.y / 3.3);
vec2 f = fract(cell);
float win = step(0.18, f.x) * step(f.x, 0.82) * step(0.28, f.y) * step(f.y, 0.78) * wall * step(2.0, vWin.y);
float far = smoothstep(600.0, 2600.0, dist);
float glass = mix(win, 0.38 * wall, far);
vec3 v = normalize(uCam - vW);
vec3 r = reflect(-v, n);
vec3 sky = skyColor(normalize(vec3(r.x, abs(r.y) * 0.6 + 0.05, r.z)));
float ndl = dot(n, uSun);
float diff = clamp((ndl + 0.1) / 1.1, 0.0, 1.0);
vec3 light = uSunCol * diff * 0.44 + mix(uAmbGnd, uAmbSky, 0.5 + 0.5 * n.y) * 0.6;
vec3 lit = col * light;
vec3 gl = mix(vec3(0.02, 0.03, 0.04) * light, sky * 0.55, 0.35 + 0.4 * pow(1.0 - max(dot(v, n), 0.0), 3.0));
lit = mix(lit, gl, glass * 0.85);
// lit windows at night
float on = step(0.55, h21(floor(cell) + floor(vW.xz / 40.0)));
lit += vec3(1.0, 0.78, 0.45) * win * on * uNight * 0.9 * (1.0 - far * 0.7);
float fa; vec3 o = aerial(lit, uCam, vW, fa);
gl_FragColor = vec4(o, 1.0);
#include <tonemapping_fragment>
#include <colorspace_fragment>
}`;
const PALETTE = [[0.66, 0.63, 0.58], [0.58, 0.52, 0.46], [0.72, 0.7, 0.66], [0.55, 0.38, 0.31], [0.78, 0.76, 0.72], [0.5, 0.5, 0.52], [0.64, 0.6, 0.53], [0.8, 0.74, 0.62]];
const TALL = [[0.46, 0.52, 0.58], [0.6, 0.63, 0.66], [0.4, 0.45, 0.5], [0.72, 0.72, 0.7], [0.55, 0.5, 0.44], [0.78, 0.8, 0.82]];
const lin = (c) => c.map((x) => x ** 2.2);
/**
* Extrude one decoded building layer into merged arrays. toWorld(px, py) -> [x, z]; ground(x, z) -> metres.
* Pure apart from THREE.ShapeUtils triangulation; returns { pos, nor, col, win, count }.
*/
export function extrude(layer, toWorld, ground, { minH = 0 } = {}) {
const P = [], N = [], C = [], W = [];
const push = (x, y, z, nx, ny, nz, c, u, h) => { P.push(x, y, z); N.push(nx, ny, nz); C.push(c[0], c[1], c[2]); W.push(u, h); };
let n = 0;
for (const f of layer.features) {
if (f.type !== 3 || f.tags.hide_3d) continue;
const top = +(f.tags.render_height ?? 0) || 0, bot = +(f.tags.render_min_height ?? 0) || 0;
const h = top > 0 ? top : 8;
if (h < minH) continue;
for (const poly of polygons(f.geom)) {
const rings = poly.map((r) => { const o = []; for (let i = 0; i < r.length; i += 2) o.push(toWorld(r[i], r[i + 1])); return o; });
const outer = rings[0];
let cx = 0, cz = 0; for (const [x, z] of outer) { cx += x; cz += z; } cx /= outer.length; cz /= outer.length;
let g = ground(cx, cz); if (g === null) continue;
for (const [x, z] of outer) { const gg = ground(x, z); if (gg !== null && gg < g) g = gg; }
const y0 = g + bot - (bot > 0 ? 0 : 1.5), y1 = g + h;
const hsh = Math.abs(Math.sin(cx * 12.9898 + cz * 78.233) * 43758.5453) % 1;
const pal = h > 45 ? TALL : PALETTE;
const c = lin(pal[Math.floor(hsh * pal.length) % pal.length]).map((v) => v * (0.85 + 0.3 * ((hsh * 7.31) % 1)));
const roofC = lin([0.42, 0.41, 0.4]).map((v) => v * (0.75 + 0.5 * ((hsh * 3.7) % 1)));
// walls
for (const ring of rings) {
let u = 0;
for (let i = 0; i < ring.length; i++) {
const [ax, az] = ring[i], [bx, bz] = ring[(i + 1) % ring.length];
const L = Math.hypot(bx - ax, bz - az); if (L < 0.05) continue;
const nx = (bz - az) / L, nz = -(bx - ax) / L; // outward for exterior rings (clockwise on screen = y down)
push(ax, y0, az, nx, 0, nz, c, u, 0); push(bx, y0, bz, nx, 0, nz, c, u + L, 0); push(bx, y1, bz, nx, 0, nz, c, u + L, y1 - y0);
push(ax, y0, az, nx, 0, nz, c, u, 0); push(bx, y1, bz, nx, 0, nz, c, u + L, y1 - y0); push(ax, y1, az, nx, 0, nz, c, u, y1 - y0);
u += L;
}
}
// roof
const v2 = (r) => r.map(([x, z]) => new THREE.Vector2(x, z));
let tris;
try { tris = THREE.ShapeUtils.triangulateShape(v2(outer), rings.slice(1).map(v2)); } catch { tris = []; }
const all = rings.flat();
for (const t of tris) {
// winding: make the roof face up
const [a, b, cc] = t.map((i) => all[i]);
const cr = (b[0] - a[0]) * (cc[1] - a[1]) - (b[1] - a[1]) * (cc[0] - a[0]);
const seq = cr < 0 ? [a, b, cc] : [a, cc, b];
for (const [x, z] of seq) push(x, y1, z, 0, 1, 0, roofC, 0, 0);
}
n++;
}
}
return { pos: new Float32Array(P), nor: new Float32Array(N), col: new Float32Array(C), win: new Float32Array(W), count: n };
}
// ---------------------------------------------------------------- streamer
export class Buildings {
constructor(scene, terrain, uniforms, opts = {}) {
this.scene = scene; this.terrain = terrain; this.U = uniforms;
this.radius = opts.radius || 5200; this.max = opts.max || 30; this.minH = opts.minH || 0; this.parallel = opts.parallel || 3;
this.budget = opts.budget || 1.2e6; // triangles drawn at most (nearest tiles first)
this.tiles = new Map(); this.active = 0; this.off = false; this.urlT = null; this.loaded = 0; this.failed = 0; this.count = 0;
this.group = new THREE.Group(); scene.add(this.group);
this.mat = new THREE.ShaderMaterial({ vertexShader: VERT, fragmentShader: FRAG, uniforms: uniforms, side: THREE.DoubleSide });
}
reset() { for (const t of this.tiles.values()) this.free(t); this.tiles.clear(); }
free(t) { if (t.mesh) { this.group.remove(t.mesh); t.mesh.geometry.dispose(); t.mesh = null; } }
async template() {
if (this.urlT) return this.urlT;
if (!this.urlP) this.urlP = fetch(TILEJSON, { mode: 'cors', credentials: 'omit' }).then((r) => r.json()).then((j) => (this.urlT = j.tiles[0])).catch(() => { this.off = true; return null; });
return this.urlP;
}
/** Each frame: want the zoom-14 tiles within radius of the camera (none when high up). */
update(cx, cy, cz) {
if (this.off) return;
const T = this.terrain, f = T.frame;
const agl = cy - Math.max(0, T.height(cx, cz).h);
const R = agl > 3500 ? 0 : this.radius * (agl > 1500 ? 0.7 : 1);
const n = 1 << BZ;
const [u, v] = f.uv(cx, cz);
const tx = Math.floor(u * n), ty = Math.floor(v * n), w = f.m / n;
const span = Math.ceil(R / w);
const want = [];
for (let j = -span; j <= span; j++) for (let i = -span; i <= span; i++) {
const x = tx + i, y = ty + j; const r = T.rect(BZ, x, y);
const d = Math.hypot(Math.max(r.x0 - cx, 0, cx - r.x0 - r.w), Math.max(r.z0 - cz, 0, cz - r.z0 - r.w));
if (d <= R) want.push({ x: ((x % n) + n) % n, y, d });
}
want.sort((a, b) => a.d - b.d);
const keep = new Set(); this.want = 0; this.drawn = 0;
for (const t of want.slice(0, this.max)) {
const k = `${t.x}/${t.y}`; keep.add(k);
let e = this.tiles.get(k);
if (!e) { e = { x: t.x, y: t.y, state: 0, mesh: null }; this.tiles.set(k, e); }
const c = this.centre(e), gz = T.height(c[0], c[1]).z;
const near = t.d < 2500, minH = near ? this.minH : Math.max(this.minH, 18); // far away only the taller buildings
// re-seat a tile on the ground once much finer elevation has arrived under it (buildings never float or sink), and
// fill in the small buildings once it is near
if (e.state === 2 && (gz >= e.gz + 2 || (near && e.minH > minH))) { this.free(e); e.state = 0; }
if (e.state === 0 && this.active < this.parallel && gz >= (near ? 13 : 11)) { e.state = 1; e.gz = gz; e.minH = minH; this.active++; this.load(e).finally(() => { this.active--; }); }
if (e.mesh) { e.tri = e.mesh.geometry.attributes.position.count / 3; this.drawn += e.tri; e.mesh.visible = this.drawn <= this.budget || t.d < 1200; }
if (e.state < 2) this.want++;
}
for (const [k, e] of this.tiles) if (!keep.has(k) && (e.state !== 1)) { this.free(e); this.tiles.delete(k); }
}
centre(e) { const r = this.terrain.rect(BZ, e.x, e.y); return [r.x0 + r.w / 2, r.z0 + r.w / 2]; }
async load(e) {
try {
const tpl = await this.template(); if (!tpl) return;
const res = await fetch(tpl.replace('{z}', BZ).replace('{x}', e.x).replace('{y}', e.y), { mode: 'cors', credentials: 'omit' });
if (!res.ok) { e.state = 3; this.failed++; return; }
const mvt = decodeMVT(new Uint8Array(await res.arrayBuffer()), ['building']);
if (!this.tiles.has(`${e.x}/${e.y}`)) return;
const layer = mvt.building; if (!layer || !layer.features.length) { e.state = 2; return; }
const T = this.terrain, r = T.rect(BZ, e.x, e.y), ext = layer.extent;
const toWorld = (px, py) => [r.x0 + (px / ext) * r.w, r.z0 + (py / ext) * r.w];
const ground = (x, z) => { const g = T.height(x, z); return g.z < 0 ? null : Math.max(g.h, 0); };
const m = extrude(layer, toWorld, ground, { minH: e.minH ?? this.minH });
if (!m.count) { e.state = 2; return; }
const g = new THREE.BufferGeometry();
g.setAttribute('position', new THREE.BufferAttribute(m.pos, 3)); g.setAttribute('normal', new THREE.BufferAttribute(m.nor, 3));
g.setAttribute('aCol', new THREE.BufferAttribute(m.col, 3)); g.setAttribute('aWin', new THREE.BufferAttribute(m.win, 2));
g.computeBoundingSphere();
const mesh = new THREE.Mesh(g, this.mat); mesh.matrixAutoUpdate = false; mesh.renderOrder = 1;
e.mesh = mesh; this.group.add(mesh); e.state = 2; this.loaded++; this.count += m.count;
} catch { e.state = 3; this.failed++; }
}
/** Tiles still to load near the camera (0 = the city around you is complete). */
pending() { return this.off ? 0 : this.want || 0; }
stats() { let tri = 0, vis = 0; for (const e of this.tiles.values()) if (e.mesh) { vis++; tri += e.mesh.geometry.attributes.position.count / 3; } return { tiles: vis, tri, active: this.active, loaded: this.loaded, failed: this.failed, off: this.off }; }
}