Skip to content

Commit

Permalink
rename ND_normalmap -> ND_normalmap_float, and also bring over ND_nor…
Browse files Browse the repository at this point in the history
…malmap_vector2 from 'main' branch
  • Loading branch information
ld-kerley committed Jan 26, 2024
1 parent 2039b71 commit 8319ff4
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 12 deletions.
7 changes: 6 additions & 1 deletion libraries/stdlib/genglsl/mx_normalmap.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
void mx_normalmap(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T, out vec3 result)
void mx_normalmap_vector2(vec3 value, int map_space, vec2 normal_scale, vec3 N, vec3 T, out vec3 result)
{
// Decode the normal map.
value = (value == vec3(0.0f)) ? vec3(0.0, 0.0, 1.0) : value * 2.0 - 1.0;
Expand All @@ -14,3 +14,8 @@ void mx_normalmap(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T,
// Normalize the result.
result = normalize(value);
}

void mx_normalmap_float(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T, out vec3 result)
{
mx_normalmap_vector2(value, map_space, vec2(normal_scale), N, T, result);
}
3 changes: 2 additions & 1 deletion libraries/stdlib/genglsl/stdlib_genglsl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
</implementation>

<!-- <normalmap> -->
<implementation name="IM_normalmap_genglsl" nodedef="ND_normalmap" file="mx_normalmap.glsl" function="mx_normalmap" target="genglsl" />
<implementation name="IM_normalmap_float_genglsl" nodedef="ND_normalmap_float" file="mx_normalmap.glsl" function="mx_normalmap_float" target="genglsl" />
<implementation name="IM_normalmap_vector2_genglsl" nodedef="ND_normalmap_vector2" file="mx_normalmap.glsl" function="mx_normalmap_vector2" target="genglsl" />

<!-- ======================================================================== -->
<!-- Procedural nodes -->
Expand Down
3 changes: 2 additions & 1 deletion libraries/stdlib/genmdl/stdlib_genmdl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
<!-- <triplanarprojection> -->

<!-- <normalmap> -->
<implementation name="IM_normalmap_genmdl" nodedef="ND_normalmap" sourcecode="mx::stdlib::mx_normalmap(mxp_in:{{in}}, mxp_space:{{space}}, mxp_scale:{{scale}}, mxp_normal:{{normal}}, mxp_tangent:{{tangent}})" target="genmdl" />
<implementation name="IM_normalmap_float_genmdl" nodedef="ND_normalmap_float" sourcecode="mx::stdlib::mx_normalmap_float(mxp_in:{{in}}, mxp_space:{{space}}, mxp_scale:{{scale}}, mxp_normal:{{normal}}, mxp_tangent:{{tangent}})" target="genmdl" />
<implementation name="IM_normalmap_vector2_genmdl" nodedef="ND_normalmap_vector2" sourcecode="mx::stdlib::mx_normalmap_vector2(mxp_in:{{in}}, mxp_space:{{space}}, mxp_scale:{{scale}}, mxp_normal:{{normal}}, mxp_tangent:{{tangent}})" target="genmdl" />

<!-- ======================================================================== -->
<!-- Procedural nodes -->
Expand Down
7 changes: 6 additions & 1 deletion libraries/stdlib/genmsl/mx_normalmap.metal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
void mx_normalmap(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T, out vec3 result)
void mx_normalmap_vector2(vec3 value, int map_space, vec2 normal_scale, vec3 N, vec3 T, out vec3 result)
{
// Decode the normal map.
value = all(value == vec3(0.0f)) ? vec3(0.0, 0.0, 1.0) : value * 2.0 - 1.0;
Expand All @@ -14,3 +14,8 @@ void mx_normalmap(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T,
// Normalize the result.
result = normalize(value);
}

void mx_normalmap_float(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T, out vec3 result)
{
mx_normalmap_vector2(value, map_space, vec2(normal_scale), N, T, result);
}
3 changes: 2 additions & 1 deletion libraries/stdlib/genmsl/stdlib_genmsl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
</implementation>

<!-- <normalmap> -->
<implementation name="IM_normalmap_genmsl" nodedef="ND_normalmap" file="mx_normalmap.metal" function="mx_normalmap" target="genmsl" />
<implementation name="IM_normalmap_float_genmsl" nodedef="ND_normalmap_float" file="mx_normalmap.metal" function="mx_normalmap_float" target="genmsl" />
<implementation name="IM_normalmap_vector2_genmsl" nodedef="ND_normalmap_vector2" file="mx_normalmap.metal" function="mx_normalmap_vector2" target="genmsl" />

<!-- ======================================================================== -->
<!-- Procedural nodes -->
Expand Down
9 changes: 7 additions & 2 deletions libraries/stdlib/genosl/mx_normalmap.osl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
void mx_normalmap(vector value, string map_space, float normal_scale, vector N, vector U, output vector result)
void mx_normalmap_vector2(vector value, string map_space, vector2 normal_scale, vector N, vector U, output vector result)
{
// Tangent space
if (map_space == "tangent")
{
vector v = value * 2.0 - 1.0;
vector T = normalize(U - dot(U, N) * N);
vector B = normalize(cross(N, T));
result = normalize(T * v[0] * normal_scale + B * v[1] * normal_scale + N * v[2]);
result = normalize(T * v[0] * normal_scale.x + B * v[1] * normal_scale.y + N * v[2]);
}
// Object space
else
Expand All @@ -15,3 +15,8 @@ void mx_normalmap(vector value, string map_space, float normal_scale, vector N,
result = normalize(n);
}
}

void mx_normalmap_float(vector value, string map_space, float normal_scale, vector N, vector U, output vector result)
{
mx_normalmap_vector2(value, map_space, vector2(normal_scale, normal_scale), N, U, result);
}
3 changes: 2 additions & 1 deletion libraries/stdlib/genosl/stdlib_genosl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
<!-- <triplanarprojection> -->

<!-- <normalmap> -->
<implementation name="IM_normalmap_genosl" nodedef="ND_normalmap" file="mx_normalmap.osl" function="mx_normalmap" target="genosl" />
<implementation name="IM_normalmap_float_genosl" nodedef="ND_normalmap_float" file="mx_normalmap.osl" function="mx_normalmap_float" target="genosl" />
<implementation name="IM_normalmap_vector2_genosl" nodedef="ND_normalmap_vector2" file="mx_normalmap.osl" function="mx_normalmap_vector2" target="genosl" />

<!-- ======================================================================== -->
<!-- Procedural nodes -->
Expand Down
10 changes: 9 additions & 1 deletion libraries/stdlib/stdlib_defs.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -2497,14 +2497,22 @@
Node: <normalmap>
Transform a normal vector from object or tangent space into "world" space.
-->
<nodedef name="ND_normalmap" node="normalmap" nodegroup="math">
<nodedef name="ND_normalmap_float" node="normalmap" nodegroup="math">
<input name="in" type="vector3" value="0.5, 0.5, 1.0" />
<input name="space" type="string" value="tangent" enum="tangent, object" uniform="true" />
<input name="scale" type="float" value="1.0" />
<input name="normal" type="vector3" defaultgeomprop="Nworld" />
<input name="tangent" type="vector3" defaultgeomprop="Tworld" />
<output name="out" type="vector3" defaultinput="normal" />
</nodedef>
<nodedef name="ND_normalmap_vector2" node="normalmap" nodegroup="math">
<input name="in" type="vector3" value="0.5, 0.5, 1.0" />
<input name="space" type="string" value="tangent" enum="tangent, object" uniform="true" />
<input name="scale" type="vector2" value="1.0, 1.0" />
<input name="normal" type="vector3" defaultgeomprop="Nworld" />
<input name="tangent" type="vector3" defaultgeomprop="Tworld" />
<output name="out" type="vector3" defaultinput="normal" />
</nodedef>

<!--
Node: <transpose>
Expand Down
11 changes: 11 additions & 0 deletions source/MaterialXCore/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,17 @@ void Document::upgradeVersion()
removeNode(top->getName());
}
}

NodePtr node = elem->asA<Node>();
if (!node)
{
continue;
}
const string& nodeDef = node->getNodeDefString();
if (nodeDef == "ND_normalmap") {
// ND_normalmap was renamed to ND_normalmap_float
node->setNodeDefString("ND_normalmap_float");
}
}

removeNodeDef("ND_thin_film_bsdf");
Expand Down
23 changes: 20 additions & 3 deletions source/MaterialXGenMdl/mdl/materialx/stdlib.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -2002,13 +2002,13 @@ export float4 mx_transformmatrix_vector4(
return mxp_mat * mxp_in;
}

export float3 mx_normalmap(
export float3 mx_normalmap_vector2(
float3 mxp_in = float3(0.5, 0.5, 1.0),
uniform string mxp_space = string("tangent")
[[
anno::description("Enumeration {tangent, object}.")
]],
float mxp_scale = float(1.0),
float2 mxp_scale = float2(1.0, 1.0),
float3 mxp_normal = float3(::state::transform_normal(::state::coordinate_internal,::state::coordinate_world,::state::normal())),
float3 mxp_tangent = float3(state::transform_vector(::state::coordinate_internal,::state::coordinate_world,::state::texture_tangent_u(0)))
)
Expand All @@ -2020,7 +2020,7 @@ export float3 mx_normalmap(
{
float3 v = mxp_in * 2.0 - 1.0;
float3 binormal = ::math::normalize(::math::cross(mxp_normal, mxp_tangent));
return ::math::normalize(mxp_tangent * v.x * mxp_scale + binormal * v.y * mxp_scale + mxp_normal * v.z);
return ::math::normalize(mxp_tangent * v.x * mxp_scale.x + binormal * v.y * mxp_scale.y + mxp_normal * v.z);
}
else
{
Expand All @@ -2029,6 +2029,23 @@ export float3 mx_normalmap(
}
}

export float3 mx_normalmap_float(
float3 mxp_in = float3(0.5, 0.5, 1.0),
uniform string mxp_space = string("tangent")
[[
anno::description("Enumeration {tangent, object}.")
]],
float mxp_scale = float(1.0),
float3 mxp_normal = float3(::state::transform_normal(::state::coordinate_internal,::state::coordinate_world,::state::normal())),
float3 mxp_tangent = float3(state::transform_vector(::state::coordinate_internal,::state::coordinate_world,::state::texture_tangent_u(0)))
)
[[
anno::description("Node Group: math")
]]
{
return mx_normalmap_vector2(mxp_in, mxp_space, float2(mxp_scale, mxp_scale), mxp_normal, mxp_tangent);
}

export float3x3 mx_transpose_matrix33(
float3x3 mxp_in = float3x3(1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0)
)
Expand Down

0 comments on commit 8319ff4

Please sign in to comment.