From 1116df31c9150c4671178c82beb8ed1059660242 Mon Sep 17 00:00:00 2001 From: James Khan Date: Thu, 30 May 2019 15:31:18 +0100 Subject: [PATCH] Add fog to Single-Pass lighting. --- .../Common/MatDefs/Light/Lighting.j3md | 6 ++++ .../Common/MatDefs/Light/SPLighting.frag | 35 +++++++++++++++++++ .../Common/MatDefs/Light/SPLighting.vert | 11 +++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md b/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md index dc3fbb943..824a17369 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md @@ -167,6 +167,12 @@ MaterialDef Phong Lighting { INSTANCING : UseInstancing NUM_MORPH_TARGETS: NumberOfMorphTargets NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers + + // fog - jayfella + USE_FOG : UseFog + FOG_LINEAR : LinearFog + FOG_EXP : ExpFog + FOG_EXPSQ : ExpSqFog } } diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.frag b/jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.frag index a510fc1db..2b5a4481c 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.frag +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.frag @@ -6,6 +6,26 @@ #import "Common/ShaderLib/Lighting.glsllib" #endif +// fog - jayfella +#ifdef USE_FOG +#import "Common/ShaderLib/MaterialFog.glsllib" +varying float fog_distance; +uniform vec4 m_FogColor; + +#ifdef FOG_LINEAR +uniform vec2 m_LinearFog; +#endif + +#ifdef FOG_EXP +uniform float m_ExpFog; +#endif + +#ifdef FOG_EXPSQ +uniform float m_ExpSqFog; +#endif + +#endif // end fog + varying vec2 texCoord; #ifdef SEPARATE_TEXCOORD varying vec2 texCoord2; @@ -226,5 +246,20 @@ void main(){ } #endif + + // add fog after the lighting because shadows will cause the fog to darken + // which just results in the geometry looking like it's changed color + #ifdef USE_FOG + #ifdef FOG_LINEAR + gl_FragColor = getFogLinear(gl_FragColor, m_FogColor, m_LinearFog.x, m_LinearFog.y, fog_distance); + #endif + #ifdef FOG_EXP + gl_FragColor = getFogExp(gl_FragColor, m_FogColor, m_ExpFog, fog_distance); + #endif + #ifdef FOG_EXPSQ + gl_FragColor = getFogExpSquare(gl_FragColor, m_FogColor, m_ExpSqFog, fog_distance); + #endif + #endif // end fog + gl_FragColor.a = alpha; } diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.vert b/jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.vert index f9fbe40cf..71233819b 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.vert +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.vert @@ -8,6 +8,11 @@ #import "Common/ShaderLib/BlinnPhongLighting.glsllib" #endif +// fog - jayfella +#ifdef USE_FOG +varying float fog_distance; +uniform vec3 g_CameraPosition; +#endif uniform vec4 m_Ambient; uniform vec4 m_Diffuse; @@ -187,5 +192,9 @@ void main(){ #ifdef USE_REFLECTION computeRef(modelSpacePos); - #endif + #endif + + #ifdef USE_FOG + fog_distance = distance(g_CameraPosition, (g_WorldMatrix * modelSpacePos).xyz); + #endif } \ No newline at end of file