| 1 |
#ifdef USE_ARRAY_TEXTURE |
| 2 |
uniform mediump sampler2DArray baseTexture; |
| 3 |
#else |
| 4 |
uniform sampler2D baseTexture; |
| 5 |
#endif |
| 6 |
VARYING_ vec4 tPos; |
| 7 |
|
| 8 |
CENTROID_ VARYING_ mediump vec2 varTexCoord; |
| 9 |
CENTROID_ VARYING_ float varTexLayer; // actually int |
| 10 |
|
| 11 |
#ifdef COLORED_SHADOWS |
| 12 |
VARYING_ vec3 varColor; |
| 13 |
|
| 14 |
// c_precision of 128 fits within 7 base-10 digits |
| 15 |
const float c_precision = 128.0; |
| 16 |
const float c_precisionp1 = c_precision + 1.0; |
| 17 |
|
| 18 |
float packColor(vec3 color) |
| 19 |
{ |
| 20 |
return floor(color.b * c_precision + 0.5) |
| 21 |
+ floor(color.g * c_precision + 0.5) * c_precisionp1 |
| 22 |
+ floor(color.r * c_precision + 0.5) * c_precisionp1 * c_precisionp1; |
| 23 |
} |
| 24 |
#endif |
| 25 |
|
| 26 |
void main() |
| 27 |
{ |
| 28 |
#ifdef USE_ARRAY_TEXTURE |
| 29 |
vec4 base = texture(baseTexture, vec3(varTexCoord, varTexLayer)).rgba; |
| 30 |
#else |
| 31 |
vec4 base = texture2D(baseTexture, varTexCoord).rgba; |
| 32 |
#endif |
| 33 |
#ifndef COLORED_SHADOWS |
| 34 |
if (base.a < 0.5) |
| 35 |
discard; |
| 36 |
#endif |
| 37 |
|
| 38 |
float depth = 0.5 + tPos.z * 0.5; |
| 39 |
// depth in [0, 1] for texture |
| 40 |
|
| 41 |
#ifdef COLORED_SHADOWS |
| 42 |
base.rgb *= varColor.rgb; |
| 43 |
// premultiply color alpha (see-through side) |
| 44 |
float packedColor = packColor(base.rgb * (1.0 - base.a)); |
| 45 |
gl_FragColor = vec4(depth, packedColor, 0.0,1.0); |
| 46 |
#else |
| 47 |
gl_FragColor = vec4(depth, 0.0, 0.0, 1.0); |
| 48 |
#endif |
| 49 |
} |