Refactor: add single entry point for creating USD materials

To be used by both USD and Hydra.
This commit is contained in:
Brecht Van Lommel
2023-08-05 17:02:26 +02:00
parent 6e4b1f78cd
commit 8ff98504ea
3 changed files with 41 additions and 38 deletions

View File

@@ -113,18 +113,8 @@ pxr::UsdShadeMaterial USDAbstractWriter::ensure_usd_material(const HierarchyCont
if (usd_material) {
return usd_material;
}
usd_material = pxr::UsdShadeMaterial::Define(stage, usd_path);
if (material->use_nodes && this->usd_export_context_.export_params.generate_preview_surface) {
std::string active_uv = get_mesh_active_uvlayer_name(context.object);
create_usd_preview_surface_material(
this->usd_export_context_, material, usd_material, active_uv);
}
else {
create_usd_viewport_material(this->usd_export_context_, material, usd_material);
}
return usd_material;
std::string active_uv = get_mesh_active_uvlayer_name(context.object);
return create_usd_material(usd_export_context_, usd_path, material, active_uv);
}
void USDAbstractWriter::write_visibility(const HierarchyContext &context,

View File

@@ -120,10 +120,10 @@ template<typename T1, typename T2>
void create_input(pxr::UsdShadeShader &shader, const InputSpec &spec, const void *value);
void set_normal_texture_range(pxr::UsdShadeShader &usd_shader, const InputSpec &input_spec);
void create_usd_preview_surface_material(const USDExporterContext &usd_export_context,
Material *material,
pxr::UsdShadeMaterial &usd_material,
const std::string &default_uv)
static void create_usd_preview_surface_material(const USDExporterContext &usd_export_context,
Material *material,
pxr::UsdShadeMaterial &usd_material,
const std::string &default_uv)
{
if (!material) {
return;
@@ -564,7 +564,15 @@ static pxr::UsdShadeShader create_usd_preview_shader(const USDExporterContext &u
return shader;
}
/* Creates a USD Preview Surface shader based on the given cycles shading node. */
/* Creates a USD Preview Surface shader based on the given cycles shading node.
* Due to the limited nodes in the USD Preview Surface specification, only the following nodes
* are supported:
* - UVMap
* - Texture Coordinate
* - Image Texture
* - Principled BSDF
* More may be added in the future.
*/
static pxr::UsdShadeShader create_usd_preview_shader(const USDExporterContext &usd_export_context,
pxr::UsdShadeMaterial &material,
bNode *node)
@@ -818,4 +826,22 @@ const pxr::TfToken token_for_input(const char *input_name)
return it->second.input_name;
}
pxr::UsdShadeMaterial create_usd_material(const USDExporterContext &usd_export_context,
pxr::SdfPath usd_path,
Material *material,
const std::string &active_uv)
{
pxr::UsdShadeMaterial usd_material = pxr::UsdShadeMaterial::Define(usd_export_context.stage,
usd_path);
if (material->use_nodes && usd_export_context.export_params.generate_preview_surface) {
create_usd_preview_surface_material(usd_export_context, material, usd_material, active_uv);
}
else {
create_usd_viewport_material(usd_export_context, material, usd_material);
}
return usd_material;
}
} // namespace blender::io::usd

View File

@@ -16,31 +16,18 @@ namespace blender::io::usd {
struct USDExporterContext;
/* Returns a USDPreviewSurface token name for a given Blender shader Socket name,
* or an empty TfToken if the input name is not found in the map. */
const pxr::TfToken token_for_input(const char *input_name);
/**
* Entry point to create an approximate USD Preview Surface network from a Cycles node graph.
* Due to the limited nodes in the USD Preview Surface specification, only the following nodes
* are supported:
* - UVMap
* - Texture Coordinate
* - Image Texture
* - Principled BSDF
* More may be added in the future.
/* Create USDMaterial from Blender material.
*
* \param default_uv: used as the default UV set name sampled by the `primvar`
* reader shaders generated for image texture nodes that don't have an attached UVMap node.
*/
void create_usd_preview_surface_material(const USDExporterContext &usd_export_context,
Material *material,
pxr::UsdShadeMaterial &usd_material,
const std::string &default_uv = "");
pxr::UsdShadeMaterial create_usd_material(const USDExporterContext &usd_export_context,
pxr::SdfPath usd_path,
Material *material,
const std::string &active_uv);
/* Entry point to create USD Shade Material network from Blender viewport display settings. */
void create_usd_viewport_material(const USDExporterContext &usd_export_context,
Material *material,
pxr::UsdShadeMaterial &usd_material);
/* Returns a USDPreviewSurface token name for a given Blender shader Socket name,
* or an empty TfToken if the input name is not found in the map. */
const pxr::TfToken token_for_input(const char *input_name);
} // namespace blender::io::usd