This repository has been archived by the owner on Feb 10, 2021. It is now read-only.
forked from jaxry/panorama-to-cubemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
198 lines (163 loc) · 5.06 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
function clamp(x, min, max) {
return Math.min(max, Math.max(x, min));
}
function mod(x, n) {
return ((x % n) + n) % n;
}
function copyPixelNearest(read, write) {
const {width, height, data} = read;
const readIndex = (x, y) => 4 * (y * width + x);
return (xFrom, yFrom, to) => {
const nearest = readIndex(
clamp(Math.round(xFrom), 0, width - 1),
clamp(Math.round(yFrom), 0, height - 1)
);
for (let channel = 0; channel < 3; channel++) {
write.data[to + channel] = data[nearest + channel];
}
};
}
function copyPixelBilinear(read, write) {
const {width, height, data} = read;
const readIndex = (x, y) => 4 * (y * width + x);
return (xFrom, yFrom, to) => {
const xl = clamp(Math.floor(xFrom), 0, width - 1);
const xr = clamp(Math.ceil(xFrom), 0, width - 1);
const xf = xFrom - xl;
const yl = clamp(Math.floor(yFrom), 0, height - 1);
const yr = clamp(Math.ceil(yFrom), 0, height - 1);
const yf = yFrom - yl;
const p00 = readIndex(xl, yl);
const p10 = readIndex(xr ,yl);
const p01 = readIndex(xl, yr);
const p11 = readIndex(xr, yr);
for (let channel = 0; channel < 3; channel++) {
const p0 = data[p00 + channel] * (1 - xf) + data[p10 + channel] * xf;
const p1 = data[p01 + channel] * (1 - xf) + data[p11 + channel] * xf;
write.data[to + channel] = Math.ceil(p0 * (1 - yf) + p1 * yf);
}
};
}
// performs a discrete convolution with a provided kernel
function kernelResample(read, write, filterSize, kernel) {
const {width, height, data} = read;
const readIndex = (x, y) => 4 * (y * width + x);
const twoFilterSize = 2*filterSize;
const xMax = width - 1;
const yMax = height - 1;
const xKernel = new Array(4);
const yKernel = new Array(4);
return (xFrom, yFrom, to) => {
const xl = Math.floor(xFrom);
const yl = Math.floor(yFrom);
const xStart = xl - filterSize + 1;
const yStart = yl - filterSize + 1;
for (let i = 0; i < twoFilterSize; i++) {
xKernel[i] = kernel(xFrom - (xStart + i));
yKernel[i] = kernel(yFrom - (yStart + i));
}
for (let channel = 0; channel < 3; channel++) {
let q = 0;
for (let i = 0; i < twoFilterSize; i++) {
const y = yStart + i;
const yClamped = clamp(y, 0, yMax);
let p = 0;
for (let j = 0; j < twoFilterSize; j++) {
const x = xStart + j;
const index = readIndex(clamp(x, 0, xMax), yClamped);
p += data[index + channel] * xKernel[j];
}
q += p * yKernel[i];
}
write.data[to + channel] = Math.round(q);
}
};
}
function copyPixelBicubic(read, write) {
const b = -0.5;
const kernel = x => {
x = Math.abs(x);
const x2 = x*x;
const x3 = x*x*x;
return x <= 1 ?
(b + 2)*x3 - (b + 3)*x2 + 1 :
b*x3 - 5*b*x2 + 8*b*x - 4*b;
};
return kernelResample(read, write, 2, kernel);
}
function copyPixelLanczos(read, write) {
const filterSize = 5;
const kernel = x => {
if (x === 0) {
return 1;
}
else {
const xp = Math.PI * x;
return filterSize * Math.sin(xp) * Math.sin(xp / filterSize) / (xp * xp);
}
};
return kernelResample(read, write, filterSize, kernel);
}
const orientations = {
pz: (out, x, y) => {
out.x = -1;
out.y = -x;
out.z = -y;
},
nz: (out, x, y) => {
out.x = 1;
out.y = x;
out.z = -y;
},
px: (out, x, y) => {
out.x = x;
out.y = -1;
out.z = -y;
},
nx: (out, x, y) => {
out.x = -x;
out.y = 1;
out.z = -y;
},
py: (out, x, y) => {
out.x = -y;
out.y = -x;
out.z = 1;
},
ny: (out, x, y) => {
out.x = y;
out.y = -x;
out.z = -1;
}
};
function renderFace({data: readData, face, rotation, interpolation, maxWidth = Infinity}) {
const faceWidth = Math.min(maxWidth, readData.width / 4);
const faceHeight = faceWidth;
const cube = {};
const orientation = orientations[face];
const writeData = new ImageData(faceWidth, faceHeight);
const copyPixel =
interpolation === 'linear' ? copyPixelBilinear(readData, writeData) :
interpolation === 'cubic' ? copyPixelBicubic(readData, writeData) :
interpolation === 'lanczos' ? copyPixelLanczos(readData, writeData) :
copyPixelNearest(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);
}
}
postMessage(writeData);
}
onmessage = function({data}) {
renderFace(data);
};