-
Notifications
You must be signed in to change notification settings - Fork 663
Expand file tree
/
Copy pathindex.html
More file actions
135 lines (120 loc) · 4.14 KB
/
index.html
File metadata and controls
135 lines (120 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Raster projection with GPU.js</title>
<script src="../../dist/gpu-browser.min.js"></script>
</head>
<body>
<h1>Raster projection with GPU.js from <a href="https://observablehq.com/d/b70d084526a1a764">https://observablehq.com/d/b70d084526a1a764</a></h1>
<div id="log-fps"></div>
</body>
<script>
function frac(n) {
return n - Math.floor(n);
}
function applyRotation(rotatex, rotatey, rotatez, lambda, phi) {
const degrees = 57.29577951308232;
lambda = lambda / degrees;
phi = phi / degrees;
const cosphi = Math.cos(phi),
x = Math.cos(lambda) * cosphi,
y = Math.sin(lambda) * cosphi,
z = Math.sin(phi);
// inverse rotation
const deltaLambda = rotatex / degrees; // rotate[0]
const deltaPhi = -rotatey / degrees; // rotate[1]
const deltaGamma = -rotatez / degrees; // rotate[2]
const cosDeltaPhi = Math.cos(deltaPhi),
sinDeltaPhi = Math.sin(deltaPhi),
cosDeltaGamma = Math.cos(deltaGamma),
sinDeltaGamma = Math.sin(deltaGamma);
let k = z * cosDeltaGamma - y * sinDeltaGamma;
lambda = Math.atan2(
y * cosDeltaGamma + z * sinDeltaGamma,
x * cosDeltaPhi + k * sinDeltaPhi
) - deltaLambda;
k = k * cosDeltaPhi - x * sinDeltaPhi;
phi = Math.asin(k);
lambda *= degrees;
phi *= degrees;
return [lambda, phi];
}
// the kernel runs for each pixel, with:
// - this.thread.x = horizontal position in pixels from the left edge
// - this.thread.y = vertical position in pixels from the bottom edge (*opposite of canvas*)
const kernel = function(pixels, rotate0, rotate1, rotate2, scale) {
// azimuthal equal area
function radius(rho) {
return 2.0 * Math.asin(rho / 2.0);
}
// orthographic
function __radius(rho) {
return Math.asin(rho);
}
// equirectangular projection (reads the (lon,lat) color from the base image)
function pixelx(lon, srcw) {
lon = frac((lon + 180) / 360);
return Math.floor(lon * srcw);
}
function pixely(lat, srch) {
lat = frac((lat + 90) / 180);
return Math.floor(lat * srch);
}
const x = (this.thread.x / this.constants.w - 1 / 2) / scale,
y = ((this.thread.y - this.constants.h / 2) / this.constants.w) / scale;
// inverse projection
const rho = Math.sqrt(x * x + y * y) + 1e-12;
const c = radius(rho),
sinc = Math.sin(c),
cosc = Math.cos(c);
// x, y : pixel coordinates if rotation was null
const lambda = Math.atan2(x * sinc, rho * cosc) * 57.29577951308232;
const z = y * sinc / rho;
if (Math.abs(z) < 1) {
const phi = Math.asin(z) * 57.29577951308232;
// apply rotation
const rotation = applyRotation(rotate0, rotate1, rotate2, lambda, phi);
const lambdan = rotation[0];
const phin = rotation[1];
//var n = n0(lambda, phi, this.constants.srcw, this.constants.srch);
//this.color(pixels[n]/256, pixels[n+1]/256,pixels[n+2]/256,1);
const pixel = pixels[pixely(phin, this.constants.srch)][pixelx(lambdan, this.constants.srcw)];
this.color(pixel[0], pixel[1], pixel[2], 1);
}
};
const gpu = new GPU();
const logFps = document.querySelector('#log-fps');
const image = new Image();
image.src = './earth-map.jpg';
image.onload = () => {
const w = 975;
const h = 975;
const render = gpu
.createKernel(kernel, { functions: [applyRotation, frac] })
.setConstants({ w, h, srcw: image.width, srch: image.height })
.setOutput([w, h])
.setTactic('precision')
.setGraphical(true);
const canvas = render.canvas;
document.body.appendChild(canvas);
let lastCalledTime;
let fps;
function callRender() {
let r0 = (-Date.now() / 30) % 360,
r1 = 35 * Math.sin((-Date.now() / 1030) % 360),
r2 = 0,
scale = 0.49;
render(image, r0, r1, r2, scale);
delta = (Date.now() - lastCalledTime)/1000;
lastCalledTime = Date.now();
fps = 1/delta;
logFps.innerHTML = fps.toFixed(0) + ' FPS';
window.requestAnimationFrame(() => {
callRender();
});
}
callRender();
};
</script>
</html>