Skip to content

Commit 8a8d381

Browse files
committed
glTF: Fixes models orientations issues
1 parent a741dc6 commit 8a8d381

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ protected Object loadFromStream(AssetInfo assetInfo, InputStream stream) throws
8383
try {
8484
dataCache.clear();
8585
info = assetInfo;
86+
skinnedSpatials.clear();
8687
rootNode = new Node();
8788

8889
if (defaultMat == null) {
@@ -127,15 +128,15 @@ protected Object loadFromStream(AssetInfo assetInfo, InputStream stream) throws
127128

128129
rootNode = customContentManager.readExtensionAndExtras("root", docRoot, rootNode);
129130

131+
setupControls();
132+
130133
//Loading animations
131134
if (animations != null) {
132135
for (int i = 0; i < animations.size(); i++) {
133136
readAnimation(i);
134137
}
135138
}
136139

137-
setupControls();
138-
139140
//only one scene let's not return the root.
140141
if (rootNode.getChildren().size() == 1) {
141142
rootNode = (Node) rootNode.getChild(0);
@@ -865,23 +866,18 @@ public void readAnimation(int animationIndex) throws IOException {
865866
if (skinIndex != -1) {
866867
//we have a bone animation.
867868
SkinData skin = fetchFromCache("skins", skinIndex, SkinData.class);
868-
if (skin.animControl == null) {
869-
skin.animControl = new AnimControl(skin.skeletonControl.getSkeleton());
870-
}
871869
skin.animControl.addAnim(anim);
872-
//the controls will be added to the right spatial in setupControls()
873870
}
874871

875-
876872
if (!spatials.isEmpty()) {
877-
//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
878873
if (skinIndex != -1) {
879874
//there are some spatial tracks in this bone animation... or the other way around. Let's add the spatials in the skinnedSpatials.
880875
SkinData skin = fetchFromCache("skins", skinIndex, SkinData.class);
881876
List<Spatial> spat = skinnedSpatials.get(skin);
882877
spat.addAll(spatials);
883878
//the animControl will be added in the setupControls();
884879
} else {
880+
//Spatial animation
885881
Spatial spatial = null;
886882
if (spatials.size() == 1) {
887883
spatial = spatials.get(0);
@@ -971,6 +967,14 @@ public void readSkins() throws IOException {
971967
computeBindTransforms(bw, skeleton);
972968
}
973969

970+
skeleton = customContentManager.readExtensionAndExtras("skin", skin, skeleton);
971+
SkinData skinData = new SkinData();
972+
skinData.bones = bones;
973+
skinData.skeletonControl = new SkeletonControl(skeleton);
974+
skinData.animControl = new AnimControl(skinData.skeletonControl.getSkeleton());
975+
addToCache("skins", index, skinData, nodes.size());
976+
skinnedSpatials.put(skinData, new ArrayList<Spatial>());
977+
974978
// Set local transforms.
975979
// The skeleton may come in a given pose, that is not the rest pose, so let 's apply it.
976980
// We will need it later for animation
@@ -985,14 +989,6 @@ public void readSkins() throws IOException {
985989
bone.setUserControl(false);
986990
}
987991
}
988-
989-
skeleton = customContentManager.readExtensionAndExtras("skin", skin, skeleton);
990-
991-
SkinData skinData = new SkinData();
992-
skinData.bones = bones;
993-
skinData.skeletonControl = new SkeletonControl(skeleton);
994-
addToCache("skins", index, skinData, nodes.size());
995-
skinnedSpatials.put(skinData, new ArrayList<Spatial>());
996992
}
997993
}
998994

@@ -1089,26 +1085,18 @@ private void findChildren(int nodeIndex) throws IOException {
10891085
private void setupControls() {
10901086
for (SkinData skinData : skinnedSpatials.keySet()) {
10911087
List<Spatial> spatials = skinnedSpatials.get(skinData);
1092-
if (spatials.isEmpty()) {
1093-
//can happen when a file contains a skin that is not used by any mesh...
1094-
continue;
1095-
}
10961088
Spatial spatial = skinData.parent;
1089+
10971090
if (spatials.size() >= 1) {
10981091
spatial = findCommonAncestor(spatials);
10991092
}
11001093

1101-
AnimControl animControl = spatial.getControl(AnimControl.class);
1102-
if (animControl != null) {
1103-
//The spatial already has an anim control, we need to merge it with the one in skinData. Then remove it.
1104-
for (String name : animControl.getAnimationNames()) {
1105-
Animation anim = animControl.getAnim(name);
1106-
skinData.animControl.addAnim(anim);
1107-
}
1108-
spatial.removeControl(animControl);
1094+
if (spatial != skinData.parent) {
1095+
skinData.rootBoneTransformOffset = spatial.getWorldTransform().invert();
1096+
skinData.rootBoneTransformOffset.combineWithParent(skinData.parent.getWorldTransform());
11091097
}
11101098

1111-
if (skinData.animControl != null) {
1099+
if (skinData.animControl != null && skinData.animControl.getSpatial() == null) {
11121100
spatial.addControl(skinData.animControl);
11131101
}
11141102
spatial.addControl(skinData.skeletonControl);
@@ -1219,9 +1207,9 @@ public void update(TrackData data) {
12191207
Vector3f scale = getScale(data, i);
12201208

12211209
Transform t = new Transform(translation, rotation, scale);
1222-
if (isRoot) {
1210+
if (isRoot && skinData.rootBoneTransformOffset != null) {
12231211
//Apply the armature transforms to the root bone anim track.
1224-
t.combineWithParent(skinData.parent.getLocalTransform());
1212+
t.combineWithParent(skinData.rootBoneTransformOffset);
12251213
}
12261214

12271215
reverseBlendAnimTransforms(t, bindTransforms);
@@ -1287,6 +1275,7 @@ private class SkinData {
12871275
SkeletonControl skeletonControl;
12881276
AnimControl animControl;
12891277
Spatial parent;
1278+
Transform rootBoneTransformOffset;
12901279
Bone[] bones;
12911280
boolean used = false;
12921281
}

0 commit comments

Comments
 (0)