Skip to content

Commit 6304fad

Browse files
authored
Merge pull request #41 from lyuma/preserve_skeletons
Preserve skeletons which do not contain a mesh
2 parents cd17ab9 + 2c89516 commit 6304fad

File tree

6 files changed

+59
-8
lines changed

6 files changed

+59
-8
lines changed

src/fbx/Fbx2Raw.cpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,19 @@ static void ReadNodeHierarchy(
688688
FbxScene* pScene,
689689
FbxNode* pNode,
690690
const long parentId,
691-
const std::string& path) {
691+
const std::string& path,
692+
int extraSkinIx) {
692693
const FbxUInt64 nodeId = pNode->GetUniqueID();
693694
const char* nodeName = pNode->GetName();
694-
const int nodeIndex = raw.AddNode(nodeId, nodeName, parentId);
695+
FbxSkeleton *skel = pNode->GetSkeleton();
696+
if (skel == nullptr) {
697+
extraSkinIx = -1;
698+
} else {
699+
if (skel->IsSkeletonRoot()) {
700+
extraSkinIx = raw.CreateExtraSkinIndex();
701+
}
702+
}
703+
const int nodeIndex = raw.AddNode(nodeId, nodeName, parentId, extraSkinIx);
695704
RawNode& node = raw.GetNode(nodeIndex);
696705

697706
FbxTransform::EInheritType lInheritType;
@@ -745,7 +754,7 @@ static void ReadNodeHierarchy(
745754
raw.SetRootNode(nodeId);
746755
}
747756
for (int child = 0; child < pNode->GetChildCount(); child++) {
748-
ReadNodeHierarchy(raw, pScene, pNode->GetChild(child), nodeId, newPath);
757+
ReadNodeHierarchy(raw, pScene, pNode->GetChild(child), nodeId, newPath, extraSkinIx);
749758
}
750759
}
751760

@@ -1174,7 +1183,7 @@ bool LoadFBXFile(
11741183
// this is always 0.01, but let's opt for clarity.
11751184
scaleFactor = FbxSystemUnit::m.GetConversionFactorFrom(FbxSystemUnit::cm);
11761185

1177-
ReadNodeHierarchy(raw, pScene, pScene->GetRootNode(), 0, "");
1186+
ReadNodeHierarchy(raw, pScene, pScene->GetRootNode(), 0, "", -1);
11781187
ReadNodeAttributes(raw, pScene, pScene->GetRootNode(), textureLocations);
11791188
ReadAnimations(raw, pScene, options);
11801189

src/gltf/Raw2Gltf.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,18 @@ ModelData* Raw2Gltf(
853853
}
854854
}
855855

856+
std::vector<std::vector<uint32_t>> extraJointIndexes;
857+
extraJointIndexes.resize(raw.GetExtraSkinCount());
858+
for (int i = 0; i < raw.GetNodeCount(); i++) {
859+
const RawNode& node = raw.GetNode(i);
860+
if (node.extraSkinIx >= 0) {
861+
extraJointIndexes[node.extraSkinIx].push_back(i);
862+
}
863+
}
864+
for (int i = 0; i < extraJointIndexes.size(); i++) {
865+
gltf->skins.hold(new SkinData(extraJointIndexes[i], true));
866+
}
867+
856868
//
857869
// cameras
858870
//

src/gltf/properties/SkinData.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,22 @@ SkinData::SkinData(
1818
: Holdable(),
1919
joints(joints),
2020
inverseBindMatrices(inverseBindMatricesAccessor.ix),
21-
skeletonRootNode(skeletonRootNode.ix) {}
21+
skeletonRootNode(skeletonRootNode.ix),
22+
isExtraSkin(false) {}
23+
24+
SkinData::SkinData(
25+
const std::vector<uint32_t> joints,
26+
bool isExtraSkin)
27+
: Holdable(),
28+
joints(joints),
29+
inverseBindMatrices(0),
30+
skeletonRootNode(0),
31+
isExtraSkin(true) {}
2232

2333
json SkinData::serialize() const {
34+
if (isExtraSkin) {
35+
return {{"joints", joints}};
36+
}
2437
return {
2538
{"joints", joints},
2639
{"inverseBindMatrices", inverseBindMatrices},

src/gltf/properties/SkinData.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ struct SkinData : Holdable {
1616
const AccessorData& inverseBindMatricesAccessor,
1717
const NodeData& skeletonRootNode);
1818

19+
SkinData(
20+
const std::vector<uint32_t> joints,
21+
bool isExtraSkin);
22+
1923
json serialize() const override;
2024

2125
const std::vector<uint32_t> joints;
2226
const uint32_t skeletonRootNode;
2327
const uint32_t inverseBindMatrices;
28+
const bool isExtraSkin;
2429
};

src/raw/RawModel.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ size_t RawVertex::Difference(const RawVertex& other) const {
6969
return attributes;
7070
}
7171

72-
RawModel::RawModel() : vertexAttributes(0) {}
72+
RawModel::RawModel() : nextExtraSkinIx(0), rootNodeId(0), vertexAttributes(0), globalMaxWeights(0) {}
7373

7474
void RawModel::AddVertexAttribute(const RawVertexAttribute attrib) {
7575
vertexAttributes |= attrib;
@@ -311,7 +311,7 @@ int RawModel::AddCameraOrthographic(
311311
return (int)cameras.size() - 1;
312312
}
313313

314-
int RawModel::AddNode(const long id, const char* name, const long parentId) {
314+
int RawModel::AddNode(const long id, const char* name, const long parentId, const int extraSkinIx) {
315315
assert(name[0] != '\0');
316316

317317
for (size_t i = 0; i < nodes.size(); i++) {
@@ -330,6 +330,7 @@ int RawModel::AddNode(const long id, const char* name, const long parentId) {
330330
joint.translation = Vec3f(0, 0, 0);
331331
joint.rotation = Quatf(0, 0, 0, 1);
332332
joint.scale = Vec3f(1, 1, 1);
333+
joint.extraSkinIx = extraSkinIx;
333334

334335
nodes.emplace_back(joint);
335336
return (int)nodes.size() - 1;

src/raw/RawModel.hpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ struct RawNode {
355355
long surfaceId;
356356
long lightIx;
357357
std::vector<std::string> userProperties;
358+
int extraSkinIx;
358359
};
359360

360361
class RawModel {
@@ -410,7 +411,7 @@ class RawModel {
410411
const float nearZ,
411412
const float farZ);
412413
int AddNode(const RawNode& node);
413-
int AddNode(const long id, const char* name, const long parentId);
414+
int AddNode(const long id, const char* name, const long parentId, const int extraSkinIx);
414415
void SetRootNode(const long nodeId) {
415416
rootNodeId = nodeId;
416417
}
@@ -541,9 +542,19 @@ class RawModel {
541542
const int keepAttribs,
542543
const bool forceDiscrete) const;
543544

545+
int CreateExtraSkinIndex() {
546+
int ret = nextExtraSkinIx;
547+
nextExtraSkinIx++;
548+
return ret;
549+
}
550+
int GetExtraSkinCount() const {
551+
return nextExtraSkinIx;
552+
}
553+
544554
private:
545555
Vec3f getFaceNormal(int verts[3]) const;
546556

557+
int nextExtraSkinIx;
547558
long rootNodeId;
548559
int vertexAttributes;
549560
int globalMaxWeights;

0 commit comments

Comments
 (0)