efl/src/modules/evas/engines/gl_common/shader_3d/parallax_occlusion_frag.shd

241 lines
6.4 KiB
Plaintext

varying vec3 vLightVector;
varying vec3 vLightHalfVector;
varying vec3 vEyeVector;
uniform sampler2D uTextureNormal0;
uniform mat3 uTextureMatrixTransformNormal0;
#ifdef NEED_TEX_COORD
varying vec2 vTexCoord;
vec3 Tex0CoordNormal;
#endif //TEX_COORD
#ifdef NORMAL_TEXTURE_BLEND
uniform sampler2D uTextureNormal1;
uniform float uTextureNormalWeight;
uniform mat3 uTextureMatrixTransformNormal1;
vec3 Tex1CoordNormal;
#endif //NORMAL_TEXTURE_BLEND
FRAGMENT_SHADER_USE_FOG
FRAGMENT_SHADER_USE_SHADOWS
FRAGMENT_SHADER_USE_DIFFUSE_TERM
FRAGMENT_SHADER_USE_SPECULAR_TERM
FRAGMENT_SHADER_USE_AMBIENT_TERM
FRAGMENT_SHADER_USE_EMISSION_TERM
FRAGMENT_SHADER_USE_LIGHT
FRAGMENT_SHADER_USE_ALPHA_TEST_GLES
const float parallaxScale = 0.2;
vec2 parallaxMapping(in vec3 view, in vec2 tex, out float parallaxHeight)
{
const float numLayers = 15.0;
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, vec2(Tex0CoordNormal)).a) * uTextureNormalWeight;
heightFromTexture += (1.0 - texture2D(uTextureNormal1, vec2(Tex1CoordNormal)).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;
}
vec4 fragmentParallaxMap()
{
float factor;
vec3 normal;
vec4 color;
vec4 fragcolor;
float parallaxHeight;
vec2 tex = parallaxMapping(vEyeVector, vec2(Tex0CoordNormal), 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
fragcolor = uLightDiffuse * color * factor;
#else
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
fragcolor += uLightSpecular * color * factor;
}
#endif //SPECULAR
#ifdef SHADOWED
fragcolor *= shadow;
#endif //SHADOWED
}
else
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
fragcolor += uLightAmbient * color;
#endif //AMBIENT
#ifdef LIGHT_ATTENUATION
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
fragcolor += color;
#endif //EMISSION
return fragcolor;
}
void main() {
vec4 color;
#ifdef NEED_TEX_COORD
Tex0CoordNormal = vec3(vTexCoord, 1.0) * uTextureMatrixTransformNormal0;
#endif // NEED_TEX_COORD
#ifdef NORMAL_TEXTURE_BLEND
Tex1CoordNormal = vec3(vTexCoord, 1.0) * uTextureMatrixTransformNormal1;
#endif //NORMAL_TEXTURE_BLEND
#ifdef SHADOWED
shadow = pcf(vLightPosition);
#endif //SHADOWED
color = fragmentParallaxMap();
#ifdef ALPHA_TEST_ENABLED
FRAGMENT_SHADER_ALPHA_TEST_GLES_APPLY(color)
#else
gl_FragColor = color;
#endif //ALPHA_TEST_ENABLED
FRAGMENT_SHADER_FOG_APPLY
}