Last active
March 26, 2026 21:56
-
-
Save partybusiness/0dcbde99cdea3ed89eb4457ba8bf6464 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| shader_type sky; | |
| uniform mat4 transform; // you can set this in gdscript from a Transform3D | |
| // for example | |
| // var transform := Transform3D.IDENTITY.rotated(Vector3.UP, 0.4) | |
| // shader_material.set_shader_parameter("transform", transform) | |
| uniform sampler2D sky_texture:source_color, repeat_enable; | |
| // I copied the approximate functions from here to make sure they match SKY_COORD | |
| // https://github.com/godotengine/godot/blob/babc272d44ecfbd86e3b3fc5397ef936b1ce12d8/servers/rendering/renderer_rd/shaders/environment/sky.glsl | |
| #define M_PI 3.14159265359 | |
| // Eberly approximation from https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/. | |
| // input [-1, 1] and output [0, PI] | |
| float acos_approx(float p_x) { | |
| float x = abs(p_x); | |
| float res = -0.156583f * x + (M_PI / 2.0); | |
| res *= sqrt(1.0f - x); | |
| return (p_x >= 0.0) ? res : M_PI - res; | |
| } | |
| // Based on https://math.stackexchange.com/questions/1098487/atan2-faster-approximation | |
| // but using the Eberly coefficients from https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/. | |
| float atan2_approx(float y, float x) { | |
| float a = min(abs(x), abs(y)) / max(abs(x), abs(y)); | |
| float s = a * a; | |
| float poly = 0.0872929f; | |
| poly = -0.301895f + poly * s; | |
| poly = 1.0f + poly * s; | |
| poly = poly * a; | |
| float r = abs(y) > abs(x) ? (M_PI / 2.0) - poly : poly; | |
| r = x < 0.0 ? M_PI - r : r; | |
| r = y < 0.0 ? -r : r; | |
| return r; | |
| } | |
| void sky() { | |
| vec3 eyedir = (transform * vec4(EYEDIR, 0.0)).xyz; | |
| float x_coord = (atan2_approx(eyedir.x, eyedir.z) / PI * -0.5 + 0.5); | |
| float y_coord = acos_approx(eyedir.y) / PI; | |
| vec2 calc_coord = vec2(x_coord, y_coord); | |
| COLOR = textureLod(sky_texture, calc_coord, 0.0).rgb; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment