diff --git a/jme3-core/src/main/java/com/jme3/renderer/RenderManager.java b/jme3-core/src/main/java/com/jme3/renderer/RenderManager.java index b6ee8c60f0..5711461e37 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/RenderManager.java +++ b/jme3-core/src/main/java/com/jme3/renderer/RenderManager.java @@ -106,7 +106,7 @@ public class RenderManager { private boolean handleTranslucentBucket = true; private AppProfiler prof; private LightFilter lightFilter = new DefaultLightFilter(); - private TechniqueDef.LightMode preferredLightMode = TechniqueDef.LightMode.MultiPass; + private TechniqueDef.LightMode preferredLightMode = TechniqueDef.LightMode.SinglePass; private int singlePassLightBatchSize = 1; private final MatParamOverride boundDrawBufferId = new MatParamOverride(VarType.Int, "BoundDrawBuffer", 0); private Predicate renderFilter; diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/GBuf.vert b/jme3-core/src/main/resources/Common/MatDefs/Light/GBuf.vert index d72712854f..c4cfae519b 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/GBuf.vert +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/GBuf.vert @@ -35,16 +35,17 @@ void main(){ gl_Position = g_WorldViewProjectionMatrix * pos; texCoord = inTexCoord; - #if defined(NORMALMAP) - vec4 wvNormal, wvTangent, wvBinormal; + #if defined(NORMALMAP) + vec4 wvNormal, wvTangent, wvBinormal; wvNormal = vec4(inNormal, 0.0); wvTangent = vec4(inTangent, 0.0); - wvNormal.xyz = normalize( (g_WorldMatrix * wvNormal).xyz ); - wvTangent.xyz = normalize( (g_WorldMatrix * wvTangent).xyz ); - wvBinormal.xyz = cross(wvNormal.xyz, wvTangent.xyz); - tbnMat = mat3(wvTangent.xyz, wvBinormal.xyz, wvNormal.xyz); + wvNormal.xyz = normalize( (g_WorldMatrix * wvNormal).xyz ); + wvTangent.xyz = normalize( (g_WorldMatrix * wvTangent).xyz ); + wvTangent.xyz = normalize(wvTangent.xyz - wvNormal.xyz * dot(wvTangent.xyz, wvNormal.xyz)); + wvBinormal.xyz = normalize(cross(wvNormal.xyz, wvTangent.xyz)); + tbnMat = mat3(wvTangent.xyz, wvBinormal.xyz, wvNormal.xyz); vNormal = wvNormal.xyz; #else diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.vert b/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.vert index c1460cc9be..66783fc413 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.vert +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.vert @@ -127,11 +127,13 @@ void main(){ wvLightPos.w = g_LightPosition.w; vec4 lightColor = g_LightColor; - #if (defined(NORMALMAP) || defined(PARALLAXMAP)) && !defined(VERTEX_LIGHTING) - vec3 wvTangent = normalize(TransformNormal(modelSpaceTan)); - vec3 wvBinormal = cross(wvNormal, wvTangent); - mat3 tbnMat = mat3(wvTangent, wvBinormal * inTangent.w,wvNormal); - #endif + #if (defined(NORMALMAP) || defined(PARALLAXMAP)) && !defined(VERTEX_LIGHTING) + vec3 tbnNormal = normalize(wvNormal); + vec3 wvTangent = normalize(TransformNormal(modelSpaceTan)); + wvTangent = normalize(wvTangent - tbnNormal * dot(wvTangent, tbnNormal)); + vec3 wvBinormal = normalize(cross(tbnNormal, wvTangent)) * inTangent.w; + mat3 tbnMat = mat3(wvTangent, wvBinormal, tbnNormal); + #endif #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING) vViewDir = -wvPosition * tbnMat; diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.frag b/jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.frag index 028ab44e89..1595395956 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.frag +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/SPLighting.frag @@ -95,7 +95,10 @@ uniform float m_Shininess; void main(){ #if !defined(VERTEX_LIGHTING) #if defined(NORMALMAP) - mat3 tbnMat = mat3(vTangent.xyz, vTangent.w * cross( (vNormal), (vTangent.xyz)), vNormal.xyz); + vec3 tbnNormal = normalize(vNormal.xyz); + vec3 tbnTangent = normalize(vTangent.xyz - tbnNormal * dot(vTangent.xyz, tbnNormal)); + vec3 tbnBinormal = normalize(cross(tbnNormal, tbnTangent)) * vTangent.w; + mat3 tbnMat = mat3(tbnTangent, tbnBinormal, tbnNormal); if (!gl_FrontFacing) { diff --git a/jme3-core/src/main/resources/Common/ShaderLib/Lighting.glsllib b/jme3-core/src/main/resources/Common/ShaderLib/Lighting.glsllib index 50eb7de874..060a610cbb 100644 --- a/jme3-core/src/main/resources/Common/ShaderLib/Lighting.glsllib +++ b/jme3-core/src/main/resources/Common/ShaderLib/Lighting.glsllib @@ -11,6 +11,10 @@ void lightComputeDir(in vec3 worldPos, in float lightType, in vec4 position, out vec3 tempVec = position.xyz * sign(posLight - 0.5) - (worldPos * posLight); lightVec = tempVec; float dist = length(tempVec); + if (dist <= 0.0) { + lightDir = vec4(0.0); + return; + } #ifdef SRGB lightDir.w = (1.0 - position.w * dist) / (1.0 + position.w * dist * dist); lightDir.w = clamp(lightDir.w, 1.0 - posLight, 1.0); diff --git a/jme3-core/src/main/resources/Common/ShaderLib/Math.glsllib b/jme3-core/src/main/resources/Common/ShaderLib/Math.glsllib index d97132fbd2..f4e3a98fa8 100644 --- a/jme3-core/src/main/resources/Common/ShaderLib/Math.glsllib +++ b/jme3-core/src/main/resources/Common/ShaderLib/Math.glsllib @@ -1,17 +1,22 @@ -#ifndef __MATH_GLSLLIB__ -#define __MATH_GLSLLIB__ - -/// Multiplies the vector by the quaternion, then returns the resultant vector. -vec3 Math_QuaternionMult(in vec4 quat, in vec3 vec){ - return vec + 2.0 * cross(quat.xyz, cross(quat.xyz, vec) + quat.w * vec); -} - -void Math_lengthAndNormalize(in vec3 vec,out float outLength,out vec3 outNormal){ - float dotv=dot(vec,vec); - float invl=inversesqrt(dotv); - outNormal=vec*invl; - outLength=invl*dotv; -} - - +#ifndef __MATH_GLSLLIB__ +#define __MATH_GLSLLIB__ + +/// Multiplies the vector by the quaternion, then returns the resultant vector. +vec3 Math_QuaternionMult(in vec4 quat, in vec3 vec){ + return vec + 2.0 * cross(quat.xyz, cross(quat.xyz, vec) + quat.w * vec); +} + +void Math_lengthAndNormalize(in vec3 vec,out float outLength,out vec3 outNormal){ + float dotv=dot(vec,vec); + if(dotv <= 0.0){ + outNormal = vec3(0.0); + outLength = 0.0; + return; + } + float invl=inversesqrt(dotv); + outNormal=vec*invl; + outLength=invl*dotv; +} + + #endif \ No newline at end of file diff --git a/jme3-core/src/main/resources/Common/ShaderLib/Tangent.glsllib b/jme3-core/src/main/resources/Common/ShaderLib/Tangent.glsllib index 308c13dd7f..4f46b5113c 100644 --- a/jme3-core/src/main/resources/Common/ShaderLib/Tangent.glsllib +++ b/jme3-core/src/main/resources/Common/ShaderLib/Tangent.glsllib @@ -5,7 +5,9 @@ void Tangent_ComputeVS(out vec3 outNormal, out vec3 outTangent){ outTangent = normalize(g_NormalMatrix * inTangent); } -mat3 Tangent_GetBasis(){ - vec3 wvBinormal = cross(wvNormal, wvTangent); - return mat3(wvTangent, wvBinormal, wvNormal); -} +mat3 Tangent_GetBasis(){ + vec3 n = normalize(wvNormal); + vec3 t = normalize(wvTangent - n * dot(wvTangent, n)); + vec3 b = normalize(cross(n, t)); + return mat3(t, b, n); +} diff --git a/jme3-core/src/main/resources/Common/ShaderLib/module/pbrlighting/PBRLightingUtils.glsllib b/jme3-core/src/main/resources/Common/ShaderLib/module/pbrlighting/PBRLightingUtils.glsllib index bf565b61fe..3e327f546f 100644 --- a/jme3-core/src/main/resources/Common/ShaderLib/module/pbrlighting/PBRLightingUtils.glsllib +++ b/jme3-core/src/main/resources/Common/ShaderLib/module/pbrlighting/PBRLightingUtils.glsllib @@ -305,8 +305,10 @@ surface.envLightContribution = vec3(0.0); #ifdef ENABLE_PBRLightingUtils_getWorldTangent - vec3 tan = normalize(wTangent.xyz); - surface.tbnMat = mat3(tan, wTangent.w * cross( surface.geometryNormal, tan), surface.geometryNormal); + vec3 n = normalize(surface.geometryNormal); + vec3 tan = normalize(wTangent.xyz - n * dot(wTangent.xyz, n)); + vec3 bitan = normalize(cross(n, tan)) * wTangent.w; + surface.tbnMat = mat3(tan, bitan, n); surface.hasTangents = true; #endif diff --git a/jme3-core/src/test/java/com/jme3/system/TestUtil.java b/jme3-core/src/test/java/com/jme3/system/TestUtil.java index b23d1846ee..80d57339f6 100644 --- a/jme3-core/src/test/java/com/jme3/system/TestUtil.java +++ b/jme3-core/src/test/java/com/jme3/system/TestUtil.java @@ -34,6 +34,7 @@ import com.jme3.asset.AssetConfig; import com.jme3.asset.AssetManager; import com.jme3.asset.DesktopAssetManager; +import com.jme3.material.TechniqueDef; import com.jme3.renderer.RenderManager; import com.jme3.renderer.Renderer; @@ -63,6 +64,7 @@ public static RenderManager createRenderManager() { public static RenderManager createRenderManager(Renderer renderer) { RenderManager rm = new RenderManager(renderer); + rm.setPreferredLightMode(TechniqueDef.LightMode.MultiPass); rm.setPassDrawBufferTargetIdToShaders(false); return rm; } diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.effects.TestIssue1773.testIssue1773_localSpace_f45.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.effects.TestIssue1773.testIssue1773_localSpace_f45.png index 1d1c733b3b..bdeeb01eb1 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.effects.TestIssue1773.testIssue1773_localSpace_f45.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.effects.TestIssue1773.testIssue1773_localSpace_f45.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.effects.TestIssue1773.testIssue1773_worldSpace_f45.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.effects.TestIssue1773.testIssue1773_worldSpace_f45.png index cf72a001e5..620d7bb3f2 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.effects.TestIssue1773.testIssue1773_worldSpace_f45.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.effects.TestIssue1773.testIssue1773_worldSpace_f45.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_DefaultDirectionalLight_f12.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_DefaultDirectionalLight_f12.png index 1589f38f1e..57d3ce522a 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_DefaultDirectionalLight_f12.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_DefaultDirectionalLight_f12.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_HighRoughness_f12.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_HighRoughness_f12.png index ab61568039..af62585963 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_HighRoughness_f12.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_HighRoughness_f12.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_LowRoughness_f12.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_LowRoughness_f12.png index 564abc0e5d..a87af6d06d 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_LowRoughness_f12.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_LowRoughness_f12.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_UpdatedDirectionalLight_f12.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_UpdatedDirectionalLight_f12.png index 4c764f9223..9c588afe5a 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_UpdatedDirectionalLight_f12.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRLighting.testPBRLighting_UpdatedDirectionalLight_f12.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRSimple.testPBRSimple_WithRealtimeBaking_f10.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRSimple.testPBRSimple_WithRealtimeBaking_f10.png index ac5bb874f5..88f2c2a0aa 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRSimple.testPBRSimple_WithRealtimeBaking_f10.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRSimple.testPBRSimple_WithRealtimeBaking_f10.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRSimple.testPBRSimple_WithoutRealtimeBaking_f10.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRSimple.testPBRSimple_WithoutRealtimeBaking_f10.png index ac5bb874f5..88f2c2a0aa 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRSimple.testPBRSimple_WithoutRealtimeBaking_f10.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.light.pbr.TestPBRSimple.testPBRSimple_WithoutRealtimeBaking_f10.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.material.TestSimpleBumps.testSimpleBumps_f10.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.material.TestSimpleBumps.testSimpleBumps_f10.png index 4608681de3..4bc480e4e3 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.material.TestSimpleBumps.testSimpleBumps_f10.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.material.TestSimpleBumps.testSimpleBumps_f10.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.material.TestSimpleBumps.testSimpleBumps_f60.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.material.TestSimpleBumps.testSimpleBumps_f60.png index 11ac17390f..bc7bc3ca84 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.material.TestSimpleBumps.testSimpleBumps_f60.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.material.TestSimpleBumps.testSimpleBumps_f60.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.post.TestCartoonEdge.testCartoonEdge_f1.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.post.TestCartoonEdge.testCartoonEdge_f1.png index 453219b949..0623641582 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.post.TestCartoonEdge.testCartoonEdge_f1.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.post.TestCartoonEdge.testCartoonEdge_f1.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.post.TestFog.testFog_f1.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.post.TestFog.testFog_f1.png index da77df67cc..39ac7fa79f 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.post.TestFog.testFog_f1.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.post.TestFog.testFog_f1.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.post.TestLightScattering.testLightScattering_f1.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.post.TestLightScattering.testLightScattering_f1.png index bf84144b5a..f8aff97dfd 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.post.TestLightScattering.testLightScattering_f1.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.post.TestLightScattering.testLightScattering_f1.png differ diff --git a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.water.TestPostWater.testPostWater_f1.png b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.water.TestPostWater.testPostWater_f1.png index eaeb6f7c02..cfc9503619 100644 Binary files a/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.water.TestPostWater.testPostWater_f1.png and b/jme3-screenshot-tests/src/test/resources/org.jmonkeyengine.screenshottests.water.TestPostWater.testPostWater_f1.png differ diff --git a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/SPTerrainLighting.frag b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/SPTerrainLighting.frag index 426b665e94..aab1558e23 100644 --- a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/SPTerrainLighting.frag +++ b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/SPTerrainLighting.frag @@ -609,7 +609,10 @@ void main(){ #else vec3 normal = calculateNormal(texCoord); #endif - mat3 tbnMat = mat3(normalize(vTangent.xyz) , normalize(vBinormal.xyz) , normalize(vNormal.xyz)); + vec3 tbnNormal = normalize(vNormal.xyz); + vec3 tbnTangent = normalize(vTangent.xyz - tbnNormal * dot(vTangent.xyz, tbnNormal)); + vec3 tbnBinormal = normalize(cross(tbnNormal, tbnTangent)) * sign(dot(vBinormal.xyz, cross(tbnNormal, tbnTangent))); + mat3 tbnMat = mat3(tbnTangent, tbnBinormal, tbnNormal); #else vec3 normal = vNormal; #endif diff --git a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/SPTerrainLighting.vert b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/SPTerrainLighting.vert index 497f06b90c..e806ca8968 100644 --- a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/SPTerrainLighting.vert +++ b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/SPTerrainLighting.vert @@ -44,8 +44,9 @@ void main(){ // specific to normal maps: //-------------------------- #if defined(NORMALMAP) || defined(NORMALMAP_1) || defined(NORMALMAP_2) || defined(NORMALMAP_3) || defined(NORMALMAP_4) || defined(NORMALMAP_5) || defined(NORMALMAP_6) || defined(NORMALMAP_7) || defined(NORMALMAP_8) || defined(NORMALMAP_9) || defined(NORMALMAP_10) || defined(NORMALMAP_11) - vTangent = g_NormalMatrix * inTangent.xyz; - vBinormal = cross(wvNormal, vTangent)* inTangent.w; + vTangent = normalize(g_NormalMatrix * inTangent.xyz); + vTangent = normalize(vTangent - wvNormal * dot(vTangent, wvNormal)); + vBinormal = normalize(cross(wvNormal, vTangent)) * inTangent.w; #endif //------------------------- @@ -64,4 +65,4 @@ void main(){ wNormal = inNormal; #endif -} \ No newline at end of file +} diff --git a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/TerrainLighting.vert b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/TerrainLighting.vert index 04ddbe5a20..b6438fc98d 100644 --- a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/TerrainLighting.vert +++ b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/TerrainLighting.vert @@ -55,17 +55,19 @@ void main(){ wvLightPos.w = g_LightPosition.w; vec4 lightColor = g_LightColor; - //-------------------------- - // specific to normal maps: - //-------------------------- - #if defined(NORMALMAP) || defined(NORMALMAP_1) || defined(NORMALMAP_2) || defined(NORMALMAP_3) || defined(NORMALMAP_4) || defined(NORMALMAP_5) || defined(NORMALMAP_6) || defined(NORMALMAP_7) || defined(NORMALMAP_8) || defined(NORMALMAP_9) || defined(NORMALMAP_10) || defined(NORMALMAP_11) - vec3 wvTangent = normalize(g_NormalMatrix * inTangent.xyz); - vec3 wvBinormal = cross(wvNormal, wvTangent); - - mat3 tbnMat = mat3(wvTangent, wvBinormal * inTangent.w,wvNormal); - - vPosition = wvPosition * tbnMat; - vViewDir = viewDir * tbnMat; + //-------------------------- + // specific to normal maps: + //-------------------------- + #if defined(NORMALMAP) || defined(NORMALMAP_1) || defined(NORMALMAP_2) || defined(NORMALMAP_3) || defined(NORMALMAP_4) || defined(NORMALMAP_5) || defined(NORMALMAP_6) || defined(NORMALMAP_7) || defined(NORMALMAP_8) || defined(NORMALMAP_9) || defined(NORMALMAP_10) || defined(NORMALMAP_11) + vec3 tbnNormal = normalize(wvNormal); + vec3 wvTangent = normalize(g_NormalMatrix * inTangent.xyz); + wvTangent = normalize(wvTangent - tbnNormal * dot(wvTangent, tbnNormal)); + vec3 wvBinormal = normalize(cross(tbnNormal, wvTangent)) * inTangent.w; + + mat3 tbnMat = mat3(wvTangent, wvBinormal, tbnNormal); + + vPosition = wvPosition * tbnMat; + vViewDir = viewDir * tbnMat; lightComputeDir(wvPosition, lightColor.w, wvLightPos, vLightDir, lightVec); vLightDir.xyz = (vLightDir.xyz * tbnMat).xyz; @@ -92,4 +94,4 @@ void main(){ wNormal = inNormal; #endif -} \ No newline at end of file +}