Skip to content

Commit 458c1b2

Browse files
authored
Add support for AoStrength in PBRLighting & GltfLoader (#1981)
* Added AoStrength factor in PBRLighting. A scalar multiplier controlling the amount of occlusion applied. * Add support for reading AoStrength in GltfLoader. * Fix ao calculation to follow gltf specs. * Update comment on AoStrength mentioning the min and max values. * Clamp ao to 0 for negative values that might cause by applying AoStrength > 1. * Use glsl clamp instead of max.
1 parent f771f9b commit 458c1b2

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ varying vec3 wPosition;
8484
#ifdef LIGHTMAP
8585
uniform sampler2D m_LightMap;
8686
#endif
87+
88+
#ifdef AO_STRENGTH
89+
uniform float m_AoStrength;
90+
#endif
8791

8892
#if defined(NORMALMAP) || defined(PARALLAXMAP)
8993
uniform sampler2D m_NormalMap;
@@ -237,6 +241,12 @@ void main(){
237241
ao = aoRoughnessMetallicValue.rrr;
238242
#endif
239243

244+
#ifdef AO_STRENGTH
245+
ao = 1.0 + m_AoStrength * (ao - 1.0);
246+
// sanity check
247+
ao = clamp(ao, 0.0, 1.0);
248+
#endif
249+
240250
float ndotv = max( dot( normal, viewDir ),0.0);
241251
for( int i = 0;i < NB_LIGHTS; i+=3){
242252
vec4 lightColor = g_LightData[i];

jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ MaterialDef PBR Lighting {
7070
// Set to Use Lightmap
7171
Texture2D LightMap
7272

73+
// A scalar multiplier controlling the amount of occlusion applied.
74+
// A value of `0.0` means no occlusion. A value of `1.0` means full occlusion.
75+
Float AoStrength
76+
7377
// Set to use TexCoord2 for the lightmap sampling
7478
Boolean SeparateTexCoord
7579
// the light map is a grayscale ao map, only the r channel will be read.
@@ -162,6 +166,7 @@ MaterialDef PBR Lighting {
162166
VERTEX_COLOR : UseVertexColor
163167
AO_MAP: LightMapAsAOMap
164168
AO_PACKED_IN_MR_MAP : AoPackedInMRMap
169+
AO_STRENGTH : AoStrength
165170
NUM_MORPH_TARGETS: NumberOfMorphTargets
166171
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
167172
HORIZON_FADE: HorizonFade

jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,12 @@ public Material readMaterial(int materialIndex) throws IOException {
673673
} else {
674674
adapter.setParam("occlusionTexture", readTexture(matData.getAsJsonObject("occlusionTexture")));
675675
}
676+
677+
Float occlusionStrength = occlusionJson != null ? getAsFloat(occlusionJson, "strength") : null;
678+
if (occlusionStrength != null) {
679+
adapter.setParam("occlusionStrength", occlusionStrength);
680+
}
681+
676682
adapter.setParam("emissiveTexture", readTexture(matData.getAsJsonObject("emissiveTexture")));
677683

678684
return adapter.getMaterial();

jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/PBRMaterialAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public PBRMaterialAdapter() {
4242
addParamMapping("normalTexture", "NormalMap");
4343
addParamMapping("normalScale", "NormalScale");
4444
addParamMapping("occlusionTexture", "LightMap");
45+
addParamMapping("occlusionStrength", "AoStrength");
4546
addParamMapping("emissiveTexture", "EmissiveMap");
4647
addParamMapping("emissiveFactor", "Emissive");
4748
addParamMapping("alphaMode", "alpha");

0 commit comments

Comments
 (0)