glTF: Fixes models orientations issues

empirephoenix-patch-1
Nehon 7 years ago
parent a741dc66f4
commit 8a8d381c0e
  1. 51
      jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java

@ -83,6 +83,7 @@ public class GltfLoader implements AssetLoader {
try { try {
dataCache.clear(); dataCache.clear();
info = assetInfo; info = assetInfo;
skinnedSpatials.clear();
rootNode = new Node(); rootNode = new Node();
if (defaultMat == null) { if (defaultMat == null) {
@ -127,6 +128,8 @@ public class GltfLoader implements AssetLoader {
rootNode = customContentManager.readExtensionAndExtras("root", docRoot, rootNode); rootNode = customContentManager.readExtensionAndExtras("root", docRoot, rootNode);
setupControls();
//Loading animations //Loading animations
if (animations != null) { if (animations != null) {
for (int i = 0; i < animations.size(); i++) { for (int i = 0; i < animations.size(); i++) {
@ -134,8 +137,6 @@ public class GltfLoader implements AssetLoader {
} }
} }
setupControls();
//only one scene let's not return the root. //only one scene let's not return the root.
if (rootNode.getChildren().size() == 1) { if (rootNode.getChildren().size() == 1) {
rootNode = (Node) rootNode.getChild(0); rootNode = (Node) rootNode.getChild(0);
@ -865,16 +866,10 @@ public class GltfLoader implements AssetLoader {
if (skinIndex != -1) { if (skinIndex != -1) {
//we have a bone animation. //we have a bone animation.
SkinData skin = fetchFromCache("skins", skinIndex, SkinData.class); SkinData skin = fetchFromCache("skins", skinIndex, SkinData.class);
if (skin.animControl == null) {
skin.animControl = new AnimControl(skin.skeletonControl.getSkeleton());
}
skin.animControl.addAnim(anim); skin.animControl.addAnim(anim);
//the controls will be added to the right spatial in setupControls()
} }
if (!spatials.isEmpty()) { if (!spatials.isEmpty()) {
//Note that it's pretty unlikely to have an animation that is both a spatial animation and a bone animation...But you never know. The specs doesn't forbids it
if (skinIndex != -1) { if (skinIndex != -1) {
//there are some spatial tracks in this bone animation... or the other way around. Let's add the spatials in the skinnedSpatials. //there are some spatial tracks in this bone animation... or the other way around. Let's add the spatials in the skinnedSpatials.
SkinData skin = fetchFromCache("skins", skinIndex, SkinData.class); SkinData skin = fetchFromCache("skins", skinIndex, SkinData.class);
@ -882,6 +877,7 @@ public class GltfLoader implements AssetLoader {
spat.addAll(spatials); spat.addAll(spatials);
//the animControl will be added in the setupControls(); //the animControl will be added in the setupControls();
} else { } else {
//Spatial animation
Spatial spatial = null; Spatial spatial = null;
if (spatials.size() == 1) { if (spatials.size() == 1) {
spatial = spatials.get(0); spatial = spatials.get(0);
@ -971,6 +967,14 @@ public class GltfLoader implements AssetLoader {
computeBindTransforms(bw, skeleton); computeBindTransforms(bw, skeleton);
} }
skeleton = customContentManager.readExtensionAndExtras("skin", skin, skeleton);
SkinData skinData = new SkinData();
skinData.bones = bones;
skinData.skeletonControl = new SkeletonControl(skeleton);
skinData.animControl = new AnimControl(skinData.skeletonControl.getSkeleton());
addToCache("skins", index, skinData, nodes.size());
skinnedSpatials.put(skinData, new ArrayList<Spatial>());
// Set local transforms. // Set local transforms.
// The skeleton may come in a given pose, that is not the rest pose, so let 's apply it. // The skeleton may come in a given pose, that is not the rest pose, so let 's apply it.
// We will need it later for animation // We will need it later for animation
@ -985,14 +989,6 @@ public class GltfLoader implements AssetLoader {
bone.setUserControl(false); bone.setUserControl(false);
} }
} }
skeleton = customContentManager.readExtensionAndExtras("skin", skin, skeleton);
SkinData skinData = new SkinData();
skinData.bones = bones;
skinData.skeletonControl = new SkeletonControl(skeleton);
addToCache("skins", index, skinData, nodes.size());
skinnedSpatials.put(skinData, new ArrayList<Spatial>());
} }
} }
@ -1089,26 +1085,18 @@ public class GltfLoader implements AssetLoader {
private void setupControls() { private void setupControls() {
for (SkinData skinData : skinnedSpatials.keySet()) { for (SkinData skinData : skinnedSpatials.keySet()) {
List<Spatial> spatials = skinnedSpatials.get(skinData); List<Spatial> spatials = skinnedSpatials.get(skinData);
if (spatials.isEmpty()) {
//can happen when a file contains a skin that is not used by any mesh...
continue;
}
Spatial spatial = skinData.parent; Spatial spatial = skinData.parent;
if (spatials.size() >= 1) { if (spatials.size() >= 1) {
spatial = findCommonAncestor(spatials); spatial = findCommonAncestor(spatials);
} }
AnimControl animControl = spatial.getControl(AnimControl.class); if (spatial != skinData.parent) {
if (animControl != null) { skinData.rootBoneTransformOffset = spatial.getWorldTransform().invert();
//The spatial already has an anim control, we need to merge it with the one in skinData. Then remove it. skinData.rootBoneTransformOffset.combineWithParent(skinData.parent.getWorldTransform());
for (String name : animControl.getAnimationNames()) {
Animation anim = animControl.getAnim(name);
skinData.animControl.addAnim(anim);
}
spatial.removeControl(animControl);
} }
if (skinData.animControl != null) { if (skinData.animControl != null && skinData.animControl.getSpatial() == null) {
spatial.addControl(skinData.animControl); spatial.addControl(skinData.animControl);
} }
spatial.addControl(skinData.skeletonControl); spatial.addControl(skinData.skeletonControl);
@ -1219,9 +1207,9 @@ public class GltfLoader implements AssetLoader {
Vector3f scale = getScale(data, i); Vector3f scale = getScale(data, i);
Transform t = new Transform(translation, rotation, scale); Transform t = new Transform(translation, rotation, scale);
if (isRoot) { if (isRoot && skinData.rootBoneTransformOffset != null) {
//Apply the armature transforms to the root bone anim track. //Apply the armature transforms to the root bone anim track.
t.combineWithParent(skinData.parent.getLocalTransform()); t.combineWithParent(skinData.rootBoneTransformOffset);
} }
reverseBlendAnimTransforms(t, bindTransforms); reverseBlendAnimTransforms(t, bindTransforms);
@ -1287,6 +1275,7 @@ public class GltfLoader implements AssetLoader {
SkeletonControl skeletonControl; SkeletonControl skeletonControl;
AnimControl animControl; AnimControl animControl;
Spatial parent; Spatial parent;
Transform rootBoneTransformOffset;
Bone[] bones; Bone[] bones;
boolean used = false; boolean used = false;
} }

Loading…
Cancel
Save