-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconvert.js
43 lines (31 loc) · 1.53 KB
/
convert.js
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
const { createImageData } = require('canvas');
const { mod, interpolations, orientations } = require('./utils')
const renderFace = ({ data: readData, face, rotation, interpolation, maxWidth = Infinity }) => {
return new Promise(resolve => {
const faceWidth = Math.min(maxWidth, readData.width / 4);
const faceHeight = faceWidth;
const cube = {};
const orientation = orientations[face];
const writeData = createImageData(faceWidth, faceHeight);
const copyPixel = interpolations[interpolation](readData, writeData)
for (let x = 0; x < faceWidth; x++) {
for (let y = 0; y < faceHeight; y++) {
const to = 4 * (y * faceWidth + x);
// fill alpha channel
writeData.data[to + 3] = 255;
// get position on cube face
// cube is centered at the origin with a side length of 2
orientation(cube, (2 * (x + 0.5) / faceWidth - 1), (2 * (y + 0.5) / faceHeight - 1));
// project cube face onto unit sphere by converting cartesian to spherical coordinates
const r = Math.sqrt(cube.x * cube.x + cube.y * cube.y + cube.z * cube.z);
const lon = mod(Math.atan2(cube.y, cube.x) + rotation, 2 * Math.PI);
const lat = Math.acos(cube.z / r);
copyPixel(readData.width * lon / Math.PI / 2 - 0.5, readData.height * lat / Math.PI - 0.5, to);
}
}
resolve(writeData)
});
}
module.exports = {
renderFace
}