Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vector2 variant of normalmap #1355

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions libraries/stdlib/genglsl/mx_normalmap.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,20 @@ void mx_normalmap(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T,
// Normalize the result.
result = normalize(value);
}

void mx_normalmap_vector2(vec3 value, int map_space, vec2 normal_scale, vec3 N, vec3 T, out vec3 result)
jstone-lucasfilm marked this conversation as resolved.
Show resolved Hide resolved
{
// Decode the normal map.
value = (value == vec3(0.0f)) ? vec3(0.0, 0.0, 1.0) : value * 2.0 - 1.0;

// Transform from tangent space if needed.
if (map_space == 0)
{
vec3 B = normalize(cross(N, T));
value.xy *= normal_scale;
value = T * value.x + B * value.y + N * value.z;
}

// Normalize the result.
result = normalize(value);
}
2 changes: 2 additions & 0 deletions libraries/stdlib/genglsl/stdlib_genglsl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

<!-- <normalmap> -->
<implementation name="IM_normalmap_genglsl" nodedef="ND_normalmap" file="mx_normalmap.glsl" function="mx_normalmap" 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
2 changes: 1 addition & 1 deletion libraries/stdlib/genmdl/stdlib_genmdl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

<!-- <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_genmdl_vector2" 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
17 changes: 17 additions & 0 deletions libraries/stdlib/genmsl/mx_normalmap.metal
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,20 @@ void mx_normalmap(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T,
// Normalize the result.
result = normalize(value);
}

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;

// Transform from tangent space if needed.
if (map_space == 0)
{
vec3 B = normalize(cross(N, T));
value.xy *= normal_scale;
value = T * value.x + B * value.y + N * value.z;
}

// Normalize the result.
result = normalize(value);
}
1 change: 1 addition & 0 deletions libraries/stdlib/genmsl/stdlib_genmsl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

<!-- <normalmap> -->
<implementation name="IM_normalmap_genmsl" nodedef="ND_normalmap" file="mx_normalmap.metal" function="mx_normalmap" 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
18 changes: 18 additions & 0 deletions libraries/stdlib/genosl/mx_normalmap.osl
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ void mx_normalmap(vector value, string map_space, float normal_scale, vector N,
result = normalize(n);
}
}

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[0] + B * v[1] * normal_scale[1] + N * v[2]);
}
// Object space
else
{
vector n = value * 2.0 - 1.0;
result = normalize(n);
}
}
2 changes: 1 addition & 1 deletion libraries/stdlib/genosl/stdlib_genosl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

<!-- <normalmap> -->
<implementation name="IM_normalmap_genosl" nodedef="ND_normalmap" file="mx_normalmap.osl" function="mx_normalmap" 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 @@ -2287,6 +2287,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>
27 changes: 27 additions & 0 deletions source/MaterialXGenMdl/mdl/materialx/stdlib.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,33 @@ 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}.")
]],
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)))
)
[[
anno::description("Node Group: math")
]]
{
if (mxp_space == "tangent")
{
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.x + binormal * v.y * mxp_scale.y + mxp_normal * v.z);
}
else
{
float3 n = mxp_in * 2.0 - 1.0;
return ::math::normalize(n);
}
}

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