Generates mikkt space tangents when a geometry has no tangent bufer but uses a material with a normal map

fix-456
Nehon 7 years ago committed by Rémy Bouquet
parent 40c4f7936d
commit 4daa0b5d9b
  1. 16
      jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java
  2. 2
      jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/PBRMaterialAdapter.java

@ -10,6 +10,7 @@ import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.*; import com.jme3.scene.*;
import com.jme3.texture.Texture; import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D; import com.jme3.texture.Texture2D;
import com.jme3.util.mikktspace.MikktspaceTangentGenerator;
import java.io.*; import java.io.*;
import java.nio.Buffer; import java.nio.Buffer;
@ -44,6 +45,7 @@ public class GltfLoader implements AssetLoader {
private AssetInfo info; private AssetInfo info;
private static Map<String, MaterialAdapter> defaultMaterialAdapters = new HashMap<>(); private static Map<String, MaterialAdapter> defaultMaterialAdapters = new HashMap<>();
private boolean useNormalsFlag = false;
static { static {
defaultMaterialAdapters.put("pbrMetallicRoughness", new PBRMaterialAdapter()); defaultMaterialAdapters.put("pbrMetallicRoughness", new PBRMaterialAdapter());
@ -131,11 +133,9 @@ public class GltfLoader implements AssetLoader {
activeChild = defaultScene.getAsInt(); activeChild = defaultScene.getAsInt();
} }
root.getChild(activeChild).setCullHint(Spatial.CullHint.Inherit); root.getChild(activeChild).setCullHint(Spatial.CullHint.Inherit);
System.err.println(nbPrim + " Geoms loaded");
return root; return root;
} }
int nbPrim = 0;
private Spatial loadNode(int nodeIndex) throws IOException { private Spatial loadNode(int nodeIndex) throws IOException {
Spatial spatial = fetchFromCache("nodes", nodeIndex, Spatial.class); Spatial spatial = fetchFromCache("nodes", nodeIndex, Spatial.class);
if (spatial != null) { if (spatial != null) {
@ -165,7 +165,6 @@ public class GltfLoader implements AssetLoader {
} }
spatial = node; spatial = node;
} }
nbPrim += primitives.length;
spatial.setName(loadMeshName(meshIndex)); spatial.setName(loadMeshName(meshIndex));
} else { } else {
@ -271,11 +270,16 @@ public class GltfLoader implements AssetLoader {
if (materialIndex == null) { if (materialIndex == null) {
geom.setMaterial(defaultMat); geom.setMaterial(defaultMat);
} else { } else {
useNormalsFlag = false;
geom.setMaterial(loadMaterial(materialIndex)); geom.setMaterial(loadMaterial(materialIndex));
if (geom.getMaterial().getAdditionalRenderState().getBlendMode() == RenderState.BlendMode.Alpha) { if (geom.getMaterial().getAdditionalRenderState().getBlendMode() == RenderState.BlendMode.Alpha) {
//Alpha blending is on on this material let's place the geom in the transparent bucket //Alpha blending is on on this material let's place the geom in the transparent bucket
geom.setQueueBucket(RenderQueue.Bucket.Transparent); geom.setQueueBucket(RenderQueue.Bucket.Transparent);
} }
if (useNormalsFlag && mesh.getBuffer(VertexBuffer.Type.Tangent) == null) {
//No tangent buffer, but there is a normal map, we have to generate them using MiiktSpace
MikktspaceTangentGenerator.generate(geom);
}
} }
if (name != null) { if (name != null) {
@ -428,7 +432,11 @@ public class GltfLoader implements AssetLoader {
adapter.setParam(mat, "baseColorTexture", readTexture(pbrMat.getAsJsonObject("baseColorTexture"))); adapter.setParam(mat, "baseColorTexture", readTexture(pbrMat.getAsJsonObject("baseColorTexture")));
adapter.setParam(mat, "metallicRoughnessTexture", readTexture(pbrMat.getAsJsonObject("metallicRoughnessTexture"))); adapter.setParam(mat, "metallicRoughnessTexture", readTexture(pbrMat.getAsJsonObject("metallicRoughnessTexture")));
adapter.setParam(mat, "normalTexture", readTexture(matData.getAsJsonObject("normalTexture"))); Texture2D normal = readTexture(matData.getAsJsonObject("normalTexture"));
adapter.setParam(mat, "normalTexture", normal);
if (normal != null) {
useNormalsFlag = true;
}
adapter.setParam(mat, "occlusionTexture", readTexture(matData.getAsJsonObject("occlusionTexture"))); adapter.setParam(mat, "occlusionTexture", readTexture(matData.getAsJsonObject("occlusionTexture")));
adapter.setParam(mat, "emissiveTexture", readTexture(matData.getAsJsonObject("emissiveTexture"))); adapter.setParam(mat, "emissiveTexture", readTexture(matData.getAsJsonObject("emissiveTexture")));

@ -19,7 +19,7 @@ public class PBRMaterialAdapter extends MaterialAdapter {
addParamMapping("emissiveTexture", "EmissiveMap"); addParamMapping("emissiveTexture", "EmissiveMap");
addParamMapping("emissiveFactor", "Emissive"); addParamMapping("emissiveFactor", "Emissive");
addParamMapping("alphaMode", "alpha"); addParamMapping("alphaMode", "alpha");
// addParamMapping("alphaCutoff", "AlphaDiscardThreshold"); addParamMapping("alphaCutoff", "AlphaDiscardThreshold");
addParamMapping("doubleSided", "doubleSided"); addParamMapping("doubleSided", "doubleSided");
} }

Loading…
Cancel
Save