2012-12-04 03:05:34 +00:00
uniform sampler2D u_DiffuseMap;
#if defined(USE_LIGHTMAP)
uniform sampler2D u_LightMap;
#endif
#if defined(USE_NORMALMAP)
uniform sampler2D u_NormalMap;
#endif
#if defined(USE_DELUXEMAP)
uniform sampler2D u_DeluxeMap;
#endif
#if defined(USE_SPECULARMAP)
uniform sampler2D u_SpecularMap;
#endif
#if defined(USE_SHADOWMAP)
uniform sampler2D u_ShadowMap;
#endif
2013-09-16 07:54:26 +00:00
#if defined(USE_CUBEMAP)
uniform samplerCube u_CubeMap;
2012-12-04 03:05:34 +00:00
#endif
2013-09-18 06:52:40 +00:00
#if defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
2012-12-04 03:05:34 +00:00
uniform vec3 u_DirectedLight;
uniform vec3 u_AmbientLight;
#endif
2013-04-02 07:17:24 +00:00
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
uniform vec3 u_PrimaryLightColor;
uniform vec3 u_PrimaryLightAmbient;
#endif
2013-09-18 06:52:40 +00:00
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
2012-12-04 03:05:34 +00:00
uniform vec2 u_MaterialInfo;
#endif
2013-09-18 06:52:40 +00:00
varying vec4 var_TexCoords;
2012-12-04 03:05:34 +00:00
varying vec4 var_Color;
2013-09-16 07:54:26 +00:00
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
2013-09-18 06:52:40 +00:00
varying vec4 var_Normal;
varying vec4 var_Tangent;
varying vec4 var_Bitangent;
2012-12-04 03:05:34 +00:00
#endif
2013-09-16 07:54:26 +00:00
#if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
2013-09-18 06:52:40 +00:00
varying vec3 var_LightColor;
2013-09-16 07:54:26 +00:00
#endif
2012-12-04 03:05:34 +00:00
2013-09-18 06:52:40 +00:00
#if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT)
2013-09-16 07:54:26 +00:00
varying vec4 var_LightDir;
2013-04-02 07:17:24 +00:00
#endif
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
2013-09-18 06:52:40 +00:00
varying vec4 var_PrimaryLightDir;
2012-12-04 03:05:34 +00:00
#endif
2013-04-02 07:17:24 +00:00
2012-12-04 03:05:34 +00:00
#define EPSILON 0.00000001
#if defined(USE_PARALLAXMAP)
2013-09-16 07:54:26 +00:00
float SampleDepth(sampler2D normalMap, vec2 t)
2012-12-04 03:05:34 +00:00
{
#if defined(SWIZZLE_NORMALMAP)
2013-09-16 07:54:26 +00:00
return 1.0 - texture2D(normalMap, t).r;
2012-12-04 03:05:34 +00:00
#else
2013-09-16 07:54:26 +00:00
return 1.0 - texture2D(normalMap, t).a;
2012-12-04 03:05:34 +00:00
#endif
}
float RayIntersectDisplaceMap(vec2 dp, vec2 ds, sampler2D normalMap)
{
const int linearSearchSteps = 16;
const int binarySearchSteps = 6;
// current size of search window
2013-09-16 07:54:26 +00:00
float size = 1.0 / float(linearSearchSteps);
2012-12-04 03:05:34 +00:00
// current depth position
float depth = 0.0;
// best match found (starts with last position 1.0)
float bestDepth = 1.0;
// search front to back for first point inside object
for(int i = 0; i < linearSearchSteps - 1; ++i)
{
depth += size;
2013-09-16 07:54:26 +00:00
float t = SampleDepth(normalMap, dp + ds * depth);
2012-12-04 03:05:34 +00:00
if(bestDepth > 0.996) // if no depth found yet
if(depth >= t)
bestDepth = depth; // store best depth
}
depth = bestDepth;
// recurse around first point (depth) for closest match
for(int i = 0; i < binarySearchSteps; ++i)
{
size *= 0.5;
2013-09-16 07:54:26 +00:00
float t = SampleDepth(normalMap, dp + ds * depth);
2012-12-04 03:05:34 +00:00
if(depth >= t)
{
bestDepth = depth;
depth -= 2.0 * size;
}
depth += size;
}
return bestDepth;
}
#endif
2013-05-03 07:32:59 +00:00
vec3 CalcDiffuse(vec3 diffuseAlbedo, vec3 N, vec3 L, vec3 E, float NE, float NL, float shininess)
2012-12-04 03:05:34 +00:00
{
#if defined(USE_OREN_NAYAR) || defined(USE_TRIACE_OREN_NAYAR)
float gamma = dot(E, L) - NE * NL;
float B = 2.22222 + 0.1 * shininess;
2013-05-03 07:32:59 +00:00
#if defined(USE_OREN_NAYAR)
2012-12-04 03:05:34 +00:00
float A = 1.0 - 1.0 / (2.0 + 0.33 * shininess);
gamma = clamp(gamma, 0.0, 1.0);
2013-05-03 07:32:59 +00:00
#endif
2012-12-04 03:05:34 +00:00
2013-05-03 07:32:59 +00:00
#if defined(USE_TRIACE_OREN_NAYAR)
2012-12-04 03:05:34 +00:00
float A = 1.0 - 1.0 / (2.0 + 0.65 * shininess);
if (gamma >= 0.0)
2013-05-03 07:32:59 +00:00
#endif
2012-12-04 03:05:34 +00:00
{
2013-09-16 07:54:26 +00:00
B = max(B * max(NL, NE), EPSILON);
2012-12-04 03:05:34 +00:00
}
2013-05-03 07:32:59 +00:00
return diffuseAlbedo * (A + gamma / B);
2012-12-04 03:05:34 +00:00
#else
2013-05-03 07:32:59 +00:00
return diffuseAlbedo;
2012-12-04 03:05:34 +00:00
#endif
}
2013-09-16 07:54:26 +00:00
vec3 EnvironmentBRDF(float gloss, float NE, vec3 specular)
2012-12-04 03:05:34 +00:00
{
2013-09-16 07:54:26 +00:00
#if 1
// from http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
vec4 t = vec4( 1/0.96, 0.475, (0.0275 - 0.25 * 0.04)/0.96,0.25 ) * gloss;
t += vec4( 0.0, 0.0, (0.015 - 0.75 * 0.04)/0.96,0.75 );
float a0 = t.x * min( t.y, exp2( -9.28 * NE ) ) + t.z;
float a1 = t.w;
return clamp( a0 + specular * ( a1 - a0 ), 0.0, 1.0 );
#elif 0
// from http://seblagarde.wordpress.com/2011/08/17/hello-world/
return mix(specular.rgb, max(specular.rgb, vec3(gloss)), CalcFresnel(NE));
#else
// from http://advances.realtimerendering.com/s2011/Lazarov-Physically-Based-Lighting-in-Black-Ops%20%28Siggraph%202011%20Advances%20in%20Real-Time%20Rendering%20Course%29.pptx
return mix(specular.rgb, vec3(1.0), CalcFresnel(NE) / (4.0 - 3.0 * gloss));
2012-12-04 03:05:34 +00:00
#endif
2013-09-16 07:54:26 +00:00
}
2012-12-04 03:05:34 +00:00
2013-09-16 07:54:26 +00:00
float CalcBlinn(float NH, float shininess)
{
#if 0
// from http://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/
float a = shininess + 0.775;
return exp(a * NH - a);
#else
return pow(NH, shininess);
#endif
}
2012-12-04 03:05:34 +00:00
2013-09-24 10:29:49 +00:00
float CalcGGX(float NH, float gloss)
2013-09-16 07:54:26 +00:00
{
2013-09-24 10:29:49 +00:00
// from http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
float a_sq = exp2(gloss * -13.0 + 1.0);
float d = ((NH * NH) * (a_sq - 1.0) + 1.0);
return a_sq / (d * d);
2013-09-16 07:54:26 +00:00
}
2012-12-04 03:05:34 +00:00
2013-09-16 07:54:26 +00:00
float CalcFresnel(float EH)
{
#if 1
2013-09-24 10:29:49 +00:00
// From http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
// not accurate, but fast
return exp2(-10.0 * EH);
#elif 0
2013-09-16 07:54:26 +00:00
// From http://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/
return exp2((-5.55473 * EH - 6.98316) * EH);
#elif 0
float blend = 1.0 - EH;
float blend2 = blend * blend;
blend *= blend2 * blend2;
return blend;
#else
2013-09-24 10:29:49 +00:00
return pow(1.0 - EH, 5.0);
2013-09-16 07:54:26 +00:00
#endif
}
2013-09-24 10:29:49 +00:00
float CalcVisibility(float NH, float NL, float NE, float EH, float gloss)
2013-09-16 07:54:26 +00:00
{
2013-09-24 10:29:49 +00:00
#if 1
// From http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
float k = min(1.0, gloss + 0.545);
return 1.0 / (k * EH * EH + (1.0 - k));
#elif 0
float roughness = exp2(gloss * -6.5);
2013-09-16 07:54:26 +00:00
#if defined(USE_GGX)
2013-09-24 10:29:49 +00:00
// From http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
float k = roughness + 1.0;
2013-09-16 07:54:26 +00:00
k *= k * 0.125;
#else
2013-09-24 10:29:49 +00:00
float k = roughness;
2013-09-16 07:54:26 +00:00
#endif
2013-09-24 10:29:49 +00:00
// Modified from http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
// NL, NE in numerator factored out from cook-torrance
2013-09-16 07:54:26 +00:00
float k2 = 1.0 - k;
float invGeo1 = NL * k2 + k;
float invGeo2 = NE * k2 + k;
return 1.0 / (invGeo1 * invGeo2);
2013-09-24 10:29:49 +00:00
#else
float geo = 2.0 * NH * min(NE, NL);
geo /= max(EH, geo);
return geo;
#endif
2013-09-16 07:54:26 +00:00
}
2012-12-04 03:05:34 +00:00
2013-09-24 10:29:49 +00:00
vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float gloss, float shininess)
2013-09-16 07:54:26 +00:00
{
float blinn = CalcBlinn(NH, shininess);
vec3 fSpecular = mix(specular, vec3(1.0), CalcFresnel(EH));
2013-09-24 10:29:49 +00:00
float vis = CalcVisibility(NH, NL, NE, EH, gloss);
2012-12-04 03:05:34 +00:00
2013-09-16 07:54:26 +00:00
#if defined(USE_BLINN)
// Normalized Blinn-Phong
return specular * blinn * (shininess * 0.125 + 1.0);
#elif defined(USE_BLINN_FRESNEL)
// Normalized Blinn-Phong with Fresnel
return fSpecular * blinn * (shininess * 0.125 + 1.0);
#elif defined(USE_MCAULEY)
// Cook-Torrance as done by Stephen McAuley
// http://blog.selfshadow.com/publications/s2012-shading-course/mcauley/s2012_pbs_farcry3_notes_v2.pdf
return fSpecular * blinn * (shininess * 0.25 + 0.125);
#elif defined(USE_GOTANDA)
// Neumann-Neumann as done by Yoshiharu Gotanda
// http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf
return fSpecular * blinn * (shininess * 0.124858 + 0.269182) / max(max(NL, NE), EPSILON);
#elif defined(USE_LAZAROV)
// Cook-Torrance as done by Dimitar Lazarov
// http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
return fSpecular * blinn * (shininess * 0.125 + 0.25) * vis;
2012-12-04 03:05:34 +00:00
#endif
2013-09-16 07:54:26 +00:00
return vec3(0.0);
2012-12-04 03:05:34 +00:00
}
2013-09-17 06:41:04 +00:00
float CalcLightAttenuation(vec3 dir, float sqrRadius)
{
// point light at >0 radius, directional otherwise
float point = float(sqrRadius > 0.0);
// inverse square light
float attenuation = sqrRadius / dot(dir, dir);
// zero light at radius, approximating q3 style
// also don't attenuate directional light
attenuation = (0.5 * attenuation - 1.5) * point + 1.0;
// clamp attenuation
#if defined(NO_LIGHT_CLAMP)
2013-09-18 06:52:40 +00:00
attenuation = max(attenuation, 0.0);
2013-09-17 06:41:04 +00:00
#else
attenuation = clamp(attenuation, 0.0, 1.0);
#endif
return attenuation;
}
2012-12-04 03:05:34 +00:00
void main()
{
2013-09-16 07:54:26 +00:00
vec3 L, N, E, H;
float NL, NH, NE, EH;
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
2013-09-18 06:52:40 +00:00
mat3 tangentToWorld = mat3(var_Tangent.xyz, var_Bitangent.xyz, var_Normal.xyz);
2012-12-04 03:05:34 +00:00
#endif
#if defined(USE_DELUXEMAP)
2013-09-18 06:52:40 +00:00
L = (2.0 * texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(1.0));
2013-09-16 07:54:26 +00:00
#if defined(USE_TANGENT_SPACE_LIGHT)
2013-09-18 06:52:40 +00:00
L = L * tangentToWorld;
2013-09-16 07:54:26 +00:00
#endif
#elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
L = var_LightDir.xyz;
#endif
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
2013-09-18 06:52:40 +00:00
E = normalize(vec3(var_Normal.w, var_Tangent.w, var_Bitangent.w));
2012-12-04 03:05:34 +00:00
#endif
#if defined(USE_LIGHTMAP)
2013-09-18 06:52:40 +00:00
vec4 lightSample = texture2D(u_LightMap, var_TexCoords.zw).rgba;
2013-09-16 12:57:14 +00:00
#if defined(RGBM_LIGHTMAP)
lightSample.rgb *= 32.0 * lightSample.a;
2012-12-04 03:05:34 +00:00
#endif
2013-04-02 07:17:24 +00:00
vec3 lightColor = lightSample.rgb;
2012-12-04 03:05:34 +00:00
#elif defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
2013-09-18 06:52:40 +00:00
vec3 lightColor = u_DirectedLight * CalcLightAttenuation(L, var_LightDir.w);
2013-09-16 07:54:26 +00:00
vec3 ambientColor = u_AmbientLight;
2012-12-04 03:05:34 +00:00
#elif defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
2013-09-18 06:52:40 +00:00
vec3 lightColor = var_LightColor;
2012-12-04 03:05:34 +00:00
#endif
2013-09-17 06:41:04 +00:00
2013-09-18 06:52:40 +00:00
vec2 texCoords = var_TexCoords.xy;
2012-12-04 03:05:34 +00:00
2013-09-16 07:54:26 +00:00
#if defined(USE_PARALLAXMAP)
#if defined(USE_TANGENT_SPACE_LIGHT)
vec3 offsetDir = E;
2012-12-04 03:05:34 +00:00
#else
2013-09-16 07:54:26 +00:00
vec3 offsetDir = E * tangentToWorld;
2013-04-02 07:17:24 +00:00
#endif
2012-12-04 03:05:34 +00:00
offsetDir.xy *= -0.05 / offsetDir.z;
2013-04-02 07:17:24 +00:00
texCoords += offsetDir.xy * RayIntersectDisplaceMap(texCoords, offsetDir.xy, u_NormalMap);
2012-12-04 03:05:34 +00:00
#endif
2013-09-16 07:54:26 +00:00
vec4 diffuse = texture2D(u_DiffuseMap, texCoords);
2013-09-24 10:29:49 +00:00
#if defined(USE_GAMMA2_TEXTURES)
2013-09-16 07:54:26 +00:00
diffuse.rgb *= diffuse.rgb;
2013-09-24 10:29:49 +00:00
#endif
2012-12-04 03:05:34 +00:00
2013-09-24 10:29:49 +00:00
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
2013-09-16 07:54:26 +00:00
#if defined(USE_NORMALMAP)
#if defined(SWIZZLE_NORMALMAP)
N.xy = 2.0 * texture2D(u_NormalMap, texCoords).ag - vec2(1.0);
#else
N.xy = 2.0 * texture2D(u_NormalMap, texCoords).rg - vec2(1.0);
#endif
N.z = sqrt(1.0 - clamp(dot(N.xy, N.xy), 0.0, 1.0));
#if !defined(USE_TANGENT_SPACE_LIGHT)
N = normalize(tangentToWorld * N);
#endif
#elif defined(USE_TANGENT_SPACE_LIGHT)
N = vec3(0.0, 0.0, 1.0);
#else
2013-09-18 06:52:40 +00:00
N = normalize(var_Normal.xyz);
2012-12-04 03:05:34 +00:00
#endif
2013-09-16 07:54:26 +00:00
2013-04-02 07:17:24 +00:00
L = normalize(L);
2012-12-04 03:05:34 +00:00
2013-04-02 07:17:24 +00:00
#if defined(USE_SHADOWMAP)
vec2 shadowTex = gl_FragCoord.xy * r_FBufScale;
float shadowValue = texture2D(u_ShadowMap, shadowTex).r;
// surfaces not facing the light are always shadowed
2013-09-16 07:54:26 +00:00
#if defined(USE_TANGENT_SPACE_LIGHT)
2013-09-24 10:29:49 +00:00
shadowValue *= step(0.0, var_PrimaryLightDir.z);
2013-09-16 07:54:26 +00:00
#else
2013-09-24 10:29:49 +00:00
shadowValue *= step(0.0, dot(var_Normal.xyz, var_PrimaryLightDir.xyz));
2013-09-16 07:54:26 +00:00
#endif
2013-04-02 07:17:24 +00:00
#if defined(SHADOWMAP_MODULATE)
//vec3 shadowColor = min(u_PrimaryLightAmbient, lightColor);
vec3 shadowColor = u_PrimaryLightAmbient * lightColor;
2013-05-03 07:32:59 +00:00
2013-04-02 07:17:24 +00:00
#if 0
// Only shadow when the world light is parallel to the primary light
2013-09-18 06:52:40 +00:00
shadowValue = 1.0 + (shadowValue - 1.0) * clamp(dot(L, var_PrimaryLightDir.xyz), 0.0, 1.0);
2013-05-03 07:32:59 +00:00
#endif
2013-04-02 07:17:24 +00:00
lightColor = mix(shadowColor, lightColor, shadowValue);
2013-05-03 07:32:59 +00:00
#endif
2013-04-02 07:17:24 +00:00
#endif
2012-12-10 22:35:57 +00:00
2012-12-04 03:05:34 +00:00
#if defined(USE_LIGHTMAP) || defined(USE_LIGHT_VERTEX)
2013-04-02 07:17:24 +00:00
vec3 ambientColor = lightColor;
2013-09-16 07:54:26 +00:00
#if defined(USE_TANGENT_SPACE_LIGHT)
float surfNL = L.z;
#else
2013-09-18 06:52:40 +00:00
float surfNL = clamp(dot(var_Normal.xyz, L), 0.0, 1.0);
2013-09-16 07:54:26 +00:00
#endif
// Scale the incoming light to compensate for the baked-in light angle
// attenuation.
lightColor /= max(surfNL, 0.25);
// Recover any unused light as ambient, in case attenuation is over 4x or
// light is below the surface
2013-04-02 07:17:24 +00:00
ambientColor -= lightColor * surfNL;
2012-12-04 03:05:34 +00:00
#endif
2013-09-16 07:54:26 +00:00
vec3 reflectance;
2013-04-02 07:17:24 +00:00
2013-09-16 07:54:26 +00:00
NL = clamp(dot(N, L), 0.0, 1.0);
NE = clamp(dot(N, E), 0.0, 1.0);
2012-12-10 22:35:57 +00:00
2012-12-04 03:05:34 +00:00
#if defined(USE_SPECULARMAP)
2013-09-16 07:54:26 +00:00
vec4 specular = texture2D(u_SpecularMap, texCoords);
2013-09-24 10:29:49 +00:00
#if defined(USE_GAMMA2_TEXTURES)
2013-09-16 07:54:26 +00:00
specular.rgb *= specular.rgb;
2013-09-24 10:29:49 +00:00
#endif
2013-09-16 07:54:26 +00:00
#else
vec4 specular = vec4(1.0);
#endif
specular *= u_MaterialInfo.xxxy;
float gloss = specular.a;
float shininess = exp2(gloss * 13.0);
#if defined(SPECULAR_IS_METALLIC)
// diffuse is actually base color, and red of specular is metallicness
float metallic = specular.r;
specular.rgb = vec3(0.04) + 0.96 * diffuse.rgb * metallic;
diffuse.rgb *= 1.0 - metallic;
#else
2013-05-03 07:32:59 +00:00
// adjust diffuse by specular reflectance, to maintain energy conservation
2013-09-16 07:54:26 +00:00
diffuse.rgb *= vec3(1.0) - specular.rgb;
2012-12-04 03:05:34 +00:00
#endif
2013-09-16 07:54:26 +00:00
reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess);
2012-12-10 22:35:57 +00:00
2013-09-16 07:54:26 +00:00
#if defined(r_deluxeSpecular) || defined(USE_LIGHT_VECTOR)
2013-09-24 10:29:49 +00:00
float adjGloss = gloss;
2013-09-16 07:54:26 +00:00
float adjShininess = shininess;
#if !defined(USE_LIGHT_VECTOR)
2013-09-24 10:29:49 +00:00
adjGloss *= r_deluxeSpecular;
adjShininess = exp2(adjGloss * 13.0);
2013-09-16 07:54:26 +00:00
#endif
H = normalize(L + E);
2013-05-03 07:32:59 +00:00
2013-09-16 07:54:26 +00:00
EH = clamp(dot(E, H), 0.0, 1.0);
NH = clamp(dot(N, H), 0.0, 1.0);
#if !defined(USE_LIGHT_VECTOR)
2013-09-24 10:29:49 +00:00
reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, adjGloss, adjShininess) * r_deluxeSpecular;
2013-05-03 07:32:59 +00:00
#else
2013-09-24 10:29:49 +00:00
reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, adjGloss, adjShininess);
2013-05-03 07:32:59 +00:00
#endif
2013-04-02 07:17:24 +00:00
#endif
2013-09-16 07:54:26 +00:00
gl_FragColor.rgb = lightColor * reflectance * NL;
gl_FragColor.rgb += ambientColor * (diffuse.rgb + specular.rgb);
#if defined(USE_CUBEMAP)
reflectance = EnvironmentBRDF(gloss, NE, specular.rgb);
2012-12-04 03:05:34 +00:00
2013-09-16 07:54:26 +00:00
vec3 R = reflect(E, N);
#if defined(USE_TANGENT_SPACE_LIGHT)
R = tangentToWorld * R;
#endif
2012-12-04 03:05:34 +00:00
2013-09-16 07:54:26 +00:00
vec3 cubeLightColor = textureCubeLod(u_CubeMap, R, 7.0 - gloss * 7.0).rgb;
#if defined(USE_LIGHTMAP)
cubeLightColor *= lightSample.rgb;
#elif defined (USE_LIGHT_VERTEX)
2013-09-18 06:52:40 +00:00
cubeLightColor *= var_LightColor;
2013-09-16 07:54:26 +00:00
#else
cubeLightColor *= lightColor * NL + ambientColor;
#endif
//gl_FragColor.rgb += diffuse.rgb * textureCubeLod(u_CubeMap, N, 7.0).rgb;
2013-09-24 10:29:49 +00:00
gl_FragColor.rgb += cubeLightColor * reflectance;
2013-09-16 07:54:26 +00:00
#endif
2013-04-02 07:17:24 +00:00
2013-09-16 07:54:26 +00:00
#if defined(USE_PRIMARY_LIGHT)
2013-09-24 10:29:49 +00:00
L = var_PrimaryLightDir.xyz; //normalize(var_PrimaryLightDir.xyz);
2013-09-16 07:54:26 +00:00
NL = clamp(dot(N, L), 0.0, 1.0);
2013-04-02 07:17:24 +00:00
2013-09-16 07:54:26 +00:00
H = normalize(L + E);
EH = clamp(dot(E, H), 0.0, 1.0);
NH = clamp(dot(N, H), 0.0, 1.0);
reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess);
2013-09-24 10:29:49 +00:00
reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, gloss, shininess);
2013-09-16 07:54:26 +00:00
2013-09-18 06:52:40 +00:00
lightColor = u_PrimaryLightColor; // * CalcLightAttenuation(L, u_PrimaryLightDir.w);
2013-09-17 06:41:04 +00:00
2013-09-16 07:54:26 +00:00
#if defined(USE_SHADOWMAP)
2013-09-17 06:41:04 +00:00
lightColor *= shadowValue;
2013-05-03 07:32:59 +00:00
#endif
2013-09-16 07:54:26 +00:00
2013-09-17 06:41:04 +00:00
gl_FragColor.rgb += lightColor * reflectance * NL;
2013-09-16 07:54:26 +00:00
#endif
gl_FragColor.a = diffuse.a;
2013-05-03 07:32:59 +00:00
#else
2013-09-16 07:54:26 +00:00
gl_FragColor = diffuse;
#if defined(USE_LIGHTMAP)
gl_FragColor.rgb *= lightColor;
#endif
2012-12-04 03:05:34 +00:00
#endif
gl_FragColor *= var_Color;
}