Skip to content

Commit

Permalink
missing commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
syoyo committed Nov 28, 2023
1 parent 536c8fd commit 4fa3db7
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/prim-reconstruct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2097,8 +2097,37 @@ bool ReconstructGPrimProperties(

for (const auto &prop : properties) {
PARSE_SINGLE_TARGET_PATH_RELATION(table, prop, kMaterialBinding, gprim->materialBinding)
PARSE_SINGLE_TARGET_PATH_RELATION(table, prop, kMaterialBindingCollection, gprim->materialBindingCollection)
PARSE_SINGLE_TARGET_PATH_RELATION(table, prop, kMaterialBindingPreview, gprim->materialBindingPreview)
// material:binding:collection
PARSE_SINGLE_TARGET_PATH_RELATION(table, prop, kMaterialBindingCollection, gprim->materialBindingCollection)
// material:binding:collection:NAME
if (startsWith(prop.first, kMaterialBindingCollection + std::string(":"))) {

if (table.count(prop.first)) {
continue;
}

if (!prop.second.is_relationship()) {
PUSH_ERROR_AND_RETURN(fmt::format("`{}` must be a Relationship", prop.first));
}

std::string collection_name = removePrefix(prop.first, kMaterialBindingCollection + std::string(":"));
if (collection_name.empty()) {
PUSH_ERROR_AND_RETURN("empty NAME is not allowed for 'mateirial:binding:collection'");
}
std::vector<std::string> names = split(collection_name, ":");
if (names.size() > 1) {
PUSH_ERROR_AND_RETURN("Nested namespace is not allowed for 'mateirial:binding:collection'");
}
collection_name = names[0];

const Relationship &rel = prop.second.get_relationship();

gprim->add_materialBindingCollection(collection_name, rel);

table.insert(prop.first);
continue;
}
PARSE_SINGLE_TARGET_PATH_RELATION(table, prop, kProxyPrim, gprim->proxyPrim)
PARSE_TYPED_ATTRIBUTE(table, prop, "doubleSided", GPrim, gprim->doubleSided)
PARSE_ENUM_PROPETY(table, prop, "visibility", VisibilityEnumHandler, GPrim,
Expand Down
75 changes: 74 additions & 1 deletion src/usdGeom.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "prim-types.hh"
#include "value-types.hh"
#include "xform.hh"
#include "usdShade.hh"

namespace tinyusdz {

Expand Down Expand Up @@ -288,7 +289,7 @@ struct GPrim : Xformable {

// Some frequently used materialBindings
nonstd::optional<Relationship> materialBinding; // material:binding
nonstd::optional<Relationship> materialBindingCollection; // material:binding:collection
nonstd::optional<Relationship> materialBindingCollection; // material:binding:collection TODO: deprecate?(seems `material:binding:collection` without leaf NAME seems ignored in pxrUSD.
nonstd::optional<Relationship> materialBindingPreview; // material:binding:preview

std::map<std::string, Property> props;
Expand Down Expand Up @@ -369,6 +370,75 @@ struct GPrim : Xformable {
return meta;
}

bool has_materialBinding() const {
return materialBinding.has_value();
}

bool has_materialBindingPreview() const {
return materialBindingPreview.has_value();
}

void clear_materialBinding() {
materialBinding.reset();
}

void clear_materialBindingPreview() {
materialBindingPreview.reset();
}

void set_materialBinding(const Relationship &rel) {
materialBinding = rel;
}

void set_materialBinding(const Relationship &rel, const MaterialBindingStrength strength) {
value::token strength_tok(to_string(strength));
materialBinding = rel;
materialBinding.value().metas().bindMaterialAs = strength_tok;
}

void set_materialBindingPreview(const Relationship &rel) {
materialBindingPreview = rel;
}

void set_materialBindingPreview(const Relationship &rel, const MaterialBindingStrength strength) {
value::token strength_tok(to_string(strength));
materialBindingPreview = rel;
materialBindingPreview.value().metas().bindMaterialAs = strength_tok;
}

bool has_materialBindingCollection(const std::string &tok) {
return _materialBindingCollectionMap.count(tok);
}

void add_materialBindingCollection(const std::string &tok, const Relationship &rel) {

// NOTE:
// https://openusd.org/release/wp_usdshade.html#basic-proposal-for-collection-based-assignment
// says: material:binding:collection defines a namespace of binding relationships to be applied in namespace order, with the earliest ordered binding relationship the strongest
//
// so the app is better first check if `tok` element alreasy exists(using has_materialBindingCollection)

_materialBindingCollectionMap[tok] = rel;
}

void clear_materialBindingCollection(const std::string &tok) {

_materialBindingCollectionMap.erase(tok);
}

void add_materialBindingCollection(const std::string &tok, const Relationship &rel, MaterialBindingStrength strength) {
value::token strength_tok(to_string(strength));

_materialBindingCollectionMap[tok] = rel;
_materialBindingCollectionMap[tok].metas().bindMaterialAs = strength_tok;

}

const std::map<std::string, Relationship> materialBindingCollectionMap() const {
return _materialBindingCollectionMap;
}


private:

//bool _valid{true}; // default behavior is valid(allow empty GPrim)
Expand All @@ -379,6 +449,9 @@ struct GPrim : Xformable {
// For Variants
std::map<std::string, VariantSet> _variantSetMap;

// For material:binding:collection:<NAME>
std::map<std::string, Relationship> _materialBindingCollectionMap;

};

struct Xform : GPrim {
Expand Down
20 changes: 20 additions & 0 deletions src/usdShade.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ constexpr auto kUsdPrimvarReader_matrix = "UsdPrimvarReader_matrix";
constexpr auto kWeaderThanDescendants = "weakerThanDescendants";
constexpr auto kStrongerThanDescendants = "strongerThanDescendants";

enum class MaterialBindingStrength
{
WeakerThanDescendants, // default
StrongerThanDescendants
};

// TODO: Move to pprinter.hh?
static std::string to_string(const MaterialBindingStrength strength) {
switch (strength) {
case MaterialBindingStrength::WeakerThanDescendants: {
return kWeaderThanDescendants;
}
case MaterialBindingStrength::StrongerThanDescendants: {
return kStrongerThanDescendants;
}
}

return "[[Invalid MaterialBindingStrength]]";
}

// TODO: Inherit from Prim?
struct UsdShadePrim {
std::string name;
Expand Down

0 comments on commit 4fa3db7

Please sign in to comment.