Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USD to SDF: material fixed #895

Merged
merged 11 commits into from
Mar 24, 2022
7 changes: 7 additions & 0 deletions usd/src/sdf_parser/Material_Sdf2Usd_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ class UsdStageFixture : public::testing::Test
/////////////////////////////////////////////////
TEST_F(UsdStageFixture, Material)
{
sdf::testing::ScopeExit removeCopiedMaterials(
[]
{
ignition::common::removeAll(
ignition::common::joinPaths(ignition::common::cwd(), "materials"));
});

sdf::setFindCallback(sdf::usd::testing::findFileCb);
ignition::common::addFindFileURICallback(
std::bind(&sdf::usd::testing::FindResourceUri, std::placeholders::_1));
Expand Down
2 changes: 1 addition & 1 deletion usd/src/usd_parser/USDData_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ TEST(USDData, Constructor)
auto systemPaths = ignition::common::systemPaths();
systemPaths->AddFilePaths(sdf::testing::TestFile("usd"));

// Open a valid USD file
{
sdf::testing::ScopeExit removeCopiedMaterials(
[]
Expand All @@ -52,6 +51,7 @@ TEST(USDData, Constructor)
ignition::common::joinPaths(ignition::common::cwd(), "materials"));
});

// Open a valid USD file
std::string filename = sdf::testing::TestFile("usd", "upAxisZ.usda");
sdf::usd::USDData usdData(filename);
EXPECT_EQ(0u, usdData.Init().size());
Expand Down
82 changes: 65 additions & 17 deletions usd/src/usd_parser/USDMaterial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,52 @@ namespace sdf
/////////////////////////////////////////////////
/// \brief Copy a file from one destination to another
/// \param[in] _ori The original file to copy
/// \param[in] _dest The destination for the copy of _ori
/// \param[inout] _dest The destination for the copy of _ori. If _dest
/// represents a file that already exists, a unique numeric suffix in the
/// form of _<number> will be appended to the end of the file name.
/// \return A list of UsdErrors. An empty list means no errors occurred when
/// copying _ori to _dest
UsdErrors copyFile(const std::string &_ori, const std::string &_dest)
UsdErrors copyFile(const std::string &_ori, std::string &_dest)
{
UsdErrors errors;
if (ignition::common::exists(_ori))
{
// If the file exists then we append a number suffix to the destination
// file
// For example:
// /bar/foo.extension
// /bar/foo_X.extension
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
if (ignition::common::exists(_dest))
{
const std::string parentPath = ignition::common::parentPath(_dest);
std::string::size_type fileExtensionIndex = _dest.rfind(".");
if (fileExtensionIndex == std::string::npos)
{
errors.emplace_back(
Error(ErrorCode::FILE_READ, "Unable to find the extension of the "
"file [" + _dest + "] which should be copied"));
return errors;
}

const std::string fileExtension = _dest.substr(fileExtensionIndex);
std::string fileNameWithoutExtension =
ignition::common::basename(_dest);
size_t pos = fileNameWithoutExtension.find(fileExtension);
if (pos != std::string::npos)
{
// If found then erase it from string
fileNameWithoutExtension.erase(pos, fileExtension.length());
}
int index = 0;
while (ignition::common::exists(_dest))
{
_dest = ignition::common::joinPaths(
parentPath,
fileNameWithoutExtension + "_" + std::to_string(index) +
fileExtension);
++index;
}
}
std::string baseName = ignition::common::basename(_dest);
std::string pathDest = ignition::common::replaceAll(_dest, baseName, "");
ignition::common::createDirectories(pathDest);
Expand Down Expand Up @@ -142,68 +180,77 @@ namespace sdf
{
pxr::SdfAssetPath materialPath =
assetPath(pxr::TfToken("diffuse_texture"), variantShader);
pbrWorkflow.SetAlbedoMap(materialPath.GetAssetPath());
std::string fullAlbedoName =
ignition::common::findFile(ignition::common::basename(
materialPath.GetAssetPath()));
materialPath.GetAssetPath()), false);
std::string dest = materialPath.GetAssetPath();
UsdErrors errorCopy = copyFile(
fullAlbedoName, materialPath.GetAssetPath());
fullAlbedoName, dest);
if (!errorCopy.empty())
{
errors.insert(errors.end(), errorCopy.begin(), errorCopy.end());
return errors;
}
pbrWorkflow.SetAlbedoMap(dest);

// We need to set diffuse and specular to (1, 1, 1) otherwise
// the texture is completely black
_material.SetDiffuse(ignition::math::Color(1, 1, 1));
_material.SetSpecular(ignition::math::Color(1, 1, 1));

isPBR = true;
}
else if (input.GetBaseName() == "normalmap_texture")
{
pxr::SdfAssetPath materialPath =
assetPath(pxr::TfToken("normalmap_texture"), variantShader);
pbrWorkflow.SetNormalMap(materialPath.GetAssetPath());
std::string fullNormalName =
ignition::common::findFile(ignition::common::basename(
materialPath.GetAssetPath()));
auto errorCopy = copyFile(fullNormalName,
materialPath.GetAssetPath());
materialPath.GetAssetPath()), false);
std::string dest = materialPath.GetAssetPath();
auto errorCopy = copyFile(fullNormalName, dest);
if (!errorCopy.empty())
{
errors.insert(errors.end(), errorCopy.begin(), errorCopy.end());
return errors;
}
pbrWorkflow.SetNormalMap(dest);
isPBR = true;
}
else if (input.GetBaseName() == "reflectionroughness_texture")
{
pxr::SdfAssetPath materialPath = assetPath(
pxr::TfToken("reflectionroughness_texture"), variantShader);
pbrWorkflow.SetRoughnessMap(materialPath.GetAssetPath());
std::string fullRoughnessName =
ignition::common::findFile(ignition::common::basename(
materialPath.GetAssetPath()));
auto errorCopy = copyFile(
fullRoughnessName, materialPath.GetAssetPath());
materialPath.GetAssetPath()), false);
std::string dest = materialPath.GetAssetPath();
auto errorCopy = copyFile(fullRoughnessName, dest);
if (!errorCopy.empty())
{
errors.insert(errors.end(), errorCopy.begin(), errorCopy.end());
return errors;
}
pbrWorkflow.SetRoughnessMap(dest);

isPBR = true;
}
else if (input.GetBaseName() == "metallic_texture")
{
pxr::SdfAssetPath materialPath = assetPath(
pxr::TfToken("metallic_texture"), variantShader);
pbrWorkflow.SetMetalnessMap(materialPath.GetAssetPath());
std::string fullMetalnessName =
ignition::common::findFile(ignition::common::basename(
materialPath.GetAssetPath()));
auto errorCopy = copyFile(
fullMetalnessName, materialPath.GetAssetPath());
materialPath.GetAssetPath()), false);
std::string dest = materialPath.GetAssetPath();
auto errorCopy = copyFile(fullMetalnessName, dest);
if (!errorCopy.empty())
{
errors.insert(errors.end(), errorCopy.begin(), errorCopy.end());
return errors;
}
pbrWorkflow.SetMetalnessMap(dest);

isPBR = true;
}
else if (input.GetBaseName() == "diffuse_color_constant")
Expand Down Expand Up @@ -310,6 +357,7 @@ namespace sdf
if (isPBR)
{
sdf::Pbr pbr;
pbrWorkflow.SetType(sdf::PbrWorkflowType::METAL);
pbr.SetWorkflow(sdf::PbrWorkflowType::METAL, pbrWorkflow);
_material.SetPbrMaterial(pbr);
}
Expand Down