123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711 |
- #ifdef GL_ES
- precision highp float;
- #endif
- #define MAP_EXPLICIT 0.
- #define MAP_SPHERICAL 1.
- #define MAP_PLANAR 2.
- #define MAP_CUBIC 3.
- #define MAP_PROJECTION 4.
- #define MAP_SKYBOX 5.
- // Constants
- uniform vec3 vEyePosition;
- uniform vec3 vAmbientColor;
- uniform vec4 vDiffuseColor;
- uniform vec4 vSpecularColor;
- uniform vec3 vEmissiveColor;
- // Input
- varying vec3 vPositionW;
- varying vec3 vNormalW;
- #ifdef VERTEXCOLOR
- varying vec3 vColor;
- #endif
- // Lights
- #ifdef LIGHT0
- uniform vec4 vLightData0;
- uniform vec4 vLightDiffuse0;
- uniform vec3 vLightSpecular0;
- #ifdef SHADOW0
- varying vec4 vPositionFromLight0;
- uniform sampler2D shadowSampler0;
- uniform float darkness0;
- #endif
- #ifdef SPOTLIGHT0
- uniform vec4 vLightDirection0;
- #endif
- #ifdef HEMILIGHT0
- uniform vec3 vLightGround0;
- #endif
- #endif
- #ifdef LIGHT1
- uniform vec4 vLightData1;
- uniform vec4 vLightDiffuse1;
- uniform vec3 vLightSpecular1;
- #ifdef SHADOW1
- varying vec4 vPositionFromLight1;
- uniform sampler2D shadowSampler1;
- uniform float darkness1;
- #endif
- #ifdef SPOTLIGHT1
- uniform vec4 vLightDirection1;
- #endif
- #ifdef HEMILIGHT1
- uniform vec3 vLightGround1;
- #endif
- #endif
- #ifdef LIGHT2
- uniform vec4 vLightData2;
- uniform vec4 vLightDiffuse2;
- uniform vec3 vLightSpecular2;
- #ifdef SHADOW2
- varying vec4 vPositionFromLight2;
- uniform sampler2D shadowSampler2;
- uniform float darkness2;
- #endif
- #ifdef SPOTLIGHT2
- uniform vec4 vLightDirection2;
- #endif
- #ifdef HEMILIGHT2
- uniform vec3 vLightGround2;
- #endif
- #endif
- #ifdef LIGHT3
- uniform vec4 vLightData3;
- uniform vec4 vLightDiffuse3;
- uniform vec3 vLightSpecular3;
- #ifdef SHADOW3
- varying vec4 vPositionFromLight3;
- uniform sampler2D shadowSampler3;
- uniform float darkness3;
- #endif
- #ifdef SPOTLIGHT3
- uniform vec4 vLightDirection3;
- #endif
- #ifdef HEMILIGHT3
- uniform vec3 vLightGround3;
- #endif
- #endif
- // Samplers
- #ifdef DIFFUSE
- varying vec2 vDiffuseUV;
- uniform sampler2D diffuseSampler;
- uniform vec2 vDiffuseInfos;
- #endif
- #ifdef AMBIENT
- varying vec2 vAmbientUV;
- uniform sampler2D ambientSampler;
- uniform vec2 vAmbientInfos;
- #endif
- #ifdef OPACITY
- varying vec2 vOpacityUV;
- uniform sampler2D opacitySampler;
- uniform vec2 vOpacityInfos;
- #endif
- #ifdef EMISSIVE
- varying vec2 vEmissiveUV;
- uniform vec2 vEmissiveInfos;
- uniform sampler2D emissiveSampler;
- #endif
- #ifdef SPECULAR
- varying vec2 vSpecularUV;
- uniform vec2 vSpecularInfos;
- uniform sampler2D specularSampler;
- #endif
- // Fresnel
- #ifdef FRESNEL
- float computeFresnelTerm(vec3 viewDirection, vec3 worldNormal, float bias, float power)
- {
- float fresnelTerm = pow(bias + abs(dot(viewDirection, worldNormal)), power);
- return clamp(fresnelTerm, 0., 1.);
- }
- #endif
- #ifdef DIFFUSEFRESNEL
- uniform vec4 diffuseLeftColor;
- uniform vec4 diffuseRightColor;
- #endif
- #ifdef OPACITYFRESNEL
- uniform vec4 opacityParts;
- #endif
- #ifdef REFLECTIONFRESNEL
- uniform vec4 reflectionLeftColor;
- uniform vec4 reflectionRightColor;
- #endif
- #ifdef EMISSIVEFRESNEL
- uniform vec4 emissiveLeftColor;
- uniform vec4 emissiveRightColor;
- #endif
- // Reflection
- #ifdef REFLECTION
- varying vec3 vPositionUVW;
- uniform samplerCube reflectionCubeSampler;
- uniform sampler2D reflection2DSampler;
- uniform vec3 vReflectionInfos;
- uniform mat4 reflectionMatrix;
- uniform mat4 view;
- vec3 computeReflectionCoords(float mode, vec4 worldPos, vec3 worldNormal)
- {
- if (mode == MAP_SPHERICAL)
- {
- vec3 coords = vec3(view * vec4(worldNormal, 0.0));
- return vec3(reflectionMatrix * vec4(coords, 1.0));
- }
- else if (mode == MAP_PLANAR)
- {
- vec3 viewDir = worldPos.xyz - vEyePosition;
- vec3 coords = normalize(reflect(viewDir, worldNormal));
- return vec3(reflectionMatrix * vec4(coords, 1));
- }
- else if (mode == MAP_CUBIC)
- {
- vec3 viewDir = worldPos.xyz - vEyePosition;
- vec3 coords = reflect(viewDir, worldNormal);
- return vec3(reflectionMatrix * vec4(coords, 0));
- }
- else if (mode == MAP_PROJECTION)
- {
- return vec3(reflectionMatrix * (view * worldPos));
- }
- else if (mode == MAP_SKYBOX)
- {
- return vPositionUVW;
- }
- return vec3(0, 0, 0);
- }
- #endif
- // Shadows
- #ifdef SHADOWS
- float unpack(vec4 color)
- {
- const vec4 bitShift = vec4(1. / (255. * 255. * 255.), 1. / (255. * 255.), 1. / 255., 1.);
- return dot(color, bitShift);
- }
- float unpackHalf(vec2 color)
- {
- return color.x + (color.y / 255.0);
- }
- float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness)
- {
- vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
- vec2 uv = 0.5 * depth.xy + vec2(0.5, 0.5);
- if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
- {
- return 1.0;
- }
- float shadow = unpack(texture2D(shadowSampler, uv));
- if (depth.z > shadow)
- {
- return darkness;
- }
- return 1.;
- }
- float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler)
- {
- vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
- vec2 uv = 0.5 * depth.xy + vec2(0.5, 0.5);
- if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
- {
- return 1.0;
- }
- float visibility = 1.;
- vec2 poissonDisk[4];
- poissonDisk[0] = vec2(-0.94201624, -0.39906216);
- poissonDisk[1] = vec2(0.94558609, -0.76890725);
- poissonDisk[2] = vec2(-0.094184101, -0.92938870);
- poissonDisk[3] = vec2(0.34495938, 0.29387760);
- // Poisson Sampling
- for (int i = 0; i<4; i++){
- if (unpack(texture2D(shadowSampler, uv + poissonDisk[i] / 1500.0)) < depth.z){
- visibility -= 0.2;
- }
- }
- return visibility;
- }
- // Thanks to http://devmaster.net/
- float ChebychevInequality(vec2 moments, float t)
- {
- if (t <= moments.x)
- {
- return 1.0;
- }
- float variance = moments.y - (moments.x * moments.x);
- variance = max(variance, 0.);
- float d = t - moments.x;
- return variance / (variance + d * d);
- }
- float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler)
- {
- vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
- vec2 uv = 0.5 * depth.xy + vec2(0.5, 0.5);
- if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
- {
- return 1.0;
- }
- vec4 texel = texture2D(shadowSampler, uv);
- vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
- return clamp(1.3 - ChebychevInequality(moments, depth.z), 0., 1.0);
- }
- #endif
- // Bump
- #ifdef BUMP
- #extension GL_OES_standard_derivatives : enable
- varying vec2 vBumpUV;
- uniform vec2 vBumpInfos;
- uniform sampler2D bumpSampler;
- // Thanks to http://www.thetenthplanet.de/archives/1180
- mat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)
- {
- // get edge vectors of the pixel triangle
- vec3 dp1 = dFdx(p);
- vec3 dp2 = dFdy(p);
- vec2 duv1 = dFdx(uv);
- vec2 duv2 = dFdy(uv);
- // solve the linear system
- vec3 dp2perp = cross(dp2, normal);
- vec3 dp1perp = cross(normal, dp1);
- vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
- vec3 binormal = dp2perp * duv1.y + dp1perp * duv2.y;
- // construct a scale-invariant frame
- float invmax = inversesqrt(max(dot(tangent, tangent), dot(binormal, binormal)));
- return mat3(tangent * invmax, binormal * invmax, normal);
- }
- vec3 perturbNormal(vec3 viewDir)
- {
- vec3 map = texture2D(bumpSampler, vBumpUV).xyz * vBumpInfos.y;
- map = map * 255. / 127. - 128. / 127.;
- mat3 TBN = cotangent_frame(vNormalW, -viewDir, vBumpUV);
- return normalize(TBN * map);
- }
- #endif
- #ifdef CLIPPLANE
- varying float fClipDistance;
- #endif
- // Fog
- #ifdef FOG
- #define FOGMODE_NONE 0.
- #define FOGMODE_EXP 1.
- #define FOGMODE_EXP2 2.
- #define FOGMODE_LINEAR 3.
- #define E 2.71828
- uniform vec4 vFogInfos;
- uniform vec3 vFogColor;
- varying float fFogDistance;
- float CalcFogFactor()
- {
- float fogCoeff = 1.0;
- float fogStart = vFogInfos.y;
- float fogEnd = vFogInfos.z;
- float fogDensity = vFogInfos.w;
- if (FOGMODE_LINEAR == vFogInfos.x)
- {
- fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
- }
- else if (FOGMODE_EXP == vFogInfos.x)
- {
- fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
- }
- else if (FOGMODE_EXP2 == vFogInfos.x)
- {
- fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
- }
- return clamp(fogCoeff, 0.0, 1.0);
- }
- #endif
- // Light Computing
- struct lightingInfo
- {
- vec3 diffuse;
- vec3 specular;
- };
- lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range) {
- lightingInfo result;
- vec3 lightVectorW;
- float attenuation = 1.0;
- if (lightData.w == 0.)
- {
- vec3 direction = lightData.xyz - vPositionW;
- attenuation = max(0., 1.0 - length(direction) / range);
- lightVectorW = normalize(direction);
- }
- else
- {
- lightVectorW = normalize(-lightData.xyz);
- }
- // diffuse
- float ndl = max(0., dot(vNormal, lightVectorW));
- // Specular
- vec3 angleW = normalize(viewDirectionW + lightVectorW);
- float specComp = max(0., dot(vNormal, angleW));
- specComp = pow(specComp, max(1., vSpecularColor.a));
- result.diffuse = ndl * diffuseColor * attenuation;
- result.specular = specComp * specularColor * attenuation;
- return result;
- }
- lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range) {
- lightingInfo result;
- vec3 direction = lightData.xyz - vPositionW;
- vec3 lightVectorW = normalize(direction);
- float attenuation = max(0., 1.0 - length(direction) / range);
- // diffuse
- float cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));
- float spotAtten = 0.0;
- if (cosAngle >= lightDirection.w)
- {
- cosAngle = max(0., pow(cosAngle, lightData.w));
- spotAtten = max(0., (cosAngle - lightDirection.w) / (1. - cosAngle));
- // Diffuse
- float ndl = max(0., dot(vNormal, -lightDirection.xyz));
- // Specular
- vec3 angleW = normalize(viewDirectionW - lightDirection.xyz);
- float specComp = max(0., dot(vNormal, angleW));
- specComp = pow(specComp, vSpecularColor.a);
- result.diffuse = ndl * spotAtten * diffuseColor * attenuation;
- result.specular = specComp * specularColor * spotAtten * attenuation;
- return result;
- }
- result.diffuse = vec3(0.);
- result.specular = vec3(0.);
- return result;
- }
- lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor) {
- lightingInfo result;
- // Diffuse
- float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
- // Specular
- vec3 angleW = normalize(viewDirectionW + lightData.xyz);
- float specComp = max(0., dot(vNormal, angleW));
- specComp = pow(specComp, vSpecularColor.a);
- result.diffuse = mix(groundColor, diffuseColor, ndl);
- result.specular = specComp * specularColor;
- return result;
- }
- void main(void) {
- // Clip plane
- #ifdef CLIPPLANE
- if (fClipDistance > 0.0)
- discard;
- #endif
- vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
- // Base color
- vec4 baseColor = vec4(1., 1., 1., 1.);
- vec3 diffuseColor = vDiffuseColor.rgb;
- // Alpha
- float alpha = vDiffuseColor.a;
- #ifdef VERTEXCOLOR
- diffuseColor *= vColor;
- #endif
- #ifdef DIFFUSE
- baseColor = texture2D(diffuseSampler, vDiffuseUV);
- #ifdef ALPHATEST
- if (baseColor.a < 0.4)
- discard;
- #endif
- #ifdef ALPHAFROMDIFFUSE
- alpha *= baseColor.a;
- #endif
- baseColor.rgb *= vDiffuseInfos.y;
- #endif
- // Bump
- vec3 normalW = normalize(vNormalW);
- #ifdef BUMP
- normalW = perturbNormal(viewDirectionW);
- #endif
- // Ambient color
- vec3 baseAmbientColor = vec3(1., 1., 1.);
- #ifdef AMBIENT
- baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
- #endif
- // Lighting
- vec3 diffuseBase = vec3(0., 0., 0.);
- vec3 specularBase = vec3(0., 0., 0.);
- float shadow = 1.;
- #ifdef LIGHT0
- #ifdef SPOTLIGHT0
- lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a);
- #endif
- #ifdef HEMILIGHT0
- lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0);
- #endif
- #ifdef POINTDIRLIGHT0
- lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a);
- #endif
- #ifdef SHADOW0
- #ifdef SHADOWVSM0
- shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0);
- #else
- #ifdef SHADOWPCF0
- shadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0);
- #else
- shadow = computeShadow(vPositionFromLight0, shadowSampler0, darkness0);
- #endif
- #endif
- #else
- shadow = 1.;
- #endif
- diffuseBase += info.diffuse * shadow;
- specularBase += info.specular * shadow;
- #endif
- #ifdef LIGHT1
- #ifdef SPOTLIGHT1
- info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a);
- #endif
- #ifdef HEMILIGHT1
- info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1);
- #endif
- #ifdef POINTDIRLIGHT1
- info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a);
- #endif
- #ifdef SHADOW1
- #ifdef SHADOWVSM1
- shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1);
- #else
- #ifdef SHADOWPCF1
- shadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1);
- #else
- shadow = computeShadow(vPositionFromLight1, shadowSampler1, darkness1);
- #endif
- #endif
- #else
- shadow = 1.;
- #endif
- diffuseBase += info.diffuse * shadow;
- specularBase += info.specular * shadow;
- #endif
- #ifdef LIGHT2
- #ifdef SPOTLIGHT2
- info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a);
- #endif
- #ifdef HEMILIGHT2
- info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2);
- #endif
- #ifdef POINTDIRLIGHT2
- info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a);
- #endif
- #ifdef SHADOW2
- #ifdef SHADOWVSM2
- shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2);
- #else
- #ifdef SHADOWPCF2
- shadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2);
- #else
- shadow = computeShadow(vPositionFromLight2, shadowSampler2, darkness2);
- #endif
- #endif
- #else
- shadow = 1.;
- #endif
- diffuseBase += info.diffuse * shadow;
- specularBase += info.specular * shadow;
- #endif
- #ifdef LIGHT3
- #ifdef SPOTLIGHT3
- info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a);
- #endif
- #ifdef HEMILIGHT3
- info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3);
- #endif
- #ifdef POINTDIRLIGHT3
- info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a);
- #endif
- #ifdef SHADOW3
- #ifdef SHADOWVSM3
- shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3);
- #else
- #ifdef SHADOWPCF3
- shadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3);
- #else
- shadow = computeShadow(vPositionFromLight3, shadowSampler3, darkness3);
- #endif
- #endif
- #else
- shadow = 1.;
- #endif
- diffuseBase += info.diffuse * shadow;
- specularBase += info.specular * shadow;
- #endif
- // Reflection
- vec3 reflectionColor = vec3(0., 0., 0.);
- #ifdef REFLECTION
- vec3 vReflectionUVW = computeReflectionCoords(vReflectionInfos.x, vec4(vPositionW, 1.0), normalW);
- if (vReflectionInfos.z != 0.0)
- {
- reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW).rgb * vReflectionInfos.y * shadow;
- }
- else
- {
- vec2 coords = vReflectionUVW.xy;
- if (vReflectionInfos.x == MAP_PROJECTION)
- {
- coords /= vReflectionUVW.z;
- }
- coords.y = 1.0 - coords.y;
- reflectionColor = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.y * shadow;
- }
- #ifdef REFLECTIONFRESNEL
- float reflectionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, reflectionRightColor.a, reflectionLeftColor.a);
- reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
- #endif
- #endif
- #ifdef OPACITY
- vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
- #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 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).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
- // Specular map
- vec3 specularColor = vSpecularColor.rgb;
- #ifdef SPECULAR
- specularColor = texture2D(specularSampler, vSpecularUV).rgb * vSpecularInfos.y;
- #endif
- // Fresnel
- #ifdef DIFFUSEFRESNEL
- float diffuseFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, diffuseRightColor.a, diffuseLeftColor.a);
- diffuseBase *= diffuseLeftColor.rgb * (1.0 - diffuseFresnelTerm) + diffuseFresnelTerm * diffuseRightColor.rgb;
- #endif
- // Composition
- vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
- vec3 finalSpecular = specularBase * specularColor;
- #ifdef SPECULAROVERALPHA
- alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
- #endif
- vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor, alpha);
- #ifdef FOG
- float fog = CalcFogFactor();
- color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
- #endif
- gl_FragColor = color;
- }
|