Attempts at raycast

This commit is contained in:
2026-01-04 15:44:29 +01:00
parent 8858e0c53e
commit 21c1d2e139
3 changed files with 245 additions and 27 deletions

View File

@@ -8,6 +8,45 @@ layout(set = 0, binding = 2, rgba16f) uniform writeonly restrict imageCube _Cube
const float INV_PI = 0.31830987;
const float HALF_INV_PI = 0.15915494;
const mat3x3 MATRIX_2D_TO_CUBE[6] = mat3x3[](
// Positive X
mat3x3(
0, 0, -1,
0, -1, 0,
1, 0, 0
),
// Negative X
mat3x3(
0, 0, 1,
0, -1, 0,
-1, 0, 0
),
// Positive Y
mat3x3(
1, 0, 0,
0, 0, 1,
0, 1, 0
),
// Negative Y
mat3x3(
1, 0, 0,
0, 0, -1,
0, -1, 0
),
// Positive Z
mat3x3(
1, 0, 0,
0, -1, 0,
0, 0, 1
),
// Negative Z
mat3x3(
-1, 0, 0,
0, -1, 0,
0, 0, -1
)
);
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
void main() {
vec2 size = vec2(imageSize(_CubemapImage).xy);
@@ -16,27 +55,7 @@ void main() {
texCoord = texCoord * vec2(2.0) - vec2(1.0); // Map to range [-1, 1]
vec3 cubeCoord;
if (layerIndex == 0) {
// Positive X
cubeCoord = vec3(1, -texCoord.y, -texCoord.x);
} else if (layerIndex == 1) {
// Negative X
cubeCoord = vec3(-1, -texCoord.y, texCoord.x);
} else if (layerIndex == 2) {
// Positive Y
cubeCoord = vec3(texCoord.x, 1, texCoord.y);
} else if (layerIndex == 3) {
// Negative Y
cubeCoord = vec3(texCoord.x, -1, -texCoord.y);
} else if (layerIndex == 4) {
// Positive Z
cubeCoord = vec3(texCoord.x, -texCoord.y, 1);
} else if (layerIndex == 5) {
// Negative Z
cubeCoord = vec3(-texCoord.x, -texCoord.y, -1);
}
vec3 cubeCoord = MATRIX_2D_TO_CUBE[layerIndex] * vec3(texCoord, 1.0);
vec3 cubeDir = normalize(cubeCoord);
float theta = atan(cubeDir.y, cubeDir.x);
@@ -45,5 +64,5 @@ void main() {
vec2 equirectCoord = vec2(theta * HALF_INV_PI, phi * INV_PI) + vec2(0.5);
vec4 irradiance = texture(sampler2D(_EquirectangularTexture, _EquirectangularSampler), equirectCoord);
imageStore(_CubemapImage, ivec3(ivec2(gl_GlobalInvocationID.xy), layerIndex), irradiance);
imageStore(_CubemapImage, ivec3(gl_GlobalInvocationID.xyz), irradiance);
}