From 233bc6f0dafec5e795b90c3acc0649efc32e9bc2 Mon Sep 17 00:00:00 2001 From: Ryan McDonough Date: Mon, 27 Jan 2020 01:42:52 -0500 Subject: [PATCH] PreShadow & PostShadow Support for PBRLighting.j3md (#1265) * Update PBRLighting.j3md * Create PreShadowPBR.frag * Create PostShadowPBR.frag --- .../Common/MatDefs/Light/PBRLighting.j3md | 6 +- .../Common/MatDefs/Shadow/PostShadowPBR.frag | 89 +++++++++++++++++++ .../Common/MatDefs/Shadow/PreShadowPBR.frag | 28 ++++++ 3 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowPBR.frag create mode 100644 jme3-core/src/main/resources/Common/MatDefs/Shadow/PreShadowPBR.frag diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md index 40b4f9a6c..87e345f6b 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md @@ -168,7 +168,7 @@ MaterialDef PBR Lighting { Technique PreShadow { VertexShader GLSL300 GLSL100 GLSL150 : Common/MatDefs/Shadow/PreShadow.vert - FragmentShader GLSL300 GLSL100 GLSL150 : Common/MatDefs/Shadow/PreShadow.frag + FragmentShader GLSL300 GLSL100 GLSL150 : Common/MatDefs/Shadow/PreShadowPBR.frag WorldParameters { WorldViewProjectionMatrix @@ -198,7 +198,7 @@ MaterialDef PBR Lighting { Technique PostShadow{ VertexShader GLSL310 GLSL300 GLSL150: Common/MatDefs/Shadow/PostShadow.vert - FragmentShader GLSL310 GLSL300 GLSL150: Common/MatDefs/Shadow/PostShadow.frag + FragmentShader GLSL310 GLSL300 GLSL150: Common/MatDefs/Shadow/PostShadowPBR.frag WorldParameters { WorldViewProjectionMatrix @@ -233,7 +233,7 @@ MaterialDef PBR Lighting { Technique PostShadow{ VertexShader GLSL100: Common/MatDefs/Shadow/PostShadow.vert - FragmentShader GLSL100: Common/MatDefs/Shadow/PostShadow.frag + FragmentShader GLSL100: Common/MatDefs/Shadow/PostShadowPBR.frag WorldParameters { WorldViewProjectionMatrix diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowPBR.frag b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowPBR.frag new file mode 100644 index 000000000..8c779e4f8 --- /dev/null +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowPBR.frag @@ -0,0 +1,89 @@ + +#import "Common/ShaderLib/GLSLCompat.glsllib" +#import "Common/ShaderLib/Shadows.glsllib" +#if defined(PSSM) || defined(FADE) +varying float shadowPosition; +#endif + +varying vec4 projCoord0; +varying vec4 projCoord1; +varying vec4 projCoord2; +varying vec4 projCoord3; +#ifndef BACKFACE_SHADOWS + varying float nDotL; +#endif + +#ifdef POINTLIGHT + varying vec4 projCoord4; + varying vec4 projCoord5; + uniform vec3 m_LightPos; + varying vec4 worldPos; +#else + #ifndef PSSM + varying float lightDot; + #endif +#endif + +#ifdef DISCARD_ALPHA + #ifdef COLOR_MAP + uniform sampler2D m_ColorMap; + #else + uniform sampler2D m_BaseColorMap; + #endif + uniform float m_AlphaDiscardThreshold; + varying vec2 texCoord; +#endif + +#ifdef FADE +uniform vec2 m_FadeInfo; +#endif + +void main(){ + + #ifdef DISCARD_ALPHA + #ifdef COLOR_MAP + float alpha = texture2D(m_ColorMap,texCoord).a; + #else + float alpha = texture2D(m_BaseColorMap,texCoord).a; + #endif + if(alpha<=m_AlphaDiscardThreshold){ + discard; + } + #endif + + #ifndef BACKFACE_SHADOWS + if(nDotL > 0.0){ + discard; + } + #endif + + + float shadow = 1.0; + + #ifdef POINTLIGHT + shadow = getPointLightShadows(worldPos, m_LightPos, + m_ShadowMap0,m_ShadowMap1,m_ShadowMap2,m_ShadowMap3,m_ShadowMap4,m_ShadowMap5, + projCoord0, projCoord1, projCoord2, projCoord3, projCoord4, projCoord5); + #else + #ifdef PSSM + shadow = getDirectionalLightShadows(m_Splits, shadowPosition, + m_ShadowMap0,m_ShadowMap1,m_ShadowMap2,m_ShadowMap3, + projCoord0, projCoord1, projCoord2, projCoord3); + #else + //spotlight + if(lightDot < 0.0){ + gl_FragColor = vec4(1.0); + return; + } + shadow = getSpotLightShadows(m_ShadowMap0,projCoord0); + #endif + #endif + + #ifdef FADE + shadow = max(0.0, mix(shadow, 1.0, max(0.0, (shadowPosition - m_FadeInfo.x) * m_FadeInfo.y))); + #endif + + shadow = shadow * m_ShadowIntensity + (1.0 - m_ShadowIntensity); + gl_FragColor = vec4(shadow, shadow, shadow, 1.0); + +} diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PreShadowPBR.frag b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PreShadowPBR.frag new file mode 100644 index 000000000..cf7f91669 --- /dev/null +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PreShadowPBR.frag @@ -0,0 +1,28 @@ +#import "Common/ShaderLib/GLSLCompat.glsllib" +varying vec2 texCoord; + +#ifdef DISCARD_ALPHA + #ifdef COLOR_MAP + uniform sampler2D m_ColorMap; + #else + uniform sampler2D m_BaseColorMap; + #endif + uniform float m_AlphaDiscardThreshold; +#endif + + +void main(){ + #ifdef DISCARD_ALPHA + #ifdef COLOR_MAP + if (texture2D(m_ColorMap, texCoord).a <= m_AlphaDiscardThreshold){ + discard; + } + #else + if (texture2D(m_BaseColorMap, texCoord).a <= m_AlphaDiscardThreshold){ + discard; + } + #endif + #endif + + gl_FragColor = vec4(1.0); +}