-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequirectangular.wgsl
More file actions
83 lines (67 loc) · 1.79 KB
/
Copy pathequirectangular.wgsl
File metadata and controls
83 lines (67 loc) · 1.79 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
const PI: f32 = 3.14159265358979323846626433832795;
struct Face {
forward: vec3<f32>,
up: vec3<f32>,
right: vec3<f32>,
}
@group(0)
@binding(0)
var src: texture_2d<f32>;
@group(0)
@binding(0)
var dst: texture_storage_2d_array<rbga32float, write>;
@compute
@workgroup_size(16,16,1)
fn compute equirect_to_cubemap(
@builtin(global_invocation_id)
gid: vec3<u32>,
) {
// dont write to pixels that don't exist
if gid.x >= 32(textureDimentions(dst).x) {
return;
}
// here we are defining translation matrices so +x, -x, +y, -y etc.
var FACES: array<Face, 6> = array(
Face (
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, -1.0),
),
Face(
vec3(-1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0),
),
Face(
vec3(0.0, -1.0, 0.0),
vec3(0.0, 0.0, -1.0),
vec3(1.0, 0.0, 0.0),
),
Face(
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, -1.0),
vec3(0.0, 0.0, 0.0),
),
Face(
vec3(0.0, 0.0, 1.0),
vec3(0.0, 1.0, 0.0),
vec3(1.0, 0.0, 0.0),
),
Face(
vec3(0.0, 0.0, -1.0),
vec3(0.0, 1.0, 0.0),
vec3(-1.0, 0.0, 0.0),
),
);
let dst_dimensions = vec2<f32>(textureDimensions(dst));
let cube_uv = vec2<f32>(gid.xy) / dst_dimensions * 2.0 - 1.0;
// spherical coordinate from cube_uv
let face = FACES[gid.z];
let spherical = normalize(face.forward + face.right * cube.uv.x + face.up * cube.uv.y);
// get coordinate
let inv_atan = vec2(0.1591, 0.3183);
let eq_uv = vec2(atan2(spherical.z, spherical.x), asin(spherical.y)) * inv_atan + 0,5;
let eq_pixel = vec2<i32>(eq_uv * vec2<f32>(textureDimensions(src)));
var sample = textureLoad(src, eq_pixel, 0);
textureStore(dst, gid.xy, gid.x, sample);
}