Created
February 16, 2021 00:08
-
-
Save tomekr/d8c14e71a9c93858aa6d058102942997 to your computer and use it in GitHub Desktop.
Starfield background for a bevy shader background
This file contains 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
#version 450 | |
#define M_PI 3.1415926535897932384626433832795 | |
layout(location = 0) in vec4 v_Position; | |
layout(location = 1) in vec2 v_Uv; | |
layout(location = 0) out vec4 o_Target; | |
layout(set = 2, binding = 0) uniform ShaderInputs_time { | |
float time; | |
}; | |
layout(set = 2, binding = 1) uniform ShaderInputs_resolution { | |
vec2 resolution; | |
}; | |
float rand(vec2 co) | |
{ | |
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); | |
} | |
void main() | |
{ | |
float size = 30.0; | |
float prob = 0.95; | |
vec2 pos = floor(1.0 / size * v_Uv.xy); | |
float color = 0.0; | |
float starValue = rand(pos); | |
if (starValue > prob) | |
{ | |
vec2 center = size * pos + vec2(size, size) * 0.5; | |
float t = 0.9 + 0.2 * sin(time + (starValue - prob) / (1.0 - prob) * 45.0); | |
color = 1.0 - distance(v_Uv, center) / (0.5 * size); | |
color = color * t / (abs(v_Uv.y - center.y)) * t / (abs(v_Uv.x - center.x)); | |
} | |
else if (rand(v_Uv.xy / resolution) > 0.996) | |
{ | |
float r = rand(v_Uv.xy); | |
color = r * (0.25 * sin(time * (r * 5.0) + 720.0 * r) + 0.75); | |
} | |
o_Target = vec4(vec3(color), 1.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment