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 46b689cd90..1235dde7fd 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 f9ead8979e..340fc162ca 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.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 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); 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 61edf8e0dc..afca96d4c0 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 5a45ed2f2f..37c3a40cf2 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); }