From 6e8f34179460c8eb21f1ff635eaffbf589a34140 Mon Sep 17 00:00:00 2001 From: Ryan McDonough Date: Sat, 10 Oct 2020 00:07:13 -0400 Subject: [PATCH 1/4] Packing AO in the metallic roughness map Updated frag shader to support reading the AmbientOcclusion value from the Red channel of the packed MetallicRoughness map. Added a single boolean define that does this when set to true, so long as a metallic roughness map is also defined. --- .../Common/MatDefs/Light/PBRLighting.frag | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag index 4eaa6a88f5..71c5c5f4b8 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag @@ -134,10 +134,12 @@ void main(){ vec4 albedo = Color; #endif + //ao in r channel, roughness in green channel, metallic in blue channel! + vec3 metallicRoughnessAoValue = vec3(1.0, 1.0, 0.0); #ifdef USE_PACKED_MR - vec2 rm = texture2D(m_MetallicRoughnessMap, newTexCoord).gb; - float Roughness = rm.x * max(m_Roughness, 1e-4); - float Metallic = rm.y * max(m_Metallic, 0.0); + metallicRoughnessAoValue = texture2D(m_MetallicRoughnessMap, newTexCoord).rgb; + float Roughness = metallicRoughnessAoValue.g * max(m_Roughness, 1e-4); + float Metallic = metallicRoughnessAoValue.b * max(m_Metallic, 0.0); #else #ifdef ROUGHNESSMAP float Roughness = texture2D(m_RoughnessMap, newTexCoord).r * max(m_Roughness, 1e-4); @@ -208,19 +210,24 @@ void main(){ gl_FragColor.rgb = vec3(0.0); vec3 ao = vec3(1.0); - #ifdef LIGHTMAP + #if defined(LIGHTMAP) || ( defined(AO_PACKED_IN_MR_MAP) && defined(USE_PACKED_MR) ) vec3 lightMapColor; - #ifdef SEPARATE_TEXCOORD - lightMapColor = texture2D(m_LightMap, texCoord2).rgb; - #else - lightMapColor = texture2D(m_LightMap, texCoord).rgb; + #ifdef AO_PACKED_IN_MR_MAP + lightMapColor = metallicRoughnessAoValue.rrr; //ao value packed in red channel of MR map + #else + #ifdef SEPARATE_TEXCOORD + lightMapColor = texture2D(m_LightMap, texCoord2).rgb; + #else + lightMapColor = texture2D(m_LightMap, texCoord).rgb; + #endif #endif - #ifdef AO_MAP - lightMapColor.gb = lightMapColor.rr; - ao = lightMapColor; + #if defined(AO_MAP) || defined(AO_PACKED_IN_MR_MAP) + lightMapColor.gb = lightMapColor.rr; + ao = lightMapColor; #else - gl_FragColor.rgb += diffuseColor.rgb * lightMapColor; + gl_FragColor.rgb += diffuseColor.rgb * lightMapColor; #endif + specularColor.rgb *= lightMapColor; #endif From ede2c7ad2fdb4a045ddab078262e058d861a6f63 Mon Sep 17 00:00:00 2001 From: Ryan McDonough Date: Sat, 10 Oct 2020 04:29:30 -0400 Subject: [PATCH 2/4] Update PBRLighting.j3md --- .../src/main/resources/Common/MatDefs/Light/PBRLighting.j3md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 7f0e970018..7d131ac92c 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md @@ -72,7 +72,7 @@ MaterialDef PBR Lighting { Boolean SeparateTexCoord // the light map is a gray scale ao map, on ly the r channel will be read. Boolean LightMapAsAOMap - + Boolean AoPackedInMRMap //shadows Int FilterMode Boolean HardwareShadows @@ -158,6 +158,7 @@ MaterialDef PBR Lighting { NORMAL_TYPE: NormalType VERTEX_COLOR : UseVertexColor AO_MAP: LightMapAsAOMap + AO_PACKED_IN_MR_MAP : AoPackedInMRMap NUM_MORPH_TARGETS: NumberOfMorphTargets NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers HORIZON_FADE: HorizonFade From 2c37f2420a9487aafbbde5218dc2ed80cb356f43 Mon Sep 17 00:00:00 2001 From: Ryan McDonough Date: Sat, 10 Oct 2020 04:31:49 -0400 Subject: [PATCH 3/4] Update PBRLighting.frag --- .../Common/MatDefs/Light/PBRLighting.frag | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag index 71c5c5f4b8..c259f529d1 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag @@ -135,11 +135,11 @@ void main(){ #endif //ao in r channel, roughness in green channel, metallic in blue channel! - vec3 metallicRoughnessAoValue = vec3(1.0, 1.0, 0.0); + vec3 aoRoughnessMetallicValue = vec3(1.0, 1.0, 0.0); #ifdef USE_PACKED_MR - metallicRoughnessAoValue = texture2D(m_MetallicRoughnessMap, newTexCoord).rgb; - float Roughness = metallicRoughnessAoValue.g * max(m_Roughness, 1e-4); - float Metallic = metallicRoughnessAoValue.b * max(m_Metallic, 0.0); + aoRoughnessMetallicValue = texture2D(m_MetallicRoughnessMap, newTexCoord).rgb; + float Roughness = aoRoughnessMetallicValue.g * max(m_Roughness, 1e-4); + float Metallic = aoRoughnessMetallicValue.b * max(m_Metallic, 0.0); #else #ifdef ROUGHNESSMAP float Roughness = texture2D(m_RoughnessMap, newTexCoord).r * max(m_Roughness, 1e-4); @@ -210,27 +210,25 @@ void main(){ gl_FragColor.rgb = vec3(0.0); vec3 ao = vec3(1.0); - #if defined(LIGHTMAP) || ( defined(AO_PACKED_IN_MR_MAP) && defined(USE_PACKED_MR) ) + #ifdef LIGHTMAP vec3 lightMapColor; - #ifdef AO_PACKED_IN_MR_MAP - lightMapColor = metallicRoughnessAoValue.rrr; //ao value packed in red channel of MR map - #else - #ifdef SEPARATE_TEXCOORD - lightMapColor = texture2D(m_LightMap, texCoord2).rgb; - #else - lightMapColor = texture2D(m_LightMap, texCoord).rgb; - #endif + #ifdef SEPARATE_TEXCOORD + lightMapColor = texture2D(m_LightMap, texCoord2).rgb; + #else + lightMapColor = texture2D(m_LightMap, texCoord).rgb; #endif - #if defined(AO_MAP) || defined(AO_PACKED_IN_MR_MAP) - lightMapColor.gb = lightMapColor.rr; - ao = lightMapColor; + #ifdef AO_MAP + lightMapColor.gb = lightMapColor.rr; + ao = lightMapColor; #else - gl_FragColor.rgb += diffuseColor.rgb * lightMapColor; + gl_FragColor.rgb += diffuseColor.rgb * lightMapColor; #endif - specularColor.rgb *= lightMapColor; #endif + #if defined(AO_PACKED_IN_MR_MAP) && defined(USE_PACKED_MR) + ao *= aoRoughnessMetallicValue.rrr; + #endif float ndotv = max( dot( normal, viewDir ),0.0); for( int i = 0;i < NB_LIGHTS; i+=3){ From 22cce9483ad664aa859dcd434c945926d10757d3 Mon Sep 17 00:00:00 2001 From: Ryan McDonough Date: Sat, 10 Oct 2020 04:43:02 -0400 Subject: [PATCH 4/4] Update PBRLighting.frag --- .../src/main/resources/Common/MatDefs/Light/PBRLighting.frag | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag index c259f529d1..b551f5a67b 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag @@ -227,7 +227,7 @@ void main(){ #endif #if defined(AO_PACKED_IN_MR_MAP) && defined(USE_PACKED_MR) - ao *= aoRoughnessMetallicValue.rrr; + ao = aoRoughnessMetallicValue.rrr; #endif float ndotv = max( dot( normal, viewDir ),0.0);