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 float intensity(vec3 color)
79 return (color.r + color.g + color.b) / 3.0;
82 float get_rgb_height(vec2 uv)
85 return intensity(texture2D(baseTexture, uv).rgb);
87 return intensity(texture2D(baseTexture, clamp(uv, 0.0, 0.999)).rgb);
91 vec4 get_normal_map(vec2 uv)
93 vec4 bump = texture2D(normalTexture, uv).rgba;
94 bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
102 vec2 uv = gl_TexCoord[0].st;
103 bool use_normalmap = false;
106 #if USE_NORMALMAPS == 1
107 if (normalTexturePresent) {
108 bump = get_normal_map(uv);
109 use_normalmap = true;
113 #if GENERATE_NORMALMAPS == 1
114 if (normalTexturePresent == false) {
115 float tl = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y + SAMPLE_STEP));
116 float t = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y - SAMPLE_STEP));
117 float tr = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y + SAMPLE_STEP));
118 float r = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y));
119 float br = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y - SAMPLE_STEP));
120 float b = get_rgb_height(vec2(uv.x, uv.y - SAMPLE_STEP));
121 float bl = get_rgb_height(vec2(uv.x -SAMPLE_STEP, uv.y - SAMPLE_STEP));
122 float l = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y));
123 float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
124 float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
125 bump = vec4(normalize(vec3 (dX, dY, NORMALMAPS_STRENGTH)), 1.0);
126 use_normalmap = true;
130 vec4 base = texture2D(baseTexture, uv).rgba;
132 #ifdef ENABLE_BUMPMAPPING
134 vec3 L = normalize(lightVec);
135 vec3 E = normalize(eyeVec);
136 float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
137 float diffuse = dot(-E,bump.xyz);
138 color = (diffuse + 0.1 * specular) * base.rgb;
146 vec4 col = vec4(color.rgb, base.a);
148 col.rgb *= gl_Color.rgb;
150 col.rgb *= emissiveColor.rgb * vIDiff;
152 #ifdef ENABLE_TONE_MAPPING
153 col = applyToneMapping(col);
156 // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
157 // the fog will only be rendered correctly if the last operation before the
158 // clamp() is an addition. Else, the clamp() seems to be ignored.
159 // E.g. the following won't work:
160 // float clarity = clamp(fogShadingParameter
161 // * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
162 // As additions usually come for free following a multiplication, the new formula
163 // should be more efficient as well.
164 // Note: clarity = (1 - fogginess)
165 float clarity = clamp(fogShadingParameter
166 - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
167 col = mix(skyBgColor, col, clarity);
169 gl_FragColor = vec4(col.rgb, base.a);