Skip to content

Commit

Permalink
Merge (#46)
Browse files Browse the repository at this point in the history
* Add enumeration support to web viewer (AcademySoftwareFoundation#1632)

- Scan for `enum` and `enumvalues` if attributes exist on input. If no `enumvalues` exist then map to "default" of 0..<enum list size>.
- Create a drop-down for each enumerated input.

* Initial nprlib with viewdirection node (AcademySoftwareFoundation#1631)

This changelist introduces an NPR (non-photorealistic rendering) data library to MaterialX, initially consisting of a single `viewdirection` node.

The NPR data library is designed for the portable expression of artistic, non-physically-based materials, with common examples being cartoon shading, architectural diagrams, and edge highlighting.  In future versions of MaterialX, we expect this data library to be extended with additional primitives and artist-facing graphs as they are requested by the graphics community and approved by the MaterialX TSC.

For shader generation, MaterialX initially supports the NPR data library in GLSL, ESSL, MSL, and OSL, with MDL being omitted for now, based on intentional restrictions in its design philosophy.

* Improve shader generation for viewdirection

This changelist improves shader generation logic for the new viewdirection node, allowing it to work correctly in derived hardware languages such as ESSL.

* Add creatematrix nodes to build matrices from vectors (AcademySoftwareFoundation#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).

---------

Co-authored-by: Jonathan Stone <[email protected]>
Co-authored-by: Frieder Erdmann <[email protected]>
  • Loading branch information
3 people authored Jan 4, 2024
1 parent ec8482c commit 6ef3b95
Show file tree
Hide file tree
Showing 29 changed files with 528 additions and 72 deletions.
34 changes: 31 additions & 3 deletions javascript/MaterialXView/source/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -949,11 +949,17 @@ export class Material
}
var step = 0;
var enumList = []
var enumValues = []
if (nodeDefInput)
{
if (nodeDefInput.hasAttribute('enum'))
{
// Get enum and enum values attributes (if present)
enumList = nodeDefInput.getAttribute('enum').split(',');
if (nodeDefInput.hasAttribute('enumvalues'))
{
enumValues = nodeDefInput.getAttribute('enumvalues').split(',').map(Number);
}
}
else
{
Expand Down Expand Up @@ -989,8 +995,30 @@ export class Material
}
else
{
// TODO: Add enum support
currentFolder.add(material.uniforms[name], 'value' ).name(path);
// Map enumList strings to values
// Map to 0..N if no values are specified via enumvalues attribute
if (enumValues.length == 0)
{
for (let i = 0; i < enumList.length; ++i)
{
enumValues.push(i);
}
}
const enumeration = {};
enumList.forEach((str, index) => {
enumeration[str] = enumValues[index];
});

// Function to handle enum drop-down
function handleDropdownChange(value) {
if (material.uniforms[name])
{
material.uniforms[name].value = value;
}
}
const defaultOption = enumList[value]; // Set the default selected option
const dropdownController = gui.add(enumeration, defaultOption, enumeration).name(path);
dropdownController.onChange(handleDropdownChange);
}
}
break;
Expand Down Expand Up @@ -1046,7 +1074,7 @@ export class Material
break;

case 'color3':
// Irksome way to mape arrays to colors and back
// Irksome way to map arrays to colors and back
uniformToUpdate = material.uniforms[name];
if (uniformToUpdate && value != null)
{
Expand Down
17 changes: 17 additions & 0 deletions libraries/nprlib/genglsl/nprlib_genglsl_impl.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<materialx version="1.38">
<!--
Copyright Contributors to the MaterialX Project
SPDX-License-Identifier: Apache-2.0
Declarations for GLSL implementations of standard nodes included in the MaterialX specification.
-->

<!-- ======================================================================== -->
<!-- View-dependent nodes -->
<!-- ======================================================================== -->

<!-- <viewdirection> -->
<implementation name="IM_viewdirection_vector3_genglsl" nodedef="ND_viewdirection_vector3" target="genglsl"/>

</materialx>
17 changes: 17 additions & 0 deletions libraries/nprlib/genmdl/nprlib_genmdl_impl.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<materialx version="1.38">
<!--
Copyright Contributors to the MaterialX Project
SPDX-License-Identifier: Apache-2.0
Declarations for MDL implementations of standard nodes included in the MaterialX specification.
-->

<!-- ======================================================================== -->
<!-- View-dependent nodes -->
<!-- ======================================================================== -->

<!-- <viewdirection> -->
<implementation name="IM_viewdirection_vector3_genmdl" nodedef="ND_viewdirection_vector3" sourcecode="mx::stdlib::mx_viewdirection_vector3()" target="genmdl"/>

</materialx>
17 changes: 17 additions & 0 deletions libraries/nprlib/genmsl/nprlib_genmsl_impl.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<materialx version="1.38">
<!--
Copyright Contributors to the MaterialX Project
SPDX-License-Identifier: Apache-2.0
Declarations for MSL implementations of standard nodes included in the MaterialX specification.
-->

<!-- ======================================================================== -->
<!-- View-dependent nodes -->
<!-- ======================================================================== -->

<!-- <viewdirection> -->
<implementation name="IM_viewdirection_vector3_genmsl" nodedef="ND_viewdirection_vector3" target="genmsl"/>

</materialx>
17 changes: 17 additions & 0 deletions libraries/nprlib/genosl/nprlib_genosl_impl.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<materialx version="1.38">
<!--
Copyright Contributors to the MaterialX Project
SPDX-License-Identifier: Apache-2.0
Declarations for OSL implementations of standard nodes included in the MaterialX specification.
-->

<!-- ======================================================================== -->
<!-- View-dependent nodes -->
<!-- ======================================================================== -->

<!-- <viewdirection> -->
<implementation name="IM_viewdirection_vector3_genosl" nodedef="ND_viewdirection_vector3" sourcecode="transform({{space}}, I)" target="genosl" />

</materialx>
23 changes: 23 additions & 0 deletions libraries/nprlib/nprlib_defs.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<materialx version="1.38">
<!--
Copyright Contributors to the MaterialX Project
SPDX-License-Identifier: Apache-2.0
Declarations of standard data types and nodes included in the MaterialX specification.
-->

<!-- ======================================================================== -->
<!-- View-dependent nodes -->
<!-- ======================================================================== -->

<!--
Node: <viewdirection>
The current scene view direction, as defined by the shading environment.
-->
<nodedef name="ND_viewdirection_vector3" node="viewdirection" nodegroup="npr">
<input name="space" type="string" value="world" enum="model,object,world" uniform="true" />
<output name="out" type="vector3" default="0.0, 0.0, 1.0" />
</nodedef>

</materialx>
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
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
<?xml version="1.0"?>
<materialx version="1.38">
<materialx version="1.38" colorspace="lin_rec709">

<!-- Custom node: <viewdir> -->
<!-- Required by environment shader graph below -->
<nodedef name="ND_viewdir_vector3" node="viewdir">
<output name="out" type="vector3" default="0.0, 0.0, 1.0" />
</nodedef>
<implementation name="IM_viewdir_vector3_genglsl" nodedef="ND_viewdir_vector3" target="genglsl" />
<implementation name="IM_viewdir_vector3_genmsl" nodedef="ND_viewdir_vector3" target="genmsl" />
<nodegraph name="environmentDraw">
<nodegraph name="envMap">

<!-- Get view direction -->
<viewdir name="viewDir" type="vector3" />
<viewdirection name="viewDir" type="vector3" />

<!-- Compute longitude coordinate -->
<atan2 name="angleXZ" type="float">
Expand Down Expand Up @@ -46,11 +39,11 @@
<input name="in2" type="float" nodename="latitude" />
</combine2>
<image name="envImage" type="color3">
<input name="file" type="filename" value="resources/Lights/san_giuseppe_bridge.hdr" uniform="true" />
<input name="file" type="filename" value="resources/Lights/san_giuseppe_bridge.hdr" />
<input name="texcoord" type="vector2" nodename="mapUvs" />
<input name="uaddressmode" type="string" value="periodic" uniform="true" />
<input name="vaddressmode" type="string" value="clamp" uniform="true" />
<input name="filtertype" type="string" value="linear" uniform="true" />
<input name="uaddressmode" type="string" value="periodic" />
<input name="vaddressmode" type="string" value="clamp" />
<input name="filtertype" type="string" value="linear" />
</image>

<!-- Return the resulting color -->
Expand Down
2 changes: 1 addition & 1 deletion resources/Materials/TestSuite/_options.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<input name="extraLibraryPaths" type="string" value="" />

<!-- List of document paths for render tests -->
<input name="renderTestPaths" type="string" value="resources/Materials/Examples/StandardSurface,resources/Materials/TestSuite/stdlib/color_management,resources/Materials/TestSuite/stdlib/convolution,resources/Materials/TestSuite/stdlib/geometric,resources/Materials/TestSuite/stdlib/noise,resources/Materials/TestSuite/pbrlib" />
<input name="renderTestPaths" type="string" value="resources/Materials/Examples/StandardSurface,resources/Materials/TestSuite/stdlib/color_management,resources/Materials/TestSuite/stdlib/convolution,resources/Materials/TestSuite/stdlib/geometric,resources/Materials/TestSuite/stdlib/noise,resources/Materials/TestSuite/pbrlib,resources/Materials/TestSuite/nprlib" />

<!-- Enable reference quality rendering. Default is false. -->
<input name="enableReferenceQuality" type="boolean" value="false" />
Expand Down
28 changes: 28 additions & 0 deletions resources/Materials/TestSuite/nprlib/edge_brighten.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<materialx version="1.38" colorspace="lin_rec709">
<nodegraph name="edge_brighten">
<viewdirection name="viewdirection_vector3" type="vector3" />
<normal name="normal_vector3" type="vector3" />
<multiply name="multiply_vector3" type="vector3">
<input name="in1" type="vector3" nodename="viewdirection_vector3" />
<input name="in2" type="float" value="-1" />
</multiply>
<dotproduct name="dotproduct_vector3" type="float">
<input name="in1" type="vector3" nodename="multiply_vector3" />
<input name="in2" type="vector3" nodename="normal_vector3" />
</dotproduct>
<clamp name="clamp_float" type="float">
<input name="in" type="float" nodename="dotproduct_vector3" />
</clamp>
<power name="power_float" type="float">
<input name="in1" type="float" nodename="clamp_float" />
<input name="in2" type="float" value="0.2" />
</power>
<mix name="mix_color3" type="color3">
<input name="fg" type="color3" value="0, 0.0986187, 0.186275" />
<input name="bg" type="color3" value="0.735294, 0.735294, 0.735294" />
<input name="mix" type="float" nodename="power_float" />
</mix>
<output name="out" type="color3" nodename="mix_color3" />
</nodegraph>
</materialx>
Loading

0 comments on commit 6ef3b95

Please sign in to comment.