(So I’m in my barebones C++ Vulkan game engine and I want to make a procedural sky shader like there is in Godot 4 and so far it’s been pretty straight forward, it’s not a skybox but a single triangle large enough to cover the whole screen rendered orthographically in front of the camera but always rendered behind everything. In Godot 4, the procedural sky shader has a very important 3D vector called EYEDIR which is the direction of the current pixel in the fragment shader, I have not quite figured out how to calculate this.)
I tried the non-optimized code from this article to transform from window space to eye space https://www.khronos.org/opengl/wiki/Compute_eye_space_from_window_space
My code looks like this:
vec4 calcEyeFromWindow()
{
vec4 ndcPos;
ndcPos.xy = (2.0 * gl_FragCoord.xy) / (vec2(screenWidth, screenHeight)) - 1;
ndcPos.z = (2.0 * gl_FragCoord.z - near - far) / (far - near);
ndcPos.w = 1.0;
vec4 clipPos = ndcPos / gl_FragCoord.w;
vec4 eyePos = invPers * clipPos;
return eyePos;
}
Now I think I’m supposed to normalize that for a direction, however, it isn’t what I’m looking for, it doesn’t take into account the camera’s rotation or field of view, it looks the same no matter where you turn and it just stretches to the window size no matter what you resize it to.
I’ve tried transforming the directions into the camera’s perspective with
vec3 transformDirection(vec4 p){
vec3 u1 = right * p.x;
vec3 u2 = forward * p.y;
vec3 u3 = up * p.z;
return normalize(u1 + u2 + u3);// the transformed direction in world space
}
but not only does this still not account for the field of view, it doesn’t even work anyway, the sky appears to twist and flip around as you look around with the camera. (yes, Z is up)
So basically I’m asking, how is EYEDIR calculated in Godot 4 sky shaders? (What is the math for figuring out the direction of each pixel in a fragment shader which assumedly takes FOV into account and aligns perfectly with the other things being rendered in the scene?)