Pārlūkot izejas kodu

Fixed a crash when loading a gltf file where textures has no sampler entry.
Also adapted how the light map is handled

Nehon 8 gadi atpakaļ
vecāks
revīzija
510562a62d

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

@@ -200,6 +200,9 @@ void main(){
        #else
           lightMapColor = texture2D(m_LightMap, texCoord).rgb;
        #endif
+       #ifdef AO_MAP
+         lightMapColor.gb = lightMapColor.rr;
+       #endif
        specularColor.rgb *= lightMapColor;
        albedo.rgb  *= lightMapColor;
     #endif

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

@@ -75,6 +75,8 @@ MaterialDef PBR Lighting {
 
         // Set to use TexCoord2 for the lightmap sampling
         Boolean SeparateTexCoord
+        // the light map is a gray scale ao map, on ly the r channel will be read.
+        Boolean LightMapAsAOMap
 
         //shadows
         Int FilterMode
@@ -154,6 +156,7 @@ MaterialDef PBR Lighting {
             GLOSSINESSMAP : GlossinessMap
             NORMAL_TYPE: NormalType
             VERTEX_COLOR : UseVertexColor
+            AO_MAP: LightMapAsAOMap
         }
     }
 

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

@@ -633,7 +633,12 @@ public class GltfLoader implements AssetLoader {
         Integer samplerIndex = getAsInteger(textureData, "sampler");
 
         Texture2D texture2d = readImage(sourceIndex, flip);
-        texture2d = readSampler(samplerIndex, texture2d);
+
+        if (samplerIndex != null) {
+            texture2d = readSampler(samplerIndex, texture2d);
+        } else {
+            texture2d.setWrap(Texture.WrapMode.Repeat);
+        }
 
         texture2d = customContentManager.readExtensionAndExtras("texture", texture, texture2d);
 

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

@@ -46,6 +46,12 @@ public abstract class PBRMaterialAdapter extends MaterialAdapter {
             //Set the normal map type to OpenGl
             getMaterial().setFloat("NormalType", 1.0f);
         }
+        if (param.getName().equals("LightMap")) {
+            //Gltf only supports AO maps (gray scales and only the r channel must be read)
+            getMaterial().setBoolean("LightMapAsAOMap", true);
+        }
+
+
 
 
         return param;