123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- #include<__decl__defaultFragment>
- #if defined(BUMP) || !defined(NORMAL)
- #extension GL_OES_standard_derivatives : enable
- #endif
- #ifdef LOGARITHMICDEPTH
- #extension GL_EXT_frag_depth : enable
- #endif
- // Constants
- #define RECIPROCAL_PI2 0.15915494
- uniform vec3 vEyePosition;
- uniform vec3 vAmbientColor;
- // Input
- varying vec3 vPositionW;
- #ifdef NORMAL
- varying vec3 vNormalW;
- #endif
- #ifdef VERTEXCOLOR
- varying vec4 vColor;
- #endif
- #ifdef MAINUV1
- varying vec2 vMainUV1;
- #endif
- #ifdef MAINUV2
- varying vec2 vMainUV2;
- #endif
- // Helper functions
- #include<helperFunctions>
- // Lights
- #include<__decl__lightFragment>[0..maxSimultaneousLights]
- #include<lightsFragmentFunctions>
- #include<shadowsFragmentFunctions>
- // Samplers
- #ifdef DIFFUSE
- #if DIFFUSEDIRECTUV == 1
- #define vDiffuseUV vMainUV1
- #elif DIFFUSEDIRECTUV == 2
- #define vDiffuseUV vMainUV2
- #else
- varying vec2 vDiffuseUV;
- #endif
- uniform sampler2D diffuseSampler;
- #endif
- #ifdef AMBIENT
- #if AMBIENTDIRECTUV == 1
- #define vAmbientUV vMainUV1
- #elif AMBIENTDIRECTUV == 2
- #define vAmbientUV vMainUV2
- #else
- varying vec2 vAmbientUV;
- #endif
- uniform sampler2D ambientSampler;
- #endif
- #ifdef OPACITY
- #if OPACITYDIRECTUV == 1
- #define vOpacityUV vMainUV1
- #elif OPACITYDIRECTUV == 2
- #define vOpacityUV vMainUV2
- #else
- varying vec2 vOpacityUV;
- #endif
- uniform sampler2D opacitySampler;
- #endif
- #ifdef EMISSIVE
- #if EMISSIVEDIRECTUV == 1
- #define vEmissiveUV vMainUV1
- #elif EMISSIVEDIRECTUV == 2
- #define vEmissiveUV vMainUV2
- #else
- varying vec2 vEmissiveUV;
- #endif
- uniform sampler2D emissiveSampler;
- #endif
- #ifdef LIGHTMAP
- #if LIGHTMAPDIRECTUV == 1
- #define vLightmapUV vMainUV1
- #elif LIGHTMAPDIRECTUV == 2
- #define vLightmapUV vMainUV2
- #else
- varying vec2 vLightmapUV;
- #endif
- uniform sampler2D lightmapSampler;
- #endif
- #ifdef REFRACTION
- #ifdef REFRACTIONMAP_3D
- uniform samplerCube refractionCubeSampler;
- #else
- uniform sampler2D refraction2DSampler;
- #endif
- #endif
- #if defined(SPECULAR) && defined(SPECULARTERM)
- #if SPECULARDIRECTUV == 1
- #define vSpecularUV vMainUV1
- #elif SPECULARDIRECTUV == 2
- #define vSpecularUV vMainUV2
- #else
- varying vec2 vSpecularUV;
- #endif
- uniform sampler2D specularSampler;
- #endif
- // Fresnel
- #include<fresnelFunction>
- // Reflection
- #ifdef REFLECTION
- #ifdef REFLECTIONMAP_3D
- uniform samplerCube reflectionCubeSampler;
- #else
- uniform sampler2D reflection2DSampler;
- #endif
- #ifdef REFLECTIONMAP_SKYBOX
- varying vec3 vPositionUVW;
- #else
- #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
- varying vec3 vDirectionW;
- #endif
- #endif
- #include<reflectionFunction>
- #endif
- #include<imageProcessingDeclaration>
- #include<imageProcessingFunctions>
- #include<bumpFragmentFunctions>
- #include<clipPlaneFragmentDeclaration>
- #include<logDepthDeclaration>
- #include<fogFragmentDeclaration>
- void main(void) {
- #include<clipPlaneFragment>
- vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
- // Base color
- vec4 baseColor = vec4(1., 1., 1., 1.);
- vec3 diffuseColor = vDiffuseColor.rgb;
- // Alpha
- float alpha = vDiffuseColor.a;
- // Bump
- #ifdef NORMAL
- vec3 normalW = normalize(vNormalW);
- #else
- vec3 normalW = normalize(-cross(dFdx(vPositionW), dFdy(vPositionW)));
- #endif
- #include<bumpFragment>
- #ifdef TWOSIDEDLIGHTING
- normalW = gl_FrontFacing ? normalW : -normalW;
- #endif
- #ifdef DIFFUSE
- baseColor = texture2D(diffuseSampler, vDiffuseUV + uvOffset);
- #ifdef ALPHATEST
- if (baseColor.a < 0.4)
- discard;
- #endif
- #ifdef ALPHAFROMDIFFUSE
- alpha *= baseColor.a;
- #endif
- baseColor.rgb *= vDiffuseInfos.y;
- #endif
- #include<depthPrePass>
- #ifdef VERTEXCOLOR
- baseColor.rgb *= vColor.rgb;
- #endif
- // Ambient color
- vec3 baseAmbientColor = vec3(1., 1., 1.);
- #ifdef AMBIENT
- baseAmbientColor = texture2D(ambientSampler, vAmbientUV + uvOffset).rgb * vAmbientInfos.y;
- #endif
- // Specular map
- #ifdef SPECULARTERM
- float glossiness = vSpecularColor.a;
- vec3 specularColor = vSpecularColor.rgb;
- #ifdef SPECULAR
- vec4 specularMapColor = texture2D(specularSampler, vSpecularUV + uvOffset);
- specularColor = specularMapColor.rgb;
- #ifdef GLOSSINESS
- glossiness = glossiness * specularMapColor.a;
- #endif
- #endif
- #else
- float glossiness = 0.;
- #endif
- // Lighting
- vec3 diffuseBase = vec3(0., 0., 0.);
- lightingInfo info;
- #ifdef SPECULARTERM
- vec3 specularBase = vec3(0., 0., 0.);
- #endif
- float shadow = 1.;
- #ifdef LIGHTMAP
- vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV + uvOffset).rgb * vLightmapInfos.y;
- #endif
- #include<lightFragment>[0..maxSimultaneousLights]
- // Refraction
- vec3 refractionColor = vec3(0., 0., 0.);
- #ifdef REFRACTION
- vec3 refractionVector = normalize(refract(-viewDirectionW, normalW, vRefractionInfos.y));
- #ifdef REFRACTIONMAP_3D
- refractionVector.y = refractionVector.y * vRefractionInfos.w;
- if (dot(refractionVector, viewDirectionW) < 1.0)
- {
- refractionColor = textureCube(refractionCubeSampler, refractionVector).rgb * vRefractionInfos.x;
- }
- #else
- vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
- vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
- refractionCoords.y = 1.0 - refractionCoords.y;
- refractionColor = texture2D(refraction2DSampler, refractionCoords).rgb * vRefractionInfos.x;
- #endif
- #endif
- // Reflection
- vec3 reflectionColor = vec3(0., 0., 0.);
- #ifdef REFLECTION
- vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
- #ifdef REFLECTIONMAP_3D
- #ifdef ROUGHNESS
- float bias = vReflectionInfos.y;
- #ifdef SPECULARTERM
- #ifdef SPECULAR
- #ifdef GLOSSINESS
- bias *= (1.0 - specularMapColor.a);
- #endif
- #endif
- #endif
- reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW, bias).rgb * vReflectionInfos.x;
- #else
- reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW).rgb * vReflectionInfos.x;
- #endif
- #else
- vec2 coords = vReflectionUVW.xy;
- #ifdef REFLECTIONMAP_PROJECTION
- coords /= vReflectionUVW.z;
- #endif
- coords.y = 1.0 - coords.y;
- reflectionColor = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.x;
- #endif
- #ifdef REFLECTIONFRESNEL
- float reflectionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, reflectionRightColor.a, reflectionLeftColor.a);
- #ifdef REFLECTIONFRESNELFROMSPECULAR
- #ifdef SPECULARTERM
- reflectionColor *= specularColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
- #else
- reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
- #endif
- #else
- reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
- #endif
- #endif
- #endif
- #ifdef REFRACTIONFRESNEL
- float refractionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, refractionRightColor.a, refractionLeftColor.a);
- refractionColor *= refractionLeftColor.rgb * (1.0 - refractionFresnelTerm) + refractionFresnelTerm * refractionRightColor.rgb;
- #endif
- #ifdef OPACITY
- vec4 opacityMap = texture2D(opacitySampler, vOpacityUV + uvOffset);
- #ifdef OPACITYRGB
- opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
- alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
- #else
- alpha *= opacityMap.a * vOpacityInfos.y;
- #endif
- #endif
- #ifdef VERTEXALPHA
- alpha *= vColor.a;
- #endif
- #ifdef OPACITYFRESNEL
- float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
- alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
- #endif
- // Emissive
- vec3 emissiveColor = vEmissiveColor;
- #ifdef EMISSIVE
- emissiveColor += texture2D(emissiveSampler, vEmissiveUV + uvOffset).rgb * vEmissiveInfos.y;
- #endif
- #ifdef EMISSIVEFRESNEL
- float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
- emissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
- #endif
- // Fresnel
- #ifdef DIFFUSEFRESNEL
- float diffuseFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, diffuseRightColor.a, diffuseLeftColor.a);
- diffuseBase *= diffuseLeftColor.rgb * (1.0 - diffuseFresnelTerm) + diffuseFresnelTerm * diffuseRightColor.rgb;
- #endif
- // Composition
- #ifdef EMISSIVEASILLUMINATION
- vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
- #else
- #ifdef LINKEMISSIVEWITHDIFFUSE
- vec3 finalDiffuse = clamp((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
- #else
- vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
- #endif
- #endif
- #ifdef SPECULARTERM
- vec3 finalSpecular = specularBase * specularColor;
- #ifdef SPECULAROVERALPHA
- alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
- #endif
- #else
- vec3 finalSpecular = vec3(0.0);
- #endif
- #ifdef REFLECTIONOVERALPHA
- alpha = clamp(alpha + dot(reflectionColor, vec3(0.3, 0.59, 0.11)), 0., 1.);
- #endif
- // Composition
- #ifdef EMISSIVEASILLUMINATION
- vec4 color = vec4(clamp(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + emissiveColor + refractionColor, 0.0, 1.0), alpha);
- #else
- vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + refractionColor, alpha);
- #endif
- //Old lightmap calculation method
- #ifdef LIGHTMAP
- #ifndef LIGHTMAPEXCLUDED
- #ifdef USELIGHTMAPASSHADOWMAP
- color.rgb *= lightmapColor;
- #else
- color.rgb += lightmapColor;
- #endif
- #endif
- #endif
- #include<logDepthFragment>
- #include<fogFragment>
- // Apply image processing if relevant. As this applies in linear space,
- // We first move from gamma to linear.
- #ifdef IMAGEPROCESSINGPOSTPROCESS
- color.rgb = toLinearSpace(color.rgb);
- #else
- #ifdef IMAGEPROCESSING
- color.rgb = toLinearSpace(color.rgb);
- color = applyImageProcessing(color);
- #endif
- #endif
- gl_FragColor = color;
- }
|