Skip to content

Commit 402a009

Browse files
authored
Fix singlepass lighting artifacts and make it the default (#2652)
* prefer singlepass lighting * keep old tests by defaulting to multipass lighting * make sure TBN is always normalized * update tests * fix sp lighting when no light is in the scene * apply Gram-Schmidt orthogonalization do SPLighting * update tests
1 parent 3df0161 commit 402a009

File tree

26 files changed

+78
-51
lines changed

26 files changed

+78
-51
lines changed

jme3-core/src/main/java/com/jme3/renderer/RenderManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public class RenderManager {
123123
private boolean handleTranslucentBucket = true;
124124
private AppProfiler prof;
125125
private LightFilter lightFilter = new DefaultLightFilter();
126-
private TechniqueDef.LightMode preferredLightMode = TechniqueDef.LightMode.MultiPass;
126+
private TechniqueDef.LightMode preferredLightMode = TechniqueDef.LightMode.SinglePass;
127127
private int singlePassLightBatchSize = 1;
128128
private int maxSinglePassLightBatchSize = 16;
129129
private final MatParamOverride boundDrawBufferId = new MatParamOverride(VarType.Int, "BoundDrawBuffer", 0);

jme3-core/src/main/resources/Common/MatDefs/Light/GBuf.vert

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ void main(){
3535
gl_Position = g_WorldViewProjectionMatrix * pos;
3636
texCoord = inTexCoord;
3737

38-
#if defined(NORMALMAP)
39-
vec4 wvNormal, wvTangent, wvBinormal;
38+
#if defined(NORMALMAP)
39+
vec4 wvNormal, wvTangent, wvBinormal;
4040

4141
wvNormal = vec4(inNormal, 0.0);
4242
wvTangent = vec4(inTangent, 0.0);
4343

44-
wvNormal.xyz = normalize( (g_WorldMatrix * wvNormal).xyz );
45-
wvTangent.xyz = normalize( (g_WorldMatrix * wvTangent).xyz );
46-
wvBinormal.xyz = cross(wvNormal.xyz, wvTangent.xyz);
47-
tbnMat = mat3(wvTangent.xyz, wvBinormal.xyz, wvNormal.xyz);
44+
wvNormal.xyz = normalize( (g_WorldMatrix * wvNormal).xyz );
45+
wvTangent.xyz = normalize( (g_WorldMatrix * wvTangent).xyz );
46+
wvTangent.xyz = normalize(wvTangent.xyz - wvNormal.xyz * dot(wvTangent.xyz, wvNormal.xyz));
47+
wvBinormal.xyz = normalize(cross(wvNormal.xyz, wvTangent.xyz));
48+
tbnMat = mat3(wvTangent.xyz, wvBinormal.xyz, wvNormal.xyz);
4849

4950
vNormal = wvNormal.xyz;
5051
#else

jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.vert

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,13 @@ void main(){
127127
wvLightPos.w = g_LightPosition.w;
128128
vec4 lightColor = g_LightColor;
129129

130-
#if (defined(NORMALMAP) || defined(PARALLAXMAP)) && !defined(VERTEX_LIGHTING)
131-
vec3 wvTangent = normalize(TransformNormal(modelSpaceTan));
132-
vec3 wvBinormal = cross(wvNormal, wvTangent);
133-
mat3 tbnMat = mat3(wvTangent, wvBinormal * inTangent.w,wvNormal);
134-
#endif
130+
#if (defined(NORMALMAP) || defined(PARALLAXMAP)) && !defined(VERTEX_LIGHTING)
131+
vec3 tbnNormal = normalize(wvNormal);
132+
vec3 wvTangent = normalize(TransformNormal(modelSpaceTan));
133+
wvTangent = normalize(wvTangent - tbnNormal * dot(wvTangent, tbnNormal));
134+
vec3 wvBinormal = normalize(cross(tbnNormal, wvTangent)) * inTangent.w;
135+
mat3 tbnMat = mat3(wvTangent, wvBinormal, tbnNormal);
136+
#endif
135137

136138
#if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
137139
vViewDir = -wvPosition * tbnMat;

jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.frag

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ uniform float m_Shininess;
9595
void main(){
9696
#if !defined(VERTEX_LIGHTING)
9797
#if defined(NORMALMAP)
98-
mat3 tbnMat = mat3(vTangent.xyz, vTangent.w * cross( (vNormal), (vTangent.xyz)), vNormal.xyz);
98+
vec3 tbnNormal = normalize(vNormal.xyz);
99+
vec3 tbnTangent = normalize(vTangent.xyz - tbnNormal * dot(vTangent.xyz, tbnNormal));
100+
vec3 tbnBinormal = normalize(cross(tbnNormal, tbnTangent)) * vTangent.w;
101+
mat3 tbnMat = mat3(tbnTangent, tbnBinormal, tbnNormal);
99102

100103
if (!gl_FrontFacing)
101104
{

jme3-core/src/main/resources/Common/ShaderLib/Lighting.glsllib

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ void lightComputeDir(in vec3 worldPos, in float lightType, in vec4 position, out
1111
vec3 tempVec = position.xyz * sign(posLight - 0.5) - (worldPos * posLight);
1212
lightVec = tempVec;
1313
float dist = length(tempVec);
14+
if (dist <= 0.0) {
15+
lightDir = vec4(0.0);
16+
return;
17+
}
1418
#ifdef SRGB
1519
lightDir.w = (1.0 - position.w * dist) / (1.0 + position.w * dist * dist);
1620
lightDir.w = clamp(lightDir.w, 1.0 - posLight, 1.0);
Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
#ifndef __MATH_GLSLLIB__
2-
#define __MATH_GLSLLIB__
3-
4-
/// Multiplies the vector by the quaternion, then returns the resultant vector.
5-
vec3 Math_QuaternionMult(in vec4 quat, in vec3 vec){
6-
return vec + 2.0 * cross(quat.xyz, cross(quat.xyz, vec) + quat.w * vec);
7-
}
8-
9-
void Math_lengthAndNormalize(in vec3 vec,out float outLength,out vec3 outNormal){
10-
float dotv=dot(vec,vec);
11-
float invl=inversesqrt(dotv);
12-
outNormal=vec*invl;
13-
outLength=invl*dotv;
14-
}
15-
16-
1+
#ifndef __MATH_GLSLLIB__
2+
#define __MATH_GLSLLIB__
3+
4+
/// Multiplies the vector by the quaternion, then returns the resultant vector.
5+
vec3 Math_QuaternionMult(in vec4 quat, in vec3 vec){
6+
return vec + 2.0 * cross(quat.xyz, cross(quat.xyz, vec) + quat.w * vec);
7+
}
8+
9+
void Math_lengthAndNormalize(in vec3 vec,out float outLength,out vec3 outNormal){
10+
float dotv=dot(vec,vec);
11+
if(dotv <= 0.0){
12+
outNormal = vec3(0.0);
13+
outLength = 0.0;
14+
return;
15+
}
16+
float invl=inversesqrt(dotv);
17+
outNormal=vec*invl;
18+
outLength=invl*dotv;
19+
}
20+
21+
1722
#endif

jme3-core/src/main/resources/Common/ShaderLib/Tangent.glsllib

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ void Tangent_ComputeVS(out vec3 outNormal, out vec3 outTangent){
55
outTangent = normalize(g_NormalMatrix * inTangent);
66
}
77

8-
mat3 Tangent_GetBasis(){
9-
vec3 wvBinormal = cross(wvNormal, wvTangent);
10-
return mat3(wvTangent, wvBinormal, wvNormal);
11-
}
8+
mat3 Tangent_GetBasis(){
9+
vec3 n = normalize(wvNormal);
10+
vec3 t = normalize(wvTangent - n * dot(wvTangent, n));
11+
vec3 b = normalize(cross(n, t));
12+
return mat3(t, b, n);
13+
}

jme3-core/src/main/resources/Common/ShaderLib/module/pbrlighting/PBRLightingUtils.glsllib

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,10 @@
305305
surface.envLightContribution = vec3(0.0);
306306

307307
#ifdef ENABLE_PBRLightingUtils_getWorldTangent
308-
vec3 tan = normalize(wTangent.xyz);
309-
surface.tbnMat = mat3(tan, wTangent.w * cross( surface.geometryNormal, tan), surface.geometryNormal);
308+
vec3 n = normalize(surface.geometryNormal);
309+
vec3 tan = normalize(wTangent.xyz - n * dot(wTangent.xyz, n));
310+
vec3 bitan = normalize(cross(n, tan)) * wTangent.w;
311+
surface.tbnMat = mat3(tan, bitan, n);
310312
surface.hasTangents = true;
311313
#endif
312314

jme3-core/src/test/java/com/jme3/system/TestUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.jme3.asset.AssetConfig;
3535
import com.jme3.asset.AssetManager;
3636
import com.jme3.asset.DesktopAssetManager;
37+
import com.jme3.material.TechniqueDef;
3738
import com.jme3.renderer.RenderManager;
3839
import com.jme3.renderer.Renderer;
3940

@@ -63,6 +64,7 @@ public static RenderManager createRenderManager() {
6364

6465
public static RenderManager createRenderManager(Renderer renderer) {
6566
RenderManager rm = new RenderManager(renderer);
67+
rm.setPreferredLightMode(TechniqueDef.LightMode.MultiPass);
6668
rm.setPassDrawBufferTargetIdToShaders(false);
6769
return rm;
6870
}
2 Bytes
Loading

0 commit comments

Comments
 (0)