From fc8135412f0236bb65684eedd14272d11bc3a620 Mon Sep 17 00:00:00 2001 From: Nehon Date: Mon, 18 Dec 2017 17:43:57 +0100 Subject: [PATCH] Fixes normal lighting in world space for PBR --- .../src/main/java/com/jme3/shader/UniformBinding.java | 9 ++++++++- .../main/java/com/jme3/shader/UniformBindingManager.java | 9 +++++++++ .../main/resources/Common/MatDefs/Light/PBRLighting.j3md | 1 + .../main/resources/Common/ShaderLib/Instancing.glsllib | 3 ++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/shader/UniformBinding.java b/jme3-core/src/main/java/com/jme3/shader/UniformBinding.java index 46b689cd9..1235dde7f 100644 --- a/jme3-core/src/main/java/com/jme3/shader/UniformBinding.java +++ b/jme3-core/src/main/java/com/jme3/shader/UniformBinding.java @@ -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; diff --git a/jme3-core/src/main/java/com/jme3/shader/UniformBindingManager.java b/jme3-core/src/main/java/com/jme3/shader/UniformBindingManager.java index f9ead8979..340fc162c 100644 --- a/jme3-core/src/main/java/com/jme3/shader/UniformBindingManager.java +++ b/jme3-core/src/main/java/com/jme3/shader/UniformBindingManager.java @@ -36,6 +36,7 @@ import com.jme3.math.*; import com.jme3.renderer.Camera; import com.jme3.renderer.RenderManager; import com.jme3.system.Timer; + import java.util.ArrayList; /** @@ -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(); @@ -114,6 +116,13 @@ public class UniformBindingManager { 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); 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 61edf8e0d..afca96d4c 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md @@ -130,6 +130,7 @@ MaterialDef PBR Lighting { WorldViewProjectionMatrix CameraPosition WorldMatrix + WorldNormalMatrix ViewProjectionMatrix ViewMatrix } diff --git a/jme3-core/src/main/resources/Common/ShaderLib/Instancing.glsllib b/jme3-core/src/main/resources/Common/ShaderLib/Instancing.glsllib index 5a45ed2f2..37c3a40cf 100644 --- a/jme3-core/src/main/resources/Common/ShaderLib/Instancing.glsllib +++ b/jme3-core/src/main/resources/Common/ShaderLib/Instancing.glsllib @@ -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 @@ -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); }