Skip to content

Commit de6c5d7

Browse files
committed
Don't use reflective specular for dynamic lights
It's wrong because in the reflection cubemaps, the world geometry is already pre-multiplied with the world lighting. For example if the location were completely dark but you added a static light, the specular reflection of the light should appear (but wouldn't previously). Fixes #1358.
1 parent 68bf1ee commit de6c5d7

File tree

3 files changed

+21
-56
lines changed

3 files changed

+21
-56
lines changed

src/engine/renderer/glsl_source/computeLight_fp.glsl

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3232
#endif
3333

3434
#if defined(USE_REFLECTIVE_SPECULAR)
35-
uniform samplerCube u_EnvironmentMap0;
36-
uniform samplerCube u_EnvironmentMap1;
37-
uniform float u_EnvironmentInterpolation;
35+
uniform samplerCube u_EnvironmentMap0;
36+
uniform samplerCube u_EnvironmentMap1;
37+
uniform float u_EnvironmentInterpolation;
38+
39+
// Only the RGB components are meaningful
40+
// FIXME: using reflective specular will always globally decrease the scene brightness
41+
// because we're multiplying with something that can only be less than 1.
42+
vec4 EnvironmentalSpecularFactor( vec3 viewDir, vec3 normal )
43+
{
44+
vec4 envColor0 = textureCube(u_EnvironmentMap0, reflect( -viewDir, normal ) );
45+
vec4 envColor1 = textureCube(u_EnvironmentMap1, reflect( -viewDir, normal ) );
46+
return mix( envColor0, envColor1, u_EnvironmentInterpolation );
47+
}
3848
#endif // USE_REFLECTIVE_SPECULAR
3949

4050
// lighting helper functions
@@ -59,15 +69,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
5969
#endif
6070

6171
#if defined(USE_DELUXE_MAPPING) || defined(USE_GRID_DELUXE_MAPPING) || (defined(r_realtimeLighting) && r_realtimeLightingRenderer == 1)
62-
#if defined(USE_REFLECTIVE_SPECULAR)
63-
void computeDeluxeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
64-
vec4 diffuseColor, vec4 materialColor,
65-
inout vec4 color, in samplerCube u_EnvironmentMap0, in samplerCube u_EnvironmentMap1 )
66-
#else // !USE_REFLECTIVE_SPECULAR
6772
void computeDeluxeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
6873
vec4 diffuseColor, vec4 materialColor,
6974
inout vec4 color )
70-
#endif // !USE_REFLECTIVE_SPECULAR
7175
{
7276
vec3 H = normalize( lightDir + viewDir );
7377

@@ -122,15 +126,6 @@ void computeDeluxeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightCol
122126
color.a = mix( diffuseColor.a, 1.0, FexpNV );
123127

124128
#else // !USE_PHYSICAL_MAPPING
125-
126-
#if defined(USE_REFLECTIVE_SPECULAR)
127-
// not implemented for PBR yet
128-
vec4 envColor0 = textureCube(u_EnvironmentMap0, reflect( -viewDir, normal ) );
129-
vec4 envColor1 = textureCube(u_EnvironmentMap1, reflect( -viewDir, normal ) );
130-
131-
materialColor.rgb *= mix( envColor0, envColor1, u_EnvironmentInterpolation ).rgb;
132-
#endif // USE_REFLECTIVE_SPECULAR
133-
134129
color.rgb += lightColor.rgb * NdotL * diffuseColor.rgb;
135130
#if defined(r_specularMapping)
136131
color.rgb += computeSpecularity(lightColor.rgb, materialColor, NdotH);
@@ -164,13 +159,8 @@ layout(std140) uniform u_Lights {
164159

165160
uniform int u_numLights;
166161

167-
#if defined(USE_REFLECTIVE_SPECULAR)
168-
void computeDynamicLight( uint idx, vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse,
169-
vec4 material, inout vec4 color, in samplerCube u_EnvironmentMap0, in samplerCube u_EnvironmentMap1 )
170-
#else // !USE_REFLECTIVE_SPECULAR
171162
void computeDynamicLight( uint idx, vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse,
172163
vec4 material, inout vec4 color )
173-
#endif // !USE_REFLECTIVE_SPECULAR
174164
{
175165
Light light = GetLight( idx );
176166
vec3 L;
@@ -202,15 +192,9 @@ void computeDynamicLight( uint idx, vec3 P, vec3 normal, vec3 viewDir, vec4 diff
202192
attenuation = 1.0;
203193
}
204194

205-
#if defined(USE_REFLECTIVE_SPECULAR)
206-
computeDeluxeLight( L, normal, viewDir,
207-
attenuation * attenuation * light.color,
208-
diffuse, material, color, u_EnvironmentMap0, u_EnvironmentMap1 );
209-
#else // !USE_REFLECTIVE_SPECULAR
210-
computeDeluxeLight( L, normal, viewDir,
211-
attenuation * attenuation * light.color,
212-
diffuse, material, color );
213-
#endif // !USE_REFLECTIVE_SPECULAR
195+
computeDeluxeLight(
196+
L, normal, viewDir, attenuation * attenuation * light.color,
197+
diffuse, material, color );
214198
}
215199

216200
const int lightsPerLayer = 16;
@@ -230,14 +214,8 @@ uint nextIdx( in uint count, in idxs_t idxs ) {
230214
return ( idxs[count / 4] >> ( 8 * ( count % 4 ) ) ) & 0xFFu;
231215
}
232216

233-
#if defined(USE_REFLECTIVE_SPECULAR)
234-
void computeDynamicLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4 material,
235-
inout vec4 color, in usampler3D u_LightTiles,
236-
in samplerCube u_EnvironmentMap0, in samplerCube u_EnvironmentMap1 )
237-
#else // !USE_REFLECTIVE_SPECULAR
238217
void computeDynamicLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4 material,
239218
inout vec4 color, in usampler3D u_LightTiles )
240-
#endif // !USE_REFLECTIVE_SPECULAR
241219
{
242220
if( u_numLights == 0 ) {
243221
return;
@@ -259,13 +237,8 @@ void computeDynamicLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4
259237
/* Light IDs are stored relative to the layer
260238
Subtract 1 because 0 means there's no light */
261239
idx = ( idx - 1 ) * NUM_LIGHT_LAYERS + layer;
262-
263-
#if defined(USE_REFLECTIVE_SPECULAR)
264-
computeDynamicLight( idx, P, normal, viewDir, diffuse, material, color, u_EnvironmentMap0, u_EnvironmentMap1 );
265-
#else // !USE_REFLECTIVE_SPECULAR
266-
computeDynamicLight( idx, P, normal, viewDir, diffuse, material, color );
267-
#endif // !USE_REFLECTIVE_SPECULAR
268240

241+
computeDynamicLight( idx, P, normal, viewDir, diffuse, material, color );
269242
lightCount++;
270243
}
271244
}

src/engine/renderer/glsl_source/lightMapping_fp.glsl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ void main()
184184
// Blend static light.
185185
#if defined(USE_DELUXE_MAPPING) || defined(USE_GRID_DELUXE_MAPPING)
186186
#if defined(USE_REFLECTIVE_SPECULAR)
187-
computeDeluxeLight(lightDir, normal, viewDir, lightColor, diffuse, material, color, u_EnvironmentMap0, u_EnvironmentMap1);
187+
vec4 modifiedSpecular = material * EnvironmentalSpecularFactor(viewDir, normal);
188+
computeDeluxeLight(lightDir, normal, viewDir, lightColor, diffuse, modifiedSpecular, color);
188189
#else // !USE_REFLECTIVE_SPECULAR
189190
computeDeluxeLight(lightDir, normal, viewDir, lightColor, diffuse, material, color);
190191
#endif // !USE_REFLECTIVE_SPECULAR
@@ -194,12 +195,7 @@ void main()
194195

195196
// Blend dynamic lights.
196197
#if defined(r_realtimeLighting) && r_realtimeLightingRenderer == 1
197-
#if defined(USE_REFLECTIVE_SPECULAR)
198-
computeDynamicLights(var_Position, normal, viewDir, diffuse, material, color, u_LightTiles,
199-
u_EnvironmentMap0, u_EnvironmentMap1);
200-
#else // !USE_REFLECTIVE_SPECULAR
201-
computeDynamicLights(var_Position, normal, viewDir, diffuse, material, color, u_LightTiles);
202-
#endif // !USE_REFLECTIVE_SPECULAR
198+
computeDynamicLights(var_Position, normal, viewDir, diffuse, material, color, u_LightTiles);
203199
#endif
204200

205201
// Add Rim Lighting to highlight the edges on model entities.

src/engine/renderer/glsl_source/liquid_fp.glsl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,7 @@ void main()
145145

146146
// compute the specular term
147147
#if defined(USE_DELUXE_MAPPING) || defined(USE_GRID_DELUXE_MAPPING)
148-
#if defined(USE_REFLECTIVE_SPECULAR)
149-
computeDeluxeLight(lightDir, normal, viewDir, lightColor, diffuse, reflectColor, color, u_EnvironmentMap0, u_EnvironmentMap1);
150-
#else // !USE_REFLECTIVE_SPECULAR
151-
computeDeluxeLight(lightDir, normal, viewDir, lightColor, diffuse, reflectColor, color);
152-
#endif // !USE_REFLECTIVE_SPECULAR
148+
computeDeluxeLight(lightDir, normal, viewDir, lightColor, diffuse, reflectColor, color);
153149
#else // !USE_DELUXE_MAPPING && !USE_GRID_DELUXE_MAPPING
154150
computeLight(lightColor, diffuse, color);
155151
#endif // !USE_DELUXE_MAPPING && !USE_GRID_DELUXE_MAPPING

0 commit comments

Comments
 (0)