Skip to content

Commit

Permalink
Add support for AoStrength in PBRLighting & GltfLoader (#1981)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Ali-RS authored Mar 11, 2023
1 parent f771f9b commit 458c1b2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ varying vec3 wPosition;
#ifdef LIGHTMAP
uniform sampler2D m_LightMap;
#endif

#ifdef AO_STRENGTH
uniform float m_AoStrength;
#endif

#if defined(NORMALMAP) || defined(PARALLAXMAP)
uniform sampler2D m_NormalMap;
Expand Down Expand Up @@ -237,6 +241,12 @@ void main(){
ao = aoRoughnessMetallicValue.rrr;
#endif

#ifdef AO_STRENGTH
ao = 1.0 + m_AoStrength * (ao - 1.0);
// sanity check
ao = clamp(ao, 0.0, 1.0);
#endif

float ndotv = max( dot( normal, viewDir ),0.0);
for( int i = 0;i < NB_LIGHTS; i+=3){
vec4 lightColor = g_LightData[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ MaterialDef PBR Lighting {
// Set to Use Lightmap
Texture2D LightMap

// A scalar multiplier controlling the amount of occlusion applied.
// A value of `0.0` means no occlusion. A value of `1.0` means full occlusion.
Float AoStrength

// Set to use TexCoord2 for the lightmap sampling
Boolean SeparateTexCoord
// the light map is a grayscale ao map, only the r channel will be read.
Expand Down Expand Up @@ -162,6 +166,7 @@ MaterialDef PBR Lighting {
VERTEX_COLOR : UseVertexColor
AO_MAP: LightMapAsAOMap
AO_PACKED_IN_MR_MAP : AoPackedInMRMap
AO_STRENGTH : AoStrength
NUM_MORPH_TARGETS: NumberOfMorphTargets
NUM_TARGETS_BUFFERS: NumberOfTargetsBuffers
HORIZON_FADE: HorizonFade
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,12 @@ public Material readMaterial(int materialIndex) throws IOException {
} else {
adapter.setParam("occlusionTexture", readTexture(matData.getAsJsonObject("occlusionTexture")));
}

Float occlusionStrength = occlusionJson != null ? getAsFloat(occlusionJson, "strength") : null;
if (occlusionStrength != null) {
adapter.setParam("occlusionStrength", occlusionStrength);
}

adapter.setParam("emissiveTexture", readTexture(matData.getAsJsonObject("emissiveTexture")));

return adapter.getMaterial();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public PBRMaterialAdapter() {
addParamMapping("normalTexture", "NormalMap");
addParamMapping("normalScale", "NormalScale");
addParamMapping("occlusionTexture", "LightMap");
addParamMapping("occlusionStrength", "AoStrength");
addParamMapping("emissiveTexture", "EmissiveMap");
addParamMapping("emissiveFactor", "Emissive");
addParamMapping("alphaMode", "alpha");
Expand Down

0 comments on commit 458c1b2

Please sign in to comment.