1 uniform sampler2D baseTexture;
2 uniform sampler2D normalTexture;
3 uniform sampler2D textureFlags;
5 uniform vec4 emissiveColor;
6 uniform vec4 skyBgColor;
7 uniform float fogDistance;
8 uniform vec3 eyePosition;
11 varying vec3 vPosition;
12 varying vec3 worldPosition;
15 varying vec3 lightVec;
18 bool normalTexturePresent = false;
19 bool texTileableHorizontal = false;
20 bool texTileableVertical = false;
21 bool texSeamless = false;
23 const float e = 2.718281828459;
24 const float BS = 10.0;
25 const float fogStart = FOG_START;
26 const float fogShadingParameter = 1 / ( 1 - fogStart);
28 #ifdef ENABLE_TONE_MAPPING
30 /* Hable's UC2 Tone mapping parameters
38 equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
41 vec3 uncharted2Tonemap(vec3 x)
43 return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
46 vec4 applyToneMapping(vec4 color)
48 color = vec4(pow(color.rgb, vec3(2.2)), color.a);
49 const float gamma = 1.6;
50 const float exposureBias = 5.5;
51 color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
52 // Precalculated white_scale from
53 //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
54 vec3 whiteScale = vec3(1.036015346);
55 color.rgb *= whiteScale;
56 return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
60 void get_texture_flags()
62 vec4 flags = texture2D(textureFlags, vec2(0.0, 0.0));
64 normalTexturePresent = true;
67 texTileableHorizontal = true;
70 texTileableVertical = true;
72 if (texTileableHorizontal && texTileableVertical) {
77 vec4 get_normal_map(vec2 uv)
79 vec4 bump = texture2D(normalTexture, uv).rgba;
80 bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
88 vec2 uv = gl_TexCoord[0].st;
89 bool use_normalmap = false;
92 #if USE_NORMALMAPS == 1
93 if (normalTexturePresent) {
94 bump = get_normal_map(uv);
99 vec4 base = texture2D(baseTexture, uv).rgba;
102 // If alpha is zero, we can just discard the pixel. This fixes transparency
103 // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa.
109 #ifdef ENABLE_BUMPMAPPING
111 vec3 L = normalize(lightVec);
112 vec3 E = normalize(eyeVec);
113 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
114 float diffuse = dot(-E,bump.xyz);
115 color = (diffuse + 0.1 * specular) * base.rgb;
123 vec4 col = vec4(color.rgb, base.a);
125 col.rgb *= gl_Color.rgb;
127 col.rgb *= emissiveColor.rgb * vIDiff;
129 #ifdef ENABLE_TONE_MAPPING
130 col = applyToneMapping(col);
133 // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
134 // the fog will only be rendered correctly if the last operation before the
135 // clamp() is an addition. Else, the clamp() seems to be ignored.
136 // E.g. the following won't work:
137 // float clarity = clamp(fogShadingParameter
138 // * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
139 // As additions usually come for free following a multiplication, the new formula
140 // should be more efficient as well.
141 // Note: clarity = (1 - fogginess)
142 float clarity = clamp(fogShadingParameter
143 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
144 col = mix(skyBgColor, col, clarity);
146 gl_FragColor = vec4(col.rgb, base.a);