Skip to content

Commit

Permalink
Add vector2 variant of normalmap (#1355)
Browse files Browse the repository at this point in the history
The materialx specification defines the scale attribute of the normalmap to be either a float or a vector2 but the vector2 variant was missing from the implementation. This PR adds it.
  • Loading branch information
mnikelsky authored Jan 8, 2024
1 parent feecfe0 commit 96276bb
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 11 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);
}
4 changes: 3 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,9 @@
</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" 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" 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" 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[0] + B * v[1] * normal_scale[1] + 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" 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
8 changes: 8 additions & 0 deletions libraries/stdlib/stdlib_defs.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,14 @@
<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
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,29 @@
</tiledimage>
<output name="normal" type="vector3" nodename="normalmap" />
</nodegraph>
<standard_surface name="NormalMappedShader2" type="surfaceshader">
<input name="base" type="float" value="0.6" />
<input name="metalness" type="float" value="1.0" />
<input name="specular" type="float" value="0.7" />
<input name="coat" type="float" value="1" />
<input name="normal" type="vector3" nodegraph="NormalMapGraph2" />
</standard_surface>
<surfacematerial name="NormalMappedShaderMaterial2" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="NormalMappedShader2" />
<input name="displacementshader" type="displacementshader" value="" />
</surfacematerial>
<nodegraph name="NormalMapGraph2">
<normalmap name="normalmap_vector2" type="vector3">
<input name="in" type="vector3" nodename="tiledimage2" />
<input name="scale" type="vector2" value="1.1, 1.1" />
<input name="space" type="string" value="tangent" />
</normalmap>
<tiledimage name="tiledimage2" type="vector3">
<input name="file" type="filename" value="resources/images/mesh_wire_norm.png" />
<input name="realworldimagesize" type="vector2" value="1.0, 2.0" unit="centimeter" unittype="distance" />
<input name="realworldtilesize" type="vector2" value="3.0, 2.0" unit="centimeter" unittype="distance" />
<input name="uvtiling" type="vector2" value="12, 10" />
</tiledimage>
<output name="normal" type="vector3" nodename="normalmap_vector2" />
</nodegraph>
</materialx>
23 changes: 20 additions & 3 deletions source/MaterialXGenMdl/mdl/materialx/stdlib.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -2060,13 +2060,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 @@ -2078,7 +2078,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 @@ -2087,6 +2087,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 96276bb

Please sign in to comment.