314 lines
8.1 KiB
Plaintext
314 lines
8.1 KiB
Plaintext
varying vec3 vLightVector;
|
|
varying vec3 vLightHalfVector;
|
|
uniform sampler2D uTextureNormal0;
|
|
varying vec3 vEyeVector;
|
|
|
|
#ifdef NEED_TEX_COORD
|
|
varying vec2 vTexCoord;
|
|
#endif //TEX_COORD
|
|
|
|
#ifdef FOG_ENABLED
|
|
uniform float uFogFactor;
|
|
uniform vec4 uFogColor;
|
|
#endif //FOG_ENABLED
|
|
|
|
#ifdef SHADOWED
|
|
varying vec4 vLightPosition;
|
|
uniform sampler2D uShadowMap;
|
|
float shadow;
|
|
#endif //SHADOWED
|
|
|
|
#ifdef NORMAL_TEXTURE_BLEND
|
|
uniform sampler2D uTextureNormal1;
|
|
uniform float uTextureNormalWeight;
|
|
#endif //NORMAL_TEXTURE_BLEND
|
|
|
|
#ifdef DIFFUSE
|
|
uniform vec4 uMaterialDiffuse;
|
|
uniform vec4 uLightDiffuse;
|
|
|
|
#ifdef DIFFUSE_TEXTURE
|
|
uniform sampler2D uTextureDiffuse0;
|
|
#endif //DIFFUSE_TEXTURE
|
|
|
|
#ifdef DIFFUSE_TEXTURE_BLEND
|
|
uniform sampler2D uTextureDiffuse1;
|
|
uniform float uTextureDiffuseWeight;
|
|
#endif //DIFFUSE_TEXTURE_BLEND
|
|
|
|
#endif //DIFFUSE
|
|
|
|
#ifdef SPECULAR
|
|
uniform vec4 uLightSpecular;
|
|
uniform float uMaterialShininess;
|
|
uniform vec4 uMaterialSpecular;
|
|
|
|
#ifdef SPECULAR_TEXTURE
|
|
uniform sampler2D uTextureSpecular0;
|
|
#endif //SPECULAR_TEXTURE
|
|
|
|
#ifdef SPECULAR_TEXTURE_BLEND
|
|
uniform sampler2D uTextureSpecular1;
|
|
uniform float uTextureSpecularWeight;
|
|
#endif //SPECULAR_TEXTURE_BLEND
|
|
|
|
#endif //SPECULAR
|
|
|
|
#ifdef AMBIENT
|
|
uniform vec4 uMaterialAmbient;
|
|
uniform vec4 uLightAmbient;
|
|
|
|
#ifdef AMBIENT_TEXTURE
|
|
uniform sampler2D uTextureAmbient0;
|
|
#endif //AMBIENT_TEXTURE
|
|
|
|
#ifdef AMBIENT_TEXTURE_BLEND
|
|
uniform sampler2D uTextureAmbient1;
|
|
uniform float uTextureAmbientWeight;
|
|
#endif //AMBIENT_TEXTURE_BLEND
|
|
|
|
#endif //AMBIENT
|
|
|
|
#ifdef EMISSION
|
|
uniform vec4 uMaterialEmission;
|
|
|
|
#ifdef EMISSION_TEXTURE
|
|
uniform sampler2D uTextureEmission0;
|
|
#endif //EMISSION_TEXTURE
|
|
|
|
#ifdef EMISSION_TEXTURE_BLEND
|
|
uniform sampler2D uTextureEmission1;
|
|
uniform float uTextureEmissionWeight;
|
|
#endif //EMISSION_TEXTURE_BLEND
|
|
|
|
#endif //EMISSION
|
|
|
|
#ifdef LIGHT_SPOT
|
|
uniform vec3 uLightSpotDir;
|
|
uniform float uLightSpotExp;
|
|
uniform float uLightSpotCutoffCos;
|
|
#endif //LIGHT_SPOT
|
|
|
|
#ifdef LIGHT_ATTENUATION
|
|
varying float vLightDist;
|
|
#endif //LIGHT_ATTENUATION
|
|
|
|
const float parallaxScale = 0.2;
|
|
|
|
vec2 parallaxMapping(in vec3 view, in vec2 tex, out float parallaxHeight)
|
|
{
|
|
const float minLayers = 10.0;
|
|
const float maxLayers = 30.0;
|
|
float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0, 0, 1), view)));
|
|
vec2 texStep = parallaxScale * view.xy / view.z / numLayers;
|
|
|
|
float layerHeight = 1.0 / numLayers;
|
|
float curLayerHeight = 0.0;
|
|
vec2 dtex = parallaxScale * view.xy / view.z / numLayers;
|
|
|
|
vec2 currentTextureCoords = tex;
|
|
|
|
#ifdef NORMAL_TEXTURE_BLEND
|
|
float heightFromTexture = (1.0 - texture2D(uTextureNormal0, currentTextureCoords).a) * uTextureNormalWeight;
|
|
heightFromTexture += (1.0 - texture2D(uTextureNormal1, currentTextureCoords).a) *
|
|
(1.0 - uTextureNormalWeight);
|
|
#else
|
|
float heightFromTexture = 1.0 - texture2D(uTextureNormal0, currentTextureCoords).a;
|
|
#endif //NORMAL_TEXTURE_BLEND
|
|
|
|
while(heightFromTexture > curLayerHeight)
|
|
{
|
|
curLayerHeight += layerHeight;
|
|
currentTextureCoords -= dtex;
|
|
|
|
#ifdef NORMAL_TEXTURE_BLEND
|
|
heightFromTexture = (1.0 - texture2D(uTextureNormal0, currentTextureCoords).a) * uTextureNormalWeight;
|
|
heightFromTexture += (1.0 - texture2D(uTextureNormal1, currentTextureCoords).a) *
|
|
(1.0 - uTextureNormalWeight);
|
|
#else
|
|
heightFromTexture = 1.0 - texture2D(uTextureNormal0, currentTextureCoords).a;
|
|
#endif //NORMAL_TEXTURE_BLEND
|
|
}
|
|
|
|
vec2 prevTCoords = currentTextureCoords + texStep;
|
|
float nextH = heightFromTexture - curLayerHeight;
|
|
|
|
#ifdef NORMAL_TEXTURE_BLEND
|
|
float prevH = (1.0 - texture2D(uTextureNormal0, prevTCoords).a
|
|
- curLayerHeight + layerHeight) * uTextureNormalWeight;
|
|
prevH += (1.0 - texture2D(uTextureNormal1, prevTCoords).a
|
|
- curLayerHeight + layerHeight) * (1.0 - uTextureNormalWeight);
|
|
#else
|
|
float prevH = 1.0 - texture2D(uTextureNormal0, prevTCoords).a
|
|
- curLayerHeight + layerHeight;
|
|
#endif //NORMAL_TEXTURE_BLEND
|
|
|
|
float weight = nextH / (nextH - prevH);
|
|
vec2 finalTexCoords = prevTCoords * weight + currentTextureCoords * (1.0-weight);
|
|
parallaxHeight = curLayerHeight + prevH * weight + nextH * (1.0 - weight);
|
|
|
|
return finalTexCoords;
|
|
}
|
|
|
|
void fragmentParallaxMap()
|
|
{
|
|
float factor;
|
|
vec3 normal;
|
|
vec4 color;
|
|
|
|
float parallaxHeight;
|
|
vec2 tex = parallaxMapping(vEyeVector, vTexCoord, parallaxHeight);
|
|
|
|
vec3 lv = normalize(vLightVector);
|
|
|
|
#ifdef NORMAL_TEXTURE_BLEND
|
|
normal = texture2D(uTextureNormal0, tex).rgb * uTextureNormalWeight / texture2D(uTextureNormal0, tex).a;
|
|
normal += texture2D(uTextureNormal1, tex).rgb *
|
|
(1.0 - uTextureNormalWeight) / texture2D(uTextureNormal1, tex).a;
|
|
#else
|
|
normal = texture2D(uTextureNormal0, tex).rgb / texture2D(uTextureNormal0, tex).a;
|
|
#endif //NORMAL_TEXTURE_BLEND
|
|
|
|
normal = 2.0 * normal - 1.0;
|
|
|
|
normal = normalize(normal);
|
|
|
|
factor = dot(lv, normal);
|
|
|
|
#ifdef LIGHT_SPOT
|
|
float f = dot(-lv, normalize(uLightSpotDir));
|
|
|
|
if (f > uLightSpotCutoffCos)
|
|
factor *= pow(f, uLightSpotExp);
|
|
else
|
|
factor = 0.0;
|
|
#endif //LIGHT_SPOT
|
|
|
|
if (factor > 0.0)
|
|
{
|
|
|
|
#ifdef DIFFUSE
|
|
|
|
#ifdef DIFFUSE_TEXTURE_BLEND
|
|
color = texture2D(uTextureDiffuse0, tex) * uTextureDiffuseWeight +
|
|
texture2D(uTextureDiffuse1, tex) * (1.0 - uTextureDiffuseWeight);
|
|
#else
|
|
|
|
#ifdef DIFFUSE_TEXTURE
|
|
color = texture2D(uTextureDiffuse0, tex);
|
|
#else
|
|
color = uMaterialDiffuse;
|
|
#endif //DIFFUSE_TEXTURE
|
|
|
|
#endif //DIFFUSE_TEXTURE_BLEND
|
|
|
|
gl_FragColor = uLightDiffuse * color * factor;
|
|
|
|
#else
|
|
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
|
|
#endif //DIFFUSE
|
|
|
|
#ifdef SPECULAR
|
|
|
|
factor = dot(normalize(vLightHalfVector), normal);
|
|
if (factor > 0.0)
|
|
{
|
|
factor = pow(factor, uMaterialShininess);
|
|
|
|
#ifdef SPECULAR_TEXTURE_BLEND
|
|
color = texture2D(uTextureSpecular0, tex) * uTextureSpecularWeight +
|
|
texture2D(uTextureSpecular1, tex) * (1.0 - uTextureSpecularWeight);
|
|
#else
|
|
#ifdef SPECULAR_TEXTURE
|
|
color = texture2D(uTextureSpecular0, tex);
|
|
#else
|
|
color = uMaterialSpecular;
|
|
#endif //SPECULAR_TEXTURE
|
|
|
|
#endif //SPECULAR_TEXTURE_BLEND
|
|
|
|
gl_FragColor += uLightSpecular * color * factor;
|
|
}
|
|
|
|
#endif //SPECULAR
|
|
|
|
#ifdef SHADOWED
|
|
gl_FragColor *= shadow;
|
|
#endif //SHADOWED
|
|
|
|
}
|
|
else
|
|
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
|
|
|
|
#ifdef AMBIENT
|
|
#ifdef AMBIENT_TEXTURE_BLEND
|
|
color = texture2D(uTextureAmbient0, tex) * uTextureAmbientWeight +
|
|
texture2D(uTextureAmbient1, tex) * (1.0 - uTextureAmbientWeight);
|
|
#else
|
|
|
|
#ifdef AMBIENT_TEXTURE
|
|
color = texture2D(uTextureAmbient0, tex);
|
|
#else
|
|
color = uMaterialAmbient;
|
|
#endif //AMBIENT_TEXTURE
|
|
|
|
#endif //AMBIENT_TEXTURE_BLEND
|
|
|
|
gl_FragColor += uLightAmbient * color;
|
|
#endif //AMBIENT
|
|
|
|
#ifdef LIGHT_ATTENUATION
|
|
gl_FragColor /= dot(uLightAtten, vec3(1.0, vLightDist, vLightDist * vLightDist));
|
|
#endif //LIGHT_ATTENUATION
|
|
|
|
#ifdef EMISSION
|
|
|
|
#ifdef EMISSION_TEXTURE_BLEND
|
|
color = texture2D(uTextureEmission0, tex) * uTextureEmissionWeight +
|
|
texture2D(uTextureEmission1, tex) * (1.0 - uTextureEmissionWeight);
|
|
#else
|
|
|
|
#ifdef EMISSION_TEXTURE
|
|
color = texture2D(uTextureEmission0, tex);
|
|
#else
|
|
color = uMaterialEmission;
|
|
#endif //EMISSION_TEXTURE
|
|
|
|
#endif //EMISSION_TEXTURE_BLEND
|
|
|
|
gl_FragColor += color;
|
|
#endif //EMISSION
|
|
|
|
}
|
|
|
|
#ifdef SHADOWED
|
|
float pcf(vec4 lpos, float size)
|
|
{
|
|
vec3 smcoord = lpos.xyz / lpos.w * 0.5 + 0.5;
|
|
float i, j, randx, randy, shadow;
|
|
shadow = 0.0;
|
|
for (i = -4.0; i < 4.0; i++)
|
|
for (j = -4.0; j < 4.0; j++)
|
|
shadow += float(smcoord.z <= texture2D(uShadowMap, smcoord.xy +vec2(i / 8.0, j / 8.0)*size).x);
|
|
return shadow / 64.0;
|
|
}
|
|
#endif //SHADOWED
|
|
|
|
void main() {
|
|
|
|
#ifdef SHADOWED
|
|
shadow = pcf(vLightPosition, 1.0 / 200.0);
|
|
#endif //SHADOWED
|
|
|
|
fragmentParallaxMap();
|
|
|
|
#ifdef FOG_ENABLED
|
|
float z = gl_FragCoord.z / gl_FragCoord.w;
|
|
float fogFactor = exp2( -uFogFactor * uFogFactor * z * z * 1.44);
|
|
fogFactor = clamp(fogFactor, 0.0, 1.0);
|
|
gl_FragColor = mix(uFogColor, gl_FragColor, fogFactor);
|
|
#endif //FOG_ENABLED
|
|
|
|
}
|
|
|