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-11-04 03:34:22 +00:00
|
|
|
#if defined(USE_NORMALMAP) || defined(USE_DELUXEMAP) || defined(USE_SPECULARMAP) || defined(USE_CUBEMAP)
|
2014-03-04 05:02:39 +00:00
|
|
|
// y = deluxe, w = cube
|
|
|
|
uniform vec4 u_EnableTextures;
|
2013-11-04 03:34:22 +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)
|
2014-03-04 05:02:39 +00:00
|
|
|
uniform vec4 u_NormalScale;
|
|
|
|
uniform vec4 u_SpecularScale;
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
2014-03-16 23:29:38 +00:00
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
|
|
|
#if defined(USE_CUBEMAP)
|
|
|
|
uniform vec4 u_CubeMapInfo;
|
|
|
|
#endif
|
|
|
|
#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-11-05 05:53:05 +00:00
|
|
|
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT))
|
|
|
|
#if defined(USE_VERT_TANGENT_SPACE)
|
|
|
|
varying vec4 var_Normal;
|
|
|
|
varying vec4 var_Tangent;
|
|
|
|
varying vec4 var_Bitangent;
|
|
|
|
#else
|
|
|
|
varying vec3 var_Normal;
|
|
|
|
varying vec3 var_ViewDir;
|
|
|
|
#endif
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
#if defined(USE_LIGHT) && !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;
|
|
|
|
|
2014-11-11 06:11:36 +00:00
|
|
|
// texture depth at best depth
|
|
|
|
float texDepth = 0.0;
|
|
|
|
|
2014-11-14 09:12:41 +00:00
|
|
|
float prevT = SampleDepth(normalMap, dp);
|
|
|
|
float prevTexDepth = prevT;
|
|
|
|
|
2012-12-04 03:05:34 +00:00
|
|
|
// 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)
|
2014-11-11 06:11:36 +00:00
|
|
|
{
|
2012-12-04 03:05:34 +00:00
|
|
|
bestDepth = depth; // store best depth
|
2014-11-11 06:11:36 +00:00
|
|
|
texDepth = t;
|
2014-11-14 09:12:41 +00:00
|
|
|
prevTexDepth = prevT;
|
2014-11-11 06:11:36 +00:00
|
|
|
}
|
2014-11-14 09:12:41 +00:00
|
|
|
prevT = t;
|
2012-12-04 03:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
depth = bestDepth;
|
2014-11-11 06:11:36 +00:00
|
|
|
|
|
|
|
#if !defined (USE_RELIEFMAP)
|
2014-11-14 09:12:41 +00:00
|
|
|
float div = 1.0 / (1.0 + (prevTexDepth - texDepth) * float(linearSearchSteps));
|
|
|
|
bestDepth -= (depth - size - prevTexDepth) * div;
|
2014-11-11 06:11:36 +00:00
|
|
|
#else
|
2012-12-04 03:05:34 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2014-11-11 06:11:36 +00:00
|
|
|
#endif
|
2012-12-04 03:05:34 +00:00
|
|
|
|
|
|
|
return bestDepth;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-12-09 05:23:50 +00:00
|
|
|
vec3 CalcDiffuse(vec3 diffuseAlbedo, float EH, float NH, float roughness)
|
2012-12-04 03:05:34 +00:00
|
|
|
{
|
2015-12-03 10:44:33 +00:00
|
|
|
#if defined(USE_BURLEY)
|
|
|
|
// modified from https://disney-animation.s3.amazonaws.com/library/s2012_pbs_disney_brdf_notes_v2.pdf
|
2015-12-09 05:23:50 +00:00
|
|
|
float fd90 = -0.5 + EH * EH * roughness;
|
2015-12-03 10:44:33 +00:00
|
|
|
float burley = 1.0 + fd90 * 0.04 / NH;
|
|
|
|
burley *= burley;
|
|
|
|
return diffuseAlbedo * burley;
|
2013-09-16 07:54:26 +00:00
|
|
|
#else
|
2015-12-03 10:44:33 +00:00
|
|
|
return diffuseAlbedo;
|
2013-09-16 07:54:26 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-12-09 05:23:50 +00:00
|
|
|
vec3 EnvironmentBRDF(float roughness, float NE, vec3 specular)
|
2013-09-16 07:54:26 +00:00
|
|
|
{
|
2015-12-03 10:44:33 +00:00
|
|
|
// from http://community.arm.com/servlet/JiveServlet/download/96891546-19496/siggraph2015-mmg-renaldas-slides.pdf
|
2015-12-09 05:23:50 +00:00
|
|
|
float v = 1.0 - max(roughness, NE);
|
2015-12-03 10:44:33 +00:00
|
|
|
v *= v * v;
|
|
|
|
return vec3(v) + specular;
|
2013-09-16 07:54:26 +00:00
|
|
|
}
|
2012-12-04 03:05:34 +00:00
|
|
|
|
2015-12-09 05:23:50 +00:00
|
|
|
vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float roughness)
|
2013-09-16 07:54:26 +00:00
|
|
|
{
|
2015-12-03 10:44:33 +00:00
|
|
|
// from http://community.arm.com/servlet/JiveServlet/download/96891546-19496/siggraph2015-mmg-renaldas-slides.pdf
|
2015-12-09 05:23:50 +00:00
|
|
|
float rr = roughness*roughness;
|
2015-12-03 10:44:33 +00:00
|
|
|
float rrrr = rr*rr;
|
|
|
|
float d = (NH * NH) * (rrrr - 1.0) + 1.0;
|
2015-12-09 05:23:50 +00:00
|
|
|
float v = (EH * EH) * (roughness + 0.5);
|
2015-12-03 10:44:33 +00:00
|
|
|
return specular * (rrrr / (4.0 * d * d * v));
|
2012-12-04 03:05:34 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 06:41:04 +00:00
|
|
|
|
2013-12-13 05:38:01 +00:00
|
|
|
float CalcLightAttenuation(float point, float normDist)
|
2013-09-17 06:41:04 +00:00
|
|
|
{
|
2013-12-13 05:38:01 +00:00
|
|
|
// zero light at 1.0, approximating q3 style
|
2013-09-17 06:41:04 +00:00
|
|
|
// also don't attenuate directional light
|
2013-12-13 05:38:01 +00:00
|
|
|
float attenuation = (0.5 * normDist - 1.5) * point + 1.0;
|
|
|
|
|
2013-09-17 06:41:04 +00:00
|
|
|
// 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
|
2013-12-13 05:38:01 +00:00
|
|
|
|
2013-09-17 06:41:04 +00:00
|
|
|
return attenuation;
|
|
|
|
}
|
|
|
|
|
2013-11-05 05:53:05 +00:00
|
|
|
// from http://www.thetenthplanet.de/archives/1180
|
|
|
|
mat3 cotangent_frame( vec3 N, vec3 p, vec2 uv )
|
|
|
|
{
|
2013-12-13 05:38:01 +00:00
|
|
|
// 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, N );
|
|
|
|
vec3 dp1perp = cross( N, dp1 );
|
|
|
|
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
|
|
|
|
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
|
|
|
|
|
|
|
|
// construct a scale-invariant frame
|
|
|
|
float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );
|
|
|
|
return mat3( T * invmax, B * invmax, N );
|
2013-11-05 05:53:05 +00:00
|
|
|
}
|
2013-09-17 06:41:04 +00:00
|
|
|
|
2012-12-04 03:05:34 +00:00
|
|
|
void main()
|
|
|
|
{
|
2014-04-16 12:26:03 +00:00
|
|
|
vec3 viewDir, lightColor, ambientColor;
|
2013-09-16 07:54:26 +00:00
|
|
|
vec3 L, N, E, H;
|
2014-04-16 12:26:03 +00:00
|
|
|
float NL, NH, NE, EH, attenuation;
|
2013-12-13 05:38:01 +00:00
|
|
|
|
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2013-11-05 05:53:05 +00:00
|
|
|
#if defined(USE_VERT_TANGENT_SPACE)
|
2013-09-18 06:52:40 +00:00
|
|
|
mat3 tangentToWorld = mat3(var_Tangent.xyz, var_Bitangent.xyz, var_Normal.xyz);
|
2014-03-16 23:29:38 +00:00
|
|
|
viewDir = vec3(var_Normal.w, var_Tangent.w, var_Bitangent.w);
|
2013-11-05 05:53:05 +00:00
|
|
|
#else
|
|
|
|
mat3 tangentToWorld = cotangent_frame(var_Normal, -var_ViewDir, var_TexCoords.xy);
|
2014-03-16 23:29:38 +00:00
|
|
|
viewDir = var_ViewDir;
|
2013-11-05 05:53:05 +00:00
|
|
|
#endif
|
2012-12-04 03:05:34 +00:00
|
|
|
|
2014-03-16 23:29:38 +00:00
|
|
|
E = normalize(viewDir);
|
2013-09-16 07:54:26 +00:00
|
|
|
|
2013-12-13 05:38:01 +00:00
|
|
|
L = var_LightDir.xyz;
|
|
|
|
#if defined(USE_DELUXEMAP)
|
|
|
|
L += (texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(0.5)) * u_EnableTextures.y;
|
2013-11-05 05:53:05 +00:00
|
|
|
#endif
|
2013-12-13 05:38:01 +00:00
|
|
|
float sqrLightDist = dot(L, L);
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_LIGHTMAP)
|
2014-04-16 12:26:03 +00:00
|
|
|
vec4 lightmapColor = texture2D(u_LightMap, var_TexCoords.zw);
|
2013-09-16 12:57:14 +00:00
|
|
|
#if defined(RGBM_LIGHTMAP)
|
2014-04-16 12:26:03 +00:00
|
|
|
lightmapColor.rgb *= lightmapColor.a;
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
#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)
|
2014-11-14 09:12:41 +00:00
|
|
|
vec3 offsetDir = viewDir * tangentToWorld;
|
2012-12-04 03:05:34 +00:00
|
|
|
|
2014-03-04 05:02:39 +00:00
|
|
|
offsetDir.xy *= -u_NormalScale.a / 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_LIGHT) && !defined(USE_FAST_LIGHT)
|
2014-04-16 12:26:03 +00:00
|
|
|
#if defined(USE_LIGHTMAP)
|
|
|
|
lightColor = lightmapColor.rgb * var_Color.rgb;
|
|
|
|
ambientColor = vec3(0.0);
|
|
|
|
attenuation = 1.0;
|
|
|
|
#elif defined(USE_LIGHT_VECTOR)
|
|
|
|
lightColor = u_DirectedLight * var_Color.rgb;
|
|
|
|
ambientColor = u_AmbientLight * var_Color.rgb;
|
|
|
|
attenuation = CalcLightAttenuation(float(var_LightDir.w > 0.0), var_LightDir.w / sqrLightDist);
|
|
|
|
#elif defined(USE_LIGHT_VERTEX)
|
|
|
|
lightColor = var_Color.rgb;
|
|
|
|
ambientColor = vec3(0.0);
|
|
|
|
attenuation = 1.0;
|
|
|
|
#endif
|
|
|
|
|
2016-02-02 05:37:23 +00:00
|
|
|
#if defined(USE_PBR)
|
|
|
|
lightColor = pow(lightColor, vec3(2.2));
|
|
|
|
ambientColor = pow(ambientColor, vec3(2.2));
|
2015-12-09 05:23:50 +00:00
|
|
|
#endif
|
|
|
|
|
2013-09-16 07:54:26 +00:00
|
|
|
#if defined(USE_NORMALMAP)
|
|
|
|
#if defined(SWIZZLE_NORMALMAP)
|
2013-11-05 06:50:53 +00:00
|
|
|
N.xy = texture2D(u_NormalMap, texCoords).ag - vec2(0.5);
|
2013-09-16 07:54:26 +00:00
|
|
|
#else
|
2013-11-05 06:50:53 +00:00
|
|
|
N.xy = texture2D(u_NormalMap, texCoords).rg - vec2(0.5);
|
2013-09-16 07:54:26 +00:00
|
|
|
#endif
|
2014-03-04 05:02:39 +00:00
|
|
|
N.xy *= u_NormalScale.xy;
|
2014-03-04 01:32:07 +00:00
|
|
|
N.z = sqrt(clamp((0.25 - N.x * N.x) - N.y * N.y, 0.0, 1.0));
|
2013-12-13 05:38:01 +00:00
|
|
|
N = tangentToWorld * N;
|
2013-09-16 07:54:26 +00:00
|
|
|
#else
|
2013-12-13 05:38:01 +00:00
|
|
|
N = var_Normal.xyz;
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
2013-12-13 05:38:01 +00:00
|
|
|
|
|
|
|
N = normalize(N);
|
|
|
|
L /= sqrt(sqrLightDist);
|
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-12-13 05:38:01 +00:00
|
|
|
shadowValue *= float(dot(var_Normal.xyz, var_PrimaryLightDir.xyz) > 0.0);
|
|
|
|
|
2013-04-02 07:17:24 +00:00
|
|
|
#if defined(SHADOWMAP_MODULATE)
|
2015-12-03 10:44:33 +00:00
|
|
|
lightColor *= shadowValue * (1.0 - u_PrimaryLightAmbient.r) + u_PrimaryLightAmbient.r;
|
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)
|
2014-04-16 12:26:03 +00:00
|
|
|
ambientColor = lightColor;
|
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
|
|
|
|
|
|
|
// 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
|
2016-02-02 05:37:23 +00:00
|
|
|
ambientColor = max(ambientColor - lightColor * surfNL, vec3(0.0));
|
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)
|
2014-03-04 05:02:39 +00:00
|
|
|
vec4 specular = texture2D(u_SpecularMap, texCoords);
|
|
|
|
#else
|
|
|
|
vec4 specular = vec4(1.0);
|
2013-09-16 07:54:26 +00:00
|
|
|
#endif
|
|
|
|
|
2014-03-04 05:02:39 +00:00
|
|
|
specular *= u_SpecularScale;
|
2013-12-13 05:38:01 +00:00
|
|
|
|
2016-02-02 05:37:23 +00:00
|
|
|
#if defined(USE_PBR)
|
|
|
|
diffuse.rgb = pow(diffuse.rgb, vec3(2.2));
|
2014-04-16 12:26:03 +00:00
|
|
|
#endif
|
|
|
|
|
2013-09-16 07:54:26 +00:00
|
|
|
float gloss = specular.a;
|
2016-02-02 05:37:23 +00:00
|
|
|
#if defined(USE_PBR)
|
|
|
|
float roughness = 1.0 - specular.r;
|
2015-12-09 05:23:50 +00:00
|
|
|
#else
|
|
|
|
float roughness = exp2(-3.0 * gloss);
|
|
|
|
#endif
|
2013-09-16 07:54:26 +00:00
|
|
|
|
2016-02-02 05:37:23 +00:00
|
|
|
#if defined(USE_PBR)
|
2015-12-03 10:44:33 +00:00
|
|
|
// diffuse is actually base color, and green of specular is metallicness
|
|
|
|
float metallic = specular.g;
|
2013-12-13 05:38:01 +00:00
|
|
|
|
2015-12-03 10:44:33 +00:00
|
|
|
specular.rgb = metallic * diffuse.rgb + vec3(0.04 - 0.04 * metallic);
|
2013-09-16 07:54:26 +00:00
|
|
|
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-12-13 05:38:01 +00:00
|
|
|
|
2015-12-09 05:23:50 +00:00
|
|
|
reflectance = CalcDiffuse(diffuse.rgb, EH, NH, roughness);
|
2013-12-13 05:38:01 +00:00
|
|
|
|
2014-04-16 12:26:03 +00:00
|
|
|
gl_FragColor.rgb = lightColor * reflectance * (attenuation * NL);
|
2016-01-15 10:46:30 +00:00
|
|
|
gl_FragColor.rgb += ambientColor * diffuse.rgb;
|
2013-12-13 05:38:01 +00:00
|
|
|
|
2013-09-16 07:54:26 +00:00
|
|
|
#if defined(USE_CUBEMAP)
|
2015-12-09 05:23:50 +00:00
|
|
|
reflectance = EnvironmentBRDF(roughness, NE, specular.rgb);
|
2012-12-04 03:05:34 +00:00
|
|
|
|
2013-09-16 07:54:26 +00:00
|
|
|
vec3 R = reflect(E, N);
|
2012-12-04 03:05:34 +00:00
|
|
|
|
2014-03-16 23:29:38 +00:00
|
|
|
// parallax corrected cubemap (cheaper trick)
|
|
|
|
// from http://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
|
2014-04-16 12:26:03 +00:00
|
|
|
vec3 parallax = u_CubeMapInfo.xyz + u_CubeMapInfo.w * viewDir;
|
2014-03-16 23:29:38 +00:00
|
|
|
|
2016-02-02 05:37:23 +00:00
|
|
|
#if defined(USE_PBR)
|
2015-12-09 11:42:12 +00:00
|
|
|
vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, 7.0 * roughness).rgb * u_EnableTextures.w;
|
2015-12-09 05:23:50 +00:00
|
|
|
#else
|
2014-04-16 12:26:03 +00:00
|
|
|
vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w;
|
2015-12-09 05:23:50 +00:00
|
|
|
#endif
|
2013-09-16 07:54:26 +00:00
|
|
|
|
2014-04-16 12:26:03 +00:00
|
|
|
// normalize cubemap based on lowest mip (~diffuse)
|
|
|
|
// multiplying cubemap values by lighting below depends on either this or the cubemap being normalized at generation
|
|
|
|
//vec3 cubeLightDiffuse = max(textureCubeLod(u_CubeMap, N, 6.0).rgb, 0.5 / 255.0);
|
|
|
|
//cubeLightColor /= dot(cubeLightDiffuse, vec3(0.2125, 0.7154, 0.0721));
|
|
|
|
|
2016-02-02 05:37:23 +00:00
|
|
|
#if defined(USE_PBR)
|
|
|
|
cubeLightColor = pow(cubeLightColor, vec3(2.2));
|
2014-04-16 12:26:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// multiply cubemap values by lighting
|
|
|
|
// not technically correct, but helps make reflections look less unnatural
|
|
|
|
//cubeLightColor *= lightColor * (attenuation * NL) + ambientColor;
|
2013-12-13 05:38:01 +00:00
|
|
|
|
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
|
|
|
|
2015-12-09 13:25:58 +00:00
|
|
|
#if defined(USE_PRIMARY_LIGHT) || defined(SHADOWMAP_MODULATE)
|
2013-12-13 05:38:01 +00:00
|
|
|
vec3 L2, H2;
|
|
|
|
float NL2, EH2, NH2;
|
2013-04-02 07:17:24 +00:00
|
|
|
|
2013-12-13 05:38:01 +00:00
|
|
|
L2 = var_PrimaryLightDir.xyz;
|
2013-09-16 07:54:26 +00:00
|
|
|
|
2013-12-13 05:38:01 +00:00
|
|
|
// enable when point lights are supported as primary lights
|
|
|
|
//sqrLightDist = dot(L2, L2);
|
|
|
|
//L2 /= sqrt(sqrLightDist);
|
|
|
|
|
|
|
|
NL2 = clamp(dot(N, L2), 0.0, 1.0);
|
|
|
|
|
|
|
|
H2 = normalize(L2 + E);
|
|
|
|
EH2 = clamp(dot(E, H2), 0.0, 1.0);
|
|
|
|
NH2 = clamp(dot(N, H2), 0.0, 1.0);
|
|
|
|
|
2015-12-09 13:25:58 +00:00
|
|
|
reflectance = CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, roughness);
|
|
|
|
|
|
|
|
// bit of a hack, with modulated shadowmaps, ignore diffuse
|
|
|
|
#if !defined(SHADOWMAP_MODULATE)
|
|
|
|
reflectance += CalcDiffuse(diffuse.rgb, EH2, NH2, roughness);
|
|
|
|
#endif
|
2013-12-13 05:38:01 +00:00
|
|
|
|
2015-12-09 13:25:58 +00:00
|
|
|
lightColor = u_PrimaryLightColor;
|
2013-12-13 05:38:01 +00:00
|
|
|
|
2016-02-02 05:37:23 +00:00
|
|
|
#if defined(USE_PBR)
|
|
|
|
lightColor = pow(lightColor, vec3(2.2));
|
2014-04-16 12:26:03 +00:00
|
|
|
#endif
|
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
|
|
|
|
2014-04-16 12:26:03 +00:00
|
|
|
// enable when point lights are supported as primary lights
|
|
|
|
//lightColor *= CalcLightAttenuation(float(u_PrimaryLightDir.w > 0.0), u_PrimaryLightDir.w / sqrLightDist);
|
|
|
|
|
2013-12-13 05:38:01 +00:00
|
|
|
gl_FragColor.rgb += lightColor * reflectance * NL2;
|
2013-09-16 07:54:26 +00:00
|
|
|
#endif
|
2016-02-02 05:37:23 +00:00
|
|
|
|
|
|
|
#if defined(USE_PBR)
|
|
|
|
gl_FragColor.rgb = pow(gl_FragColor.rgb, vec3(1.0 / 2.2));
|
|
|
|
#endif
|
|
|
|
|
2013-05-03 07:32:59 +00:00
|
|
|
#else
|
2014-04-16 12:26:03 +00:00
|
|
|
lightColor = var_Color.rgb;
|
|
|
|
|
2013-09-16 07:54:26 +00:00
|
|
|
#if defined(USE_LIGHTMAP)
|
2014-04-16 12:26:03 +00:00
|
|
|
lightColor *= lightmapColor.rgb;
|
2013-09-16 07:54:26 +00:00
|
|
|
#endif
|
2014-04-16 12:26:03 +00:00
|
|
|
|
|
|
|
gl_FragColor.rgb = diffuse.rgb * lightColor;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
gl_FragColor.a = diffuse.a * var_Color.a;
|
2012-12-04 03:05:34 +00:00
|
|
|
}
|