Skip to content

Commit fc81354

Browse files
committed
Fixes normal lighting in world space for PBR
1 parent 435f2d4 commit fc81354

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

jme3-core/src/main/java/com/jme3/shader/UniformBinding.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,14 @@ public enum UniformBinding {
195195
* The light color when rendering in multi pass mode
196196
* Type: vec4
197197
*/
198-
LightColor("vec4");
198+
LightColor("vec4"),
199+
200+
/**
201+
* The normal matrix in world space for World space lighting. The inverse transpose of the world matrix.
202+
* Converts normals from model space to world space.
203+
* Type: mat3
204+
*/
205+
WorldNormalMatrix("mat3");
199206

200207
String glslType;
201208

jme3-core/src/main/java/com/jme3/shader/UniformBindingManager.java

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.jme3.renderer.Camera;
3737
import com.jme3.renderer.RenderManager;
3838
import com.jme3.system.Timer;
39+
3940
import java.util.ArrayList;
4041

4142
/**
@@ -66,6 +67,7 @@ public class UniformBindingManager {
6667
private Matrix4f worldViewMatrix = new Matrix4f();
6768
private Matrix4f worldViewProjMatrix = new Matrix4f();
6869
private Matrix3f normalMatrix = new Matrix3f();
70+
private Matrix3f worldNormalMatrix = new Matrix3f();
6971
private Matrix4f worldMatrixInv = new Matrix4f();
7072
private Matrix3f worldMatrixInvTrsp = new Matrix3f();
7173
private Matrix4f viewMatrixInv = new Matrix4f();
@@ -114,6 +116,13 @@ public void updateUniformBindings(Shader shader) {
114116
normalMatrix.transposeLocal();
115117
u.setValue(VarType.Matrix3, normalMatrix);
116118
break;
119+
case WorldNormalMatrix:
120+
tempMatrix.set(worldMatrix);
121+
tempMatrix.toRotationMatrix(worldNormalMatrix);
122+
worldNormalMatrix.invertLocal();
123+
worldNormalMatrix.transposeLocal();
124+
u.setValue(VarType.Matrix3, worldNormalMatrix);
125+
break;
117126
case WorldViewProjectionMatrix:
118127
worldViewProjMatrix.set(viewProjMatrix);
119128
worldViewProjMatrix.multLocal(worldMatrix);

jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ MaterialDef PBR Lighting {
130130
WorldViewProjectionMatrix
131131
CameraPosition
132132
WorldMatrix
133+
WorldNormalMatrix
133134
ViewProjectionMatrix
134135
ViewMatrix
135136
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ uniform mat4 g_WorldViewMatrix;
2525
uniform mat4 g_WorldViewProjectionMatrix;
2626
uniform mat4 g_ViewProjectionMatrix;
2727
uniform mat3 g_NormalMatrix;
28+
uniform mat3 g_WorldNormalMatrix;
2829

2930
#if defined INSTANCING
3031

@@ -101,7 +102,7 @@ vec3 TransformNormal(vec3 normal) {
101102
}
102103

103104
vec3 TransformWorldNormal(vec3 normal) {
104-
return normalize((g_WorldMatrix * vec4(normal,0.0)).xyz);
105+
return normalize(g_WorldNormalMatrix * normal);
105106
}
106107

107108

0 commit comments

Comments
 (0)