2011-09-05 15:02:06 +00:00
|
|
|
// -*-C++-*-
|
2011-12-01 20:58:14 +00:00
|
|
|
// Texture switching based on face slope and snow level
|
|
|
|
// based on earlier work by Frederic Bouvier, Tim Moore, and Yves Sablonier.
|
2012-07-02 20:12:24 +00:00
|
|
|
// © Emilian Huminiuc 2011
|
2011-10-30 09:07:03 +00:00
|
|
|
|
2012-07-02 20:12:24 +00:00
|
|
|
#version 120
|
|
|
|
|
2013-01-17 09:51:23 +00:00
|
|
|
varying float RawPosZ;
|
|
|
|
varying vec3 WorldPos;
|
2012-05-17 17:10:30 +00:00
|
|
|
varying vec3 normal;
|
|
|
|
varying vec3 Vnormal;
|
|
|
|
|
|
|
|
uniform float SnowLevel;
|
|
|
|
uniform float Transitions;
|
|
|
|
uniform float InverseSlope;
|
|
|
|
uniform float RainNorm;
|
|
|
|
|
2012-07-02 20:12:24 +00:00
|
|
|
uniform float CloudCover0;
|
|
|
|
uniform float CloudCover1;
|
|
|
|
uniform float CloudCover2;
|
|
|
|
uniform float CloudCover3;
|
|
|
|
uniform float CloudCover4;
|
2012-05-17 17:10:30 +00:00
|
|
|
|
|
|
|
uniform sampler2D BaseTex;
|
|
|
|
uniform sampler2D SecondTex;
|
|
|
|
uniform sampler2D ThirdTex;
|
|
|
|
uniform sampler2D SnowTex;
|
2011-09-05 15:02:06 +00:00
|
|
|
|
|
|
|
uniform sampler3D NoiseTex;
|
|
|
|
|
2011-12-01 20:58:14 +00:00
|
|
|
////fog "include" /////
|
|
|
|
uniform int fogType;
|
|
|
|
|
|
|
|
vec3 fog_Func(vec3 color, int type);
|
|
|
|
//////////////////////
|
|
|
|
|
2020-11-10 00:45:54 +00:00
|
|
|
float getShadowing();
|
|
|
|
|
2011-09-05 15:02:06 +00:00
|
|
|
void main()
|
2011-12-01 20:58:14 +00:00
|
|
|
{
|
2012-07-02 20:34:00 +00:00
|
|
|
float pf = 0.0;
|
|
|
|
|
|
|
|
vec3 lightDir = gl_LightSource[0].position.xyz;
|
|
|
|
vec3 halfVector = gl_LightSource[0].halfVector.xyz;
|
|
|
|
|
|
|
|
vec4 texel = vec4(0.0);
|
|
|
|
vec4 specular = vec4(0.0);
|
|
|
|
|
|
|
|
float cover = min(min(min(min(CloudCover0, CloudCover1),CloudCover2),CloudCover3),CloudCover4);
|
|
|
|
|
2013-01-17 09:51:23 +00:00
|
|
|
vec4 Noise = texture3D(NoiseTex, WorldPos.xyz*0.0011);
|
|
|
|
vec4 Noise2 = texture3D(NoiseTex, WorldPos.xyz * 0.00008);
|
|
|
|
float MixFactor = Noise.r * Noise.g * Noise.b; //Mixing Factor to create a more organic looking boundary
|
|
|
|
float MixFactor2 = Noise2.r * Noise2.g * Noise2.b;
|
|
|
|
MixFactor *= 300.0;
|
|
|
|
MixFactor2 *= 300.0;
|
|
|
|
MixFactor = clamp(MixFactor, 0.0, 1.0);
|
|
|
|
MixFactor2 = clamp(MixFactor2, 0.0, 1.0);
|
|
|
|
float L1 = 0.90 - 0.02 * MixFactor; //first transition slope
|
|
|
|
float L2 = 0.78 + 0.04 * MixFactor; //Second transition slope
|
2011-09-05 15:02:06 +00:00
|
|
|
|
|
|
|
// If gl_Color.a == 0, this is a back-facing polygon and the
|
|
|
|
// Vnormal should be reversed.
|
2012-07-08 12:53:47 +00:00
|
|
|
// vec3 n = (2.0 * gl_Color.a - 1.0) * Vnormal;
|
|
|
|
vec3 n = normalize(Vnormal);
|
2011-09-05 15:02:06 +00:00
|
|
|
|
2012-07-02 20:34:00 +00:00
|
|
|
float nDotVP = max(0.0, dot(n, normalize(gl_LightSource[0].position.xyz)));
|
|
|
|
float nDotHV = max(0.0, dot(n, normalize(gl_LightSource[0].halfVector.xyz)));
|
2020-11-10 00:45:54 +00:00
|
|
|
vec4 Diffuse = gl_LightSource[0].diffuse * nDotVP * getShadowing();
|
2012-06-28 11:37:45 +00:00
|
|
|
|
2012-07-02 20:34:00 +00:00
|
|
|
if (nDotVP > 0.0)
|
2012-06-28 11:37:45 +00:00
|
|
|
pf = pow(nDotHV, gl_FrontMaterial.shininess);
|
|
|
|
|
|
|
|
if (gl_FrontMaterial.shininess > 0.0)
|
2013-01-17 09:51:23 +00:00
|
|
|
specular = gl_FrontMaterial.specular * gl_LightSource[0].diffuse * pf;
|
2012-06-28 11:37:45 +00:00
|
|
|
|
2012-08-13 13:44:41 +00:00
|
|
|
// vec4 diffuseColor = gl_FrontMaterial.emission +
|
|
|
|
// vec4(1.0) * (gl_LightModel.ambient + gl_LightSource[0].ambient) +
|
|
|
|
// Diffuse * gl_FrontMaterial.diffuse;
|
|
|
|
vec4 ambientColor = gl_LightModel.ambient + gl_LightSource[0].ambient;
|
2012-08-13 22:16:27 +00:00
|
|
|
//vec4 diffuseColor = gl_Color + Diffuse * gl_FrontMaterial.diffuse + ambientColor;
|
|
|
|
vec4 diffuseColor = vec4(Diffuse) + ambientColor; //ATI workaround
|
2012-07-02 20:34:00 +00:00
|
|
|
diffuseColor += specular * gl_FrontMaterial.specular;
|
2012-06-28 11:37:45 +00:00
|
|
|
|
2011-09-05 15:02:06 +00:00
|
|
|
// This shouldn't be necessary, but our lighting becomes very
|
|
|
|
// saturated. Clamping the color before modulating by the texture
|
|
|
|
// is closer to what the OpenGL fixed function pipeline does.
|
2012-07-02 20:34:00 +00:00
|
|
|
diffuseColor = clamp(diffuseColor, 0.0, 1.0);
|
2011-09-05 15:02:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
//Select texture based on slope
|
2012-07-02 20:34:00 +00:00
|
|
|
float slope = normalize(normal).z;
|
2011-09-05 15:02:06 +00:00
|
|
|
|
2011-10-30 09:07:03 +00:00
|
|
|
//pull the texture fetch outside flow control to fix aliasing artefacts :(
|
2012-07-02 20:34:00 +00:00
|
|
|
vec4 baseTexel = texture2D(BaseTex, gl_TexCoord[0].st);
|
|
|
|
vec4 secondTexel = texture2D(SecondTex, gl_TexCoord[0].st);
|
|
|
|
vec4 thirdTexel = texture2D(ThirdTex, gl_TexCoord[0].st);
|
|
|
|
vec4 snowTexel = texture2D(SnowTex, gl_TexCoord[0].st);
|
2012-05-17 17:10:30 +00:00
|
|
|
|
2011-10-30 09:07:03 +00:00
|
|
|
//Normal transition. For more abrupt faces apply another texture (or 2).
|
|
|
|
if (InverseSlope == 0.0) {
|
|
|
|
//Do we do an intermediate transition
|
|
|
|
if (Transitions >= 1.5) {
|
|
|
|
if (slope >= L1) {
|
|
|
|
texel = baseTexel;
|
2011-12-01 20:58:14 +00:00
|
|
|
}
|
2011-10-30 09:07:03 +00:00
|
|
|
if (slope >= L2 && slope < L1){
|
|
|
|
texel = mix(secondTexel, baseTexel, smoothstep(L2, L1 - 0.06 * MixFactor, slope));
|
2011-12-01 20:58:14 +00:00
|
|
|
}
|
2011-10-30 09:07:03 +00:00
|
|
|
if (slope < L2){
|
|
|
|
texel = mix(thirdTexel, secondTexel, smoothstep(L2 - 0.13 * MixFactor, L2, slope));
|
2011-12-01 20:58:14 +00:00
|
|
|
}
|
2011-10-30 09:07:03 +00:00
|
|
|
// Just one transition
|
2011-12-01 20:58:14 +00:00
|
|
|
} else if (Transitions < 1.5) {
|
|
|
|
if (slope >= L1) {
|
|
|
|
texel = baseTexel;
|
|
|
|
}
|
|
|
|
if (slope < L1) {
|
|
|
|
texel = mix(thirdTexel, baseTexel, smoothstep(L2 - 0.13 * MixFactor, L1, slope));
|
|
|
|
}
|
2011-10-30 09:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Invert the transition: keep original texture on abrupt slopes and switch to another on flatter terrain
|
2011-12-01 20:58:14 +00:00
|
|
|
} else if (InverseSlope > 0.0) {
|
|
|
|
//Interemdiate transition ?
|
|
|
|
if (Transitions >= 1.5) {
|
|
|
|
if (slope >= L1 + 0.1) {
|
|
|
|
texel = thirdTexel;
|
|
|
|
}
|
|
|
|
if (slope >= L2 && slope < L1 + 0.1){
|
|
|
|
texel = mix(secondTexel, thirdTexel, smoothstep(L2 + 0.06 * MixFactor, L1 + 0.1, slope));
|
|
|
|
}
|
|
|
|
if (slope <= L2){
|
|
|
|
texel = mix(baseTexel, secondTexel, smoothstep(L2 - 0.06 * MixFactor, L2, slope));
|
|
|
|
}
|
|
|
|
//just one
|
|
|
|
} else if (Transitions < 1.5) {
|
|
|
|
if (slope > L1 + 0.1) {
|
|
|
|
texel = thirdTexel;
|
|
|
|
}
|
|
|
|
if (slope <= L1 + 0.1){
|
|
|
|
texel = mix(baseTexel, thirdTexel, smoothstep(L2 - 0.06 * MixFactor, L1 + 0.1, slope));
|
|
|
|
}
|
|
|
|
}
|
2011-10-30 09:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//darken textures with wetness
|
2012-07-02 20:34:00 +00:00
|
|
|
float wetness = 1.0 - 0.3 * RainNorm;
|
2011-10-30 09:07:03 +00:00
|
|
|
texel.rgb = texel.rgb * wetness;
|
|
|
|
|
2013-01-17 09:51:23 +00:00
|
|
|
float altitude = RawPosZ;
|
2011-10-30 09:07:03 +00:00
|
|
|
//Snow texture for areas higher than SnowLevel
|
2011-12-13 11:21:59 +00:00
|
|
|
if (altitude >= SnowLevel - (1000.0 * slope + 300.0 * MixFactor) && slope > L2 - 0.12) {
|
2012-05-17 17:10:30 +00:00
|
|
|
texel = mix(texel, mix(texel, snowTexel, smoothstep(L2 - 0.09 * MixFactor, L2, slope)),
|
|
|
|
smoothstep(SnowLevel - (1000.0 * slope + 300.0 * MixFactor),
|
|
|
|
SnowLevel - (1000.0 * slope - 150.0 * MixFactor),
|
|
|
|
altitude)
|
|
|
|
);
|
2011-12-01 20:58:14 +00:00
|
|
|
}
|
2011-10-30 09:07:03 +00:00
|
|
|
|
2012-07-02 20:34:00 +00:00
|
|
|
vec4 fragColor = diffuseColor * texel + specular;
|
2011-10-30 09:07:03 +00:00
|
|
|
|
|
|
|
if(cover >= 2.5){
|
|
|
|
fragColor.rgb = fragColor.rgb * 1.2;
|
2011-12-01 20:58:14 +00:00
|
|
|
} else {
|
|
|
|
fragColor.rg = fragColor.rg * (0.6 + 0.2 * cover);
|
|
|
|
fragColor.b = fragColor.b * (0.5 + 0.25 * cover);
|
|
|
|
}
|
2011-09-22 13:37:32 +00:00
|
|
|
|
2013-01-17 09:51:23 +00:00
|
|
|
fragColor.rgb *= 1.2 - 0.6 * MixFactor * MixFactor2;
|
2011-12-01 20:58:14 +00:00
|
|
|
fragColor.rgb = fog_Func(fragColor.rgb, fogType);
|
|
|
|
gl_FragColor = fragColor;
|
2012-05-17 17:10:30 +00:00
|
|
|
}
|