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

sdf -> usd: Materials #831

Merged
merged 38 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
9dfc9f2
sdf -> usd Parse links
ahcorde Jan 25, 2022
ffaf2f4
sdf -> usd: Added Visual and geometries
ahcorde Jan 26, 2022
2e3a060
sdf -> usd: Materials
ahcorde Jan 26, 2022
0a5a9a7
Fixed test
ahcorde Jan 26, 2022
071dc4b
Merge remote-tracking branch 'origin/ahcorde/sdf_to_usd_visuals' into…
ahcorde Jan 26, 2022
89daa0d
review feedback
adlarkin Jan 27, 2022
f48b35b
dont segfault when mesh failed to load
Jan 31, 2022
d932e90
fix error reporting
Jan 31, 2022
54b35e9
Merge branch 'sdf12' into ahcorde/sdf_to_usd_links
ahcorde Feb 1, 2022
f0331ca
Fixed scale and submesh name
ahcorde Feb 10, 2022
e85f60c
Merge remote-tracking branch 'origin/sdf12' into ahcorde/sdf_to_usd_l…
ahcorde Feb 14, 2022
a02a6e1
Use UsdError
ahcorde Feb 14, 2022
f898d26
codecheck, use USD component's visibility macro, properly construct w…
adlarkin Feb 14, 2022
258606c
Merge branch 'ahcorde/sdf_to_usd_links' into ahcorde/sdf_to_usd_visuals
adlarkin Feb 15, 2022
c43ba24
usd UsdErrors and USD component visibility
adlarkin Feb 15, 2022
f816dd4
Merge branch 'sdf12' into ahcorde/sdf_to_usd_links
adlarkin Feb 15, 2022
6f3490e
Merge branch 'ahcorde/sdf_to_usd_links' into ahcorde/sdf_to_usd_visuals
adlarkin Feb 15, 2022
379defd
review feedback
adlarkin Feb 17, 2022
0f4075f
Merge branch 'sdf12' into ahcorde/sdf_to_usd_visuals
adlarkin Feb 18, 2022
a98d481
Merge branch 'sdf12' into ahcorde/sdf_to_usd_visuals
adlarkin Feb 18, 2022
0cb1cc0
Merge branch 'sdf12' into ahcorde/sdf_to_usd_visuals
ahcorde Feb 18, 2022
00a55c5
Merge branch 'sdf12' into ahcorde/sdf_to_usd_visuals
ahcorde Feb 21, 2022
5ac37f7
Merge remote-tracking branch 'origin/ahcorde/sdf_to_usd_visuals' into…
ahcorde Feb 21, 2022
2085347
Merge branch 'sdf12' into ahcorde/sdf_to_usd_materials
ahcorde Feb 24, 2022
94969a0
Merge branch 'sdf12' into ahcorde/sdf_to_usd_materials
ahcorde Feb 25, 2022
361c569
Merge branch 'sdf12' into ahcorde/sdf_to_usd_materials
adlarkin Feb 25, 2022
c7114f8
Merge branch 'sdf12' into ahcorde/sdf_to_usd_materials
ahcorde Feb 25, 2022
7130c1f
initial review feedback
adlarkin Feb 25, 2022
14354fb
feedback
ahcorde Feb 28, 2022
430579f
more review feedback
adlarkin Mar 1, 2022
e40448f
Added feedback
ahcorde Mar 1, 2022
a603603
Fixed typo and docs
ahcorde Mar 1, 2022
67c54df
Merge branch 'sdf12' into ahcorde/sdf_to_usd_materials
scpeters Mar 2, 2022
830c11a
Feedback
ahcorde Mar 3, 2022
5f33b20
Merge branch 'sdf12' into ahcorde/sdf_to_usd_materials
ahcorde Mar 3, 2022
cdc0257
review feedback
adlarkin Mar 3, 2022
8b97a1c
common -> sdf material: use const raw pointer, not std::shared_ptr
adlarkin Mar 3, 2022
3095a50
sdf -> common material conversion: don't use std::shared_ptr
adlarkin Mar 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions test/sdf/basic_shapes.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@
<length>0.1</length>
</capsule>
</geometry>
<material>
<diffuse>0 0.1 0.2</diffuse>
<emissive>0.12 0.23 0.34 0.56</emissive>
</material>
</visual>
</link>
</model>
Expand All @@ -136,6 +140,18 @@
<scale>1.2 2.3 3.4</scale>
</mesh>
</geometry>
<material>
<diffuse>0.2 0.5 0.1 1.0</diffuse>
<specular>0.7 0.3 0.5 0.9</specular>
<pbr>
<specular>
<albedo_map>albedo_map.png</albedo_map>
<normal_map>normal_map.png</normal_map>
<roughness_map>roughness_map.png</roughness_map>
<metalness_map>metalness_map.png</metalness_map>
</specular>
</pbr>
</material>
</visual>
</link>
</model>
Expand Down
56 changes: 56 additions & 0 deletions usd/include/sdf/usd/Conversions.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/


#ifndef SDF_USD_CONVERSIONS_HH_
#define SDF_USD_CONVERSIONS_HH_

#include <memory>

#include <ignition/common/Material.hh>

#include "sdf/Material.hh"
#include "sdf/sdf_config.h"
#include "sdf/usd/Export.hh"

namespace sdf
{
// Inline bracket to help doxygen filtering.
inline namespace SDF_VERSION_NAMESPACE {
//
namespace usd
{
/// \brief Specialized conversion from an Ignition Common Material
/// to a SDF material
/// \param[in] _in Ignition Common Material.
/// \return SDF material.
IGNITION_SDFORMAT_USD_VISIBLE
sdf::Material convert(
const std::shared_ptr<ignition::common::Material> &_in);
adlarkin marked this conversation as resolved.
Show resolved Hide resolved

/// \brief Specialized conversion from an SDF material to a Ignition Common
/// material.
/// \param[in] _in SDF material.
/// \return Ignition Common Material.
IGNITION_SDFORMAT_USD_VISIBLE
std::shared_ptr<ignition::common::Material> convert(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why return a shared_ptr here? Is it important that the resulting Material be allocated on the heap? we return an sdf::Material object from the other function

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really sure about how to deal with this, because I can't return a ignition::common::Material because is a non-copyable class (it has a unique_ptr inside). I decided touse the same type in both methods.

@scpeters what do you think is right type ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, that's reasonable. I'll open an issue in ign-common suggesting that we improve the Material class to make it copyable

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it has a move ctor, then you should be might be able to return ignition::common::Material and the compiler will use copy elision to construct the object at the call location.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't have a move ctor https://github.com/ignitionrobotics/ign-common/blob/ign-common4/graphics/include/ignition/common/Material.hh#L90-L97

@azeey, does it make sense to include it ? or can we keep the shared_ptr ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, since it has a user declared destructor, the compiler will not generate a move ctor. Simply removing the destructor would fix this, but I think that will break ABI. So, returning a smart pointer seems to be our best bet, but if we have to do that, my preference would be to use a unique_ptr unless there is a particular reason to use a shared_ptr.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SubMesh::MaterialByIndex returns a shared_ptr which mean we need to convert this shared_ptr to unique_ptr to finally get the sdf::Material. I will bet for the shared_ptrto simplify things.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an idea for getting around the std::shared_ptr issue - see 3095a50. If you guys think this isn't a good solution, feel free to revert this commit.

const sdf::Material &_in);
}
}
}

#endif
54 changes: 54 additions & 0 deletions usd/include/sdf/usd/sdf_parser/Material.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef SDF_USD_SDF_PARSER_MATERIALS_HH_
#define SDF_USD_SDF_PARSER_MATERIALS_HH_

// TODO(ahcorde) this is to remove deprecated "warnings" in usd, these warnings
// are reported using #pragma message so normal diagnostic flags cannot remove
// them. This workaround requires this block to be used whenever usd is
// included.
#pragma push_macro ("__DEPRECATED")
#undef __DEPRECATED
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usdShade/material.h>
#pragma pop_macro ("__DEPRECATED")

#include "sdf/Material.hh"
#include "sdf/usd/Export.hh"
#include "sdf/sdf_config.h"

namespace sdf
{
// Inline bracket to help doxygen filtering.
inline namespace SDF_VERSION_NAMESPACE {
//
namespace usd
{
/// \brief Parse an SDF material into a USD stage.
/// \param[in] _material The SDF material to parse.
/// \param[in] _stage The stage that should contain the USD representation
/// of _material.
/// \return The parsed usd material (invalid material if parsing failed)
pxr::UsdShadeMaterial IGNITION_SDFORMAT_USD_VISIBLE
ParseSdfMaterial(const sdf::Material *_material,
pxr::UsdStageRefPtr &_stage);
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

#endif
4 changes: 4 additions & 0 deletions usd/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
set(sources
Conversions.cc
UsdError.cc
sdf_parser/Geometry.cc
sdf_parser/Joint.cc
sdf_parser/Light.cc
sdf_parser/Link.cc
sdf_parser/Material.cc
sdf_parser/Model.cc
sdf_parser/Sensor.cc
sdf_parser/Visual.cc
Expand All @@ -30,11 +32,13 @@ set(gtest_sources
sdf_parser/Joint_Sdf2Usd_TEST.cc
sdf_parser/Light_Sdf2Usd_TEST.cc
sdf_parser/Link_Sdf2Usd_TEST.cc
sdf_parser/Material_Sdf2Usd_TEST.cc
# TODO(adlarkin) add a test for SDF -> USD models once model parsing
# functionality is complete
sdf_parser/Sensors_Sdf2Usd_TEST.cc
sdf_parser/Visual_Sdf2Usd_TEST.cc
sdf_parser/World_Sdf2Usd_TEST.cc
Conversions_TEST.cc
UsdError_TEST.cc
UsdUtils_TEST.cc
)
Expand Down
154 changes: 154 additions & 0 deletions usd/src/Conversions.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "sdf/usd/Conversions.hh"

#include <ignition/common/Pbr.hh>

#include "sdf/Pbr.hh"

namespace sdf
{
inline namespace SDF_VERSION_NAMESPACE {
//
namespace usd
{
sdf::Material convert(const std::shared_ptr<ignition::common::Material> &_in)
{
sdf::Material out;
out.SetEmissive(_in->Emissive());
out.SetDiffuse(_in->Diffuse());
out.SetSpecular(_in->Specular());
out.SetAmbient(_in->Ambient());
out.SetRenderOrder(_in->RenderOrder());
out.SetLighting(_in->Lighting());
out.SetDoubleSided(_in->TwoSidedEnabled());
const ignition::common::Pbr * pbr = _in->PbrMaterial();
if (pbr != nullptr)
{
out.SetNormalMap(pbr->NormalMap());
sdf::Pbr pbrOut;
sdf::PbrWorkflow pbrWorkflow;
pbrWorkflow.SetAlbedoMap(pbr->AlbedoMap());
pbrWorkflow.SetMetalnessMap(pbr->MetalnessMap());
pbrWorkflow.SetEmissiveMap(pbr->EmissiveMap());
pbrWorkflow.SetRoughnessMap(pbr->RoughnessMap());
pbrWorkflow.SetSpecularMap(pbr->SpecularMap());
pbrWorkflow.SetEnvironmentMap(pbr->EnvironmentMap());
pbrWorkflow.SetAmbientOcclusionMap(pbr->AmbientOcclusionMap());
pbrWorkflow.SetLightMap(pbr->LightMap());
pbrWorkflow.SetRoughness(pbr->Roughness());
pbrWorkflow.SetGlossiness(pbr->Glossiness());
pbrWorkflow.SetMetalness(pbr->Metalness());

if (pbr->NormalMapType() == ignition::common::NormalMapSpace::TANGENT)
{
pbrWorkflow.SetNormalMap(
pbr->NormalMap(), sdf::NormalMapSpace::TANGENT);
}
else
{
pbrWorkflow.SetNormalMap(
pbr->NormalMap(), sdf::NormalMapSpace::OBJECT);
}

if (pbr->Type() == ignition::common::PbrType::METAL)
{
pbrOut.SetWorkflow(sdf::PbrWorkflowType::METAL, pbrWorkflow);
}
else if (pbr->Type() == ignition::common::PbrType::SPECULAR)
{
pbrOut.SetWorkflow(sdf::PbrWorkflowType::SPECULAR, pbrWorkflow);
}
out.SetPbrMaterial(pbrOut);
}
else if (!_in->TextureImage().empty())
{
sdf::Pbr pbrOut;
sdf::PbrWorkflow pbrWorkflow;
pbrWorkflow.SetAlbedoMap(_in->TextureImage());
pbrOut.SetWorkflow(sdf::PbrWorkflowType::SPECULAR, pbrWorkflow);
out.SetPbrMaterial(pbrOut);
}

return out;
}

std::shared_ptr<ignition::common::Material> convert(const sdf::Material &_in)
{
auto out = std::make_shared<ignition::common::Material>();
out->SetEmissive(_in.Emissive());
out->SetDiffuse(_in.Diffuse());
out->SetSpecular(_in.Specular());
out->SetAmbient(_in.Ambient());
out->SetRenderOrder(_in.RenderOrder());
out->SetLighting(_in.Lighting());
out->SetAlphaFromTexture(false, 0.5, _in.DoubleSided());

const sdf::Pbr * pbr = _in.PbrMaterial();
if (pbr != nullptr)
{
ignition::common::Pbr pbrOut;

const sdf::PbrWorkflow * pbrWorkflow =
pbr->Workflow(sdf::PbrWorkflowType::METAL);
if (pbrWorkflow)
{
pbrOut.SetType(ignition::common::PbrType::METAL);
}
else
{
pbrWorkflow = pbr->Workflow(sdf::PbrWorkflowType::SPECULAR);
if (pbrWorkflow)
{
pbrOut.SetType(ignition::common::PbrType::SPECULAR);
}
}
if (pbrWorkflow != nullptr)
{
pbrOut.SetAlbedoMap(pbrWorkflow->AlbedoMap());
pbrOut.SetMetalnessMap(pbrWorkflow->MetalnessMap());
pbrOut.SetEmissiveMap(pbrWorkflow->EmissiveMap());
pbrOut.SetRoughnessMap(pbrWorkflow->RoughnessMap());
pbrOut.SetSpecularMap(pbrWorkflow->SpecularMap());
pbrOut.SetEnvironmentMap(pbrWorkflow->EnvironmentMap());
pbrOut.SetAmbientOcclusionMap(pbrWorkflow->AmbientOcclusionMap());
pbrOut.SetLightMap(pbrWorkflow->LightMap());
pbrOut.SetRoughness(pbrWorkflow->Roughness());
pbrOut.SetGlossiness(pbrWorkflow->Glossiness());
pbrOut.SetMetalness(pbrWorkflow->Metalness());

if (pbrWorkflow->NormalMapType() == sdf::NormalMapSpace::TANGENT)
{
pbrOut.SetNormalMap(
pbrWorkflow->NormalMap(),
ignition::common::NormalMapSpace::TANGENT);
}
else if (pbrWorkflow->NormalMapType() == sdf::NormalMapSpace::OBJECT)
{
pbrOut.SetNormalMap(
pbrWorkflow->NormalMap(),
ignition::common::NormalMapSpace::OBJECT);
}
}
out->SetPbrMaterial(pbrOut);
}
return out;
}
}
}
}
Loading