2014-12-15 14:10:19 +00:00
|
|
|
#version 150
|
2014-10-20 15:31:26 +00:00
|
|
|
|
|
|
|
in vec3 vNormal;
|
|
|
|
in vec2 vTexCoord;
|
2014-11-03 23:47:35 +00:00
|
|
|
in vec4 fragPosition;
|
2015-02-28 13:40:54 +00:00
|
|
|
in vec4 shadowCoord0;
|
|
|
|
in vec4 shadowCoord1;
|
|
|
|
in vec4 shadowCoord2;
|
2014-10-20 15:31:26 +00:00
|
|
|
|
|
|
|
out vec4 oColor;
|
|
|
|
|
|
|
|
uniform sampler2D uTexture;
|
2015-02-28 13:40:54 +00:00
|
|
|
uniform sampler2DShadow shadowMap_directional0;
|
|
|
|
uniform sampler2DShadow shadowMap_directional1;
|
|
|
|
uniform sampler2DShadow shadowMap_directional2;
|
2015-02-09 20:29:44 +00:00
|
|
|
uniform samplerCubeShadow shadowMap_cube0;
|
|
|
|
uniform samplerCubeShadow shadowMap_cube1;
|
|
|
|
uniform samplerCubeShadow shadowMap_cube2;
|
|
|
|
uniform samplerCubeShadow shadowMap_cube3;
|
|
|
|
uniform samplerCubeShadow shadowMap_cube4;
|
|
|
|
uniform samplerCubeShadow shadowMap_cube5;
|
|
|
|
uniform samplerCubeShadow shadowMap_cube6;
|
|
|
|
uniform samplerCubeShadow shadowMap_cube7;
|
|
|
|
uniform samplerCubeShadow shadowMap_cube8;
|
|
|
|
uniform samplerCubeShadow shadowMap_cube9;
|
2014-11-03 22:54:35 +00:00
|
|
|
uniform vec3 ambientColor;
|
2014-10-31 09:38:57 +00:00
|
|
|
uniform float ambientFactor;
|
2014-11-07 10:37:26 +00:00
|
|
|
uniform float diffuseFactor;
|
|
|
|
uniform float specularFactor;
|
|
|
|
uniform vec3 camera;
|
|
|
|
uniform float shininess;
|
|
|
|
uniform int lightCount;
|
2015-02-13 16:09:25 +00:00
|
|
|
uniform int maxShadowRenderCount;
|
2014-11-17 16:51:15 +00:00
|
|
|
uniform vec3 directionalLightVector;
|
|
|
|
uniform vec3 directionalColor;
|
|
|
|
uniform float directionalIntensity;
|
2014-12-15 00:16:43 +00:00
|
|
|
uniform vec3 lightSources[32];
|
|
|
|
uniform vec3 lightColors[32];
|
|
|
|
uniform float lightIntensities[32];
|
2015-03-08 00:59:38 +00:00
|
|
|
uniform bool isFlame[32];
|
2014-12-19 12:27:05 +00:00
|
|
|
uniform float farPlane;
|
2015-03-08 23:39:59 +00:00
|
|
|
uniform vec4 fogColorDay;
|
|
|
|
uniform vec4 fogColorRise;
|
|
|
|
uniform vec4 fogColorNight;
|
2014-11-21 01:46:02 +00:00
|
|
|
uniform vec3 cameraCenter;
|
2015-03-07 13:56:16 +00:00
|
|
|
uniform bool movingTexture;
|
|
|
|
uniform vec2 movement;
|
|
|
|
uniform float time;
|
2014-10-20 15:31:26 +00:00
|
|
|
|
2014-12-04 13:53:07 +00:00
|
|
|
vec2 poissonDisk[16] = vec2[](
|
|
|
|
vec2( -0.94201624, -0.39906216 ),
|
|
|
|
vec2( 0.94558609, -0.76890725 ),
|
|
|
|
vec2( -0.094184101, -0.92938870 ),
|
|
|
|
vec2( 0.34495938, 0.29387760 ),
|
|
|
|
vec2( -0.91588581, 0.45771432 ),
|
|
|
|
vec2( -0.81544232, -0.87912464 ),
|
|
|
|
vec2( -0.38277543, 0.27676845 ),
|
|
|
|
vec2( 0.97484398, 0.75648379 ),
|
|
|
|
vec2( 0.44323325, -0.97511554 ),
|
|
|
|
vec2( 0.53742981, -0.47373420 ),
|
|
|
|
vec2( -0.26496911, -0.41893023 ),
|
|
|
|
vec2( 0.79197514, 0.19090188 ),
|
|
|
|
vec2( -0.24188840, 0.99706507 ),
|
|
|
|
vec2( -0.81409955, 0.91437590 ),
|
|
|
|
vec2( 0.19984126, 0.78641367 ),
|
|
|
|
vec2( 0.14383161, -0.14100790 )
|
|
|
|
);
|
|
|
|
|
2015-03-08 22:13:16 +00:00
|
|
|
float flickerFunction(int index) {
|
|
|
|
return 0.7*pow(sin(20.0*time + lightSources[index].x*lightSources[index].z), 2) + 0.3;
|
2015-03-08 00:59:38 +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>0.51) {
|
|
|
|
dayFactor = 1.0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dayFactor = sin(3*dot);
|
|
|
|
}
|
|
|
|
if (dot <0.0) {
|
|
|
|
return mix(fogColorNight, fogColorRise, riseFactor);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return mix(fogColorRise, fogColorDay, dayFactor);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-03 10:04:39 +00:00
|
|
|
float sampleDirectionalShadow(sampler2DShadow shadowMap, vec4 shadowCoord, float maxBias ) {
|
2014-12-04 16:19:58 +00:00
|
|
|
float visibility = 1.0;
|
2015-02-28 14:10:14 +00:00
|
|
|
const float stretching = 650.0;
|
2014-12-04 16:19:58 +00:00
|
|
|
float bias = 0.001*tan(acos(clamp(dot(vNormal, -directionalLightVector), 0.0, 1.0)));
|
2015-03-03 10:04:39 +00:00
|
|
|
bias = clamp(bias, 0.0, maxBias);
|
2015-01-19 12:44:38 +00:00
|
|
|
for (int i=0; i<4; i++) {
|
2015-02-28 14:10:14 +00:00
|
|
|
visibility -= directionalIntensity/16*(1.0-texture(shadowMap, vec3(shadowCoord.xy + poissonDisk[i]/stretching, shadowCoord.z - bias)));
|
2014-12-04 16:19:58 +00:00
|
|
|
}
|
|
|
|
if (visibility == 1.0-(directionalIntensity/16)*4)
|
|
|
|
{
|
|
|
|
visibility = 1.0-directionalIntensity;
|
|
|
|
}
|
|
|
|
else if (visibility != 1.0) {
|
|
|
|
for (int i=0; i<12; i++) {
|
2015-02-28 14:10:14 +00:00
|
|
|
visibility -= directionalIntensity/16*(1.0-texture(shadowMap, vec3(shadowCoord.xy + poissonDisk[i]/stretching, shadowCoord.z - bias)));
|
2014-12-04 16:19:58 +00:00
|
|
|
}
|
2015-01-19 12:44:38 +00:00
|
|
|
}
|
|
|
|
return visibility;
|
2014-12-04 16:19:58 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 13:27:41 +00:00
|
|
|
float samplePointShadow(samplerCubeShadow shadowMap, vec3 lightDirection) {
|
2015-01-22 20:21:09 +00:00
|
|
|
float nearPlane = 0.1;
|
|
|
|
float A = -(farPlane+nearPlane)/(farPlane-nearPlane);
|
|
|
|
float B = -2*(farPlane*nearPlane)/(farPlane - nearPlane);
|
|
|
|
float compValue = 0.5*(-A*length(lightDirection) + B)/length(lightDirection) + 0.5;
|
2015-02-09 23:27:13 +00:00
|
|
|
float bias = 0.001*tan(acos(clamp(dot(vNormal, -directionalLightVector), 0.0, 1.0)));
|
|
|
|
bias = clamp(bias, 0.0, 0.01);
|
2015-02-05 12:19:51 +00:00
|
|
|
return texture(shadowMap, vec4(lightDirection , compValue - bias));
|
2014-12-15 14:58:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 23:57:17 +00:00
|
|
|
float distanceToBorder(vec2 vector) {
|
|
|
|
float xDistance = min(vector.x, 1.0-vector.x);
|
|
|
|
float yDistance = min(vector.y, 1.0-vector.y);
|
|
|
|
return min(xDistance, yDistance);
|
|
|
|
}
|
|
|
|
|
2014-10-20 15:31:26 +00:00
|
|
|
void main()
|
2014-10-31 09:38:57 +00:00
|
|
|
{
|
2014-11-03 22:54:35 +00:00
|
|
|
vec3 ambientColor = ambientFactor * ambientColor;
|
2014-10-31 11:56:09 +00:00
|
|
|
vec3 diffuseColor = vec3(0.0, 0.0, 0.0);
|
2014-11-07 10:37:26 +00:00
|
|
|
vec3 specularColor = vec3(0.0, 0.0, 0.0);
|
2014-11-17 16:51:15 +00:00
|
|
|
|
2014-12-04 00:13:59 +00:00
|
|
|
|
|
|
|
// direction lighting
|
2015-03-08 23:39:59 +00:00
|
|
|
float sunAngle = -1.0;
|
2014-11-17 16:51:15 +00:00
|
|
|
if(length(directionalLightVector)>0.0f) {
|
|
|
|
vec3 directionalVector = normalize(directionalLightVector);
|
2015-03-08 23:39:59 +00:00
|
|
|
sunAngle = dot(vec3(0.0, 1.0, 0.0), directionalVector);
|
|
|
|
if ( sunAngle > -0.5) {
|
2015-03-08 13:56:18 +00:00
|
|
|
float directionalVisibility = 1.0f;
|
|
|
|
if (distanceToBorder(shadowCoord1.xy) <= 0.5 && distanceToBorder(shadowCoord1.xy) > 0.2) {
|
|
|
|
if (distanceToBorder(shadowCoord0.xy) <= 0.5 && distanceToBorder(shadowCoord0.xy) > 0.2) {
|
|
|
|
directionalVisibility = sampleDirectionalShadow(shadowMap_directional0, shadowCoord0, 0.001);
|
|
|
|
}
|
|
|
|
else if (distanceToBorder(shadowCoord0.xy) <= 0.5 && distanceToBorder(shadowCoord0.xy) > 0.0) {
|
|
|
|
float directionalVisibility0 = sampleDirectionalShadow(shadowMap_directional0, shadowCoord0, 0.001);
|
|
|
|
float directionalVisibility1 = sampleDirectionalShadow(shadowMap_directional1, shadowCoord1, 0.002);
|
|
|
|
directionalVisibility = mix(directionalVisibility0, directionalVisibility1, distanceToBorder(shadowCoord0.xy) * 5);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
directionalVisibility = sampleDirectionalShadow(shadowMap_directional1, shadowCoord1, 0.002);
|
|
|
|
}
|
2015-02-28 13:40:54 +00:00
|
|
|
}
|
2015-03-08 13:56:18 +00:00
|
|
|
else if (distanceToBorder(shadowCoord1.xy) <= 0.5 && distanceToBorder(shadowCoord1.xy) > 0.0) {
|
|
|
|
float directionalVisibility1 = sampleDirectionalShadow(shadowMap_directional1, shadowCoord1, 0.01);
|
|
|
|
float directionalVisibility2 = sampleDirectionalShadow(shadowMap_directional2, shadowCoord2, 0.01);
|
|
|
|
directionalVisibility = mix(directionalVisibility1, directionalVisibility2, distanceToBorder(shadowCoord1.xy) * 5);
|
2015-02-28 14:10:14 +00:00
|
|
|
}
|
2015-02-28 13:40:54 +00:00
|
|
|
else {
|
2015-03-08 13:56:18 +00:00
|
|
|
directionalVisibility = sampleDirectionalShadow(shadowMap_directional2, shadowCoord2, 0.01);
|
2015-02-28 13:40:54 +00:00
|
|
|
}
|
2015-03-08 13:56:18 +00:00
|
|
|
diffuseColor += clamp(dot(normalize(vNormal), directionalVector)
|
|
|
|
*diffuseFactor*directionalIntensity*directionalColor, 0.0, 1.0)*directionalVisibility;
|
|
|
|
vec3 cameraVector = normalize(camera - vec3(fragPosition));
|
|
|
|
specularColor += clamp(pow((dot((cameraVector+directionalVector),normalize(vNormal))/
|
|
|
|
(length(cameraVector+directionalVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0)
|
|
|
|
*specularFactor*directionalIntensity*directionalColor*directionalVisibility;
|
2015-02-28 13:40:54 +00:00
|
|
|
}
|
2014-11-17 16:51:15 +00:00
|
|
|
}
|
2014-12-04 00:13:59 +00:00
|
|
|
|
|
|
|
// point lights
|
2014-12-15 14:58:55 +00:00
|
|
|
float visibility = 1.0;
|
2014-10-31 11:56:09 +00:00
|
|
|
for(int i = 0; i<lightCount; i++) {
|
2014-12-15 14:58:55 +00:00
|
|
|
vec3 lightDirection = vec3(fragPosition) - lightSources[i];
|
|
|
|
float distance = length(lightDirection);
|
2015-02-13 16:09:25 +00:00
|
|
|
float pointVisibility = 1.0f;
|
2014-11-04 11:24:19 +00:00
|
|
|
// only take lights into account with meaningful contribution
|
2015-02-13 14:51:49 +00:00
|
|
|
if (distance < farPlane) {
|
2015-02-13 16:09:25 +00:00
|
|
|
if (i == 0 && i<maxShadowRenderCount) {
|
2015-02-13 14:51:49 +00:00
|
|
|
pointVisibility = samplePointShadow(shadowMap_cube0, lightDirection);
|
2015-02-09 20:29:44 +00:00
|
|
|
}
|
2015-02-13 16:09:25 +00:00
|
|
|
if (i == 1 && i<maxShadowRenderCount) {
|
2015-02-13 14:51:49 +00:00
|
|
|
pointVisibility = samplePointShadow(shadowMap_cube1, lightDirection);
|
2015-02-10 17:19:26 +00:00
|
|
|
}
|
2015-02-13 16:09:25 +00:00
|
|
|
if (i == 2 && i<maxShadowRenderCount) {
|
2015-02-13 14:51:49 +00:00
|
|
|
pointVisibility = samplePointShadow(shadowMap_cube2, lightDirection);
|
2015-02-10 17:19:26 +00:00
|
|
|
}
|
2015-02-13 16:09:25 +00:00
|
|
|
if (i == 3 && i<maxShadowRenderCount) {
|
2015-02-13 14:51:49 +00:00
|
|
|
pointVisibility = samplePointShadow(shadowMap_cube3, lightDirection);
|
2015-02-10 17:19:26 +00:00
|
|
|
}
|
2015-02-13 16:09:25 +00:00
|
|
|
if (i == 4 && i<maxShadowRenderCount) {
|
2015-02-13 14:51:49 +00:00
|
|
|
pointVisibility = samplePointShadow(shadowMap_cube4, lightDirection);
|
2015-02-10 17:19:26 +00:00
|
|
|
}
|
2015-02-13 16:09:25 +00:00
|
|
|
if (i == 5 && i<maxShadowRenderCount) {
|
2015-02-13 14:51:49 +00:00
|
|
|
pointVisibility = samplePointShadow(shadowMap_cube5, lightDirection);
|
2015-02-10 17:19:26 +00:00
|
|
|
}
|
2015-02-13 16:09:25 +00:00
|
|
|
if (i == 6 && i<maxShadowRenderCount) {
|
2015-02-13 14:51:49 +00:00
|
|
|
pointVisibility = samplePointShadow(shadowMap_cube6, lightDirection);
|
2015-02-10 17:19:26 +00:00
|
|
|
}
|
2015-02-13 16:09:25 +00:00
|
|
|
if (i == 7 && i<maxShadowRenderCount) {
|
2015-02-13 14:51:49 +00:00
|
|
|
pointVisibility = samplePointShadow(shadowMap_cube7, lightDirection);
|
2015-02-10 17:19:26 +00:00
|
|
|
}
|
2015-02-13 16:09:25 +00:00
|
|
|
if (i == 8 && i<maxShadowRenderCount) {
|
2015-02-13 14:51:49 +00:00
|
|
|
pointVisibility = samplePointShadow(shadowMap_cube8, lightDirection);
|
2015-02-10 17:19:26 +00:00
|
|
|
}
|
2015-02-13 16:09:25 +00:00
|
|
|
if (i == 9 && i<maxShadowRenderCount) {
|
2015-02-13 14:51:49 +00:00
|
|
|
pointVisibility = samplePointShadow(shadowMap_cube9, lightDirection);
|
2015-02-10 17:19:26 +00:00
|
|
|
}
|
2015-02-13 14:51:49 +00:00
|
|
|
vec3 lightVector = normalize(lightSources[i]-vec3(fragPosition));
|
2015-03-08 00:59:38 +00:00
|
|
|
float intensity = 0.0f;
|
|
|
|
if (isFlame[i] == true) {
|
2015-03-08 22:13:16 +00:00
|
|
|
intensity = clamp(exp(-(1/(lightIntensities[i] + flickerFunction(i)))*distance), 0.0, 1.0);
|
2015-03-08 00:59:38 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
intensity = clamp(exp(-(1/lightIntensities[i])*distance), 0.0, 1.0);
|
|
|
|
}
|
2015-02-13 14:51:49 +00:00
|
|
|
diffuseColor += clamp(dot(normalize(vNormal), lightVector)
|
|
|
|
*diffuseFactor*intensity*lightColors[i], 0.0, 1.0)*pointVisibility;
|
|
|
|
vec3 cameraVector = normalize(camera - vec3(fragPosition));
|
|
|
|
specularColor += clamp(pow((dot((cameraVector+lightVector),normalize(vNormal))/
|
|
|
|
(length(cameraVector+lightVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0)
|
|
|
|
*specularFactor*intensity*lightColors[i]*pointVisibility;
|
2014-11-04 11:24:19 +00:00
|
|
|
}
|
2014-10-31 11:56:09 +00:00
|
|
|
}
|
2014-11-19 00:58:48 +00:00
|
|
|
|
2014-12-04 00:13:59 +00:00
|
|
|
|
2014-11-07 10:37:26 +00:00
|
|
|
vec3 finalColor = specularColor + diffuseColor + ambientColor;
|
2014-11-21 01:46:02 +00:00
|
|
|
float distanceCameraCenter = distance(cameraCenter, vec3(fragPosition));
|
2014-12-19 12:27:05 +00:00
|
|
|
float fogFactor = clamp((1.0 - ((farPlane - 35.0) -distanceCameraCenter)/30.0), 0.0, 1.0);
|
2015-01-19 17:21:29 +00:00
|
|
|
fogFactor *= clamp((1.0-((fragPosition.y-40.0)/30.0)), 0.0, 1.0);
|
2014-10-20 15:31:26 +00:00
|
|
|
|
2015-03-07 13:56:16 +00:00
|
|
|
vec4 textureColor = vec4(0.0, 0.0, 0.0, 1.0);
|
|
|
|
if (movingTexture == true) {
|
|
|
|
textureColor = texture(uTexture, vec2(vTexCoord.x + movement.x * time, vTexCoord.y + movement.y * time)).rgba;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
textureColor = texture(uTexture, vTexCoord).rgba;
|
|
|
|
}
|
|
|
|
oColor = vec4(finalColor, 1.0f)*textureColor;
|
2015-03-08 23:39:59 +00:00
|
|
|
oColor = mix(oColor, fogColor(sunAngle), fogFactor);
|
2014-10-20 15:31:26 +00:00
|
|
|
}
|