|
| 1 | +uniform sampler2D baseTexture; |
| 2 | +uniform sampler2D normalTexture; |
| 3 | +uniform sampler2D textureFlags; |
| 4 | + |
| 5 | +uniform vec4 skyBgColor; |
| 6 | +uniform float fogDistance; |
| 7 | +uniform vec3 eyePosition; |
| 8 | + |
| 9 | +varying vec3 vPosition; |
| 10 | +varying vec3 worldPosition; |
| 11 | + |
| 12 | +varying vec3 eyeVec; |
| 13 | +varying vec3 lightVec; |
| 14 | + |
| 15 | +bool normalTexturePresent = false; |
| 16 | +bool texTileableHorizontal = false; |
| 17 | +bool texTileableVertical = false; |
| 18 | +bool texSeamless = false; |
| 19 | + |
| 20 | +const float e = 2.718281828459; |
| 21 | +const float BS = 10.0; |
| 22 | + |
| 23 | +void get_texture_flags() |
| 24 | +{ |
| 25 | + vec4 flags = texture2D(textureFlags, vec2(0.0, 0.0)); |
| 26 | + if (flags.r > 0.5) { |
| 27 | + normalTexturePresent = true; |
| 28 | + } |
| 29 | + if (flags.g > 0.5) { |
| 30 | + texTileableHorizontal = true; |
| 31 | + } |
| 32 | + if (flags.b > 0.5) { |
| 33 | + texTileableVertical = true; |
| 34 | + } |
| 35 | + if (texTileableHorizontal && texTileableVertical) { |
| 36 | + texSeamless = true; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +float intensity(vec3 color) |
| 41 | +{ |
| 42 | + return (color.r + color.g + color.b) / 3.0; |
| 43 | +} |
| 44 | + |
| 45 | +float get_rgb_height(vec2 uv) |
| 46 | +{ |
| 47 | + if (texSeamless) { |
| 48 | + return intensity(texture2D(baseTexture, uv).rgb); |
| 49 | + } else { |
| 50 | + return intensity(texture2D(baseTexture, clamp(uv, 0.0, 0.999)).rgb); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +vec4 get_normal_map(vec2 uv) |
| 55 | +{ |
| 56 | + vec4 bump = texture2D(normalTexture, uv).rgba; |
| 57 | + bump.xyz = normalize(bump.xyz * 2.0 - 1.0); |
| 58 | + return bump; |
| 59 | +} |
| 60 | + |
| 61 | +void main(void) |
| 62 | +{ |
| 63 | + vec3 color; |
| 64 | + vec4 bump; |
| 65 | + vec2 uv = gl_TexCoord[0].st; |
| 66 | + bool use_normalmap = false; |
| 67 | + get_texture_flags(); |
| 68 | + |
| 69 | +#if USE_NORMALMAPS == 1 |
| 70 | + if (normalTexturePresent) { |
| 71 | + bump = get_normal_map(uv); |
| 72 | + use_normalmap = true; |
| 73 | + } |
| 74 | +#endif |
| 75 | + |
| 76 | + if (GENERATE_NORMALMAPS == 1 && normalTexturePresent == false) { |
| 77 | + float tl = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y + SAMPLE_STEP)); |
| 78 | + float t = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y - SAMPLE_STEP)); |
| 79 | + float tr = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y + SAMPLE_STEP)); |
| 80 | + float r = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y)); |
| 81 | + float br = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y - SAMPLE_STEP)); |
| 82 | + float b = get_rgb_height(vec2(uv.x, uv.y - SAMPLE_STEP)); |
| 83 | + float bl = get_rgb_height(vec2(uv.x -SAMPLE_STEP, uv.y - SAMPLE_STEP)); |
| 84 | + float l = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y)); |
| 85 | + float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl); |
| 86 | + float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr); |
| 87 | + bump = vec4(normalize(vec3 (dX, dY, NORMALMAPS_STRENGTH)), 1.0); |
| 88 | + use_normalmap = true; |
| 89 | + } |
| 90 | + |
| 91 | + vec4 base = texture2D(baseTexture, uv).rgba; |
| 92 | + |
| 93 | +#ifdef ENABLE_BUMPMAPPING |
| 94 | + if (use_normalmap) { |
| 95 | + vec3 L = normalize(lightVec); |
| 96 | + vec3 E = normalize(eyeVec); |
| 97 | + float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0); |
| 98 | + float diffuse = dot(-E,bump.xyz); |
| 99 | + color = (diffuse + 0.1 * specular) * base.rgb; |
| 100 | + } else { |
| 101 | + color = base.rgb; |
| 102 | + } |
| 103 | +#else |
| 104 | + color = base.rgb; |
| 105 | +#endif |
| 106 | + |
| 107 | + vec4 col = vec4(color.rgb, base.a); |
| 108 | + col *= gl_Color; |
| 109 | + if (fogDistance != 0.0) { |
| 110 | + float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0)); |
| 111 | + col = mix(col, skyBgColor, d); |
| 112 | + } |
| 113 | + gl_FragColor = vec4(col.rgb, base.a); |
| 114 | +} |
0 commit comments