Skip to content

Commit

Permalink
Add creatematrix nodes to build matrices from vectors (AcademySoftwar…
Browse files Browse the repository at this point in the history
…eFoundation#1553)

I'm opening this PR to add creatematrix, a constructor for Matrix33 from 3 Vector3s, Matrix44 from 4 Vector3s and Matrix44 from 4 Vector4s from the specification document (https://github.com/AcademySoftwareFoundation/MaterialX/blob/main/documents/Specification/MaterialX.Specification.md#math-nodes).
  • Loading branch information
friedererdmann authored Jan 4, 2024
1 parent 8ab122f commit 5def239
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
void mx_creatematrix_vector3_matrix33(vec3 in1, vec3 in2, vec3 in3, out mat3 result)
{
result = mat3(in1.x, in1.y, in1.z,
in2.x, in2.y, in2.z,
in3.x, in3.y, in3.z);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
void mx_creatematrix_vector3_matrix44(vec3 in1, vec3 in2, vec3 in3, vec3 in4, out mat4 result)
{
result = mat4(in1.x, in1.y, in1.z, 0.0,
in2.x, in2.y, in2.z, 0.0,
in3.x, in3.y, in3.z, 0.0,
in4.x, in4.y, in4.z, 1.0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
void mx_creatematrix_vector4_matrix44(vec4 in1, vec4 in2, vec4 in3, vec4 in4, out mat4 result)
{
result = mat4(in1.x, in1.y, in1.z, in1.w,
in2.x, in2.y, in2.z, in2.w,
in3.x, in3.y, in3.z, in3.w,
in4.x, in4.y, in4.z, in4.w);
}
5 changes: 5 additions & 0 deletions libraries/stdlib/genglsl/stdlib_genglsl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,11 @@
<implementation name="IM_combine4_color4_genglsl" nodedef="ND_combine4_color4" target="genglsl" />
<implementation name="IM_combine4_vector4_genglsl" nodedef="ND_combine4_vector4" target="genglsl" />

<!-- <creatematrix> -->
<implementation name="IM_creatematrix_vector3_matrix33_genglsl" nodedef="ND_creatematrix_vector3_matrix33" file="mx_creatematrix_vector3_matrix33.glsl" function="mx_creatematrix_vector3_matrix33" target="genglsl" />
<implementation name="IM_creatematrix_vector3_matrix44_genglsl" nodedef="ND_creatematrix_vector3_matrix44" file="mx_creatematrix_vector3_matrix44.glsl" function="mx_creatematrix_vector3_matrix44" target="genglsl" />
<implementation name="IM_creatematrix_vector4_matrix44_genglsl" nodedef="ND_creatematrix_vector4_matrix44" file="mx_creatematrix_vector4_matrix44.glsl" function="mx_creatematrix_vector4_matrix44" target="genglsl" />

<!-- ======================================================================== -->
<!-- Convolution nodes -->
<!-- ======================================================================== -->
Expand Down
5 changes: 5 additions & 0 deletions libraries/stdlib/genmdl/stdlib_genmdl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,11 @@
<implementation name="IM_combine4_color4_genmdl" nodedef="ND_combine4_color4" target="genmdl" />
<implementation name="IM_combine4_vector4_genmdl" nodedef="ND_combine4_vector4" target="genmdl" />

<!-- <creatematrix> -->
<implementation name="IM_creatematrix_vector3_matrix33_genmdl" nodedef="ND_creatematrix_vector3_matrix33" sourcecode="mx::stdlib::mx_creatematrix_vector3_matrix33({{in1}}, {{in2}}, {{in3}})" target="genmdl" />
<implementation name="IM_creatematrix_vector3_matrix44_genmdl" nodedef="ND_creatematrix_vector3_matrix44" sourcecode="mx::stdlib::mx_creatematrix_vector3_matrix44({{in1}}, {{in2}}, {{in3}}, {{in4}})" target="genmdl" />
<implementation name="IM_creatematrix_vector4_matrix44_genmdl" nodedef="ND_creatematrix_vector4_matrix44" sourcecode="mx::stdlib::mx_creatematrix_vector4_matrix44({{in1}}, {{in2}}, {{in3}}, {{in4}})" target="genmdl" />

<!-- ======================================================================== -->
<!-- Convolution nodes -->
<!-- ======================================================================== -->
Expand Down
5 changes: 5 additions & 0 deletions libraries/stdlib/genmsl/stdlib_genmsl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,11 @@
<implementation name="IM_combine4_color4_genmsl" nodedef="ND_combine4_color4" target="genmsl" />
<implementation name="IM_combine4_vector4_genmsl" nodedef="ND_combine4_vector4" target="genmsl" />

<!-- <creatematrix> -->
<implementation name="IM_creatematrix_vector3_matrix33_genmsl" nodedef="ND_creatematrix_vector3_matrix33" file="../genglsl/mx_creatematrix_vector3_matrix33.glsl" function="mx_creatematrix_vector3_matrix33" nodegroup="genmsl" />
<implementation name="IM_creatematrix_vector3_matrix44_genmsl" nodedef="ND_creatematrix_vector3_matrix44" file="../genglsl/mx_creatematrix_vector3_matrix44.glsl" function="mx_creatematrix_vector3_matrix44" nodegroup="genmsl" />
<implementation name="IM_creatematrix_vector4_matrix44_genmsl" nodedef="ND_creatematrix_vector4_matrix44" file="../genglsl/mx_creatematrix_vector4_matrix44.glsl" function="mx_creatematrix_vector4_matrix44" nodegroup="genmsl" />

<!-- ======================================================================== -->
<!-- Convolution nodes -->
<!-- ======================================================================== -->
Expand Down
23 changes: 23 additions & 0 deletions libraries/stdlib/genosl/mx_creatematrix.osl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
void mx_creatematrix_vector3_matrix33(vector in1, vector in2, vector in3, out matrix result)
{
result = matrix(in1.x, in1.y, in1.z, 0.0,
in2.x, in2.y, in2.z, 0.0,
in3.x, in3.y, in3.z, 0.0,
0.0, 0.0, 0.0, 1.0);
}

void mx_creatematrix_vector3_matrix44(vector3 in1, vector3 in2, vector3 in3, vector3 in4, out matrix result)
{
result = matrix(in1.x, in1.y, in1.z, 0.0,
in2.x, in2.y, in2.z, 0.0,
in3.x, in3.y, in3.z, 0.0,
in4.x, in4.y, in4.z, 1.0);
}

void mx_creatematrix_vector4_matrix44(vector4 in1, vector4 in2, vector4 in3, vector4 in4, out matrix result)
{
result = matrix(in1.x, in1.y, in1.z, in1.w,
in2.x, in2.y, in2.z, in2.w,
in3.x, in3.y, in3.z, in3.w,
in4.x, in4.y, in4.z, in4.w);
}
5 changes: 5 additions & 0 deletions libraries/stdlib/genosl/stdlib_genosl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,11 @@
<implementation name="IM_combine4_color4_genosl" nodedef="ND_combine4_color4" target="genosl" />
<implementation name="IM_combine4_vector4_genosl" nodedef="ND_combine4_vector4" target="genosl" />

<!-- <creatematrix> -->
<implementation name="IM_creatematrix_vector3_matrix33_genosl" nodedef="ND_creatematrix_vector3_matrix33" file="mx_creatematrix.osl" function="mx_creatematrix_vector3_matrix33" nodegroup="genosl" />
<implementation name="IM_creatematrix_vector3_matrix44_genosl" nodedef="ND_creatematrix_vector3_matrix44" file="mx_creatematrix.osl" function="mx_creatematrix_vector3_matrix44" nodegroup="genosl" />
<implementation name="IM_creatematrix_vector4_matrix44_genosl" nodedef="ND_creatematrix_vector4_matrix44" file="mx_creatematrix.osl" function="mx_creatematrix_vector4_matrix44" nodegroup="genosl" />

<!-- ======================================================================== -->
<!-- Convolution nodes -->
<!-- ======================================================================== -->
Expand Down
27 changes: 27 additions & 0 deletions libraries/stdlib/stdlib_defs.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -4359,6 +4359,33 @@
<output name="out" type="vector4" default="0.0, 0.0, 0.0, 0.0" />
</nodedef>

<!--
Node <creatematrix>
Combine the the three vectors3 from stream into a matrix 33.
-->
<nodedef name="ND_creatematrix_vector3_matrix33" node="creatematrix" nodegroup="math">
<input name="in1" type="vector3" value="1.0, 0.0, 0.0" />
<input name="in2" type="vector3" value="0.0, 1.0, 0.0" />
<input name="in3" type="vector3" value="0.0, 0.0, 1.0" />
<output name="out" type="matrix33" default="1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0" />
</nodedef>

<nodedef name="ND_creatematrix_vector3_matrix44" node="creatematrix" nodegroup="math">
<input name="in1" type="vector3" value="1.0, 0.0, 0.0" />
<input name="in2" type="vector3" value="0.0, 1.0, 0.0" />
<input name="in3" type="vector3" value="0.0, 0.0, 1.0" />
<input name="in4" type="vector3" value="0.0, 0.0, 0.0" />
<output name="out" type="matrix44" default="1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0" />
</nodedef>

<nodedef name="ND_creatematrix_vector4_matrix44" node="creatematrix" nodegroup="math">
<input name="in1" type="vector4" value="1.0, 0.0, 0.0, 0.0" />
<input name="in2" type="vector4" value="0.0, 1.0, 0.0, 0.0" />
<input name="in3" type="vector4" value="0.0, 0.0, 1.0, 0.0" />
<input name="in4" type="vector4" value="0.0, 0.0, 0.0, 1.0" />
<output name="out" type="matrix44" default="1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0" />
</nodedef>

<!--
Node: <extract> Supplemental Node
Extract a single channel from a colorN or vectorN stream, outputting a float.
Expand Down
29 changes: 29 additions & 0 deletions resources/Materials/TestSuite/stdlib/math/matrix.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<materialx version="1.38">
<nodegraph name="creatematrix_vector3_matrix33">
<creatematrix name="creatematrix_vector3_matrix33" type="matrix33">
<input name="in1" type="vector3" value="1, 0, 0" />
<input name="in2" type="vector3" value="0, 1, 0" />
<input name="in3" type="vector3" value="0, 0, 1" />
</creatematrix>
<output name="out" type="matrix33" nodename="creatematrix_vector3_matrix33" />
</nodegraph>
<nodegraph name="creatematrix_vector3_matrix44">
<creatematrix name="creatematrix_vector3_matrix44" type="matrix44">
<input name="in1" type="vector3" value="1, 0, 0" />
<input name="in2" type="vector3" value="0, 1, 0" />
<input name="in3" type="vector3" value="0, 0, 1" />
<input name="in4" type="vector3" value="0, 0, 0" />
</creatematrix>
<output name="out" type="matrix44" nodename="creatematrix_vector3_matrix44" />
</nodegraph>
<nodegraph name="creatematrix_vector4_matrix44">
<creatematrix name="creatematrix_vector4_matrix44" type="matrix44">
<input name="in1" type="vector4" value="1, 0, 0, 0" />
<input name="in2" type="vector4" value="0, 1, 0, 0" />
<input name="in3" type="vector4" value="0, 0, 1, 0" />
<input name="in4" type="vector4" value="0, 0, 0, 1" />
</creatematrix>
<output name="out" type="matrix44" nodename="creatematrix_vector4_matrix44" />
</nodegraph>
</materialx>
40 changes: 40 additions & 0 deletions source/MaterialXGenMdl/mdl/materialx/stdlib.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -4147,6 +4147,46 @@ export float4 mx_combine4_vector4(
return float4(mxp_in1, mxp_in2, mxp_in3, mxp_in4);
}

export float3x3 mx_creatematrix_vector3_matrix33(
float3 mxp_in1 = float3(1.0, 0.0, 0.0),
float3 mxp_in2 = float3(0.0, 1.0, 0.0),
float3 mxp_in3 = float3(0.0, 0.0, 1.0)
)
[[
anno::description("Node Group: math")
]]
{
return float3x3(mxp_in1[0],mxp_in1[1],mxp_in1[2],mxp_in2[0],mxp_in2[1],mxp_in2[2],mxp_in3[0],mxp_in3[1],mxp_in3[2]);
}


export float4x4 mx_creatematrix_vector3_matrix44(
float3 mxp_in1 = float3(1.0, 0.0, 0.0),
float3 mxp_in2 = float3(0.0, 1.0, 0.0),
float3 mxp_in3 = float3(0.0, 0.0, 1.0)
float3 mxp_in4 = float3(0.0, 0.0, 0.0)
)
[[
anno::description("Node Group: math")
]]
{
return float4x4(mxp_in1[0],mxp_in1[1],mxp_in1[2],0.0, mxp_in2[0],mxp_in2[1],mxp_in2[2],0.0, mxp_in3[0],mxp_in3[1],mxp_in3[2]0.0, mxp_in4[0],mxp_in4[1],mxp_in4[2],1.0);
}


export float4x4 mx_creatematrix_vector4_matrix44(
float4 mxp_in1 = float4(1.0, 0.0, 0.0, 0.0),
float4 mxp_in2 = float4(0.0, 1.0, 0.0, 0.0),
float4 mxp_in3 = float4(0.0, 0.0, 1.0, 0.0)
float4 mxp_in4 = float4(0.0, 0.0, 0.0, 1.0)
)
[[
anno::description("Node Group: math")
]]
{
return float4x4(mxp_in1[0],mxp_in1[1],mxp_in1[2],mxp_in1[3],mxp_in2[0],mxp_in2[1],mxp_in2[2],mxp_in2[3],mxp_in3[0],mxp_in3[1],mxp_in3[2],mxp_in3[3],mxp_in4[0],mxp_in4[1],mxp_in4[2],mxp_in4[3]);
}

// Nodedef: ND_extract_color3 is represented by a nodegraph: NG_extract_color3
// Nodedef: ND_extract_color4 is represented by a nodegraph: NG_extract_color4
// Nodedef: ND_extract_vector2 is represented by a nodegraph: NG_extract_vector2
Expand Down

0 comments on commit 5def239

Please sign in to comment.