Skip to content

Commit f17c2b1

Browse files
authored
Add support for displacement in MDL generation (#1396)
1 parent 4075137 commit f17c2b1

6 files changed

Lines changed: 49 additions & 21 deletions

File tree

source/MaterialXGenMdl/MdlShaderGenerator.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ const vector<string> DEFAULT_IMPORTS =
4242
"import ::anno::*",
4343
"import ::tex::*",
4444
"import ::mx::swizzle::*",
45-
"import ::mx::cm::*",
4645
"using ::mx::core import *",
4746
"using ::mx::stdlib import *",
4847
"using ::mx::pbrlib import *",
@@ -273,9 +272,22 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G
273272
// Get final result
274273
const string result = getUpstreamResult(outputSocket, context);
275274

275+
const TypeDesc* outputType = outputSocket->getType();
276276
if (graph.hasClassification(ShaderNode::Classification::TEXTURE))
277277
{
278-
emitLine("color finalOutput__ = mk_color3(" + result + ")", stage);
278+
if (outputType == Type::DISPLACEMENTSHADER)
279+
{
280+
emitLine("float3 displacement__ = " + result + ".geometry.displacement", stage);
281+
emitLine("color finalOutput__ = mk_color3("
282+
"r: math::dot(displacement__, state::texture_tangent_u(0)),"
283+
"g: math::dot(displacement__, state::texture_tangent_v(0)),"
284+
"b: math::dot(displacement__, state::normal()))", stage);
285+
}
286+
else
287+
{
288+
emitLine("float3 displacement__ = float3(0.0)", stage);
289+
emitLine("color finalOutput__ = mk_color3(" + result + ")", stage);
290+
}
279291

280292
// End shader body
281293
emitScopeEnd(stage);
@@ -289,13 +301,16 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G
289301
" intensity : finalOutput__ * math::PI,\n"
290302
" mode : intensity_radiant_exitance\n"
291303
" )\n"
304+
" ),\n"
305+
" geometry: material_geometry(\n"
306+
" displacement : displacement__\n"
292307
" )\n"
293308
");";
294309
emitBlock(textureMaterial, FilePath(), context, stage);
295310
}
296311
else
297312
{
298-
emitLine(_syntax->getTypeSyntax(outputSocket->getType()).getName() + " finalOutput__ = " + result, stage);
313+
emitLine(_syntax->getTypeSyntax(outputType).getName() + " finalOutput__ = " + result, stage);
299314

300315
// End shader body
301316
emitScopeEnd(stage);

source/MaterialXGenMdl/MdlSyntax.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ MdlSyntax::MdlSyntax()
397397
registerTypeSyntax(
398398
Type::DISPLACEMENTSHADER,
399399
std::make_shared<ScalarTypeSyntax>(
400-
"float3",
401-
"float3(0.0)",
402-
"float3(0.0)"));
400+
"material",
401+
"material()",
402+
"material()"));
403403

404404
registerTypeSyntax(
405405
Type::LIGHTSHADER,

source/MaterialXGenMdl/Nodes/MaterialNodeMdl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ void MaterialNodeMdl::emitFunctionCall(const ShaderNode& _node, GenContext& cont
4646
for (ShaderInput* input : node.getInputs())
4747
{
4848
shadergen.emitString(delim, stage);
49+
shadergen.emitString("mxp_", stage);
50+
shadergen.emitString(input->getName(), stage);
51+
shadergen.emitString(": ", stage);
4952
shadergen.emitInput(input, context, stage);
5053
delim = ", ";
5154
}

source/MaterialXGenMdl/mdl/materialx/pbrlib.mdl

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -641,23 +641,33 @@ export material mx_light(
641641
// TODO: Check if this works in the translator and higher level nodes, like
642642
// mxp_material, if not, we have to switch to the material type and
643643
// use two different names for mxp_displacemnnt.
644-
export float3 mx_displacement_float(
644+
export material mx_displacement_float(
645645
float mxp_displacement = 0.0,
646646
float mxp_scale = 1.0
647-
)
648-
{
649-
return mxp_displacement * mxp_scale * state::normal();
650-
}
647+
) [[
648+
anno::usage( "materialx:displacementshader")
649+
]] = material(
650+
geometry: material_geometry(
651+
displacement: mxp_displacement * mxp_scale * state::scene_units_per_meter() * state::normal()
652+
)
653+
);
651654

652-
export float3 mx_displacement_vector3(
655+
export material mx_displacement_vector3(
653656
float3 mxp_displacement = float3(0.0),
654657
float mxp_scale = 1.0
655-
)
658+
) [[
659+
anno::usage( "materialx:displacementshader")
660+
]] = let
656661
{
657-
return mxp_scale * ( mxp_displacement.x * state::texture_tangent_u(0)
658-
+ mxp_displacement.y * state::texture_tangent_v(0)
659-
+ mxp_displacement.z * state::normal());
660-
}
662+
float3 vec = (mxp_displacement.x * state::texture_tangent_u(0)
663+
+ mxp_displacement.y * state::texture_tangent_v(0)
664+
+ mxp_displacement.z * state::normal());
665+
} in material(
666+
geometry: material_geometry(
667+
displacement: vec * mxp_scale * state::scene_units_per_meter()
668+
)
669+
);
670+
661671

662672
export material mx_mix_bsdf(
663673
material mxp_fg = material() [[ anno::usage( "materialx:bsdf") ]],

source/MaterialXGenMdl/mdl/materialx/stdlib.mdl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ import ::df::*;
3636
// Shader Nodes
3737

3838
export material mx_surfacematerial(
39-
material mxp_surfaceshader = material() [[ anno::usage( "materialx:surfaceshader") ]],
40-
float3 mxp_displacement = float3(0.0, 0.0, 0.0)
39+
material mxp_surfaceshader = material() [[ anno::usage( "materialx:surfaceshader") ]],
40+
material mxp_displacementshader = material() [[ anno::usage( "materialx:displacementshader") ]]
4141
)
4242
= material(
4343
thin_walled: false,
4444
surface: mxp_surfaceshader.surface,
4545
backface: mxp_surfaceshader.backface,
4646
geometry: material_geometry(
4747
cutout_opacity: mxp_surfaceshader.geometry.cutout_opacity,
48-
displacement : mxp_displacement,
48+
displacement : mxp_displacementshader.geometry.displacement,
4949
normal: mxp_surfaceshader.geometry.normal
5050
),
5151
ior: mxp_surfaceshader.ior,

source/MaterialXTest/MaterialXGenMdl/GenMdl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void MdlShaderGeneratorTester::compileSource(const std::vector<mx::FilePath>& so
288288
std::string iblFile = (rootPath / "resources/lights/san_giuseppe_bridge.hdr").asString();
289289
renderCommand += " --hdr \"" + iblFile + "\" --hdr_rotate 90";
290290
// set scene
291-
renderCommand += " --uv_scale 0.5 1.0 --uv_offset 0.0 0.0 --uv_repeat";
291+
renderCommand += " --uv_scale 0.5 1.0 --uv_offset 0.0 0.0 --uv_repeat --uv_flip";
292292
renderCommand += " --camera 0 0 3 0 0 0 --fov 45";
293293

294294
// set the material

0 commit comments

Comments
 (0)