Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pablode committed Jul 16, 2024
1 parent cb1e959 commit d0c8146
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/libguc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ set(LIBGUC_DEFINES
GUC_VERSION_STRING="${CMAKE_PROJECT_VERSION}"
)

# TODO: allow monolithic USD
set(LIBGUC_SHARED_LIBRARIES
# Header-only library without link dependencies --
# we can get away with not installing the target.
Expand All @@ -49,6 +50,7 @@ set(LIBGUC_SHARED_LIBRARIES
usdShade
usdUtils
usdMtlx
usdSkel
MaterialXCore
MaterialXFormat
)
Expand Down
44 changes: 42 additions & 2 deletions src/libguc/src/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <pxr/usd/usdMtlx/utils.h>
#include <pxr/usd/usd/modelAPI.h>
#include <pxr/usd/kind/registry.h>
#include <pxr/usd/usdSkel/bindingAPI.h>

#include <MaterialXFormat/XmlIo.h>
#include <MaterialXFormat/Util.h>
Expand Down Expand Up @@ -535,7 +536,7 @@ namespace guc
std::string meshName = nodeData->mesh->name ? std::string(nodeData->mesh->name) : "mesh";
auto meshPath = makeUniqueStageSubpath(m_stage, path, meshName);

createOrOverMesh(nodeData->mesh, meshPath);
createOrOverMesh(nodeData->mesh, nodeData->skin, meshPath);
}

if (nodeData->camera)
Expand Down Expand Up @@ -689,7 +690,7 @@ namespace guc
m_uniquePaths[(void*) lightData] = path;
}

void Converter::createOrOverMesh(const cgltf_mesh* meshData, SdfPath path)
void Converter::createOrOverMesh(const cgltf_mesh* meshData, const cgltf_skin* skinData, SdfPath path)
{
auto xform = UsdGeomXform::Define(m_stage, path);

Expand Down Expand Up @@ -757,6 +758,44 @@ namespace guc
submesh.SetDisplayName(meshData->name);
}
}

if (skinData)
{
fprintf(stderr, "skin %s found for mesh %s\n", skinData->name, path.GetText());

// TODO: Xform isn't boundable -> doesn't work. What to do? can we replace sub-meshes with GeomSubsets?
UsdPrim prim = xform.GetPrim();

auto skelApi = UsdSkelBindingAPI::Apply(prim);

// TODO: create and link skeleton (TODO: lazy/global or not?)
// m_uniquePaths[skinData->skeleton] = skelPath;

bool isConstant = true;

// joint indices
{
VtArray<int> jointIndices(skinData->joints_count);
for (size_t i = 0; i < jointIndices.size(); i++)
{
jointIndices[i] = (int) cgltf_node_index(m_data, skinData->joints[i]);

isConstant &= (i == 0) || (jointIndices[i - 1] == jointIndices[i]);
}

UsdGeomPrimvar primvar = skelApi.CreateJointIndicesPrimvar(isConstant, jointIndices.size()); // TODO: create Attr with VtValue default?
primvar.Set(jointIndices);
}

// weights
{
VtArray<float> jointWeights(meshData->weights_count);
memcpy(jointWeights.data(), meshData->weights, jointWeights.size()); // TODO: better way?

UsdGeomPrimvar primvar = skelApi.CreateJointWeightsPrimvar(isConstant, jointWeights.size());
primvar.Set(jointWeights);
}
}
}

void Converter::createMaterialBinding(UsdPrim& prim, const std::string& materialName)
Expand All @@ -782,6 +821,7 @@ namespace guc
}
}

// TODO: path -> const path&
bool Converter::createPrimitive(const cgltf_primitive* primitiveData, SdfPath path, UsdPrim& prim)
{
const cgltf_material* material = primitiveData->material;
Expand Down
2 changes: 1 addition & 1 deletion src/libguc/src/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace guc
void createNodesRecursively(const cgltf_node* nodeData, SdfPath path);
void createOrOverCamera(const cgltf_camera* cameraData, SdfPath path);
void createOrOverLight(const cgltf_light* lightData, SdfPath path);
void createOrOverMesh(const cgltf_mesh* meshData, SdfPath path);
void createOrOverMesh(const cgltf_mesh* meshData, const cgltf_skin* skin, SdfPath path);
void createMaterialBinding(UsdPrim& prim, const std::string& materialName);
bool createPrimitive(const cgltf_primitive* primitiveData, SdfPath path, UsdPrim& prim);

Expand Down

0 comments on commit d0c8146

Please sign in to comment.