// 1) Make a random "building" polygon near Kolkata (GeoJSON)
const buildingLng = 88.3639 + (Math.random() - 0.5) * 0.001
const buildingLat = 22.5726 + (Math.random() - 0.5) * 0.001
const height = 30 + Math.random() * 70
const color = `#${Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0')}`
const polygon = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[buildingLng, buildingLat],
[buildingLng + 0.0005, buildingLat],
[buildingLng + 0.0005, buildingLat + 0.0004],
[buildingLng, buildingLat + 0.0004],
[buildingLng, buildingLat],
],
],
},
properties: { height, color },
},
],
}
map.addSource('custom-building', { type: 'geojson', data: polygon })
map.addLayer({
id: '3d-building',
type: 'fill-extrusion',
source: 'custom-building',
paint: {
'fill-extrusion-color': ['get', 'color'],
'fill-extrusion-height': ['get', 'height'],
'fill-extrusion-base': 0,
'fill-extrusion-opacity': 0.9,
},
})
// 2) Add a custom overlay layer with an animated "random shader"
let program, aPos, buffer, uMatrix, uTime, uColor
const shaderColor = [Math.random(), Math.random(), Math.random()]
const randomShaderOverlay = {
id: 'random-shader-overlay',
type: 'custom',
onAdd(map, gl) {
const vertexSource = `#version 300 es
uniform mat4 u_matrix;
in vec2 a_pos;
void main() {
gl_Position = u_matrix * vec4(a_pos, 0.0, 1.0);
}`
const fragmentSource = `#version 300 es
precision highp float;
out vec4 fragColor;
uniform float u_time;
uniform vec3 u_color;
float rand(vec2 co) {
return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
}
void main() {
float n = rand(gl_FragCoord.xy + u_time * 60.0);
vec3 c = mix(u_color, vec3(n, 1.0 - n, 0.5 + 0.5 * sin(u_time)), 0.65);
fragColor = vec4(c, 0.45);
}`
const vs = gl.createShader(gl.VERTEX_SHADER)
gl.shaderSource(vs, vertexSource)
gl.compileShader(vs)
const fs = gl.createShader(gl.FRAGMENT_SHADER)
gl.shaderSource(fs, fragmentSource)
gl.compileShader(fs)
program = gl.createProgram()
gl.attachShader(program, vs)
gl.attachShader(program, fs)
gl.linkProgram(program)
aPos = gl.getAttribLocation(program, 'a_pos')
uMatrix = gl.getUniformLocation(program, 'u_matrix')
uTime = gl.getUniformLocation(program, 'u_time')
uColor = gl.getUniformLocation(program, 'u_color')
// Rectangle vertices matching the random building footprint (in Mercator coords)
const mc = olaMaps.getMercatorCoordinate()
const c1 = mc.fromLngLat({ lng: buildingLng, lat: buildingLat })
const c2 = mc.fromLngLat({ lng: buildingLng + 0.0005, lat: buildingLat })
const c3 = mc.fromLngLat({ lng: buildingLng, lat: buildingLat + 0.0004 })
const c4 = mc.fromLngLat({ lng: buildingLng + 0.0005, lat: buildingLat + 0.0004 })
buffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array([
c1.x, c1.y, // bottom-left
c2.x, c2.y, // bottom-right
c3.x, c3.y, // top-left
c4.x, c4.y, // top-right
]),
gl.STATIC_DRAW,
)
},
render(gl, matrix) {
// Some SDK/runtime versions pass a render input object instead of a raw 4x4 array.
const raw =
matrix?.matrix ??
matrix?.projectionMatrix ??
matrix?.defaultProjectionData?.mainMatrix ??
matrix?.defaultProjectionData?.projectionMatrix ??
matrix
const mat4 = raw instanceof Float32Array ? raw : Array.isArray(raw) ? new Float32Array(raw) : null
if (!mat4 || mat4.length !== 16) return
gl.useProgram(program)
gl.uniformMatrix4fv(uMatrix, false, mat4)
gl.uniform1f(uTime, performance.now() / 1000)
gl.uniform3fv(uColor, shaderColor)
gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
gl.enableVertexAttribArray(aPos)
gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0)
gl.enable(gl.BLEND)
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4)
map.triggerRepaint()
},
}
map.addLayer(randomShaderOverlay)