diff --git a/src/engine/renderer/glsl_source/computeLight_fp.glsl b/src/engine/renderer/glsl_source/computeLight_fp.glsl index 9932cc37fe..7843001f06 100644 --- a/src/engine/renderer/glsl_source/computeLight_fp.glsl +++ b/src/engine/renderer/glsl_source/computeLight_fp.glsl @@ -21,6 +21,12 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ // computeLight_fp.glsl - Light computing helper functions +#if defined(USE_REFLECTIVE_SPECULAR) +uniform samplerCube u_EnvironmentMap0; +uniform samplerCube u_EnvironmentMap1; +uniform float u_EnvironmentInterpolation; +#endif // USE_REFLECTIVE_SPECULAR + struct light { vec4 center_radius; vec4 color_type; @@ -49,6 +55,14 @@ uniform int u_numLights; uniform vec2 u_SpecularExponent; // lighting helper functions + +void ReadLightGrid(in vec4 texel, out vec3 ambientColor, out vec3 lightColor) { + float ambientScale = 2.0 * texel.a; + float directedScale = 2.0 - ambientScale; + ambientColor = ambientScale * texel.rgb; + lightColor = directedScale * texel.rgb; +} + void computeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor, vec4 diffuseColor, vec4 materialColor, inout vec4 color ) { @@ -101,6 +115,14 @@ void computeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor, NdotL = clamp( NdotL, 0.0, 1.0 ); #endif +#if defined(USE_REFLECTIVE_SPECULAR) + // not implemented for PBR yet + vec4 envColor0 = textureCube(u_EnvironmentMap0, reflect(-viewDir, normal)); + vec4 envColor1 = textureCube(u_EnvironmentMap1, reflect(-viewDir, normal)); + + materialColor.rgb *= mix(envColor0, envColor1, u_EnvironmentInterpolation).rgb; +#endif // USE_REFLECTIVE_SPECULAR + color.rgb += diffuseColor.rgb * lightColor.rgb * NdotL; #if defined(r_specularMapping) && !defined(USE_PHYSICAL_SHADING) color.rgb += materialColor.rgb * lightColor.rgb * pow( NdotH, u_SpecularExponent.x * materialColor.a + u_SpecularExponent.y) * r_SpecularScale; diff --git a/src/engine/renderer/glsl_source/lightMapping_fp.glsl b/src/engine/renderer/glsl_source/lightMapping_fp.glsl index a420c61b3c..446552028c 100644 --- a/src/engine/renderer/glsl_source/lightMapping_fp.glsl +++ b/src/engine/renderer/glsl_source/lightMapping_fp.glsl @@ -61,15 +61,15 @@ void main() // compute the diffuse term vec4 diffuse = texture2D(u_DiffuseMap, texCoords); - // vertex blend operation like: alphaGen vertex - diffuse *= var_Color; - if( abs(diffuse.a + u_AlphaThreshold) <= 1.0 ) { discard; return; } + // vertex blend operation like: alphaGen vertex + diffuse *= var_Color; + // compute normal in world space from normalmap vec3 normal = NormalInWorldSpace(texCoords, tangentToWorldMatrix); @@ -85,7 +85,6 @@ void main() #if defined(USE_DELUXE_MAPPING) // compute light direction in world space vec4 deluxe = texture2D(u_DeluxeMap, var_TexLight); - vec3 lightDir = normalize(2.0 * deluxe.xyz - 1.0); // Lightmaps generated by q3map2 don't store the raw light value, but diff --git a/src/engine/renderer/glsl_source/liquid_fp.glsl b/src/engine/renderer/glsl_source/liquid_fp.glsl index 959700dc09..cec2099141 100644 --- a/src/engine/renderer/glsl_source/liquid_fp.glsl +++ b/src/engine/renderer/glsl_source/liquid_fp.glsl @@ -48,19 +48,6 @@ IN(smooth) vec3 var_Normal; DECLARE_OUTPUT(vec4) -void ReadLightGrid(in vec3 pos, out vec3 lightDir, - out vec3 ambientColor, out vec3 lightColor) { - vec4 texel1 = texture3D(u_LightGrid1, pos); - vec4 texel2 = texture3D(u_LightGrid2, pos); - - float ambientScale = 2.0 * texel1.a; - float directedScale = 2.0 - ambientScale; - ambientColor = ambientScale * texel1.rgb; - lightColor = directedScale * texel1.rgb; - - lightDir = normalize( texel2.rgb - (128.0 / 255.0) ); -} - void main() { // compute incident ray @@ -129,12 +116,15 @@ void main() color.rgb = mix(u_FogColor, color.rgb, fogFactor); } - vec3 lightDir; - vec3 ambientColor; - vec3 lightColor; + vec3 lightGridPos = (var_Position - u_LightGridOrigin) * u_LightGridScale; + + // compute light color from light grid + vec3 ambientColor, lightColor; + ReadLightGrid(texture3D(u_LightGrid1, lightGridPos), ambientColor, lightColor); - ReadLightGrid((var_Position - u_LightGridOrigin) * u_LightGridScale, - lightDir, ambientColor, lightColor); + // compute light direction in world space + vec4 texel = texture3D(u_LightGrid2, lightGridPos); + vec3 lightDir = normalize(texel.xyz - (128.0 / 255.0)); vec4 diffuse = vec4(0.0, 0.0, 0.0, 1.0); diff --git a/src/engine/renderer/glsl_source/vertexLighting_DBS_entity_fp.glsl b/src/engine/renderer/glsl_source/vertexLighting_DBS_entity_fp.glsl index e05a030c2a..ca3210adf8 100644 --- a/src/engine/renderer/glsl_source/vertexLighting_DBS_entity_fp.glsl +++ b/src/engine/renderer/glsl_source/vertexLighting_DBS_entity_fp.glsl @@ -26,12 +26,6 @@ uniform sampler2D u_DiffuseMap; uniform sampler2D u_MaterialMap; uniform sampler2D u_GlowMap; -#if defined(USE_REFLECTIVE_SPECULAR) -uniform samplerCube u_EnvironmentMap0; -uniform samplerCube u_EnvironmentMap1; -uniform float u_EnvironmentInterpolation; -#endif // USE_REFLECTIVE_SPECULAR - uniform float u_AlphaThreshold; uniform vec3 u_ViewOrigin; @@ -49,19 +43,6 @@ IN(smooth) vec3 var_Normal; DECLARE_OUTPUT(vec4) -void ReadLightGrid(in vec3 pos, out vec3 lightDir, - out vec3 ambientColor, out vec3 lightColor) { - vec4 texel1 = texture3D(u_LightGrid1, pos); - vec4 texel2 = texture3D(u_LightGrid2, pos); - - float ambientScale = 2.0 * texel1.a; - float directedScale = 2.0 - ambientScale; - ambientColor = ambientScale * texel1.rgb; - lightColor = directedScale * texel1.rgb; - - lightDir = normalize( texel2.rgb - (128.0 / 255.0) ); -} - void main() { // compute view direction in world space @@ -71,12 +52,15 @@ void main() mat3 tangentToWorldMatrix = mat3(var_Tangent.xyz, var_Binormal.xyz, var_Normal.xyz); - // compute light direction in world space - vec3 lightDir; - vec3 ambientColor; - vec3 lightColor; + vec3 lightGridPos = (var_Position - u_LightGridOrigin) * u_LightGridScale; - ReadLightGrid((var_Position - u_LightGridOrigin) * u_LightGridScale, lightDir, ambientColor, lightColor); + // compute light color from light grid + vec3 ambientColor, lightColor; + ReadLightGrid(texture3D(u_LightGrid1, lightGridPos), ambientColor, lightColor); + + // compute light direction in world space + vec4 texel = texture3D(u_LightGrid2, lightGridPos); + vec3 lightDir = normalize(texel.xyz - (128.0 / 255.0)); #if defined(USE_PARALLAX_MAPPING) // compute texcoords offset from heightmap @@ -88,29 +72,21 @@ void main() // compute the diffuse term vec4 diffuse = texture2D(u_DiffuseMap, texCoords); - // vertex blend operation like: alphaGen vertex - diffuse *= var_Color; - if( abs(diffuse.a + u_AlphaThreshold) <= 1.0 ) { discard; return; } + // vertex blend operation like: alphaGen vertex + diffuse *= var_Color; + // compute normal in world space from normalmap vec3 normal = NormalInWorldSpace(texCoords, tangentToWorldMatrix); // compute the material term vec4 material = texture2D(u_MaterialMap, texCoords); -#if defined(USE_REFLECTIVE_SPECULAR) && !defined(USE_PHYSICAL_MAPPING) - // not implemented for PBR yet - vec4 envColor0 = textureCube(u_EnvironmentMap0, reflect(-viewDir, normal)); - vec4 envColor1 = textureCube(u_EnvironmentMap1, reflect(-viewDir, normal)); - - material.rgb *= mix(envColor0, envColor1, u_EnvironmentInterpolation).rgb; -#endif // USE_REFLECTIVE_SPECULAR && !USE_PHYSICAL_MAPPING - // compute final color vec4 color = vec4(ambientColor * r_AmbientScale * diffuse.rgb, diffuse.a); diff --git a/src/engine/renderer/glsl_source/vertexLighting_DBS_world_fp.glsl b/src/engine/renderer/glsl_source/vertexLighting_DBS_world_fp.glsl index f40ef23823..ec781cad46 100644 --- a/src/engine/renderer/glsl_source/vertexLighting_DBS_world_fp.glsl +++ b/src/engine/renderer/glsl_source/vertexLighting_DBS_world_fp.glsl @@ -43,19 +43,6 @@ IN(smooth) vec3 var_Normal; DECLARE_OUTPUT(vec4) -void ReadLightGrid(in vec3 pos, out vec3 lightDir, - out vec3 ambientColor, out vec3 lightColor) { - vec4 texel1 = texture3D(u_LightGrid1, pos); - vec4 texel2 = texture3D(u_LightGrid2, pos); - - float ambientScale = 2.0 * texel1.a; - float directedScale = 2.0 - ambientScale; - ambientColor = ambientScale * texel1.rgb; - lightColor = directedScale * texel1.rgb; - - lightDir = normalize( texel2.rgb - (128.0 / 255.0) ); -} - void main() { // compute view direction in world space @@ -65,12 +52,15 @@ void main() mat3 tangentToWorldMatrix = mat3(var_Tangent.xyz, var_Binormal.xyz, var_Normal.xyz); - // compute light direction in world space - vec3 lightDir; - vec3 ambientColor; - vec3 lightColor; + vec3 lightGridPos = (var_Position - u_LightGridOrigin) * u_LightGridScale; + + // compute light color from light grid + vec3 ambientColor, lightColor; + ReadLightGrid(texture3D(u_LightGrid1, lightGridPos), ambientColor, lightColor); - ReadLightGrid((var_Position - u_LightGridOrigin) * u_LightGridScale, lightDir, ambientColor, lightColor); + // compute light direction in world space + vec4 texel = texture3D(u_LightGrid2, lightGridPos); + vec3 lightDir = normalize(texel.xyz - (128.0 / 255.0)); #if defined(USE_PARALLAX_MAPPING) // compute texcoords offset from heightmap @@ -82,15 +72,15 @@ void main() // compute the diffuse term vec4 diffuse = texture2D(u_DiffuseMap, texCoords); - // vertex blend operation like: alphaGen vertex - diffuse *= var_Color; - if( abs(diffuse.a + u_AlphaThreshold) <= 1.0 ) { discard; return; } + // vertex blend operation like: alphaGen vertex + diffuse *= var_Color; + // compute normal in world space from normalmap vec3 normal = NormalInWorldSpace(texCoords, tangentToWorldMatrix);