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 imagearray node #2132

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions documents/Specification/MaterialX.Specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,17 @@ The type of the <image> node determines the number of channels output, which
* `vaddressmode` (uniform string): determines how V coordinates outside the 0-1 range are processed before sampling the image; see below. Default is "periodic".
* `filtertype` (uniform string): the type of texture filtering to use; standard values include "closest" (nearest-neighbor single-sample), "linear", and "cubic". If not specified, an application may use its own default texture filtering method.

<a id="node-imagearray"> </a>

* **`imagearray`**: samples data from an array of images, the image to be used is selected by index in to the array. The selected image is mapped on the geometry using the same process as described above for <image>.
* `files` (uniform filenamearray): an array of URI of image files.
* `index` (integer): the index of the image to use from the array of images provided. If the value for `index` is outside the range of the provided image filenames, then the behavior is undefined.
* `default` (float or color<em>N</em> or vector<em>N</em>): a default value to use if the indexed filename from `files` can not be resolved (e.g. if the resolved file URI cannot be read). The `default` value must be the same type as the `<imagearray>` element itself. If `default` is not defined, the default color value will be 0.0 in all channels.
* `texcoord` (vector2): the name of a vector2-type node specifying the 2D texture coordinate at which the image data is read. Default is to use the current u,v coordinate.
* `uaddressmode` (uniform string): determines how U coordinates outside the 0-1 range are processed before sampling the image; see below. Default is "periodic".
* `vaddressmode` (uniform string): determines how V coordinates outside the 0-1 range are processed before sampling the image; see below. Default is "periodic".
* `filtertype` (uniform string): the type of texture filtering to use; standard values include "closest" (nearest-neighbor single-sample), "linear", and "cubic". If not specified, an application may use its own default texture filtering method.

<a id="node-tiledimage"> </a>

* **`tiledimage`** (NG): samples data from a single image, with provisions for tiling and offsetting the image across uv space.
Expand Down
7 changes: 7 additions & 0 deletions libraries/stdlib/genglsl/mx_imagearray_color3.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "lib/$fileTransformUv"

void mx_imagearray_color3(sampler2DArray tex_sampler, int index, vec3 defaultval, vec2 texcoord, int uaddressmode, int vaddressmode, int filtertype, vec2 uv_scale, vec2 uv_offset, out vec3 result)
{
vec2 uv = mx_transform_uv(texcoord, uv_scale, uv_offset);
result = texture(tex_sampler, uv, index).rgb;
}
7 changes: 7 additions & 0 deletions libraries/stdlib/genglsl/mx_imagearray_color4.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "lib/$fileTransformUv"

void mx_imagearray_color4(sampler2DArray tex_sampler, int index, vec4 defaultval, vec2 texcoord, int uaddressmode, int vaddressmode, int filtertype, vec2 uv_scale, vec2 uv_offset, out vec4 result)
{
vec2 uv = mx_transform_uv(texcoord, uv_scale, uv_offset);
result = texture(tex_sampler, uv, index);
}
7 changes: 7 additions & 0 deletions libraries/stdlib/genglsl/mx_imagearray_float.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "lib/$fileTransformUv"

void mx_imagearray_float(sampler2DArray tex_sampler, int index, float defaultval, vec2 texcoord, int uaddressmode, int vaddressmode, int filtertype, vec2 uv_scale, vec2 uv_offset, out float result)
{
vec2 uv = mx_transform_uv(texcoord, uv_scale, uv_offset);
result = texture(tex_sampler, uv, index).r;
}
7 changes: 7 additions & 0 deletions libraries/stdlib/genglsl/mx_imagearray_vector2.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "lib/$fileTransformUv"

void mx_imagearray_vector2(sampler2DArray tex_sampler, int index, vec2 defaultval, vec2 texcoord, int uaddressmode, int vaddressmode, int filtertype, vec2 uv_scale, vec2 uv_offset, out vec2 result)
{
vec2 uv = mx_transform_uv(texcoord, uv_scale, uv_offset);
result = texture(tex_sampler, uv, index).rg;
}
7 changes: 7 additions & 0 deletions libraries/stdlib/genglsl/mx_imagearray_vector3.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "lib/$fileTransformUv"

void mx_imagearray_vector3(sampler2DArray tex_sampler, int index, vec3 defaultval, vec2 texcoord, int uaddressmode, int vaddressmode, int filtertype, vec2 uv_scale, vec2 uv_offset, out vec3 result)
{
vec2 uv = mx_transform_uv(texcoord, uv_scale, uv_offset);
result = texture(tex_sampler, uv, index).rgb;
}
7 changes: 7 additions & 0 deletions libraries/stdlib/genglsl/mx_imagearray_vector4.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "lib/$fileTransformUv"

void mx_imagearray_vector4(sampler2DArray tex_sampler, int index, vec4 defaultval, vec2 texcoord, int uaddressmode, int vaddressmode, int filtertype, vec2 uv_scale, vec2 uv_offset, out vec4 result)
{
vec2 uv = mx_transform_uv(texcoord, uv_scale, uv_offset);
result = texture(tex_sampler, uv, index);
}
22 changes: 22 additions & 0 deletions libraries/stdlib/genglsl/stdlib_genglsl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@
<input name="default" type="vector4" implname="default_value" />
</implementation>

<!-- <imagearray> -->
<implementation name="IM_imagearray_float_genglsl" nodedef="ND_imagearray_float" file="mx_imagearray_float.glsl" function="mx_imagearray_float" target="genglsl">
<input name="default" type="float" implname="default_value" />
</implementation>
<implementation name="IM_imagearray_color3_genglsl" nodedef="ND_imagearray_color3" file="mx_imagearray_color3.glsl" function="mx_imagearray_color3" target="genglsl">
<input name="default" type="color3" implname="default_value" />
</implementation>
<implementation name="IM_imagearray_color4_genglsl" nodedef="ND_imagearray_color4" file="mx_imagearray_color4.glsl" function="mx_imagearray_color4" target="genglsl">
<input name="default" type="color4" implname="default_value" />
</implementation>
<implementation name="IM_imagearray_vector2_genglsl" nodedef="ND_imagearray_vector2" file="mx_imagearray_vector2.glsl" function="mx_imagearray_vector2" target="genglsl">
<input name="default" type="vector2" implname="default_value" />
</implementation>
<implementation name="IM_imagearray_vector3_genglsl" nodedef="ND_imagearray_vector3" file="mx_imagearray_vector3.glsl" function="mx_imagearray_vector3" target="genglsl">
<input name="default" type="vector3" implname="default_value" />
</implementation>
<implementation name="IM_imagearray_vector4_genglsl" nodedef="ND_imagearray_vector4" file="mx_imagearray_vector4.glsl" function="mx_imagearray_vector4" target="genglsl">
<input name="default" type="vector4" implname="default_value" />
</implementation>

<implementation name="IM_elementat_filenamearray_genglsl" nodedef="ND_elementat_filenamearray" target="genglsl" sourcecode="{{in}}[{{index}}]" />

<!-- <normalmap> -->
<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" />
Expand Down
2 changes: 2 additions & 0 deletions libraries/stdlib/genmdl/stdlib_genmdl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
<input name="default" type="vector4" implname="default_value" />
</implementation>

<implementation name="IM_elementat_filenamearray_genmdl" nodedef="ND_elementat_filenamearray" target="genmdl" sourcecode="{{in}}[{{index}}]" />

<!-- <triplanarprojection> -->

<!-- <normalmap> -->
Expand Down
32 changes: 32 additions & 0 deletions libraries/stdlib/genmsl/lib/mx_texture_array.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
struct MetalTextureArray
{
texture2d_array<float> texArray;
sampler s;
};

float4 texture(MetalTextureArray mtlTex, float2 uv, int index)
{
float4 ret = float4(0, 0, 1, 0);

if (index >= -0.5 && !is_null_texture(mtlTex.texArray)) {
ret = vec4(mtlTex.texArray.sample(mtlTex.s, uv, index));
}

return ret;
}

float4 textureLod(MetalTextureArray mtlTex, float2 uv, int index, float lod)
{
float4 ret = float4(0, 0, 0, 0);

if (index >= -0.5 && !is_null_texture(mtlTex.texArray)) {
ret = vec4(mtlTex.texArray.sample(mtlTex.s, uv, index, level(lod)));
}

return ret;
}

int2 textureSize(MetalTextureArray mtlTex, int mipLevel)
{
return int2(mtlTex.texArray.get_width(), mtlTex.texArray.get_height());
}
22 changes: 22 additions & 0 deletions libraries/stdlib/genmsl/stdlib_genmsl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@
<input name="default" type="vector4" implname="default_value" />
</implementation>

<!-- <imagearray> -->
<implementation name="IM_imagearray_float_genmsl" nodedef="ND_imagearray_float" file="../genglsl/mx_imagearray_float.glsl" function="mx_imagearray_float" target="genmsl">
<input name="default" type="float" implname="default_value" />
</implementation>
<implementation name="IM_imagearray_color3_genmsl" nodedef="ND_imagearray_color3" file="../genglsl/mx_imagearray_color3.glsl" function="mx_imagearray_color3" target="genmsl">
<input name="default" type="color3" implname="default_value" />
</implementation>
<implementation name="IM_imagearray_color4_genmsl" nodedef="ND_imagearray_color4" file="../genglsl/mx_imagearray_color4.glsl" function="mx_imagearray_color4" target="genmsl">
<input name="default" type="color4" implname="default_value" />
</implementation>
<implementation name="IM_imagearray_vector2_genmsl" nodedef="ND_imagearray_vector2" file="../genglsl/mx_imagearray_vector2.glsl" function="mx_imagearray_vector2" target="genmsl">
<input name="default" type="vector2" implname="default_value" />
</implementation>
<implementation name="IM_imagearray_vector3_genmsl" nodedef="ND_imagearray_vector3" file="../genglsl/mx_imagearray_vector3.glsl" function="mx_imagearray_vector3" target="genmsl">
<input name="default" type="vector3" implname="default_value" />
</implementation>
<implementation name="IM_imagearray_vector4_genmsl" nodedef="ND_imagearray_vector4" file="../genglsl/mx_imagearray_vector4.glsl" function="mx_imagearray_vector4" target="genmsl">
<input name="default" type="vector4" implname="default_value" />
</implementation>

<implementation name="IM_elementat_filenamearray_genmsl" nodedef="ND_elementat_filenamearray" target="genmsl" sourcecode="{{in}}[{{index}}]" />

<!-- ======================================================================== -->
<!-- Procedural nodes -->
<!-- ======================================================================== -->
Expand Down
8 changes: 8 additions & 0 deletions libraries/stdlib/genosl/mx_elementat_filenamearray.osl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
void mx_elementat_filenamearray(textureresources in, int index, output textureresource out)
{
// we currently need the intermediate variable to use the list initializer syntax
// this was reported to OSL project in issue #1906 - if that gets fixed we may be able
// to revert to direct assignment to the output variable.
textureresource tmp = { in.filename[index], in.colorspace };
out = tmp;
}
2 changes: 2 additions & 0 deletions libraries/stdlib/genosl/stdlib_genosl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
<input name="default" type="vector4" implname="default_value" />
</implementation>

<implementation name="IM_elementat_filenamearray_genosl" nodedef="ND_elementat_filenamearray" file="mx_elementat_filenamearray.osl" function="mx_elementat_filenamearray" target="genosl"/>

<!-- <triplanarprojection> -->

<!-- <normalmap> -->
Expand Down
72 changes: 72 additions & 0 deletions libraries/stdlib/stdlib_defs.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<typedef name="vector3array" />
<typedef name="vector4array" />
<typedef name="stringarray" />
<typedef name="filenamearray" />
<typedef name="geomnamearray" />

<!-- ======================================================================== -->
Expand Down Expand Up @@ -202,6 +203,77 @@
<output name="out" type="vector4" default="0.0, 0.0, 0.0, 0.0" />
</nodedef>

<!--
Node: <imagearray>
Samples data from a single image, or from a layer within a multi-layer image.
-->
<nodedef name="ND_imagearray_float" node="imagearray" nodegroup="texture2d">
<input name="files" type="filenamearray" value="" uiname="Filenames" uniform="true" />
<input name="index" type="integer" value="0"/>
<input name="default" type="float" value="0.0" uiname="Default Value" />
<input name="texcoord" type="vector2" defaultgeomprop="UV0" uiname="Texture Coordinates" />
<input name="uaddressmode" type="string" value="periodic" enum="constant,clamp,periodic,mirror" uiname="Address Mode U" uniform="true" />
<input name="vaddressmode" type="string" value="periodic" enum="constant,clamp,periodic,mirror" uiname="Address Mode V" uniform="true" />
<input name="filtertype" type="string" value="linear" enum="closest,linear,cubic" uiname="Filter Type" uniform="true" />
<output name="out" type="float" default="0.0" />
</nodedef>
<nodedef name="ND_imagearray_color3" node="imagearray" nodegroup="texture2d">
<input name="files" type="filenamearray" value="" uiname="Filenames" uniform="true" />
<input name="index" type="integer" value="0"/>
<input name="default" type="color3" value="0.0, 0.0, 0.0" uiname="Default Value" />
<input name="texcoord" type="vector2" defaultgeomprop="UV0" uiname="Texture Coordinates" />
<input name="uaddressmode" type="string" value="periodic" enum="constant,clamp,periodic,mirror" uiname="Address Mode U" uniform="true" />
<input name="vaddressmode" type="string" value="periodic" enum="constant,clamp,periodic,mirror" uiname="Address Mode V" uniform="true" />
<input name="filtertype" type="string" value="linear" enum="closest,linear,cubic" uiname="Filter Type" uniform="true" />
<output name="out" type="color3" default="0.0, 0.0, 0.0" />
</nodedef>
<nodedef name="ND_imagearray_color4" node="imagearray" nodegroup="texture2d">
<input name="files" type="filenamearray" value="" uiname="Filenames" uniform="true" />
<input name="index" type="integer" value="0"/>
<input name="default" type="color4" value="0.0, 0.0, 0.0, 0.0" uiname="Default Value" />
<input name="texcoord" type="vector2" defaultgeomprop="UV0" uiname="Texture Coordinates" />
<input name="uaddressmode" type="string" value="periodic" enum="constant,clamp,periodic,mirror" uiname="Address Mode U" uniform="true" />
<input name="vaddressmode" type="string" value="periodic" enum="constant,clamp,periodic,mirror" uiname="Address Mode V" uniform="true" />
<input name="filtertype" type="string" value="linear" enum="closest,linear,cubic" uiname="Filter Type" uniform="true" />
<output name="out" type="color4" default="0.0, 0.0, 0.0, 0.0" />
</nodedef>
<nodedef name="ND_imagearray_vector2" node="imagearray" nodegroup="texture2d">
<input name="files" type="filenamearray" value="" uiname="Filenames" uniform="true" />
<input name="index" type="integer" value="0"/>
<input name="default" type="vector2" value="0.0, 0.0" uiname="Default Value" />
<input name="texcoord" type="vector2" defaultgeomprop="UV0" uiname="Texture Coordinates" />
<input name="uaddressmode" type="string" value="periodic" enum="constant,clamp,periodic,mirror" uiname="Address Mode U" uniform="true" />
<input name="vaddressmode" type="string" value="periodic" enum="constant,clamp,periodic,mirror" uiname="Address Mode V" uniform="true" />
<input name="filtertype" type="string" value="linear" enum="closest,linear,cubic" uiname="Filter Type" uniform="true" />
<output name="out" type="vector2" default="0.0, 0.0" />
</nodedef>
<nodedef name="ND_imagearray_vector3" node="imagearray" nodegroup="texture2d">
<input name="files" type="filenamearray" value="" uiname="Filenames" uniform="true" />
<input name="index" type="integer" value="0"/>
<input name="default" type="vector3" value="0.0, 0.0, 0.0" uiname="Default Value" />
<input name="texcoord" type="vector2" defaultgeomprop="UV0" uiname="Texture Coordinates" />
<input name="uaddressmode" type="string" value="periodic" enum="constant,clamp,periodic,mirror" uiname="Address Mode U" uniform="true" />
<input name="vaddressmode" type="string" value="periodic" enum="constant,clamp,periodic,mirror" uiname="Address Mode V" uniform="true" />
<input name="filtertype" type="string" value="linear" enum="closest,linear,cubic" uiname="Filter Type" uniform="true" />
<output name="out" type="vector3" default="0.0, 0.0, 0.0" />
</nodedef>
<nodedef name="ND_imagearray_vector4" node="imagearray" nodegroup="texture2d">
<input name="files" type="filenamearray" value="" uiname="Filenames" uniform="true" />
<input name="index" type="integer" value="0"/>
<input name="default" type="vector4" value="0.0, 0.0, 0.0, 0.0" uiname="Default Value" />
<input name="texcoord" type="vector2" defaultgeomprop="UV0" uiname="Texture Coordinates" />
<input name="uaddressmode" type="string" value="periodic" enum="constant,clamp,periodic,mirror" uiname="Address Mode U" uniform="true" />
<input name="vaddressmode" type="string" value="periodic" enum="constant,clamp,periodic,mirror" uiname="Address Mode V" uniform="true" />
<input name="filtertype" type="string" value="linear" enum="closest,linear,cubic" uiname="Filter Type" uniform="true" />
<output name="out" type="vector4" default="0.0, 0.0, 0.0, 0.0" />
</nodedef>

<nodedef name="ND_elementat_filenamearray" node="elementat" nodegroup="procedural">
<input name="in" type="filenamearray" value="" uniform="true"/>
<input name="index" type="integer" value="0"/>
<output name="out" type="filename" default=""/>
</nodedef>

<!--
Node: <tiledimage> Supplemental Node
Samples data from a single image, with provisions for tiling and offsetting the image
Expand Down
Loading
Loading