From 8ff98504ea122e3b4652bb99cd602cfc1c8b4fef Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Sat, 5 Aug 2023 17:02:26 +0200 Subject: [PATCH] Refactor: add single entry point for creating USD materials To be used by both USD and Hydra. --- .../io/usd/intern/usd_writer_abstract.cc | 14 ++------ .../io/usd/intern/usd_writer_material.cc | 36 ++++++++++++++++--- .../io/usd/intern/usd_writer_material.h | 29 +++++---------- 3 files changed, 41 insertions(+), 38 deletions(-) diff --git a/source/blender/io/usd/intern/usd_writer_abstract.cc b/source/blender/io/usd/intern/usd_writer_abstract.cc index 2c616dccdfd..50c027e69d0 100644 --- a/source/blender/io/usd/intern/usd_writer_abstract.cc +++ b/source/blender/io/usd/intern/usd_writer_abstract.cc @@ -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, diff --git a/source/blender/io/usd/intern/usd_writer_material.cc b/source/blender/io/usd/intern/usd_writer_material.cc index 34251aed39b..7049390dbe3 100644 --- a/source/blender/io/usd/intern/usd_writer_material.cc +++ b/source/blender/io/usd/intern/usd_writer_material.cc @@ -120,10 +120,10 @@ template 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 diff --git a/source/blender/io/usd/intern/usd_writer_material.h b/source/blender/io/usd/intern/usd_writer_material.h index 013fce46d67..e302e6d95b9 100644 --- a/source/blender/io/usd/intern/usd_writer_material.h +++ b/source/blender/io/usd/intern/usd_writer_material.h @@ -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