Skip to content

Commit

Permalink
sdf -> usd: Materials (#831)
Browse files Browse the repository at this point in the history
Support materials in the sdf2usd converter

Co-authored-by: Ashton Larkin <[email protected]>
Co-authored-by: Teo Koon Peng <[email protected]>
Co-authored-by: Steve Peters <[email protected]>
  • Loading branch information
4 people authored Mar 4, 2022
1 parent c7fd82d commit 9641718
Show file tree
Hide file tree
Showing 12 changed files with 1,534 additions and 2 deletions.
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>
<metal>
<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>
</metal>
</pbr>
</material>
</visual>
</link>
</model>
Expand Down
54 changes: 54 additions & 0 deletions usd/include/sdf/usd/Conversions.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 ignition::common::Material *_in);

/// \brief Specialized conversion from an SDF material to a Ignition Common
/// material.
/// \param[in] _in SDF material.
/// \param[out] _out The Ignition Common Material.
IGNITION_SDFORMAT_USD_VISIBLE
void convert(const sdf::Material &_in, ignition::common::Material &_out);
}
}
}

#endif
3 changes: 3 additions & 0 deletions usd/include/sdf/usd/UsdError.hh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ namespace sdf

/// \brief Invalid submesh primitive type
INVALID_SUBMESH_PRIMITIVE_TYPE,

/// \brief Invalid material
INVALID_MATERIAL,
};

class IGNITION_SDFORMAT_USD_VISIBLE UsdError
Expand Down
60 changes: 60 additions & 0 deletions usd/include/sdf/usd/sdf_parser/Material.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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/sdf/path.h>
#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/usd/UsdError.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] _materialSdf The SDF material to parse.
/// \param[in] _stage The stage that should contain the USD representation
/// of _material.
/// \param[out] _materialPath USD Material path
/// \return UsdErrors, which is a list of UsdError objects. This list is
/// empty if no errors occurred when parsing _materialSdf its USD
/// representation
UsdErrors IGNITION_SDFORMAT_USD_VISIBLE ParseSdfMaterial(
const sdf::Material *_materialSdf,
pxr::UsdStageRefPtr &_stage,
pxr::SdfPath &_materialPath);
}
}
}

#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
152 changes: 152 additions & 0 deletions usd/src/Conversions.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* 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 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;
}

void convert(const sdf::Material &_in, ignition::common::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.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);
}
}
}
}
}
Loading

0 comments on commit 9641718

Please sign in to comment.