Skip to content

Commit

Permalink
Fixes normal lighting in world space for PBR
Browse files Browse the repository at this point in the history
  • Loading branch information
Nehon committed Dec 18, 2017
1 parent 435f2d4 commit fc81354
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion jme3-core/src/main/java/com/jme3/shader/UniformBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,14 @@ public enum UniformBinding {
* The light color when rendering in multi pass mode
* Type: vec4
*/
LightColor("vec4");
LightColor("vec4"),

/**
* The normal matrix in world space for World space lighting. The inverse transpose of the world matrix.
* Converts normals from model space to world space.
* Type: mat3
*/
WorldNormalMatrix("mat3");

String glslType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.system.Timer;

import java.util.ArrayList;

/**
Expand Down Expand Up @@ -66,6 +67,7 @@ public class UniformBindingManager {
private Matrix4f worldViewMatrix = new Matrix4f();
private Matrix4f worldViewProjMatrix = new Matrix4f();
private Matrix3f normalMatrix = new Matrix3f();
private Matrix3f worldNormalMatrix = new Matrix3f();
private Matrix4f worldMatrixInv = new Matrix4f();
private Matrix3f worldMatrixInvTrsp = new Matrix3f();
private Matrix4f viewMatrixInv = new Matrix4f();
Expand Down Expand Up @@ -114,6 +116,13 @@ public void updateUniformBindings(Shader shader) {
normalMatrix.transposeLocal();
u.setValue(VarType.Matrix3, normalMatrix);
break;
case WorldNormalMatrix:
tempMatrix.set(worldMatrix);
tempMatrix.toRotationMatrix(worldNormalMatrix);
worldNormalMatrix.invertLocal();
worldNormalMatrix.transposeLocal();
u.setValue(VarType.Matrix3, worldNormalMatrix);
break;
case WorldViewProjectionMatrix:
worldViewProjMatrix.set(viewProjMatrix);
worldViewProjMatrix.multLocal(worldMatrix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ MaterialDef PBR Lighting {
WorldViewProjectionMatrix
CameraPosition
WorldMatrix
WorldNormalMatrix
ViewProjectionMatrix
ViewMatrix
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ uniform mat4 g_WorldViewMatrix;
uniform mat4 g_WorldViewProjectionMatrix;
uniform mat4 g_ViewProjectionMatrix;
uniform mat3 g_NormalMatrix;
uniform mat3 g_WorldNormalMatrix;

#if defined INSTANCING

Expand Down Expand Up @@ -101,7 +102,7 @@ vec3 TransformNormal(vec3 normal) {
}

vec3 TransformWorldNormal(vec3 normal) {
return normalize((g_WorldMatrix * vec4(normal,0.0)).xyz);
return normalize(g_WorldNormalMatrix * normal);
}


Expand Down

0 comments on commit fc81354

Please sign in to comment.