From f17c2b1fc4579aac49769c216a9a1101c1c6d923 Mon Sep 17 00:00:00 2001 From: krohmerNV <42233792+krohmerNV@users.noreply.github.com> Date: Sun, 9 Jul 2023 05:37:48 +0200 Subject: [PATCH] Add support for displacement in MDL generation (#1396) --- source/MaterialXGenMdl/MdlShaderGenerator.cpp | 21 ++++++++++-- source/MaterialXGenMdl/MdlSyntax.cpp | 6 ++-- .../MaterialXGenMdl/Nodes/MaterialNodeMdl.cpp | 3 ++ .../MaterialXGenMdl/mdl/materialx/pbrlib.mdl | 32 ++++++++++++------- .../MaterialXGenMdl/mdl/materialx/stdlib.mdl | 6 ++-- .../MaterialXTest/MaterialXGenMdl/GenMdl.cpp | 2 +- 6 files changed, 49 insertions(+), 21 deletions(-) diff --git a/source/MaterialXGenMdl/MdlShaderGenerator.cpp b/source/MaterialXGenMdl/MdlShaderGenerator.cpp index d1399e8aed..dddf1f2f13 100644 --- a/source/MaterialXGenMdl/MdlShaderGenerator.cpp +++ b/source/MaterialXGenMdl/MdlShaderGenerator.cpp @@ -42,7 +42,6 @@ const vector DEFAULT_IMPORTS = "import ::anno::*", "import ::tex::*", "import ::mx::swizzle::*", - "import ::mx::cm::*", "using ::mx::core import *", "using ::mx::stdlib import *", "using ::mx::pbrlib import *", @@ -273,9 +272,22 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G // Get final result const string result = getUpstreamResult(outputSocket, context); + const TypeDesc* outputType = outputSocket->getType(); if (graph.hasClassification(ShaderNode::Classification::TEXTURE)) { - emitLine("color finalOutput__ = mk_color3(" + result + ")", stage); + if (outputType == Type::DISPLACEMENTSHADER) + { + emitLine("float3 displacement__ = " + result + ".geometry.displacement", stage); + emitLine("color finalOutput__ = mk_color3(" + "r: math::dot(displacement__, state::texture_tangent_u(0))," + "g: math::dot(displacement__, state::texture_tangent_v(0))," + "b: math::dot(displacement__, state::normal()))", stage); + } + else + { + emitLine("float3 displacement__ = float3(0.0)", stage); + emitLine("color finalOutput__ = mk_color3(" + result + ")", stage); + } // End shader body emitScopeEnd(stage); @@ -289,13 +301,16 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G " intensity : finalOutput__ * math::PI,\n" " mode : intensity_radiant_exitance\n" " )\n" + " ),\n" + " geometry: material_geometry(\n" + " displacement : displacement__\n" " )\n" ");"; emitBlock(textureMaterial, FilePath(), context, stage); } else { - emitLine(_syntax->getTypeSyntax(outputSocket->getType()).getName() + " finalOutput__ = " + result, stage); + emitLine(_syntax->getTypeSyntax(outputType).getName() + " finalOutput__ = " + result, stage); // End shader body emitScopeEnd(stage); diff --git a/source/MaterialXGenMdl/MdlSyntax.cpp b/source/MaterialXGenMdl/MdlSyntax.cpp index 260eb3e38a..9d5483bcca 100644 --- a/source/MaterialXGenMdl/MdlSyntax.cpp +++ b/source/MaterialXGenMdl/MdlSyntax.cpp @@ -397,9 +397,9 @@ MdlSyntax::MdlSyntax() registerTypeSyntax( Type::DISPLACEMENTSHADER, std::make_shared( - "float3", - "float3(0.0)", - "float3(0.0)")); + "material", + "material()", + "material()")); registerTypeSyntax( Type::LIGHTSHADER, diff --git a/source/MaterialXGenMdl/Nodes/MaterialNodeMdl.cpp b/source/MaterialXGenMdl/Nodes/MaterialNodeMdl.cpp index e4fff0567b..936526d59d 100644 --- a/source/MaterialXGenMdl/Nodes/MaterialNodeMdl.cpp +++ b/source/MaterialXGenMdl/Nodes/MaterialNodeMdl.cpp @@ -46,6 +46,9 @@ void MaterialNodeMdl::emitFunctionCall(const ShaderNode& _node, GenContext& cont for (ShaderInput* input : node.getInputs()) { shadergen.emitString(delim, stage); + shadergen.emitString("mxp_", stage); + shadergen.emitString(input->getName(), stage); + shadergen.emitString(": ", stage); shadergen.emitInput(input, context, stage); delim = ", "; } diff --git a/source/MaterialXGenMdl/mdl/materialx/pbrlib.mdl b/source/MaterialXGenMdl/mdl/materialx/pbrlib.mdl index 3f64767491..9c166f7468 100644 --- a/source/MaterialXGenMdl/mdl/materialx/pbrlib.mdl +++ b/source/MaterialXGenMdl/mdl/materialx/pbrlib.mdl @@ -641,23 +641,33 @@ export material mx_light( // TODO: Check if this works in the translator and higher level nodes, like // mxp_material, if not, we have to switch to the material type and // use two different names for mxp_displacemnnt. -export float3 mx_displacement_float( +export material mx_displacement_float( float mxp_displacement = 0.0, float mxp_scale = 1.0 -) -{ - return mxp_displacement * mxp_scale * state::normal(); -} +) [[ + anno::usage( "materialx:displacementshader") +]] = material( + geometry: material_geometry( + displacement: mxp_displacement * mxp_scale * state::scene_units_per_meter() * state::normal() + ) +); -export float3 mx_displacement_vector3( +export material mx_displacement_vector3( float3 mxp_displacement = float3(0.0), float mxp_scale = 1.0 -) +) [[ + anno::usage( "materialx:displacementshader") +]] = let { - return mxp_scale * ( mxp_displacement.x * state::texture_tangent_u(0) - + mxp_displacement.y * state::texture_tangent_v(0) - + mxp_displacement.z * state::normal()); -} + float3 vec = (mxp_displacement.x * state::texture_tangent_u(0) + + mxp_displacement.y * state::texture_tangent_v(0) + + mxp_displacement.z * state::normal()); +} in material( + geometry: material_geometry( + displacement: vec * mxp_scale * state::scene_units_per_meter() + ) +); + export material mx_mix_bsdf( material mxp_fg = material() [[ anno::usage( "materialx:bsdf") ]], diff --git a/source/MaterialXGenMdl/mdl/materialx/stdlib.mdl b/source/MaterialXGenMdl/mdl/materialx/stdlib.mdl index b73e881fcb..2cb313221c 100644 --- a/source/MaterialXGenMdl/mdl/materialx/stdlib.mdl +++ b/source/MaterialXGenMdl/mdl/materialx/stdlib.mdl @@ -36,8 +36,8 @@ import ::df::*; // Shader Nodes export material mx_surfacematerial( - material mxp_surfaceshader = material() [[ anno::usage( "materialx:surfaceshader") ]], - float3 mxp_displacement = float3(0.0, 0.0, 0.0) + material mxp_surfaceshader = material() [[ anno::usage( "materialx:surfaceshader") ]], + material mxp_displacementshader = material() [[ anno::usage( "materialx:displacementshader") ]] ) = material( thin_walled: false, @@ -45,7 +45,7 @@ export material mx_surfacematerial( backface: mxp_surfaceshader.backface, geometry: material_geometry( cutout_opacity: mxp_surfaceshader.geometry.cutout_opacity, - displacement : mxp_displacement, + displacement : mxp_displacementshader.geometry.displacement, normal: mxp_surfaceshader.geometry.normal ), ior: mxp_surfaceshader.ior, diff --git a/source/MaterialXTest/MaterialXGenMdl/GenMdl.cpp b/source/MaterialXTest/MaterialXGenMdl/GenMdl.cpp index fbe19a5957..e565071caa 100644 --- a/source/MaterialXTest/MaterialXGenMdl/GenMdl.cpp +++ b/source/MaterialXTest/MaterialXGenMdl/GenMdl.cpp @@ -288,7 +288,7 @@ void MdlShaderGeneratorTester::compileSource(const std::vector& so std::string iblFile = (rootPath / "resources/lights/san_giuseppe_bridge.hdr").asString(); renderCommand += " --hdr \"" + iblFile + "\" --hdr_rotate 90"; // set scene - renderCommand += " --uv_scale 0.5 1.0 --uv_offset 0.0 0.0 --uv_repeat"; + renderCommand += " --uv_scale 0.5 1.0 --uv_offset 0.0 0.0 --uv_repeat --uv_flip"; renderCommand += " --camera 0 0 3 0 0 0 --fov 45"; // set the material