Published
Edited
Mar 11, 2021
Insert cell
Insert cell
gl.canvas
Insert cell
features = countries.features.flatMap(feature => {
// console.log(feature)
let coordinates;
if (feature.geometry.type == "Polygon") {
coordinates = [feature.geometry.coordinates[0].map(([lon, lat]) => polarToCartesian(lon, lat, 1))]
} else if (feature.geometry.type == "MultiPolygon") {
coordinates = feature.geometry.coordinates.map(coord => coord[0].map(([lon, lat]) => polarToCartesian(lon, lat, 1)))
} else {
throw `All elements must be polygons or multipolgyons. ${feature.geometry}`
}
console.log('features', coordinates)
// return coordinates.map(points => {
const all_coords = []
const all_vertices = []
// console.log(feature.properties.name)
// for (const polygon of polygons) {
const current_vertex = all_coords.length / 3
// console.log(polygon)
const { coords, vertices } = polygon_to_triangles([coordinates[0].slice(0, 61)]);
all_coords.push(...coords)
// If need to shift because we may be storing multiple triangle sets on a feature.
all_vertices.push(...vertices.map(d => d + current_vertex))
// }
// console.log(all_coords)
return {
position: gl.regl.buffer(all_coords.flat(8)),
elements: gl.regl.elements({
primitive: "triangles",
count: all_vertices.length,
data: all_vertices.flat(10),
// type: all_coords.length < 2**8 ? 'uint8' : all_coords.length < 2**16 ? 'uint16' : 'uint32'
}),
}
// })
})
Insert cell
Insert cell
Insert cell
d3 = require("d3@6", "d3-geo-projection")
Insert cell
projection = d3.geoOrthographic().precision(0.1)
Insert cell
createREGL = require('regl')
Insert cell
function createGL (opts) {
var canvas = html`<canvas width="960" height="960">`
var regl = createREGL(Object.assign({canvas: canvas}, opts || {}))
return { canvas, regl }
}
Insert cell
gl = createGL()
Insert cell
drawSphere = gl.regl({
vert: `
precision mediump float;

attribute vec3 position;
uniform mat4 model, view, projection;

void main () {
gl_Position = projection * view * model * vec4(vec3(0.99) * position, 1);
}`,
frag: `
precision mediump float;
void main () {
gl_FragColor = vec4(0.8, 0.3, 0.2, 0.4);
}`,
attributes: {
position: gl.regl.buffer(sphere.positions)
},
elements: gl.regl.elements(sphere.cells),
uniforms: {
model: mat4.identity([]),
view: gl.regl.prop('view'),
projection: ({ viewportWidth, viewportHeight }) =>
mat4.perspective([],
Math.PI / 2,
viewportWidth / viewportHeight,
0.01,
1000),
},
cull: {
enable: false,
face: 'back'
},
depth: {
enable: false
},
blend: {
enable: true,
func: {
srcRGB: 'src alpha',
srcAlpha: 'src alpha',
dstRGB: 'one minus src alpha',
dstAlpha: 'one minus src alpha',
},
},
})

Insert cell
drawCountry = gl.regl({
vert: `
precision mediump float;

attribute vec3 position;
uniform mat4 model, view, projection;

void main () {
gl_Position = projection * view * model * vec4(position, 1);
}
`,
frag: `
precision mediump float;

void main () {
gl_FragColor = vec4(1, 0, 0, 1);
}
`,
attributes: {
position: gl.regl.prop("position")
},
uniforms: {
model: mat4.identity([]),
view: gl.regl.prop('view'),
projection: ({ viewportWidth, viewportHeight }) =>
mat4.perspective([],
Math.PI / 2,
viewportWidth / viewportHeight,
0.01,
1000),
},
elements: gl.regl.prop("elements"),
})
Insert cell
function draw (t) {
gl.regl.clear({
color: [1, 1, 1, 1],
depth: 1,
})
const view = mat4.lookAt(
[],
[2 * Math.cos(t), 2 * Math.sin(t), 0],
[0, 0, 0],
[0, 0, 1]
)

// drawSphere({
// view,
// })
drawCountry(features.map(feature => ({
view,
position: feature.position,
elements: feature.elements,
})))
}
Insert cell
draw(now / 10000)
Insert cell
glMatrix = import('gl-matrix')
Insert cell
mat4 = glMatrix.mat4
Insert cell
sphere = icosphere(3)
Insert cell
function icosphere(subdivisions) {
subdivisions = +subdivisions|0

var positions = []
var faces = []
var t = 0.5 + Math.sqrt(5) / 2

positions.push([-1, +t, 0])
positions.push([+1, +t, 0])
positions.push([-1, -t, 0])
positions.push([+1, -t, 0])

positions.push([ 0, -1, +t])
positions.push([ 0, +1, +t])
positions.push([ 0, -1, -t])
positions.push([ 0, +1, -t])

positions.push([+t, 0, -1])
positions.push([+t, 0, +1])
positions.push([-t, 0, -1])
positions.push([-t, 0, +1])

faces.push([0, 11, 5])
faces.push([0, 5, 1])
faces.push([0, 1, 7])
faces.push([0, 7, 10])
faces.push([0, 10, 11])

faces.push([1, 5, 9])
faces.push([5, 11, 4])
faces.push([11, 10, 2])
faces.push([10, 7, 6])
faces.push([7, 1, 8])

faces.push([3, 9, 4])
faces.push([3, 4, 2])
faces.push([3, 2, 6])
faces.push([3, 6, 8])
faces.push([3, 8, 9])

faces.push([4, 9, 5])
faces.push([2, 4, 11])
faces.push([6, 2, 10])
faces.push([8, 6, 7])
faces.push([9, 8, 1])

var complex = {
cells: faces
, positions: positions
}

while (subdivisions-- > 0) {
complex = subdivide(complex)
}

positions = complex.positions
for (var i = 0; i < positions.length; i++) {
normalize(positions[i])
}

return complex
}

Insert cell

// TODO: work out the second half of loop subdivision
// and extract this into its own module.
function subdivide(complex) {
var positions = complex.positions
var cells = complex.cells

var newCells = []
var newPositions = []
var midpoints = {}
var f = [0, 1, 2]
var l = 0

for (var i = 0; i < cells.length; i++) {
var cell = cells[i]
var c0 = cell[0]
var c1 = cell[1]
var c2 = cell[2]
var v0 = positions[c0]
var v1 = positions[c1]
var v2 = positions[c2]

var a = getMidpoint(v0, v1)
var b = getMidpoint(v1, v2)
var c = getMidpoint(v2, v0)

var ai = newPositions.indexOf(a)
if (ai === -1) ai = l++, newPositions.push(a)
var bi = newPositions.indexOf(b)
if (bi === -1) bi = l++, newPositions.push(b)
var ci = newPositions.indexOf(c)
if (ci === -1) ci = l++, newPositions.push(c)

var v0i = newPositions.indexOf(v0)
if (v0i === -1) v0i = l++, newPositions.push(v0)
var v1i = newPositions.indexOf(v1)
if (v1i === -1) v1i = l++, newPositions.push(v1)
var v2i = newPositions.indexOf(v2)
if (v2i === -1) v2i = l++, newPositions.push(v2)

newCells.push([v0i, ai, ci])
newCells.push([v1i, bi, ai])
newCells.push([v2i, ci, bi])
newCells.push([ai, bi, ci])
}

return {
cells: newCells
, positions: newPositions
}

// reuse midpoint vertices between iterations.
// Otherwise, there'll be duplicate vertices in the final
// mesh, resulting in sharp edges.
function getMidpoint(a, b) {
var point = midpoint(a, b)
var pointKey = pointToKey(point)
var cachedPoint = midpoints[pointKey]
if (cachedPoint) {
return cachedPoint
} else {
return midpoints[pointKey] = point
}
}

function pointToKey(point) {
return point[0].toPrecision(6) + ','
+ point[1].toPrecision(6) + ','
+ point[2].toPrecision(6)
}

function midpoint(a, b) {
return [
(a[0] + b[0]) / 2
, (a[1] + b[1]) / 2
, (a[2] + b[2]) / 2
]
}
}
Insert cell
function normalize(vec) {
var mag = 0
for (var n = 0; n < vec.length; n++) {
mag += vec[n] * vec[n]
}
mag = Math.sqrt(mag)

// avoid dividing by zero
if (mag === 0) {
return Array.apply(null, new Array(vec.length)).map(Number.prototype.valueOf, 0)
}

for (var n = 0; n < vec.length; n++) {
vec[n] /= mag
}

return vec
}
Insert cell
function polarToCartesian(lon0, lat0, R) {
const lat = lat0 * Math.PI / 180
const lon = lon0 * Math.PI / 180
const x = R * Math.cos(lat) * Math.cos(lon)
const y = R * Math.cos(lat) * Math.sin(lon)
const z = R * Math.sin(lat)

return [x, y, z];
}
Insert cell
function polygon_to_triangles(polygon0) {
const polygon = polygon0.map(points => {
const ret = [];
for (const p of points) {
if (true || ret.every(x => !(x[0] === p[0] && x[1] === p[1] && x[2] == p[2]))) {
ret.push(p);
}
}
// return ret.slice(0, 70);
// ret.push(points[0])
return ret
})
// Actually perform the earcut work
const data = earcut.flatten(polygon)
// console.log(data)
const coords = polygon
const vertices = earcut(data.vertices, data.holes, data.dimensions)
// console.log(vertices);
return { coords, vertices }
}
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more