diff --git a/data/__internal/shaders/StarfieldSky/Sky.frag.glsl b/data/__internal/shaders/StarfieldSky/StarfieldSky.frag.glsl similarity index 67% rename from data/__internal/shaders/StarfieldSky/Sky.frag.glsl rename to data/__internal/shaders/StarfieldSky/StarfieldSky.frag.glsl index 47697dc..732bd72 100644 --- a/data/__internal/shaders/StarfieldSky/Sky.frag.glsl +++ b/data/__internal/shaders/StarfieldSky/StarfieldSky.frag.glsl @@ -38,10 +38,13 @@ 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; @@ -49,12 +52,19 @@ 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; diff --git a/data/__internal/shaders/StarfieldSky/Sky.vert.glsl b/data/__internal/shaders/StarfieldSky/StarfieldSky.vert.glsl similarity index 100% rename from data/__internal/shaders/StarfieldSky/Sky.vert.glsl rename to data/__internal/shaders/StarfieldSky/StarfieldSky.vert.glsl diff --git a/src/dagon/extra/starfieldsky.d b/src/dagon/extra/starfieldsky.d index 0a2c044..67ba002 100644 --- a/src/dagon/extra/starfieldsky.d +++ b/src/dagon/extra/starfieldsky.d @@ -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); @@ -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) {