Skip to content

Commit

Permalink
dagon.extra.starfieldsky
Browse files Browse the repository at this point in the history
  • Loading branch information
gecko0307 committed Nov 22, 2024
1 parent 177a1c0 commit 489ffe7
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 1 deletion.
68 changes: 68 additions & 0 deletions data/__internal/shaders/StarfieldSky/Sky.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#version 400 core

#define PI 3.14159265359
const float PI2 = PI * 2.0;

uniform mat4 invViewMatrix;
uniform float gbufferMask;
uniform float blurMask;

in vec3 eyePosition;
in vec3 worldNormal;

in vec4 currPosition;
in vec4 prevPosition;

layout(location = 0) out vec4 fragColor;
layout(location = 3) out vec4 fragRadiance;
layout(location = 4) out vec4 fragVelocity;

vec3 toLinear(vec3 v)
{
return pow(v, vec3(2.2));
}

float hash(float n)
{
return fract((1.0 + sin(n)) * 415.92653);
}

float noise3d(vec3 x)
{
float xhash = hash(round(400 * x.x) * 37.0);
float yhash = hash(round(400 * x.y) * 57.0);
float zhash = hash(round(400 * x.z) * 67.0);
return fract(xhash + yhash + zhash);
}

uniform vec3 spaceColor;
uniform float starsThreshold;
uniform float starsBrightness;

uniform vec3 sunDirection;
uniform vec3 sunColor;

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;

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

vec2 posScreen = (currPosition.xy / currPosition.w) * 0.5 + 0.5;
vec2 prevPosScreen = (prevPosition.xy / prevPosition.w) * 0.5 + 0.5;
vec2 velocity = posScreen - prevPosScreen;

fragColor = vec4(radiance, gbufferMask);
fragRadiance = vec4(radiance, 0.0);
fragVelocity = vec4(velocity, blurMask, 0.0);
}
26 changes: 26 additions & 0 deletions data/__internal/shaders/StarfieldSky/Sky.vert.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#version 400 core

layout (location = 0) in vec3 va_Vertex;

uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;

uniform mat4 prevModelViewMatrix;

out vec3 eyePosition;
out vec3 worldNormal;

out vec4 currPosition;
out vec4 prevPosition;

void main()
{
vec4 pos = modelViewMatrix * vec4(va_Vertex, 1.0);
eyePosition = pos.xyz;
worldNormal = normalize(va_Vertex);

currPosition = projectionMatrix * pos;
prevPosition = projectionMatrix * prevModelViewMatrix * vec4(va_Vertex, 1.0);

gl_Position = currPosition;
}
1 change: 1 addition & 0 deletions src/dagon/extra/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ module dagon.extra;
public
{
import dagon.extra.rayleigh;
import dagon.extra.starfieldsky;
import dagon.extra.water;
}
108 changes: 108 additions & 0 deletions src/dagon/extra/starfieldsky.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright (c) 2024 Timur Gafarov
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

module dagon.extra.starfieldsky;

import std.stdio;
import std.math;
import std.conv;

import dlib.core.memory;
import dlib.core.ownership;
import dlib.math.vector;
import dlib.image.color;
import dlib.text.str;

import dagon.core.bindings;
import dagon.graphics.state;
import dagon.graphics.shader;

class StarfieldSkyShader: Shader
{
String vs, fs;
Color4f spaceColor = Color4f(0.0f, 0.0f, 0.0f, 1.0f);
float starsThreshold = 0.995f;
float starsBrightness = 8.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");

auto myProgram = New!ShaderProgram(vs, fs, this);
super(myProgram, owner);
}

~this()
{
vs.free();
fs.free();
}

override void bindParameters(GraphicsState* state)
{
setParameter("modelViewMatrix", state.modelViewMatrix);
setParameter("projectionMatrix", state.projectionMatrix);
setParameter("normalMatrix", state.normalMatrix);
setParameter("viewMatrix", state.viewMatrix);
setParameter("invViewMatrix", state.invViewMatrix);
setParameter("prevModelViewMatrix", state.prevModelViewMatrix);

setParameter("gbufferMask", state.gbufferMask);
setParameter("blurMask", state.blurMask);

setParameter("spaceColor", spaceColor.rgb);
setParameter("starsThreshold", starsThreshold);
setParameter("starsBrightness", starsBrightness);

if (state.material.sun)
{
setParameter("sunDirection", state.material.sun.directionAbsolute);
setParameter("sunColor", state.material.sun.color.rgb);
}
else if (state.environment.sun)
{
setParameter("sunDirection", state.environment.sun.directionAbsolute);
setParameter("sunColor", state.environment.sun.color.rgb);
}
else
{
setParameter("sunDirection", -sunDirection);
setParameter("sunColor", sunColor.rgb);
}

super.bindParameters(state);
}

override void unbindParameters(GraphicsState* state)
{
super.unbindParameters(state);
}
}
1 change: 0 additions & 1 deletion src/dagon/resource/obj.d
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ class OBJAsset: Asset
);
}

writeln("Mesh...");
mesh = fillMesh(
tmpFaces.data,
tmpTexcoords,
Expand Down

0 comments on commit 489ffe7

Please sign in to comment.