townforge/GameData/Shaders/GLSL/TownforgeTerrain.glsl

316 lines
11 KiB
GLSL

#include "Uniforms.glsl"
#include "Samplers.glsl"
#include "Transform.glsl"
#include "ScreenPos.glsl"
#include "Lighting.glsl"
#include "Fog.glsl"
#include "PostProcess.glsl"
varying vec2 vTexCoord;
#ifndef GL_ES
varying vec2 vDetailTexCoord;
#else
varying mediump vec2 vDetailTexCoord;
#endif
varying vec3 vNormal;
varying vec4 vWorldPos;
#ifdef PERPIXEL
#ifdef SHADOW
#ifndef GL_ES
varying vec4 vShadowPos[NUMCASCADES];
#else
varying highp vec4 vShadowPos[NUMCASCADES];
#endif
#endif
#ifdef SPOTLIGHT
varying vec4 vSpotPos;
#endif
#ifdef POINTLIGHT
varying vec3 vCubeMaskVec;
#endif
#else
varying vec3 vVertexLight;
varying vec4 vScreenPos;
#ifdef ENVCUBEMAP
varying vec3 vReflectionVec;
#endif
#if defined(LIGHTMAP) || defined(AO)
varying vec2 vTexCoord2;
#endif
#endif
uniform sampler2D sWeightMap0;
uniform sampler2D sDetailMap1;
uniform sampler2D sDetailMap2;
uniform sampler2D sDetailMap3;
uniform sampler2D sDetailMap4;
uniform sampler2D sHighlightMap5;
#ifndef NOSNOW
#ifdef SNOWTEXTURE
uniform sampler2D sSnowMap6;
#endif
#endif
uniform sampler2D sDetailMap7;
#ifndef GL_ES
uniform vec2 cDetailTiling;
varying vec4 vEyeVec;
#else
uniform mediump vec2 cDetailTiling;
varying highp vec4 vEyeVec;
#endif
#ifndef NOSNOW
#
uniform float cSnowinessMinTemperature;
uniform float cSnowinessMaxTemperature;
uniform float cMaxSnowDepth;
const float min_snow_threshold = 0.25;
const float max_snow_threshold = 0.95;
const float snow_saturation = 0.15;
varying float snow_depth;
varying float snowiness;
#endif
uniform vec4 cHighlightColor;
uniform float cTemperature;
varying float grassiness;
uniform float cTemperatureAltitudeDecrease;
uniform float cTemperatureAltitudeFloor;
const float ambientMultiplier = 1.5;
float GetTemperatureAtHeight(float height)
{
height = max(height - cTemperatureAltitudeFloor, 0.0);
float temperature = cTemperature - height * cTemperatureAltitudeDecrease;
temperature -= height * cTemperatureAltitudeDecrease;
return temperature;
}
void VS()
{
mat4 modelMatrix = iModelMatrix;
vec3 worldPos = GetWorldPos(modelMatrix);
gl_Position = GetClipPos(worldPos);
vNormal = GetWorldNormal(modelMatrix);
vWorldPos = vec4(worldPos, GetDepth(gl_Position));
vTexCoord = GetTexCoord(iTexCoord);
vDetailTexCoord = cDetailTiling * vTexCoord;
vEyeVec = vec4(cCameraPos - worldPos, GetDepth(gl_Position));
#ifdef PERPIXEL
// Per-pixel forward lighting
vec4 projWorldPos = vec4(worldPos, 1.0);
#ifdef SHADOW
// Shadow projection: transform from world space to shadow space
for (int i = 0; i < NUMCASCADES; i++)
vShadowPos[i] = GetShadowPos(i, vNormal, projWorldPos);
#endif
#ifdef SPOTLIGHT
// Spotlight projection: transform from world space to projector texture coordinates
vSpotPos = projWorldPos * cLightMatrices[0];
#endif
#ifdef POINTLIGHT
vCubeMaskVec = (worldPos - cLightPos.xyz) * mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz);
#endif
#else
// Ambient & per-vertex lighting
#if defined(LIGHTMAP) || defined(AO)
// If using lightmap, disregard zone ambient light
// If using AO, calculate ambient in the PS
vVertexLight = vec3(0.0, 0.0, 0.0);
vTexCoord2 = iTexCoord1;
#else
vVertexLight = GetAmbient(GetZonePos(worldPos));
#endif
#ifdef NUMVERTEXLIGHTS
for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
vVertexLight += GetVertexLight(i, worldPos, vNormal) * cVertexLights[i * 3].rgb;
#endif
vScreenPos = GetScreenPos(gl_Position);
#ifdef ENVCUBEMAP
vReflectionVec = worldPos - cCameraPos;
#endif
#endif
float posTemperature = GetTemperatureAtHeight(vWorldPos.y);
grassiness = max(cTemperature, -2.0);
grassiness = min(grassiness, 10.0);
grassiness = (grassiness + 2.0) / (10.0);
//grassiness = .5;
#ifndef NOSNOW
snowiness = max(posTemperature, cSnowinessMinTemperature);
snowiness = min(snowiness, cSnowinessMaxTemperature);
snowiness = (cSnowinessMaxTemperature - snowiness) / (cSnowinessMaxTemperature - cSnowinessMinTemperature);
float snow_threshold = min_snow_threshold * snowiness + max_snow_threshold * (1.0 - snowiness);
float upness = min((max(2.0 * snowiness * dot(vNormal, vec3(0.0, 1.0, 0.0)), snow_threshold) - snow_threshold) / (1.0 + snow_saturation - snow_threshold), 1.0);
snow_depth = snowiness * max((upness - 0.5), 0.0) * 2;
#endif
}
void PS()
{
// // Weights
vec3 weights = texture2D(sWeightMap0, vTexCoord).rgb;
float sumWeights = weights.r + weights.g + weights.b;
weights /= sumWeights;
// // Textures
vec4 tGrassMask = texture2D(sDetailMap1, vDetailTexCoord);
vec4 tGroundMask = texture2D(sDetailMap1, vDetailTexCoord * 2);
vec4 tMountain = texture2D(sDetailMap7, vDetailTexCoord) * .35 + texture2D(sDetailMap7, vDetailTexCoord * 5) * .4;
vec4 tGround = texture2D(sDetailMap2, vDetailTexCoord * 10) * .8 * (1.0 - texture2D(sDetailMap2, vDetailTexCoord).r * .3);
vec4 tGrass = texture2D(sDetailMap3, vDetailTexCoord * 10) * .7 * (1.0 - texture2D(sDetailMap3, vDetailTexCoord).r * .3);
//Tilling Mix
#ifndef NOSNOW
if (snow_depth != 1.0)
{
tGround = mix(tGround, texture2D(sDetailMap2, vDetailTexCoord * 12) * .6, tGrassMask.r);
tGround = mix(tGround, texture2D(sDetailMap2, vDetailTexCoord * 14) * .7, tGrassMask.g);
tGrass = mix(tGrass, texture2D(sDetailMap3, vDetailTexCoord * 12) * .6, tGrassMask.r);
tGrass = mix(tGrass, texture2D(sDetailMap3, vDetailTexCoord * 14) * .5, tGrassMask.b);
}
#endif
#ifdef NOSNOW
tGround = mix(tGround, texture2D(sDetailMap2, vDetailTexCoord * 12) * .6, tGrassMask.r);
tGround = mix(tGround, texture2D(sDetailMap2, vDetailTexCoord * 14) * .7, tGrassMask.g);
tGrass = mix(tGrass, texture2D(sDetailMap3, vDetailTexCoord * 12) * .6, tGrassMask.r);
tGrass = mix(tGrass, texture2D(sDetailMap3, vDetailTexCoord * 14) * .5, tGrassMask.b);
#endif
vec4 dtGrass = mix(tGround, tGrass, grassiness);
tGround = mix(tGround * .8, tGround, grassiness);
tGrass = mix(tGrass * .9, tGrass, grassiness);
// Terrain Mix
vec4 groundA = mix(tGrass, dtGrass, smoothstep(.2, .8, max(tGrassMask.r, tGrassMask.b)));
groundA = mix(groundA, tGround, smoothstep(.2, .8, tGrassMask.g));
vec4 groundB = mix(tGround, dtGrass, smoothstep(.3, .7, max(tGroundMask.r, tGroundMask.b)));
groundB = mix(groundB, tGrass, smoothstep(.2, .8, tGrassMask.g));
vec4 diffColor = mix(groundA, groundB, smoothstep(.4, .6, weights.r));
diffColor = mix(diffColor, tMountain, weights.g);
// Get material specular from diffuse
vec3 specColor = cMatSpecColor.rgb * smoothstep(.2, .3, diffColor.g) * .1;
// //Add Snow
#ifndef NOSNOW
vec4 tSnowOverlay = texture2D(sDetailMap4, vDetailTexCoord) * (1.0 - texture2D(sDetailMap4, vDetailTexCoord * 5)) * mix(.0, .5, snowiness);
vec4 tSnow = (texture2D(sSnowMap6, vTexCoord) * .225 + texture2D(sSnowMap6, vTexCoord * 2) * .2);
if (snowiness != .0 && snow_depth != 1.0)
diffColor = diffColor * (1.0 - tSnowOverlay * (1.0 - weights.g * .5) * (1.0 - weights.r * .8)) +
tSnowOverlay * (1.0 - weights.g) * (1.0 - weights.r * .6);
if (snow_depth != .0)
diffColor = diffColor * (1.0 - snow_depth) + tSnow * snow_depth;
#endif
// // Fresnel Borders
float fresnel = pow(1.0 - clamp(dot(normalize(vEyeVec.xyz), vNormal), 0.0, 1.0), 4.0);
diffColor = diffColor * mix(1.1, 1.2, smoothstep(.5, 1.0, fresnel));
// Get normal
#ifdef APPROXIMATENORMAL
vec3 normal = vNormal;
#else
vec3 normal = normalize(vNormal);
#endif
// Get fog factor
#ifdef HEIGHTFOG
float fogFactor = GetHeightFogFactor(vWorldPos.w, vWorldPos.y);
#else
float fogFactor = GetFogFactor(vWorldPos.w);
#endif
#if defined(PERPIXEL)
// Per-pixel forward lighting
vec3 lightColor;
vec3 lightDir;
vec3 finalColor;
float diff = GetDiffuse(normal, vWorldPos.xyz, lightDir);
#ifdef SHADOW
diff *= GetShadow(vShadowPos, vWorldPos.w);
#endif
#if defined(SPOTLIGHT)
lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
#elif defined(CUBEMASK)
lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
#else
lightColor = cLightColor.rgb;
#endif
#ifdef SPECULAR
float spec = GetSpecular(normal, cCameraPosPS - vWorldPos.xyz, lightDir, cMatSpecColor.a);
finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
#else
finalColor = diff * lightColor * diffColor.rgb;
#endif
#ifdef AMBIENT
finalColor += cAmbientColor.rgb * diffColor.rgb * ambientMultiplier;
finalColor += cMatEmissiveColor;
gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
#else
gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
#endif
#elif defined(PREPASS)
// Fill light pre-pass G-Buffer
float specPower = cMatSpecColor.a / 255.0;
gl_FragData[0] = vec4(normal * 0.5 + 0.5, specPower);
gl_FragData[1] = vec4(EncodeDepth(vWorldPos.w), 0.0);
#elif defined(DEFERRED)
// Fill deferred G-buffer
float specIntensity = specColor.g;
float specPower = cMatSpecColor.a / 255.0;
gl_FragData[0] = vec4(GetFog(vVertexLight * diffColor.rgb, fogFactor), 1.0);
gl_FragData[1] = fogFactor * vec4(diffColor.rgb, specIntensity);
gl_FragData[2] = vec4(normal * 0.5 + 0.5, specPower);
gl_FragData[3] = vec4(EncodeDepth(vWorldPos.w), 0.0);
#else
// Ambient & per-vertex lighting
vec3 finalColor = vVertexLight * diffColor.rgb * ambientMultiplier;
#ifdef MATERIAL
// Add light pre-pass accumulation result
// Lights are accumulated at half intensity. Bring back to full intensity now
vec4 lightInput = 2.0 * texture2DProj(sLightBuffer, vScreenPos);
vec3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
#endif
gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
#endif
float selected = texture2D(sHighlightMap5, vTexCoord).r;
gl_FragColor = gl_FragColor * (1.0 - selected / 2.0) + cHighlightColor * selected / 2.0;
}