Permalink
Browse files

Lightmapping fixes.

  • Loading branch information...
gimhael committed Feb 5, 2016
1 parent 841fdda commit 48fabc93447a5fc4dc21359e2872cd71c621141d

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -111,9 +111,12 @@ void main()
// compute light color from world space lightmap
vec3 lightColor = texture2D(u_LightMap, var_TexLight).rgb;
// divide by cosine term to restore original light color
lightColor /= clamp(dot(normalize(var_Normal), L), 0.004, 1.0);
// compute final color
vec4 color = vec4( 0.0, 0.0, 0.0, 1.0 );
computeLight( L, L, N, I, lightColor, diffuse, specular, color );
vec4 color = vec4( 0.0, 0.0, 0.0, diffuse.a );
computeLight( L, N, I, lightColor, diffuse, specular, color );
#else // USE_NORMAL_MAPPING
@@ -133,8 +136,8 @@ void main()
// compute light color from object space lightmap
vec3 lightColor = texture2D(u_LightMap, var_TexLight).rgb;
vec4 color = vec4( 0.0, 0.0, 0.0, 1.0 );
computeLight( N, N, N, I, lightColor, diffuse, specular, color );
vec4 color = vec4( 0.0, 0.0, 0.0, diffuse.a );
computeLight( N, N, I, lightColor, diffuse, specular, color );
#endif
#if defined( USE_SHADER_LIGHTS )
@@ -79,18 +79,18 @@ void pushIdxs(in int idx, inout vec4 idxs ) {
#define exportIdxs(x) gl_fragColor = ( x ) * (1.0/255.0)
#endif
bool lightOutsidePlane( in vec4 plane, inout vec3 center, inout float radius ) {
void lightOutsidePlane( in vec4 plane, inout vec3 center, inout float radius ) {
float dist = dot( plane, vec4( center, 1.0 ) );
if( dist >= radius )
return true; // light completely outside plane
if( dist >= radius ) {
radius = 0.0; // light completely outside plane
return;
}
if( dist >= 0.0 ) {
// light is outside plane, but intersects the volume
center = center - dist * plane.xyz;
radius = sqrt( radius * radius - dist * dist );
}
return false;
}
void main() {
@@ -106,23 +106,19 @@ void main() {
for( int i = u_lightLayer; i < u_numLights; i += numLayers ) {
vec3 center = ( u_ModelMatrix * vec4( lights[ i ].center, 1.0 ) ).xyz;
float radius = lights[ i ].radius;
float radius = 2.0 * lights[ i ].radius;
// todo: better checks for spotlights
if( lightOutsidePlane( plane1, center, radius ) )
continue;
if( lightOutsidePlane( plane2, center, radius ) )
continue;
if( lightOutsidePlane( plane3, center, radius ) )
continue;
if( lightOutsidePlane( plane4, center, radius ) )
continue;
if( lightOutsidePlane( plane5, center, radius ) )
continue;
if( lightOutsidePlane( plane6, center, radius ) )
continue;
pushIdxs( i, idxs );
lightOutsidePlane( plane1, center, radius );
lightOutsidePlane( plane2, center, radius );
lightOutsidePlane( plane3, center, radius );
lightOutsidePlane( plane4, center, radius );
lightOutsidePlane( plane5, center, radius );
lightOutsidePlane( plane6, center, radius );
if( radius > 0.0 ) {
pushIdxs( i, idxs );
}
}
exportIdxs( idxs );
@@ -52,17 +52,24 @@ uniform int u_numLights;
uniform vec2 u_SpecularExponent;
// lighting helper functions
void computeLight( vec3 lightDir, vec3 normalDiff, vec3 normalSpec,
vec3 eyeDir, vec3 lightColor,
void computeLight( vec3 lightDir, vec3 normal, vec3 eyeDir, vec3 lightColor,
vec4 diffuseColor, vec4 specularColor,
inout vec4 accumulator ) {
vec3 H = normalize( lightDir + eyeDir );
float NdotL = clamp( dot( normalDiff, lightDir ), 0.0, 1.0 );
float NdotH = clamp( dot( normalSpec, H ), 0.0, 1.0 );
float NdotL = dot( normal, lightDir );
#if defined(r_HalfLambertLighting)
// http://developer.valvesoftware.com/wiki/Half_Lambert
NdotL = NdotL * 0.5 + 0.5;
NdotL *= NdotL;
#elif defined(r_WrapAroundLighting)
NdotL = clamp( NdotL + r_WrapAroundLighting, 0.0, 1.0) / clamp(1.0 + r_WrapAroundLighting, 0.0, 1.0);
#else
NdotL = clamp( NdotL, 0.0, 1.0 );
#endif
float NdotH = clamp( dot( normal, H ), 0.0, 1.0 );
accumulator.xyz += diffuseColor.xyz * lightColor.xyz * NdotL;
accumulator.xyz += specularColor.xyz * lightColor.xyz * pow( NdotH, u_SpecularExponent.x * specularColor.w + u_SpecularExponent.y) * r_SpecularScale;
accumulator.w *= diffuseColor.w;
}
#if defined( USE_SHADER_LIGHTS )
@@ -100,7 +107,7 @@ void computeDLight( int idx, vec3 P, vec3 N, vec3 I, vec4 diffuse,
vec4 specular, inout vec4 color ) {
vec3 L = lights[idx].center - P;
float attenuation = 1.0 / (1.0 + 8.0 * length(L) / lights[idx].radius);
computeLight( normalize( L ), N, N, I,
computeLight( normalize( L ), N, I,
attenuation * attenuation * lights[idx].color,
diffuse, specular, color );
}
@@ -181,8 +181,8 @@ void main()
#endif
// compute final color
vec4 color = vec4(ambCol * r_AmbientScale * diffuse.xyz, 1.0);
computeLight( L, N, N, V, lgtCol, diffuse, specBase, color );
vec4 color = vec4(ambCol * r_AmbientScale * diffuse.xyz, diffuse.a);
computeLight( L, N, V, lgtCol, diffuse, specBase, color );
#ifdef USE_SHADER_LIGHTS
computeDLights( var_Position, N, V, diffuse, specBase, color );
@@ -135,8 +135,8 @@ void main()
N = normalize(N);
// compute final color
vec4 color = vec4( ambCol, 1.0 );
computeLight( L, N, N, V, dirCol, diffuse, specular, color );
vec4 color = vec4( ambCol, diffuse.a );
computeLight( L, N, V, dirCol, diffuse, specular, color );
#if defined(USE_GLOW_MAPPING)
color.rgb += texture2D(u_GlowMap, texGlow).rgb;
@@ -158,8 +158,8 @@ void main()
vec4 specular = vec4(0.0);
vec4 color = vec4( 0.0, 0.0, 0.0, 1.0 );
computeLight( N, N, N, N, vec3(1.0), diffuse, specular, color );
vec4 color = vec4( 0.0, 0.0, 0.0, diffuse.a );
computeLight( N, N, N, vec3(1.0), diffuse, specular, color );
#if defined(USE_GLOW_MAPPING)
color.rgb += texture2D(u_GlowMap, texGlow).rgb;

0 comments on commit 48fabc9

Please sign in to comment.