Saxum/data/shader/skydome.fsh

90 lines
2.2 KiB
Plaintext
Raw Normal View History

2015-03-04 15:08:03 +00:00
#version 150
in vec2 vTexCoord;
in vec4 fragPosition;
2015-03-04 16:09:46 +00:00
in vec4 sunPosition;
2015-03-04 15:08:03 +00:00
out vec4 oColor;
uniform sampler2D uTexture;
uniform sampler2D nightTexture;
2015-03-04 15:08:03 +00:00
uniform float farPlane;
2015-03-08 23:39:59 +00:00
uniform vec4 fogColorDay;
uniform vec4 fogColorRise;
uniform vec4 fogColorNight;
2015-03-04 15:08:03 +00:00
uniform vec3 cameraCenter;
2015-03-04 16:09:46 +00:00
uniform vec3 sunColor;
uniform vec3 directionalVector;
2015-03-04 15:08:03 +00:00
2015-03-06 15:14:59 +00:00
const float sunSize = 20.0;
2015-03-04 16:47:34 +00:00
2015-03-08 23:39:59 +00:00
vec4 fogColor(float dot) {
float riseFactor = 0.0;
float dayFactor = 0.0;
if(dot<-0.52) {
riseFactor = 0.0;
}
else if (dot>0.52) {
riseFactor = 0.0;
}
else {
riseFactor = cos(3*dot);
}
if(dot<0.0) {
dayFactor = 0.0;
}
else if(dot>1.0) {
2015-03-08 23:39:59 +00:00
dayFactor = 1.0;
}
else {
dayFactor = sin(1.5*dot);
2015-03-08 23:39:59 +00:00
}
if (dot <0.0) {
return mix(fogColorNight, fogColorRise, riseFactor);
}
else {
return mix(fogColorRise, fogColorDay, dayFactor);
}
}
vec3 sunColorFunc(float dot) {
float riseFactor = 0.0;
if(dot<-0.79) {
riseFactor = 0.0;
}
else if (dot>0.876) {
riseFactor = 0.0;
}
else {
riseFactor = cos(2*dot);
}
return mix(sunColor, vec3(fogColorRise), riseFactor);
}
2015-03-04 15:08:03 +00:00
void main() {
vec4 textureColor = vec4(0.0, 0.0, 0.0, 1.0);
float sunAngle = -dot(normalize(directionalVector), vec3(0.0, 1.0, 0.0));
vec4 dayColor = texture(uTexture, vTexCoord);
if (sunAngle >= 0.0) {
textureColor = mix(dayColor, texture(nightTexture, vTexCoord), sunAngle);
}
else {
textureColor = dayColor;
}
2015-03-04 16:09:46 +00:00
float distanceToSun = length(sunPosition - fragPosition);
2015-03-04 15:08:03 +00:00
float distanceCameraCenter = distance(cameraCenter, vec3(fragPosition));
float fogFactor = clamp((1.0 - ((farPlane - 35.0) -distanceCameraCenter)/30.0), 0.0, 1.0);
fogFactor *= clamp((1.0-((fragPosition.y-40.0)/30.0)), 0.0, 1.0);
2015-03-04 16:47:34 +00:00
if (distanceToSun < sunSize) {
float sunIntensity = clamp(0.3*exp(1/(distanceToSun/sunSize))-exp(1.0)*0.3, 0.0, 1.0);
2015-03-08 23:39:59 +00:00
vec4 color = mix(textureColor, vec4(sunColorFunc(sunAngle), sunIntensity), sunIntensity);
oColor = mix(color, fogColor(-sunAngle), fogFactor);
2015-03-04 16:09:46 +00:00
}
else {
2015-03-08 23:39:59 +00:00
oColor = mix(textureColor, fogColor(-sunAngle), fogFactor);
2015-03-04 16:09:46 +00:00
}
2015-03-04 15:08:03 +00:00
}