From 10f4ac856f4cb82440e54ae9fb29f8ffe7670d70 Mon Sep 17 00:00:00 2001 From: hhalen Date: Fri, 19 Apr 2019 15:59:07 -0700 Subject: [PATCH] Allow arbitrary number of skinning weights. Controlled through command line parameter. Defaults to 4. --- src/FBX2glTF.cpp | 2 +- src/gltf/Raw2Gltf.cpp | 2 -- src/gltf/Raw2Gltf.hpp | 4 ++-- src/raw/RawModel.cpp | 17 ++++++++--------- src/raw/RawModel.hpp | 11 ++++------- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/FBX2glTF.cpp b/src/FBX2glTF.cpp index d31a62d..610a407 100644 --- a/src/FBX2glTF.cpp +++ b/src/FBX2glTF.cpp @@ -151,7 +151,7 @@ int main(int argc, char* argv[]) { gltfOptions.maxSkinningWeights, "How many joint influences per vertex are allowed.", true) - ->check(CLI::Range(1, RawModel::MAX_SUPPORTED_WEIGHTS)); + ->check(CLI::Range(0, 512)); app.add_option( "-k,--keep-attribute", diff --git a/src/gltf/Raw2Gltf.cpp b/src/gltf/Raw2Gltf.cpp index 880bd37..352e7c2 100644 --- a/src/gltf/Raw2Gltf.cpp +++ b/src/gltf/Raw2Gltf.cpp @@ -536,7 +536,6 @@ ModelData* Raw2Gltf( for (int i = 0; i < surfaceModel.GetGlobalWeightCount(); i += 4) { const AttributeArrayDefinition ATTR_JOINTS( std::string("JOINTS_") + std::to_string(i/4), - //"JOINTS_0", &RawVertex::jointIndices, GLT_VEC4I, draco::GeometryAttribute::GENERIC, @@ -549,7 +548,6 @@ ModelData* Raw2Gltf( for (int i = 0; i < surfaceModel.GetGlobalWeightCount(); i += 4) { const AttributeArrayDefinition ATTR_WEIGHTS( std::string("WEIGHTS_") + std::to_string(i/4), - //"WEIGHTS_0", &RawVertex::jointWeights, GLT_VEC4F, draco::GeometryAttribute::GENERIC, diff --git a/src/gltf/Raw2Gltf.hpp b/src/gltf/Raw2Gltf.hpp index fb0d6c9..ec9dac0 100644 --- a/src/gltf/Raw2Gltf.hpp +++ b/src/gltf/Raw2Gltf.hpp @@ -173,7 +173,7 @@ struct AttributeDefinition { template struct AttributeArrayDefinition { const std::string gltfName; - const T (RawVertex::*rawAttributeIx)[(RawModel::MAX_SUPPORTED_WEIGHTS -1 ) / 4 + 1]; + const std::vector RawVertex::*rawAttributeIx; const GLType glType; const int arrayOffset; const draco::GeometryAttribute::Type dracoAttribute; @@ -181,7 +181,7 @@ struct AttributeArrayDefinition { AttributeArrayDefinition( const std::string gltfName, - const T (RawVertex::*rawAttributeIx)[(RawModel::MAX_SUPPORTED_WEIGHTS - 1) / 4 + 1], + const std::vector RawVertex::*rawAttributeIx, const GLType& _glType, const draco::GeometryAttribute::Type dracoAttribute, const draco::DataType dracoComponentType, diff --git a/src/raw/RawModel.cpp b/src/raw/RawModel.cpp index 7977ccf..5f75d28 100644 --- a/src/raw/RawModel.cpp +++ b/src/raw/RawModel.cpp @@ -425,19 +425,20 @@ void RawModel::Condense(const int maxSkinningWeights, const bool normalizeWeight } - assert(globalMaxWeights <= RawModel::MAX_SUPPORTED_WEIGHTS); + assert(globalMaxWeights >= 0); // Copy to gltf friendly structure for (auto& vertex : vertices) { - for (int i = 0; i < globalMaxWeights; i += 4) { + vertex.jointIndices.reserve(globalMaxWeights); + vertex.jointWeights.reserve(globalMaxWeights); + for (int i = 0; i < globalMaxWeights; i += 4) { // ensure every vertex has the same amount of weights Vec4f weights{0.0}; Vec4i jointIds{0,0,0,0}; for (int j = i; j < i + 4 && j < vertex.skinningInfo.size(); j++) { weights[j - i] = vertex.skinningInfo[j].jointWeight; jointIds[j - i] = vertex.skinningInfo[j].jointIndex; } - const int vertexStream = i / 4; - vertex.jointIndices[vertexStream] = jointIds; - vertex.jointWeights[vertexStream] = weights; + vertex.jointIndices.push_back(jointIds); + vertex.jointWeights.push_back(weights); } } } @@ -660,12 +661,10 @@ void RawModel::CreateMaterialModels( vertex.uv1 = defaultVertex.uv1; } if ((keep & RAW_VERTEX_ATTRIBUTE_JOINT_INDICES) == 0) { - for (int i = 0; i < RawModel::MAX_SUPPORTED_WEIGHTS ; i++) - vertex.jointIndices[i] = defaultVertex.jointIndices[i]; + vertex.jointIndices = defaultVertex.jointIndices; } if ((keep & RAW_VERTEX_ATTRIBUTE_JOINT_WEIGHTS) == 0) { - for (int i = 0; i < RawModel::MAX_SUPPORTED_WEIGHTS; i++) - vertex.jointWeights[i] = defaultVertex.jointWeights[i]; + vertex.jointWeights = defaultVertex.jointWeights; } } diff --git a/src/raw/RawModel.hpp b/src/raw/RawModel.hpp index 45fe3a9..99be544 100644 --- a/src/raw/RawModel.hpp +++ b/src/raw/RawModel.hpp @@ -15,8 +15,6 @@ #include "FBX2glTF.h" -#define MAX_SKINNING_WEIGHTS 32 - enum RawVertexAttribute { RAW_VERTEX_ATTRIBUTE_POSITION = 1 << 0, RAW_VERTEX_ATTRIBUTE_NORMAL = 1 << 1, @@ -61,8 +59,8 @@ struct RawVertex { Vec4f color{0.0f}; Vec2f uv0{0.0f}; Vec2f uv1{0.0f}; - Vec4i jointIndices[(MAX_SKINNING_WEIGHTS - 1) / 4 + 1]; - Vec4f jointWeights[(MAX_SKINNING_WEIGHTS - 1) / 4 + 1]; + std::vector jointIndices; + std::vector jointWeights; std::vector skinningInfo; @@ -372,7 +370,6 @@ struct RawNode { class RawModel { public: - static const int MAX_SUPPORTED_WEIGHTS = MAX_SKINNING_WEIGHTS; RawModel(); @@ -542,7 +539,7 @@ class RawModel { // Returns true if the vertices store the particular attribute. template void GetArrayAttributeArray(std::vector<_attrib_type_>& out, - const _attrib_type_ (RawVertex::*ptr)[(RawModel::MAX_SUPPORTED_WEIGHTS - 1) / 4 + 1], + const std::vector<_attrib_type_> RawVertex::*ptr, const int arrayOffset) const; @@ -586,7 +583,7 @@ void RawModel::GetAttributeArray( template void RawModel::GetArrayAttributeArray( std::vector<_attrib_type_>& out, - const _attrib_type_ (RawVertex::*ptr)[(RawModel::MAX_SUPPORTED_WEIGHTS - 1) / 4 + 1], + const std::vector<_attrib_type_> RawVertex::*ptr, const int arrayOffset) const { out.resize(vertices.size()); for (size_t i = 0; i < vertices.size(); i++) {