Created
July 12, 2024 21:00
-
-
Save SED4906/dc604e706fcdce0ba65a6de57fd7c3f2 to your computer and use it in GitHub Desktop.
Iridescent Godot Shader, based on work by Alan Zucconi
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 spatial; | |
float saturate(float x) | |
{ | |
return clamp(x,0.0,1.0); | |
} | |
vec3 saturate3(vec3 x) | |
{ | |
return clamp(x, vec3(0), vec3(1)); | |
} | |
vec3 bump3y(vec3 x, vec3 yoffset) | |
{ | |
vec3 y = vec3(1.,1.,1.) - x * x; | |
y = saturate3(y-yoffset); | |
return y; | |
} | |
vec3 spectral_zucconi(float wavelength) { | |
float x = saturate((wavelength - 400.0) / 300.0); | |
const vec3 cs = vec3(3.54541723, 2.86670055, 2.29421995); | |
const vec3 xs = vec3(0.69548916, 0.49416934, 0.28269708); | |
const vec3 ys = vec3(0.02320775, 0.15936245, 0.53520021); | |
return bump3y(cs * (x - xs), ys); | |
} | |
void light() { | |
// Called for every pixel for every light affecting the material. | |
// Uncomment to replace the default light processing function with this one. | |
float d = 1600.0; | |
vec2 uv = UV * 2.0 - vec2(1,1); | |
vec3 uv_tangent = vec3(-uv.y, 0, uv.x); | |
vec3 world_tangent = normalize(MODEL_MATRIX * vec4(uv_tangent,0)).xyz; | |
float cos_thetaL = dot(LIGHT,world_tangent); | |
float cos_thetaV = dot(VIEW,world_tangent); | |
float u = abs(cos_thetaL - cos_thetaV); | |
if (u != 0.0) { | |
vec3 color = vec3(0); | |
for (int n = 1; n <= 4; n++) { | |
float wavelength = u * d / float(n); | |
color += spectral_zucconi(wavelength) / float(n); | |
} | |
color = saturate3(color); | |
SPECULAR_LIGHT += color; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment