Skip to content

Commit

Permalink
Stars twinkle
Browse files Browse the repository at this point in the history
  • Loading branch information
gecko0307 committed Nov 22, 2024
1 parent 489ffe7 commit 0691ba9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,33 @@ float noise3d(vec3 x)
uniform vec3 spaceColor;
uniform float starsThreshold;
uniform float starsBrightness;
uniform float starsTwinkleSpeed;

uniform vec3 sunDirection;
uniform vec3 sunColor;

uniform float localTime;

const float sunEnergy = 100.0;
const float sunAngularDiameterCos = 0.9999;

void main()
{
vec3 radiance = toLinear(spaceColor);

float stars = noise3d(normalize(worldNormal));
float starsRadiance = (stars >= starsThreshold)? pow((stars - starsThreshold) / (1.0 - starsThreshold), starsBrightness) : 0.0;
vec3 starsColor = vec3(1.0, 1.0, 1.0); // Make uniform
radiance += toLinear(starsColor) * starsRadiance;
vec3 n = normalize(worldNormal);

float randomFactor = noise3d(n);
float starsRadiance = (randomFactor >= starsThreshold)? pow((randomFactor - starsThreshold) / (1.0 - starsThreshold), starsBrightness) : 0.0;
vec3 starsColor = vec3(0.9 + 0.1 * randomFactor, 0.9, 1.0 - 0.1 * randomFactor);

float twinkleRandomFactor = hash(dot(n, vec3(12.9898, 78.233, 45.164)));
float twinkle = 0.5 + 0.5 * sin((twinkleRandomFactor + localTime * starsTwinkleSpeed) * PI2);
twinkle = mix(0.1, 1.5, twinkle);

radiance += toLinear(starsColor) * starsRadiance * twinkle;

float cosTheta = clamp(dot(normalize(worldNormal), sunDirection), 0.0, 1.0);
float cosTheta = clamp(dot(n, sunDirection), 0.0, 1.0);
float sunDisk = smoothstep(sunAngularDiameterCos, sunAngularDiameterCos + 0.00002, cosTheta);
radiance += sunColor * sunDisk * sunEnergy;

Expand Down
8 changes: 6 additions & 2 deletions src/dagon/extra/starfieldsky.d
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ class StarfieldSkyShader: Shader
Color4f spaceColor = Color4f(0.0f, 0.0f, 0.0f, 1.0f);
float starsThreshold = 0.995f;
float starsBrightness = 8.0f;
float starsTwinkleSpeed = 1.0f;

Vector3f sunDirection = Vector3f(-1.0f, -1.0f, -1.0f).normalized;
Color4f sunColor = Color4f(1.0f, 1.0f, 1.0f, 1.0f);

this(Owner owner)
{
vs = Shader.load("data/__internal/shaders/Sky/Sky.vert.glsl");
fs = Shader.load("data/__internal/shaders/Sky/Sky.frag.glsl");
vs = Shader.load("data/__internal/shaders/StarfieldSky/StarfieldSky.vert.glsl");
fs = Shader.load("data/__internal/shaders/StarfieldSky/StarfieldSky.frag.glsl");

auto myProgram = New!ShaderProgram(vs, fs, this);
super(myProgram, owner);
Expand All @@ -81,6 +82,9 @@ class StarfieldSkyShader: Shader
setParameter("spaceColor", spaceColor.rgb);
setParameter("starsThreshold", starsThreshold);
setParameter("starsBrightness", starsBrightness);
setParameter("starsTwinkleSpeed", starsTwinkleSpeed);

setParameter("localTime", state.localTime);

if (state.material.sun)
{
Expand Down

0 comments on commit 0691ba9

Please sign in to comment.