Expose Camera Forward Direction Built-in To Sky Shaders
Describe the Project You Are Working on
View-dependent stylized skybox effects are a crucial aspect of creating immersive and visually appealing environments in games and other interactive applications. However, implementing these effects can be challenging due to the limitations of sky shaders. In this article, we will explore a proposal to expose the camera's forward direction as a built-in variable in sky shaders, making it easier to create complex and engaging skybox effects.
Describe the Problem or Limitation You Are Having in Your Project
One of the most popular skybox shaders is the Mario 64-style skybox, which offsets an image in screen space based on the current camera's rotation. However, this approach has a significant limitation: it cannot be implemented in a proper sky shader since the sky shader built-ins lack any information about which way the camera is actually facing. This makes it difficult to create more complex skybox effects that rely on the camera's direction.
Describe the Feature / Enhancement and How It Helps to Overcome the Problem or Limitation
To overcome this limitation, we propose adding a built-in in vec3 FORWARD
variable to sky shaders. This would provide the camera's forward vector in world space, allowing developers to calculate the camera's rotation and create various effects that involve comparing the camera direction against other vectors. This feature would be particularly useful for creating retro-style graphics, which often rely on stylized skybox effects.
Describe How Your Proposal Will Work, with Code, Pseudo-code, Mock-ups, and/or Diagrams
Here are a few examples of how the proposed feature can be used to create complex skybox effects:
Mario 64 Style Skybox
The Mario 64-style skybox is a classic example of a stylized skybox effect. By exposing the camera's forward direction as a built-in variable, developers can easily calculate the camera's rotation and create a seamless and immersive skybox experience.
// Camera YX rotation
y = atan(FORWARD.x, FORWARD.z);
x = asin(FORWARD.y);
vec2 uvOffset = vec2(y * -0.5, x) / PI;
uvOffset.y += 0.5;
...
Directional Blend
The directional blend effect is another example of how the proposed feature can be used to create complex skybox effects. By comparing the camera's forward direction against a reference vector, developers can create a seamless transition between different skybox textures.
float FdotN = dot(FORWARD, vec3(0, 0, 1));
float t = smoothstep(-0.5, 0.5, FdotN);
COLOR = mix(skyA, skyB, t);
Sun Glare
The sun glare effect is a classic example of a stylized skybox effect that relies on the camera's direction. By exposing the camera's forward direction as a built-in variable, developers can easily calculate the camera's rotation and create a realistic sun glare effect.
float FdotL = dot(FORWARD, LIGHT0_DIRECTION);
float glare = pow(saturate(FdotL), power);
COLOR += vec3(glare) * strength;
If This Enhancement Will Not Be Used Often, Can It Be Worked Around with a Few Lines of Script?
While it is technically possible to work around the limitation of sky shaders by sending camera rotation to a sky shader with a script, this approach becomes more complicated for multi-camera setups. Additionally, creating a custom skybox object and using the built-ins available to spatial shaders is also possible, but this approach requires additional setup and can be inconvenient for sharing shaders.
Is There a Reason Why This Should Be Core and Not an Add-on in the Asset Library?
While most of the use cases for this feature are niche, there is a significant interest among Godot developers in creating retro-style graphics. By exposing the camera's forward direction as a built-in variable in sky shaders, developers can easily create complex and engaging skybox effects without having to resort to workarounds or custom solutions. This feature should be considered core and not an add-on in the asset library, as it provides a significant value to developers and enhances the overall Godot experience.
Q: What is the current limitation of sky shaders in Godot?
A: The current limitation of sky shaders in Godot is that they lack any information about which way the camera is actually facing. This makes it difficult to create more complex skybox effects that rely on the camera's direction.
Q: How does the proposed feature address this limitation?
A: The proposed feature addresses this limitation by adding a built-in in vec3 FORWARD
variable to sky shaders. This would provide the camera's forward vector in world space, allowing developers to calculate the camera's rotation and create various effects that involve comparing the camera direction against other vectors.
Q: What are some examples of how the proposed feature can be used?
A: Some examples of how the proposed feature can be used include:
- Creating a Mario 64-style skybox effect by offsetting an image in screen space based on the current camera's rotation.
- Creating a directional blend effect by comparing the camera's forward direction against a reference vector.
- Creating a sun glare effect by calculating the camera's rotation and applying a realistic sun glare effect.
Q: Can this feature be worked around with a few lines of script?
A: While it is technically possible to work around the limitation of sky shaders by sending camera rotation to a sky shader with a script, this approach becomes more complicated for multi-camera setups. Additionally, creating a custom skybox object and using the built-ins available to spatial shaders is also possible, but this approach requires additional setup and can be inconvenient for sharing shaders.
Q: Why should this feature be considered core and not an add-on in the asset library?
A: While most of the use cases for this feature are niche, there is a significant interest among Godot developers in creating retro-style graphics. By exposing the camera's forward direction as a built-in variable in sky shaders, developers can easily create complex and engaging skybox effects without having to resort to workarounds or custom solutions. This feature provides a significant value to developers and enhances the overall Godot experience.
Q: What are the benefits of having this feature as a built-in variable in sky shaders?
A: The benefits of having this feature as a built-in variable in sky shaders include:
- Simplified development: By providing a built-in variable for the camera's forward direction, developers can easily create complex skybox effects without having to resort to workarounds or custom solutions.
- Improved performance: By reducing the need for script-based solutions, this feature can improve performance and reduce the load on the engine.
- Enhanced creativity: By providing a powerful tool for creating complex skybox effects, this feature can enhance creativity and allow developers to push the boundaries of what is possible in Godot.
Q: What is the current status of this feature?
A: The current status of this feature is that it is being considered for inclusion in the Godot engine. If implemented, it would provide a significant value to developers and enhance the overall Godot experience.
Q: How can developers provide feedback on this feature?
A: Developers can provide feedback on this feature by submitting comments and suggestions on the Godot forums or by participating in the Godot community.