Skip to content

Commit

Permalink
Add support for displacement in MDL generation (AcademySoftwareFounda…
Browse files Browse the repository at this point in the history
  • Loading branch information
krohmerNV authored Jul 9, 2023
1 parent bf3ceef commit e0f87cb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 21 deletions.
21 changes: 18 additions & 3 deletions source/MaterialXGenMdl/MdlShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const vector<string> DEFAULT_IMPORTS =
"import ::anno::*",
"import ::tex::*",
"import ::mx::swizzle::*",
"import ::mx::cm::*",
"using ::mx::core import *",
"using ::mx::stdlib import *",
"using ::mx::pbrlib import *",
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions source/MaterialXGenMdl/MdlSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ MdlSyntax::MdlSyntax()
registerTypeSyntax(
Type::DISPLACEMENTSHADER,
std::make_shared<ScalarTypeSyntax>(
"float3",
"float3(0.0)",
"float3(0.0)"));
"material",
"material()",
"material()"));

registerTypeSyntax(
Type::LIGHTSHADER,
Expand Down
3 changes: 3 additions & 0 deletions source/MaterialXGenMdl/Nodes/MaterialNodeMdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ", ";
}
Expand Down
32 changes: 21 additions & 11 deletions source/MaterialXGenMdl/mdl/materialx/pbrlib.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -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") ]],
Expand Down
6 changes: 3 additions & 3 deletions source/MaterialXGenMdl/mdl/materialx/stdlib.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ 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,
surface: mxp_surfaceshader.surface,
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,
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXTest/MaterialXGenMdl/GenMdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ void MdlShaderGeneratorTester::compileSource(const std::vector<mx::FilePath>& 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
Expand Down

0 comments on commit e0f87cb

Please sign in to comment.