2013-06-23 09:13:01 +00:00
|
|
|
|
2019-02-22 10:30:48 +00:00
|
|
|
layout(location = 0) in vec4 vTexCoord;
|
|
|
|
layout(location = 1) in vec4 vColor;
|
|
|
|
|
|
|
|
#ifndef SIMPLE // we do not need these for simple shaders
|
|
|
|
layout(location = 2) in vec4 pixelpos;
|
|
|
|
layout(location = 3) in vec3 glowdist;
|
|
|
|
layout(location = 4) in vec3 gradientdist;
|
|
|
|
layout(location = 5) in vec4 vWorldNormal;
|
|
|
|
layout(location = 6) in vec4 vEyeNormal;
|
|
|
|
#endif
|
2013-06-23 09:13:01 +00:00
|
|
|
|
2018-06-12 21:52:33 +00:00
|
|
|
layout(location=0) out vec4 FragColor;
|
2016-09-20 00:57:57 +00:00
|
|
|
#ifdef GBUFFER_PASS
|
2018-06-12 21:52:33 +00:00
|
|
|
layout(location=1) out vec4 FragFog;
|
|
|
|
layout(location=2) out vec4 FragNormal;
|
2016-09-20 00:57:57 +00:00
|
|
|
#endif
|
2014-07-14 23:05:53 +00:00
|
|
|
|
2018-07-15 21:01:40 +00:00
|
|
|
struct Material
|
|
|
|
{
|
|
|
|
vec4 Base;
|
|
|
|
vec4 Bright;
|
|
|
|
vec3 Normal;
|
|
|
|
vec3 Specular;
|
|
|
|
float Glossiness;
|
|
|
|
float SpecularLevel;
|
|
|
|
float Metallic;
|
|
|
|
float Roughness;
|
|
|
|
float AO;
|
|
|
|
};
|
|
|
|
|
2014-05-12 12:45:41 +00:00
|
|
|
vec4 Process(vec4 color);
|
|
|
|
vec4 ProcessTexel();
|
2018-07-15 21:01:40 +00:00
|
|
|
Material ProcessMaterial();
|
|
|
|
vec4 ProcessLight(Material mat, vec4 color);
|
|
|
|
vec3 ProcessMaterialLight(Material material, vec3 color);
|
2013-06-23 09:13:01 +00:00
|
|
|
|
2018-03-29 21:21:25 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Color to grayscale
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
float grayscale(vec4 color)
|
|
|
|
{
|
2018-07-02 14:31:48 +00:00
|
|
|
return dot(color.rgb, vec3(0.3, 0.56, 0.14));
|
2018-03-29 21:21:25 +00:00
|
|
|
}
|
|
|
|
|
2014-05-12 12:45:41 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Desaturate a color
|
|
|
|
//
|
|
|
|
//===========================================================================
|
2013-06-23 09:13:01 +00:00
|
|
|
|
2014-05-12 12:45:41 +00:00
|
|
|
vec4 desaturate(vec4 texel)
|
|
|
|
{
|
|
|
|
if (uDesaturationFactor > 0.0)
|
|
|
|
{
|
2018-03-29 21:21:25 +00:00
|
|
|
float gray = grayscale(texel);
|
2014-05-21 13:25:25 +00:00
|
|
|
return mix (texel, vec4(gray,gray,gray,texel.a), uDesaturationFactor);
|
2014-05-12 12:45:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return texel;
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 09:13:01 +00:00
|
|
|
|
2014-05-12 12:45:41 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// This function is common for all (non-special-effect) fragment shaders
|
|
|
|
//
|
|
|
|
//===========================================================================
|
2013-06-23 09:13:01 +00:00
|
|
|
|
2014-05-12 12:45:41 +00:00
|
|
|
vec4 getTexel(vec2 st)
|
|
|
|
{
|
2014-11-28 11:28:45 +00:00
|
|
|
vec4 texel = texture(tex, st);
|
2014-05-12 12:45:41 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Apply texture modes
|
|
|
|
//
|
|
|
|
switch (uTextureMode)
|
|
|
|
{
|
2018-10-21 06:14:48 +00:00
|
|
|
case 1: // TM_STENCIL
|
2014-05-12 12:45:41 +00:00
|
|
|
texel.rgb = vec3(1.0,1.0,1.0);
|
|
|
|
break;
|
|
|
|
|
2014-09-09 11:21:36 +00:00
|
|
|
case 2: // TM_OPAQUE
|
2014-05-12 12:45:41 +00:00
|
|
|
texel.a = 1.0;
|
|
|
|
break;
|
|
|
|
|
2014-09-09 11:21:36 +00:00
|
|
|
case 3: // TM_INVERSE
|
2014-05-12 12:45:41 +00:00
|
|
|
texel = vec4(1.0-texel.r, 1.0-texel.b, 1.0-texel.g, texel.a);
|
|
|
|
break;
|
2014-09-09 11:21:36 +00:00
|
|
|
|
2018-10-21 06:14:48 +00:00
|
|
|
case 4: // TM_ALPHATEXTURE
|
2018-06-16 11:47:24 +00:00
|
|
|
{
|
2018-03-29 21:21:25 +00:00
|
|
|
float gray = grayscale(texel);
|
2018-03-22 19:42:17 +00:00
|
|
|
texel = vec4(1.0, 1.0, 1.0, gray*texel.a);
|
2014-09-09 11:21:36 +00:00
|
|
|
break;
|
2018-06-16 11:47:24 +00:00
|
|
|
}
|
2014-09-21 09:08:17 +00:00
|
|
|
|
|
|
|
case 5: // TM_CLAMPY
|
|
|
|
if (st.t < 0.0 || st.t > 1.0)
|
|
|
|
{
|
|
|
|
texel.a = 0.0;
|
|
|
|
}
|
|
|
|
break;
|
2018-03-28 17:07:43 +00:00
|
|
|
|
|
|
|
case 6: // TM_OPAQUEINVERSE
|
|
|
|
texel = vec4(1.0-texel.r, 1.0-texel.b, 1.0-texel.g, 1.0);
|
|
|
|
break;
|
2018-06-14 19:28:03 +00:00
|
|
|
|
|
|
|
case 7: //TM_FOGLAYER
|
|
|
|
return texel;
|
2018-06-16 11:47:24 +00:00
|
|
|
|
2014-05-12 12:45:41 +00:00
|
|
|
}
|
2013-06-23 09:13:01 +00:00
|
|
|
|
2018-12-19 16:44:45 +00:00
|
|
|
texel.rgb += uAddColor.rgb;
|
2018-12-19 07:51:30 +00:00
|
|
|
|
2018-12-21 07:11:57 +00:00
|
|
|
if (uObjectColor2.a == 0.0) texel *= uObjectColor;
|
|
|
|
else texel *= mix(uObjectColor, uObjectColor2, gradientdist.z);
|
|
|
|
|
2014-05-12 12:45:41 +00:00
|
|
|
return desaturate(texel);
|
|
|
|
}
|
2013-06-23 09:13:01 +00:00
|
|
|
|
2014-05-12 12:45:41 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
2018-12-15 06:11:28 +00:00
|
|
|
// Vanilla Doom wall colormap equation
|
2014-05-12 12:45:41 +00:00
|
|
|
//
|
|
|
|
//===========================================================================
|
2018-12-19 10:07:39 +00:00
|
|
|
float R_WallColormap(float lightnum, float z, vec3 normal)
|
2018-12-15 06:11:28 +00:00
|
|
|
{
|
|
|
|
// R_ScaleFromGlobalAngle calculation
|
|
|
|
float projection = 160.0; // projection depends on SCREENBLOCKS!! 160 is the fullscreen value
|
2018-12-15 09:02:57 +00:00
|
|
|
vec2 line_v1 = pixelpos.xz; // in vanilla this is the first curline vertex
|
2018-12-19 10:07:39 +00:00
|
|
|
vec2 line_normal = normal.xz;
|
2018-12-15 09:02:57 +00:00
|
|
|
float texscale = projection * clamp(dot(normalize(uCameraPos.xz - line_v1), line_normal), 0.0, 1.0) / z;
|
2018-12-15 06:11:28 +00:00
|
|
|
|
|
|
|
float lightz = clamp(16.0 * texscale, 0.0, 47.0);
|
|
|
|
|
|
|
|
// scalelight[lightnum][lightz] lookup
|
|
|
|
float startmap = (15.0 - lightnum) * 4.0;
|
|
|
|
return startmap - lightz * 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Vanilla Doom plane colormap equation
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
float R_PlaneColormap(float lightnum, float z)
|
|
|
|
{
|
|
|
|
float lightz = clamp(z / 16.0f, 0.0, 127.0);
|
|
|
|
|
|
|
|
// zlight[lightnum][lightz] lookup
|
|
|
|
float startmap = (15.0 - lightnum) * 4.0;
|
|
|
|
float scale = 160.0 / (lightz + 1.0);
|
|
|
|
return startmap - scale * 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// zdoom colormap equation
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
float R_ZDoomColormap(float light, float z)
|
2013-06-23 09:13:01 +00:00
|
|
|
{
|
2016-07-23 14:30:28 +00:00
|
|
|
float L = light * 255.0;
|
2018-12-15 06:11:28 +00:00
|
|
|
float vis = min(uGlobVis / z, 24.0 / 32.0);
|
|
|
|
float shade = 2.0 - (L + 12.0) / 128.0;
|
|
|
|
float lightscale = shade - vis;
|
|
|
|
return lightscale * 31.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float R_DoomColormap(float light, float z)
|
|
|
|
{
|
|
|
|
if ((uPalLightLevels >> 16) == 16) // gl_lightmode 16
|
|
|
|
{
|
|
|
|
float lightnum = clamp(light * 15.0, 0.0, 15.0);
|
|
|
|
|
|
|
|
if (dot(vWorldNormal.xyz, vWorldNormal.xyz) > 0.5)
|
|
|
|
{
|
2018-12-19 10:07:39 +00:00
|
|
|
vec3 normal = normalize(vWorldNormal.xyz);
|
|
|
|
return mix(R_WallColormap(lightnum, z, normal), R_PlaneColormap(lightnum, z), abs(normal.y));
|
2018-12-15 06:11:28 +00:00
|
|
|
}
|
|
|
|
else // vWorldNormal is not set on sprites
|
|
|
|
{
|
|
|
|
return R_PlaneColormap(lightnum, z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return R_ZDoomColormap(light, z);
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 09:13:01 +00:00
|
|
|
|
2018-12-15 06:11:28 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Doom software lighting equation
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
float R_DoomLightingEquation(float light)
|
|
|
|
{
|
|
|
|
// z is the depth in view space, positive going into the screen
|
2017-06-25 13:02:34 +00:00
|
|
|
float z;
|
2018-12-15 06:11:28 +00:00
|
|
|
if (((uPalLightLevels >> 8) & 0xff) == 2)
|
2017-06-25 13:02:34 +00:00
|
|
|
{
|
2017-06-25 13:37:14 +00:00
|
|
|
z = distance(pixelpos.xyz, uCameraPos.xyz);
|
2017-06-25 13:02:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-25 13:37:14 +00:00
|
|
|
z = pixelpos.w;
|
2017-06-25 13:02:34 +00:00
|
|
|
}
|
2013-06-23 09:13:01 +00:00
|
|
|
|
2018-12-15 06:11:28 +00:00
|
|
|
float colormap = R_DoomColormap(light, z);
|
|
|
|
|
2017-06-25 13:02:34 +00:00
|
|
|
if ((uPalLightLevels & 0xff) != 0)
|
2018-12-15 06:11:28 +00:00
|
|
|
colormap = floor(colormap) + 0.5;
|
2013-06-23 09:13:01 +00:00
|
|
|
|
2016-07-23 14:30:28 +00:00
|
|
|
// Result is the normalized colormap index (0 bright .. 1 dark)
|
2018-12-15 06:11:28 +00:00
|
|
|
return clamp(colormap, 0.0, 31.0) / 32.0;
|
2013-06-23 09:13:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 18:10:57 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Check if light is in shadow according to its 1D shadow map
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2017-03-10 18:10:40 +00:00
|
|
|
#ifdef SUPPORTS_SHADOWMAPS
|
|
|
|
|
2017-08-13 11:49:02 +00:00
|
|
|
float shadowDirToU(vec2 dir)
|
2017-03-02 18:10:57 +00:00
|
|
|
{
|
2018-10-04 13:33:07 +00:00
|
|
|
if (abs(dir.y) > abs(dir.x))
|
2017-03-02 18:10:57 +00:00
|
|
|
{
|
2018-10-04 13:33:07 +00:00
|
|
|
float x = dir.x / dir.y * 0.125;
|
|
|
|
if (dir.y >= 0.0)
|
|
|
|
return 0.125 + x;
|
2017-03-02 18:10:57 +00:00
|
|
|
else
|
2018-10-04 13:33:07 +00:00
|
|
|
return (0.50 + 0.125) + x;
|
2017-03-02 18:10:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-04 13:33:07 +00:00
|
|
|
float y = dir.y / dir.x * 0.125;
|
|
|
|
if (dir.x >= 0.0)
|
|
|
|
return (0.25 + 0.125) - y;
|
2017-03-02 18:10:57 +00:00
|
|
|
else
|
2018-10-04 13:33:07 +00:00
|
|
|
return (0.75 + 0.125) - y;
|
2017-03-02 18:10:57 +00:00
|
|
|
}
|
2017-08-13 11:49:02 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 02:36:11 +00:00
|
|
|
vec2 shadowUToDir(float u)
|
2017-08-13 11:49:02 +00:00
|
|
|
{
|
2018-10-05 02:36:11 +00:00
|
|
|
u *= 4.0;
|
|
|
|
vec2 raydir;
|
|
|
|
switch (int(u))
|
|
|
|
{
|
|
|
|
case 0: raydir = vec2(u * 2.0 - 1.0, 1.0); break;
|
|
|
|
case 1: raydir = vec2(1.0, 1.0 - (u - 1.0) * 2.0); break;
|
|
|
|
case 2: raydir = vec2(1.0 - (u - 2.0) * 2.0, -1.0); break;
|
|
|
|
case 3: raydir = vec2(-1.0, (u - 3.0) * 2.0 - 1.0); break;
|
|
|
|
}
|
|
|
|
return raydir;
|
2017-08-13 11:49:02 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 02:36:11 +00:00
|
|
|
float sampleShadowmap(vec3 planePoint, float v)
|
2018-10-04 13:33:07 +00:00
|
|
|
{
|
2018-10-05 02:36:11 +00:00
|
|
|
float bias = 1.0;
|
|
|
|
float negD = dot(vWorldNormal.xyz, planePoint);
|
2018-10-04 13:33:07 +00:00
|
|
|
|
2018-10-05 02:36:11 +00:00
|
|
|
vec3 ray = planePoint;
|
2018-10-04 13:33:07 +00:00
|
|
|
|
|
|
|
vec2 isize = textureSize(ShadowMap, 0);
|
|
|
|
float scale = float(isize.x) * 0.25;
|
|
|
|
|
|
|
|
// Snap to shadow map texel grid
|
|
|
|
if (abs(ray.z) > abs(ray.x))
|
|
|
|
{
|
|
|
|
ray.y = ray.y / abs(ray.z);
|
|
|
|
ray.x = ray.x / abs(ray.z);
|
|
|
|
ray.x = (floor((ray.x + 1.0) * 0.5 * scale) + 0.5) / scale * 2.0 - 1.0;
|
|
|
|
ray.z = sign(ray.z);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ray.y = ray.y / abs(ray.x);
|
|
|
|
ray.z = ray.z / abs(ray.x);
|
|
|
|
ray.z = (floor((ray.z + 1.0) * 0.5 * scale) + 0.5) / scale * 2.0 - 1.0;
|
|
|
|
ray.x = sign(ray.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
float t = negD / dot(vWorldNormal.xyz, ray) - bias;
|
2018-10-05 02:36:11 +00:00
|
|
|
vec2 dir = ray.xz * t;
|
|
|
|
|
|
|
|
float u = shadowDirToU(dir);
|
|
|
|
float dist2 = dot(dir, dir);
|
|
|
|
return step(dist2, texture(ShadowMap, vec2(u, v)).x);
|
2018-10-04 13:33:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 02:36:11 +00:00
|
|
|
float sampleShadowmapPCF(vec3 planePoint, float v)
|
|
|
|
{
|
|
|
|
float bias = 1.0;
|
|
|
|
float negD = dot(vWorldNormal.xyz, planePoint);
|
|
|
|
|
|
|
|
vec3 ray = planePoint;
|
|
|
|
|
|
|
|
if (abs(ray.z) > abs(ray.x))
|
|
|
|
ray.y = ray.y / abs(ray.z);
|
|
|
|
else
|
|
|
|
ray.y = ray.y / abs(ray.x);
|
|
|
|
|
|
|
|
vec2 isize = textureSize(ShadowMap, 0);
|
|
|
|
float scale = float(isize.x);
|
|
|
|
float texelPos = floor(shadowDirToU(ray.xz) * scale);
|
|
|
|
|
|
|
|
float sum = 0.0;
|
|
|
|
float step_count = uShadowmapFilter;
|
|
|
|
|
|
|
|
texelPos -= step_count + 0.5;
|
|
|
|
for (float x = -step_count; x <= step_count; x++)
|
|
|
|
{
|
2018-10-05 19:30:16 +00:00
|
|
|
float u = fract(texelPos / scale);
|
2018-10-05 02:36:11 +00:00
|
|
|
vec2 dir = shadowUToDir(u);
|
|
|
|
|
|
|
|
ray.x = dir.x;
|
|
|
|
ray.z = dir.y;
|
|
|
|
float t = negD / dot(vWorldNormal.xyz, ray) - bias;
|
|
|
|
dir = ray.xz * t;
|
|
|
|
|
|
|
|
float dist2 = dot(dir, dir);
|
|
|
|
sum += step(dist2, texture(ShadowMap, vec2(u, v)).x);
|
|
|
|
texelPos++;
|
|
|
|
}
|
|
|
|
return sum / (uShadowmapFilter * 2.0 + 1.0);
|
|
|
|
}
|
2017-03-08 00:35:07 +00:00
|
|
|
|
|
|
|
float shadowmapAttenuation(vec4 lightpos, float shadowIndex)
|
|
|
|
{
|
2017-03-10 21:08:55 +00:00
|
|
|
if (shadowIndex >= 1024.0)
|
2017-03-10 18:10:40 +00:00
|
|
|
return 1.0; // No shadowmap available for this light
|
|
|
|
|
2018-10-05 02:36:11 +00:00
|
|
|
vec3 planePoint = pixelpos.xyz - lightpos.xyz;
|
2018-12-23 14:13:38 +00:00
|
|
|
planePoint += 0.01; // nudge light position slightly as Doom maps tend to have their lights perfectly aligned with planes
|
2017-03-10 21:08:55 +00:00
|
|
|
|
2018-10-05 02:36:11 +00:00
|
|
|
if (dot(planePoint.xz, planePoint.xz) < 1.0)
|
|
|
|
return 1.0; // Light is too close
|
|
|
|
|
|
|
|
float v = (shadowIndex + 0.5) / 1024.0;
|
2017-03-10 21:08:55 +00:00
|
|
|
|
2018-10-03 11:45:54 +00:00
|
|
|
if (uShadowmapFilter <= 0)
|
2018-09-29 11:23:40 +00:00
|
|
|
{
|
2018-10-05 02:36:11 +00:00
|
|
|
return sampleShadowmap(planePoint, v);
|
2018-09-29 11:23:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-05 02:36:11 +00:00
|
|
|
return sampleShadowmapPCF(planePoint, v);
|
2017-03-08 00:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 01:01:33 +00:00
|
|
|
float shadowAttenuation(vec4 lightpos, float lightcolorA)
|
|
|
|
{
|
|
|
|
float shadowIndex = abs(lightcolorA) - 1.0;
|
|
|
|
return shadowmapAttenuation(lightpos, shadowIndex);
|
|
|
|
}
|
2017-03-10 18:10:40 +00:00
|
|
|
|
2018-02-19 01:01:33 +00:00
|
|
|
#else
|
2016-10-03 14:09:32 +00:00
|
|
|
|
2018-02-19 01:01:33 +00:00
|
|
|
float shadowAttenuation(vec4 lightpos, float lightcolorA)
|
2016-10-03 14:09:32 +00:00
|
|
|
{
|
2018-02-19 01:01:33 +00:00
|
|
|
return 1.0;
|
2016-10-03 14:09:32 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 01:01:33 +00:00
|
|
|
#endif
|
2018-01-23 22:10:28 +00:00
|
|
|
|
2018-02-19 01:01:33 +00:00
|
|
|
float spotLightAttenuation(vec4 lightpos, vec3 spotdir, float lightCosInnerAngle, float lightCosOuterAngle)
|
2018-01-23 22:10:28 +00:00
|
|
|
{
|
2018-02-19 01:01:33 +00:00
|
|
|
vec3 lightDirection = normalize(lightpos.xyz - pixelpos.xyz);
|
|
|
|
float cosDir = dot(lightDirection, spotdir);
|
|
|
|
return smoothstep(lightCosOuterAngle, lightCosInnerAngle, cosDir);
|
2018-01-23 22:10:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Adjust normal vector according to the normal map
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2018-02-19 23:13:05 +00:00
|
|
|
#if defined(NORMALMAP)
|
2018-01-23 22:10:28 +00:00
|
|
|
mat3 cotangent_frame(vec3 n, vec3 p, vec2 uv)
|
|
|
|
{
|
|
|
|
// get edge vectors of the pixel triangle
|
|
|
|
vec3 dp1 = dFdx(p);
|
|
|
|
vec3 dp2 = dFdy(p);
|
|
|
|
vec2 duv1 = dFdx(uv);
|
|
|
|
vec2 duv2 = dFdy(uv);
|
|
|
|
|
|
|
|
// solve the linear system
|
|
|
|
vec3 dp2perp = cross(n, dp2); // cross(dp2, n);
|
|
|
|
vec3 dp1perp = cross(dp1, n); // cross(n, dp1);
|
|
|
|
vec3 t = dp2perp * duv1.x + dp1perp * duv2.x;
|
|
|
|
vec3 b = dp2perp * duv1.y + dp1perp * duv2.y;
|
|
|
|
|
|
|
|
// construct a scale-invariant frame
|
|
|
|
float invmax = inversesqrt(max(dot(t,t), dot(b,b)));
|
|
|
|
return mat3(t * invmax, b * invmax, n);
|
|
|
|
}
|
|
|
|
|
2018-07-15 21:01:40 +00:00
|
|
|
vec3 ApplyNormalMap(vec2 texcoord)
|
2018-01-23 22:10:28 +00:00
|
|
|
{
|
|
|
|
#define WITH_NORMALMAP_UNSIGNED
|
|
|
|
#define WITH_NORMALMAP_GREEN_UP
|
|
|
|
//#define WITH_NORMALMAP_2CHANNEL
|
|
|
|
|
2018-01-23 22:59:58 +00:00
|
|
|
vec3 interpolatedNormal = normalize(vWorldNormal.xyz);
|
2018-01-23 22:10:28 +00:00
|
|
|
|
2018-07-15 21:01:40 +00:00
|
|
|
vec3 map = texture(normaltexture, texcoord).xyz;
|
2018-01-23 22:10:28 +00:00
|
|
|
#if defined(WITH_NORMALMAP_UNSIGNED)
|
|
|
|
map = map * 255./127. - 128./127.; // Math so "odd" because 0.5 cannot be precisely described in an unsigned format
|
|
|
|
#endif
|
|
|
|
#if defined(WITH_NORMALMAP_2CHANNEL)
|
|
|
|
map.z = sqrt(1 - dot(map.xy, map.xy));
|
|
|
|
#endif
|
|
|
|
#if defined(WITH_NORMALMAP_GREEN_UP)
|
|
|
|
map.y = -map.y;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mat3 tbn = cotangent_frame(interpolatedNormal, pixelpos.xyz, vTexCoord.st);
|
|
|
|
vec3 bumpedNormal = normalize(tbn * map);
|
|
|
|
return bumpedNormal;
|
|
|
|
}
|
|
|
|
#else
|
2018-07-15 21:01:40 +00:00
|
|
|
vec3 ApplyNormalMap(vec2 texcoord)
|
2018-01-23 22:10:28 +00:00
|
|
|
{
|
|
|
|
return normalize(vWorldNormal.xyz);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-23 09:13:01 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
2014-05-12 12:45:41 +00:00
|
|
|
// Calculate light
|
2013-06-23 09:13:01 +00:00
|
|
|
//
|
2014-05-12 12:45:41 +00:00
|
|
|
// It is important to note that the light color is not desaturated
|
|
|
|
// due to ZDoom's implementation weirdness. Everything that's added
|
|
|
|
// on top of it, e.g. dynamic lights and glows are, though, because
|
|
|
|
// the objects emitting these lights are also.
|
2013-06-23 09:13:01 +00:00
|
|
|
//
|
2014-05-12 12:45:41 +00:00
|
|
|
// This is making this a bit more complicated than it needs to
|
|
|
|
// because we can't just desaturate the final fragment color.
|
2013-06-23 09:13:01 +00:00
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2018-07-15 21:01:40 +00:00
|
|
|
vec4 getLightColor(Material material, float fogdist, float fogfactor)
|
2013-06-23 09:13:01 +00:00
|
|
|
{
|
2014-07-14 22:19:41 +00:00
|
|
|
vec4 color = vColor;
|
2014-05-12 12:45:41 +00:00
|
|
|
|
|
|
|
if (uLightLevel >= 0.0)
|
2013-06-23 09:13:01 +00:00
|
|
|
{
|
2016-07-23 14:30:28 +00:00
|
|
|
float newlightlevel = 1.0 - R_DoomLightingEquation(uLightLevel);
|
2014-05-12 12:45:41 +00:00
|
|
|
color.rgb *= newlightlevel;
|
|
|
|
}
|
2018-09-16 20:38:20 +00:00
|
|
|
else if (uFogEnabled > 0)
|
2014-05-12 12:45:41 +00:00
|
|
|
{
|
|
|
|
// brightening around the player for light mode 2
|
|
|
|
if (fogdist < uLightDist)
|
|
|
|
{
|
|
|
|
color.rgb *= uLightFactor - (fogdist / uLightDist) * (uLightFactor - 1.0);
|
|
|
|
}
|
2013-06-23 09:13:01 +00:00
|
|
|
|
2014-05-12 12:45:41 +00:00
|
|
|
//
|
|
|
|
// apply light diminishing through fog equation
|
|
|
|
//
|
2013-06-23 09:13:01 +00:00
|
|
|
color.rgb = mix(vec3(0.0, 0.0, 0.0), color.rgb, fogfactor);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// handle glowing walls
|
|
|
|
//
|
2014-05-12 12:45:41 +00:00
|
|
|
if (uGlowTopColor.a > 0.0 && glowdist.x < uGlowTopColor.a)
|
2013-06-23 09:13:01 +00:00
|
|
|
{
|
2014-05-12 12:45:41 +00:00
|
|
|
color.rgb += desaturate(uGlowTopColor * (1.0 - glowdist.x / uGlowTopColor.a)).rgb;
|
2013-06-23 09:13:01 +00:00
|
|
|
}
|
2014-05-12 12:45:41 +00:00
|
|
|
if (uGlowBottomColor.a > 0.0 && glowdist.y < uGlowBottomColor.a)
|
2013-06-23 09:13:01 +00:00
|
|
|
{
|
2014-05-12 12:45:41 +00:00
|
|
|
color.rgb += desaturate(uGlowBottomColor * (1.0 - glowdist.y / uGlowBottomColor.a)).rgb;
|
2013-06-23 09:13:01 +00:00
|
|
|
}
|
|
|
|
color = min(color, 1.0);
|
|
|
|
|
2014-05-12 12:45:41 +00:00
|
|
|
//
|
|
|
|
// apply brightmaps (or other light manipulation by custom shaders.
|
|
|
|
//
|
2018-07-15 21:01:40 +00:00
|
|
|
color = ProcessLight(material, color);
|
2013-06-23 09:13:01 +00:00
|
|
|
|
|
|
|
//
|
2018-02-19 01:01:33 +00:00
|
|
|
// apply dynamic lights
|
2013-06-23 09:13:01 +00:00
|
|
|
//
|
2018-07-15 21:01:40 +00:00
|
|
|
return vec4(ProcessMaterialLight(material, color.rgb), material.Base.a * vColor.a);
|
2013-06-23 09:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Applies colored fog
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
vec4 applyFog(vec4 frag, float fogfactor)
|
|
|
|
{
|
2014-05-12 12:45:41 +00:00
|
|
|
return vec4(mix(uFogColor.rgb, frag.rgb, fogfactor), frag.a);
|
2013-06-23 09:13:01 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 00:04:56 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// The color of the fragment if it is fully occluded by ambient lighting
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
vec3 AmbientOcclusionColor()
|
|
|
|
{
|
|
|
|
float fogdist;
|
|
|
|
float fogfactor;
|
|
|
|
|
|
|
|
//
|
|
|
|
// calculate fog factor
|
|
|
|
//
|
2018-09-16 20:38:20 +00:00
|
|
|
if (uFogEnabled == -1)
|
2016-09-21 00:04:56 +00:00
|
|
|
{
|
2018-10-09 08:38:18 +00:00
|
|
|
fogdist = max(16.0, pixelpos.w);
|
2016-09-21 00:04:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-13 16:08:04 +00:00
|
|
|
fogdist = max(16.0, distance(pixelpos.xyz, uCameraPos.xyz));
|
2016-09-21 00:04:56 +00:00
|
|
|
}
|
|
|
|
fogfactor = exp2 (uFogDensity * fogdist);
|
|
|
|
|
|
|
|
return mix(uFogColor.rgb, vec3(0.0), fogfactor);
|
|
|
|
}
|
2013-06-23 09:13:01 +00:00
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Main shader routine
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2018-07-15 21:01:40 +00:00
|
|
|
Material material = ProcessMaterial();
|
|
|
|
vec4 frag = material.Base;
|
2014-07-14 19:14:43 +00:00
|
|
|
|
2014-07-30 21:13:16 +00:00
|
|
|
#ifndef NO_ALPHATEST
|
2014-07-14 19:14:43 +00:00
|
|
|
if (frag.a <= uAlphaThreshold) discard;
|
|
|
|
#endif
|
|
|
|
|
2018-09-16 20:38:20 +00:00
|
|
|
if (uFogEnabled != -3) // check for special 2D 'fog' mode.
|
2018-06-14 19:28:03 +00:00
|
|
|
{
|
2018-06-16 11:47:24 +00:00
|
|
|
float fogdist = 0.0;
|
2018-09-16 20:38:20 +00:00
|
|
|
float fogfactor = 0.0;
|
2018-06-14 19:28:03 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// calculate fog factor
|
|
|
|
//
|
2018-09-16 20:38:20 +00:00
|
|
|
if (uFogEnabled != 0)
|
2018-06-14 19:28:03 +00:00
|
|
|
{
|
2018-09-16 20:38:20 +00:00
|
|
|
if (uFogEnabled == 1 || uFogEnabled == -1)
|
2018-06-16 11:47:24 +00:00
|
|
|
{
|
2018-10-09 08:38:18 +00:00
|
|
|
fogdist = max(16.0, pixelpos.w);
|
2018-06-16 11:47:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fogdist = max(16.0, distance(pixelpos.xyz, uCameraPos.xyz));
|
|
|
|
}
|
|
|
|
fogfactor = exp2 (uFogDensity * fogdist);
|
2018-06-14 19:28:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 11:47:24 +00:00
|
|
|
if (uTextureMode != 7)
|
2014-05-11 11:27:51 +00:00
|
|
|
{
|
2018-07-15 21:01:40 +00:00
|
|
|
frag = getLightColor(material, fogdist, fogfactor);
|
2014-05-12 12:45:41 +00:00
|
|
|
//
|
|
|
|
// colored fog
|
|
|
|
//
|
2018-09-16 20:38:20 +00:00
|
|
|
if (uFogEnabled < 0)
|
2014-05-12 12:45:41 +00:00
|
|
|
{
|
|
|
|
frag = applyFog(frag, fogfactor);
|
|
|
|
}
|
2013-06-23 09:13:01 +00:00
|
|
|
}
|
2018-06-16 11:47:24 +00:00
|
|
|
else
|
2014-05-12 12:45:41 +00:00
|
|
|
{
|
2018-06-16 11:47:24 +00:00
|
|
|
frag = vec4(uFogColor.rgb, (1.0 - fogfactor) * frag.a * 0.75 * vColor.a);
|
2014-05-12 12:45:41 +00:00
|
|
|
}
|
2018-06-16 11:47:24 +00:00
|
|
|
}
|
|
|
|
else // simple 2D (uses the fog color to add a color overlay)
|
|
|
|
{
|
|
|
|
if (uTextureMode == 7)
|
2013-06-23 09:13:01 +00:00
|
|
|
{
|
2018-06-16 11:47:24 +00:00
|
|
|
float gray = grayscale(frag);
|
|
|
|
vec4 cm = (uObjectColor + gray * (uObjectColor2 - uObjectColor)) * 2;
|
|
|
|
frag = vec4(clamp(cm.rgb, 0.0, 1.0), frag.a);
|
2013-06-23 09:13:01 +00:00
|
|
|
}
|
2018-09-16 20:38:20 +00:00
|
|
|
frag = frag * ProcessLight(material, vColor);
|
2018-06-16 11:47:24 +00:00
|
|
|
frag.rgb = frag.rgb + uFogColor.rgb;
|
2014-05-12 12:45:41 +00:00
|
|
|
}
|
2014-07-14 23:05:53 +00:00
|
|
|
FragColor = frag;
|
2016-09-20 00:57:57 +00:00
|
|
|
#ifdef GBUFFER_PASS
|
2016-10-05 05:57:27 +00:00
|
|
|
FragFog = vec4(AmbientOcclusionColor(), 1.0);
|
|
|
|
FragNormal = vec4(vEyeNormal.xyz * 0.5 + 0.5, 1.0);
|
2016-09-20 00:57:57 +00:00
|
|
|
#endif
|
2013-06-23 09:13:01 +00:00
|
|
|
}
|