Published
Edited
2 forks
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
threedee = true
Insert cell
function avertex(v) {
//var r = Math.sqrt(Math.pow(v.x, 2) + Math.pow(v.y, 2) + Math.pow(v.z, 2));

var r = radius;
var lat = (Math.asin(v.y / r) * 180) / Math.PI || 0;
var lon = (Math.atan2(v.x, v.z) * 180) / Math.PI || 0;
lon += 270; //-90;

var p1 = 173 - Math.ceil(lat + 90);

var p0 = 170 + Math.floor(lon);
//p0 = p0 < 359 ? p0 : p0 % 360;

var ix = Math.floor((p0 % 360) + 360 * (p1 % 180));

if (p1 > 149) {
// eliminate the antarctic
var hmap = 0.2;
} else {
var hmap = h[ix] || 0.4;

hmap = hmap / 255;
}
//console.log(p0, p1, lon, lat, hmap, ix);

//hmap = 0;

return vertex([lon, lat], radius - 1 + 6 * hmap ** 1.6);

// const lambda = (longitude * Math.PI) / 180;
//const phi = (latitude * Math.PI) / 180;
// radius * Math.cos(phi) * Math.cos(lambda),
// radius * Math.sin(phi),
// -radius * Math.cos(phi) * Math.sin(lambda)
// );
}
Insert cell
function vertex([longitude, latitude], radius) {
const lambda = (longitude * Math.PI) / 180;
const phi = (latitude * Math.PI) / 180;
return new THREE.Vector3(
radius * Math.cos(phi) * Math.cos(lambda),
radius * Math.sin(phi),
-radius * Math.cos(phi) * Math.sin(lambda)
);
}
Insert cell
Insert cell
function wireframe(multilinestring, radius, material) {
const geometry = new THREE.Geometry();
for (const P of multilinestring.coordinates) {
for (let p0, p1 = vertex(P[0], radius), i = 1; i < P.length; ++i) {
geometry.vertices.push(p0 = p1, p1 = vertex(P[i], radius));
}
}
return new THREE.LineSegments(geometry, material);
}
Insert cell
Insert cell
land = {
const response = await fetch(
"https://cdn.jsdelivr.net/npm/world-atlas@2/land-50m.json"
);
const topology = await response.json();
const mesh = topojson.mesh(topology, topology.objects.land);
return wireframe(
mesh,
radius,
new THREE.LineBasicMaterial({
color: new THREE.Color('steelblue'),
linewidth: 10,
transparent: true
})
);
}
Insert cell
tube = {
const response = await fetch(
"https://cdn.jsdelivr.net/npm/world-atlas@2/countries-110m.json"
//"https://cdn.jsdelivr.net/npm/world-atlas@2/land-50m.json"
);
const topology = await response.json();
const mesh = await topojson.mesh(topology, topology.objects.countries); //land);

var cs = new THREE.Group();
//return mesh.coordinates;

for (const d of mesh.coordinates) {
//TubeGeometry(path : Curve, tubularSegments : Integer, radius : Float, radialSegments : Integer, closed : Boolean)
//return d.map(e => vertex(e, radius));
var pathb = d.map(e => vertex(e, radius));

var pathBase = new THREE.CatmullRomCurve3(pathb);
var h = Math.ceil(d.length * 1.5);
var tgeometry = new THREE.TubeBufferGeometry(
// /*vec["_" + 7 + "_"]*/ path,
pathBase,
h,
.21,
2,
false
);
var tmaterial = new THREE.MeshBasicMaterial({
color: new THREE.Color('steelblue'),
transparent: true
});
var tmesh = new THREE.Mesh(tgeometry, tmaterial);

cs.add(tmesh);
}

return cs;
}
Insert cell
graticule = {
const mesh = graticule10();
return wireframe(mesh, radius, new THREE.LineBasicMaterial({color: 0xaaaaaa}));
}
Insert cell
sphere = {
var texture = new THREE.Texture(image);
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
// texture.repeat.set(fitTo, fitTo);
texture.needsUpdate = true;

var atexture = new THREE.Texture(imap);
atexture.wrapS = THREE.RepeatWrapping;
atexture.wrapT = THREE.RepeatWrapping;
// texture.repeat.set(fitTo, fitTo);
atexture.needsUpdate = true;

var geometry = new THREE.SphereGeometry(radius, radius * 2.6, radius * 2);
var material = new THREE.MeshBasicMaterial({
// color: new THREE.Color('blue'),
wireframe: true,
map: texture,
alphaMap: atexture,
combine: 1
});
var sphere = new THREE.Mesh(geometry, material);

if (threedee) {
sphere.geometry.vertices = sphere.geometry.vertices.map(avertex);
sphere.geometry.computeVertexNormals();
sphere.geometry.normalsNeedUpdate = true;
sphere.geometry.verticesNeedUpdate = true;

//sphere.colorsNeedUpdate = true;
}
return sphere;
}
Insert cell
scene = {
const scene = new THREE.Scene();
scene.background = new THREE.Color('#222');
//scene.add(graticule);
scene.add(sphere);
scene.add(land);
scene.add(tube);
return scene;
}
Insert cell
camera = {
const fov = 90;
const aspect = width / height;
const near = 1;
const far = 1000;
return new THREE.PerspectiveCamera(fov, aspect, near, far);
}
Insert cell
radius = 200
Insert cell
height = width
Insert cell
Insert cell
function meridian(x, y0, y1, dy = 2.5) {
return Array.from(range(y0, y1 + 1e-6, dy), y => [x, y]);
}
Insert cell
function parallel(y, x0, x1, dx = 2.5) {
return Array.from(range(x0, x1 + 1e-6, dx), x => [x, y]);
}
Insert cell
function* range(start, stop, step) {
for (let i = 0, v = start; v < stop; v = start + (++i * step)) {
yield v;
}
}
Insert cell
image = await FileAttachment("bathymetry_diffuse_4k.jpg").image()
Insert cell
dem = await FileAttachment("dem.jpg").image()
Insert cell
canvas = {
const ctx = DOM.context2d(360, 180);

ctx.scale(dpr, dpr);
ctx.drawImage(dem, 0, 0, 360, 180);

//let ratio = imap.height / imap.width;
return ctx.canvas;
}
Insert cell
dpr = window.devicePixelRatio - 1
Insert cell
h = {
if (threedee) {
var h = canvas
.getContext('2d')
.getImageData(0, 0, 360, 180)
.data.filter(function(value, index, Arr) {
return (index - 1) % 4 == 0;
});

// var e = [...h];
// h.forEach(d => e.push(d));

// return e;
return h;
}
}
Insert cell
testscan = {
const ct = DOM.context2d(360, 180);
for (var i = 0; i < 360; i++) {
for (var j = 0; j < 180; j++) {
var l = h[Math.floor(i + (360 - j) * 360)] / 255;
//console.log(l, i, j);
ct.fillStyle = `rgba(20,200,200,${l})`;
ct.fill();
ct.fillRect(i, j, 1, 1);
}
}
return ct.canvas;
}
Insert cell
imap = await FileAttachment("bathymetry_bw_composite_4k.jpg").image()
Insert cell
topojson = require("topojson-client@3")
Insert cell
THREE = require("three@0.99.0/build/three.min.js")
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