Parcourir la source

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.
Ali-RS il y a 2 ans
Parent
commit
458c1b2297

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

@@ -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;   
@@ -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];

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

@@ -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.
@@ -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

+ 6 - 0
jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java

@@ -673,6 +673,12 @@ public class GltfLoader implements AssetLoader {
         } 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();

+ 1 - 0
jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/PBRMaterialAdapter.java

@@ -42,6 +42,7 @@ public abstract class PBRMaterialAdapter extends MaterialAdapter {
         addParamMapping("normalTexture", "NormalMap");
         addParamMapping("normalScale", "NormalScale");
         addParamMapping("occlusionTexture", "LightMap");
+        addParamMapping("occlusionStrength", "AoStrength");
         addParamMapping("emissiveTexture", "EmissiveMap");
         addParamMapping("emissiveFactor", "Emissive");
         addParamMapping("alphaMode", "alpha");