From 2276197a2b5cdf6e9e4962690a9e25fb0af72296 Mon Sep 17 00:00:00 2001 From: MeFisto94 Date: Sat, 4 Apr 2020 13:44:10 +0200 Subject: [PATCH] Fixing PBR on <= GLSL 130: - GLSLCompat now implements mat3(mat4) and determinant() and inverse() for mat2 and mat3 - PBR.glslib uses this functions again instead of one-off workarounds --- .../Common/ShaderLib/GLSLCompat.glsllib | 36 +++++++++++++++++++ .../resources/Common/ShaderLib/PBR.glsllib | 6 +--- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/jme3-core/src/main/resources/Common/ShaderLib/GLSLCompat.glsllib b/jme3-core/src/main/resources/Common/ShaderLib/GLSLCompat.glsllib index 697db195d..c8eddf308 100644 --- a/jme3-core/src/main/resources/Common/ShaderLib/GLSLCompat.glsllib +++ b/jme3-core/src/main/resources/Common/ShaderLib/GLSLCompat.glsllib @@ -42,4 +42,40 @@ out vec4 outFragColor; # define isnan(val) !(val<0.0||val>0.0||val==0.0) #endif +#if __VERSION__ == 110 +mat3 mat3(mat4 m) { + return mat3(m[0].xyz, m[1].xyz, m[2].xyz); +} +#endif + +#if __VERSION__ <= 140 +float determinant(mat2 m) { + return m[0][0] * m[1][1] - m[1][0] * m[0][1]; +} + +float determinant(mat3 m) { + return + m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]) + - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0]) + + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]); +} +#endif + +#if __VERSION__ <= 130 +mat2 inverse(mat2 m) { + return mat2(m[1][1], -m[0][1], -m[1][0], m[0][0]) / determinant(m); +} + +mat3 inverse(mat3 m) { + return mat3( + + (m[1][1] * m[2][2] - m[2][1] * m[1][2]), + - (m[1][0] * m[2][2] - m[2][0] * m[1][2]), + + (m[1][0] * m[2][1] - m[2][0] * m[1][1]), + - (m[0][1] * m[2][2] - m[2][1] * m[0][2]), + + (m[0][0] * m[2][2] - m[2][0] * m[0][2]), + - (m[0][0] * m[2][1] - m[2][0] * m[0][1]), + + (m[0][1] * m[1][2] - m[1][1] * m[0][2]), + - (m[0][0] * m[1][2] - m[1][0] * m[0][2]), + + (m[0][0] * m[1][1] - m[1][0] * m[0][1])) / determinant(m); +} +#endif diff --git a/jme3-core/src/main/resources/Common/ShaderLib/PBR.glsllib b/jme3-core/src/main/resources/Common/ShaderLib/PBR.glsllib index 692dd8619..bf23d547d 100644 --- a/jme3-core/src/main/resources/Common/ShaderLib/PBR.glsllib +++ b/jme3-core/src/main/resources/Common/ShaderLib/PBR.glsllib @@ -143,12 +143,8 @@ float renderProbe(vec3 viewDir, vec3 worldPos, vec3 normal, vec3 norm, float Rou if(lightProbeData[0][3] != 0.0){ // oriented box probe - mat3 wToLocalRot; - wToLocalRot[0].xyz = lightProbeData[0].xyz; - wToLocalRot[1].xyz = lightProbeData[1].xyz; - wToLocalRot[2].xyz = lightProbeData[2].xyz; + mat3 wToLocalRot = inverse(mat3(lightProbeData)); - wToLocalRot = inverse(wToLocalRot); vec3 scale = vec3(lightProbeData[0][3], lightProbeData[1][3], lightProbeData[2][3]); #if NB_PROBES >= 2 // probe blending