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
72 changes: 56 additions & 16 deletions usd/src/usd_parser/USDMaterial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,41 @@ namespace sdf
/// \param[in] _dest The destination for the copy of _ori
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
/// \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

std::string tmpDest = _dest;
std::string parentPath = ignition::common::parentPath(_dest);
std::string::size_type fileExtensionIndex = tmpDest.rfind(".");

std::string fileExtension = "." + tmpDest.substr(fileExtensionIndex + 1);
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
std::string fileNameWithoutExtension = ignition::common::basename(_dest);
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
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(tmpDest))
{
tmpDest = ignition::common::joinPaths(
parentPath,
fileNameWithoutExtension + "_" + std::to_string(index) +
fileExtension);
++index;
}

_dest = tmpDest;

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 +172,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 +349,7 @@ namespace sdf
if (isPBR)
{
sdf::Pbr pbr;
pbrWorkflow.SetType(sdf::PbrWorkflowType::METAL);
pbr.SetWorkflow(sdf::PbrWorkflowType::METAL, pbrWorkflow);
_material.SetPbrMaterial(pbr);
}
Expand Down