From 4e63056be9b4cd2acf57917b49c9aa999f782bd7 Mon Sep 17 00:00:00 2001 From: "rem..om" Date: Sat, 8 Feb 2014 13:28:12 +0000 Subject: [PATCH] SpotLightShadowRenderer, fixed an issue where a spot light would wast shadows behind itself git-svn-id: https://jmonkeyengine.googlecode.com/svn/branches/gradle-restructure@11027 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../jme3/shadow/SpotLightShadowRenderer.java | 2 ++ .../Common/MatDefs/Light/Lighting.j3md | 1 + .../Common/MatDefs/Misc/Unshaded.j3md | 1 + .../Common/MatDefs/Shadow/PostShadow.frag | 8 ++++++ .../Common/MatDefs/Shadow/PostShadow.j3md | 1 + .../Common/MatDefs/Shadow/PostShadow.vert | 24 ++++++++++------ .../Common/MatDefs/Shadow/PostShadow15.frag | 12 ++++++-- .../Common/MatDefs/Shadow/PostShadow15.vert | 28 +++++++++++-------- .../MatDefs/Shadow/PostShadowFilter.frag | 15 +++++++++- .../MatDefs/Shadow/PostShadowFilter.j3md | 1 + .../MatDefs/Shadow/PostShadowFilter15.frag | 12 ++++++++ 11 files changed, 81 insertions(+), 24 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/shadow/SpotLightShadowRenderer.java b/jme3-core/src/main/java/com/jme3/shadow/SpotLightShadowRenderer.java index f257e0caa..5432a53f0 100644 --- a/jme3-core/src/main/java/com/jme3/shadow/SpotLightShadowRenderer.java +++ b/jme3-core/src/main/java/com/jme3/shadow/SpotLightShadowRenderer.java @@ -164,6 +164,8 @@ public class SpotLightShadowRenderer extends AbstractShadowRenderer { @Override protected void setMaterialParameters(Material material) { + material.setVector3("LightPos", light.getPosition()); + material.setVector3("LightDir", light.getDirection()); } /** 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 acfbc7e72..daad21612 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md @@ -124,6 +124,7 @@ MaterialDef Phong Lighting { Matrix4 LightViewProjectionMatrix4 Matrix4 LightViewProjectionMatrix5 Vector3 LightPos + Vector3 LightDir Float PCFEdge Float ShadowMapSize diff --git a/jme3-core/src/main/resources/Common/MatDefs/Misc/Unshaded.j3md b/jme3-core/src/main/resources/Common/MatDefs/Misc/Unshaded.j3md index aae534582..f3711e6bc 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Misc/Unshaded.j3md +++ b/jme3-core/src/main/resources/Common/MatDefs/Misc/Unshaded.j3md @@ -43,6 +43,7 @@ MaterialDef Unshaded { Matrix4 LightViewProjectionMatrix4 Matrix4 LightViewProjectionMatrix5 Vector3 LightPos + Vector3 LightDir Float PCFEdge diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.frag b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.frag index 06276fd0f..22c7c3808 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.frag +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.frag @@ -14,6 +14,10 @@ varying vec4 projCoord3; varying vec4 projCoord5; uniform vec3 m_LightPos; varying vec4 worldPos; +#else + #ifndef PSSM + varying float lightDot; + #endif #endif #ifdef DISCARD_ALPHA @@ -57,6 +61,10 @@ void main(){ projCoord0, projCoord1, projCoord2, projCoord3); #else //spotlight + if(lightDot < 0){ + outFragColor = vec4(1.0); + return; + } shadow = getSpotLightShadows(m_ShadowMap0,projCoord0); #endif #endif diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.j3md b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.j3md index a79e7ba61..1ac99f079 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.j3md +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.j3md @@ -24,6 +24,7 @@ MaterialDef Post Shadow { Matrix4 LightViewProjectionMatrix4 Matrix4 LightViewProjectionMatrix5 Vector3 LightPos + Vector3 LightDir Float PCFEdge diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.vert b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.vert index 50c228d63..b412a6573 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.vert +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.vert @@ -15,11 +15,17 @@ varying vec4 projCoord2; varying vec4 projCoord3; #ifdef POINTLIGHT -uniform mat4 m_LightViewProjectionMatrix4; -uniform mat4 m_LightViewProjectionMatrix5; -varying vec4 projCoord4; -varying vec4 projCoord5; -varying vec4 worldPos; + uniform mat4 m_LightViewProjectionMatrix4; + uniform mat4 m_LightViewProjectionMatrix5; + varying vec4 projCoord4; + varying vec4 projCoord5; + varying vec4 worldPos; +#else + #ifndef PSSM + uniform vec3 m_LightPos; + uniform vec3 m_LightDir; + varying float lightDot; + #endif #endif #ifdef PSSM @@ -70,9 +76,9 @@ void main(){ projCoord4 = biasMat * m_LightViewProjectionMatrix4 * worldPos; projCoord5 = biasMat * m_LightViewProjectionMatrix5 * worldPos; #else - - vec4 vLightPos = g_ViewMatrix * vec4(m_LightPos,1.0); - vec4 vPos = g_ViewMatrix * worldPos; - lightVec = vLightPos.xyz - vPos.xyz; + #ifndef PSSM + vec3 lightDir = worldPos.xyz - m_LightPos; + lightDot = dot(m_LightDir,lightDir); + #endif #endif } \ No newline at end of file diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow15.frag b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow15.frag index 255610e11..b87f7f517 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow15.frag +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow15.frag @@ -16,6 +16,10 @@ in vec4 projCoord3; in vec4 projCoord5; in vec4 worldPos; uniform vec3 m_LightPos; +#else + #ifndef PSSM + in float lightDot; + #endif #endif #ifdef DISCARD_ALPHA @@ -33,7 +37,7 @@ uniform vec2 m_FadeInfo; #endif void main(){ - + #ifdef DISCARD_ALPHA #ifdef COLOR_MAP float alpha = texture2D(m_ColorMap,texCoord).a; @@ -56,8 +60,12 @@ void main(){ shadow = getDirectionalLightShadows(m_Splits, shadowPosition, m_ShadowMap0,m_ShadowMap1,m_ShadowMap2,m_ShadowMap3, projCoord0, projCoord1, projCoord2, projCoord3); - #else + #else //spotlight + if(lightDot < 0){ + outFragColor = vec4(1.0); + return; + } shadow = getSpotLightShadows(m_ShadowMap0,projCoord0); #endif #endif diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow15.vert b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow15.vert index 0488440e1..c473cafaa 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow15.vert +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow15.vert @@ -6,8 +6,6 @@ uniform mat4 m_LightViewProjectionMatrix3; uniform mat4 g_WorldViewProjectionMatrix; uniform mat4 g_WorldMatrix; -uniform mat4 g_ViewMatrix; -uniform vec3 m_LightPos; out vec4 projCoord0; out vec4 projCoord1; @@ -15,11 +13,17 @@ out vec4 projCoord2; out vec4 projCoord3; #ifdef POINTLIGHT -uniform mat4 m_LightViewProjectionMatrix4; -uniform mat4 m_LightViewProjectionMatrix5; -out vec4 projCoord4; -out vec4 projCoord5; -out vec4 worldPos; + uniform mat4 m_LightViewProjectionMatrix4; + uniform mat4 m_LightViewProjectionMatrix5; + out vec4 projCoord4; + out vec4 projCoord5; + out vec4 worldPos; +#else + #ifndef PSSM + uniform vec3 m_LightPos; + uniform vec3 m_LightDir; + out float lightDot; + #endif #endif #ifdef PSSM @@ -69,10 +73,10 @@ void main(){ #ifdef POINTLIGHT projCoord4 = biasMat * m_LightViewProjectionMatrix4 * worldPos; projCoord5 = biasMat * m_LightViewProjectionMatrix5 * worldPos; - #else - - vec4 vLightPos = g_ViewMatrix * vec4(m_LightPos,1.0); - vec4 vPos = g_ViewMatrix * worldPos; - lightVec = vLightPos.xyz - vPos.xyz; + #else + #ifndef PSSM + vec3 lightDir = worldPos.xyz - m_LightPos; + lightDot = dot(m_LightDir,lightDir); + #endif #endif } \ No newline at end of file diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter.frag b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter.frag index f22a93af2..02c109427 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter.frag +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter.frag @@ -22,6 +22,11 @@ uniform mat4 m_LightViewProjectionMatrix3; uniform vec3 m_LightPos; uniform mat4 m_LightViewProjectionMatrix4; uniform mat4 m_LightViewProjectionMatrix5; +#else + #ifndef PSSM + uniform vec3 m_LightPos; + uniform vec3 m_LightDir; + #endif #endif #ifdef FADE @@ -51,7 +56,15 @@ void main(){ // get the vertex in world space vec4 worldPos = vec4(getPosition(depth,texCoord),1.0); - + + #if (!defined(POINTLIGHT) && !defined(PSSM)) + vec3 lightDir = worldPos.xyz - m_LightPos; + if( dot(m_LightDir,lightDir)<0){ + gl_FragColor = color; + return; + } + #endif + // populate the light view matrices array and convert vertex to light viewProj space vec4 projCoord0 = biasMat * m_LightViewProjectionMatrix0 * worldPos; vec4 projCoord1 = biasMat * m_LightViewProjectionMatrix1 * worldPos; diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter.j3md b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter.j3md index c97f0a323..9a78752ae 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter.j3md +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter.j3md @@ -24,6 +24,7 @@ MaterialDef Post Shadow { Matrix4 LightViewProjectionMatrix4 Matrix4 LightViewProjectionMatrix5 Vector3 LightPos + Vector3 LightDir Float PCFEdge diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter15.frag b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter15.frag index c4579fdf8..7a6c2025c 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter15.frag +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadowFilter15.frag @@ -24,6 +24,11 @@ uniform mat4 m_LightViewProjectionMatrix3; uniform vec3 m_LightPos; uniform mat4 m_LightViewProjectionMatrix4; uniform mat4 m_LightViewProjectionMatrix5; +#else + #ifndef PSSM + uniform vec3 m_LightPos; + uniform vec3 m_LightDir; + #endif #endif #ifdef FADE @@ -48,6 +53,13 @@ vec4 main_multiSample(in int numSample){ // get the vertex in world space vec4 worldPos = vec4(getPosition(depth,texCoord),1.0); + #if (!defined(POINTLIGHT) && !defined(PSSM)) + vec3 lightDir = worldPos.xyz - m_LightPos; + if( dot(m_LightDir,lightDir)<0){ + return color; + } + #endif + // populate the light view matrices array and convert vertex to light viewProj space vec4 projCoord0 = biasMat * m_LightViewProjectionMatrix0 * worldPos; vec4 projCoord1 = biasMat * m_LightViewProjectionMatrix1 * worldPos;