Skip to content

Commit

Permalink
MaterialX 1.36 additions: rotate, transforms, rgb/hsv, extract, separ…
Browse files Browse the repository at this point in the history
…ate (#905)

* Add a variant of rotate() that just takes an axis, rotate about the origin.

* MaterialX: add rotate node (new with MaterialX 1.36)

* rotate2d note is kept for back-compatibility, but considered deprecated, since it was dropped from 1.36.

* MaterialX: implement new transformpoint, transformvector, transformnormal nodes

* MaterialX: add new nodes rgbtohsv, hsvtorgb

* MaterialX: add new 1.36 node 'hsvadjust', deprecate 'hueshift'

* MatrialX: Add new 1.36 nodes: extract, separate
  • Loading branch information
lgritz authored Jul 28, 2018
1 parent 7a56abd commit 3e3b528
Show file tree
Hide file tree
Showing 19 changed files with 423 additions and 37 deletions.
8 changes: 5 additions & 3 deletions src/doc/languagespec.tex
Original file line number Diff line number Diff line change
Expand Up @@ -3247,10 +3247,12 @@ \section{Geometric functions}
containing {\cf I} to that on the opposite side of the surface.
\apiend

\apiitem{point {\ce rotate} (point Q, float angle, point P0, point P1)}
\apiitem{point {\ce rotate} (point Q, float angle, point P0, point P1) \\
point {\ce rotate} (point Q, float angle, vector axis)}
\indexapi{rotate()}
Returns the point computed by rotating point {\cf Q} by {\cf angle}
radians about the axis that passes from point {\cf P0} to {\cf P1}.
Returns the point computed by rotating point {\cf Q} by {\cf angle}
radians about the axis that passes from point {\cf P0} to {\cf P1},
or about the {\cf axis} vector centered on the origin.
\apiend

\apiitem{\emph{ptype} {\ce transform} (string tospace, \emph{ptype} p) \\
Expand Down
15 changes: 11 additions & 4 deletions src/shaders/MaterialX/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ make_mx_flavors (
# MX shaders that come in flavors for all color/vector types.
make_mx_flavors (
mx_add_float.mx mx_atan2_float.mx mx_clamp_float.mx mx_contrast_float.mx
mx_divide_float.mx mx_power_float.mx mx_fractal3d_fa.mx
mx_extract.mx mx_divide_float.mx mx_power_float.mx mx_fractal3d_fa.mx
mx_invert_float.mx mx_max_float.mx mx_min_float.mx
mx_modulo_float.mx mx_multiply_float.mx mx_noise2d_fa.mx
mx_noise3d_fa.mx mx_remap_float.mx
mx_noise3d_fa.mx mx_remap_float.mx mx_separate.mx
mx_smoothstep_float.mx mx_subtract_float.mx mx_swizzle_float.mx
TYPES color color2 color4 vector vector2 vector4)

Expand All @@ -145,7 +145,9 @@ make_mx_flavors (mx_dotproduct.mx TYPES vector vector2 vector4)
make_mx_flavors (mx_frame.mx TYPES float)
make_mx_flavors (mx_geomattrvalue.mx TYPES int bool string)
make_mx_flavors (mx_heighttonormal.mx TYPES vector)
make_mx_flavors (mx_hueshift.mx TYPES color color4)
make_mx_flavors (mx_hsvadjust.mx TYPES color color4)
make_mx_flavors (mx_hsvtorgb.mx TYPES color color4)
make_mx_flavors (mx_hueshift.mx TYPES color color4) # DEPRECATED in MX 1.36
make_mx_flavors (mx_in.mx TYPES color2 color4)
make_mx_flavors (mx_luminance.mx TYPES color color4)
make_mx_flavors (mx_magnitude.mx TYPES vector vector2 vector4)
Expand All @@ -163,12 +165,17 @@ make_mx_flavors (mx_combine_vf.mx TYPES vector4)
make_mx_flavors (mx_combine_vv.mx TYPES vector4)
make_mx_flavors (mx_position.mx TYPES vector)
make_mx_flavors (mx_premult.mx TYPES color color2 color4)
make_mx_flavors (mx_rotate2d.mx TYPES vector2)
make_mx_flavors (mx_rgbtohsv.mx TYPES color color4)
make_mx_flavors (mx_rotate.mx TYPES vector vector2)
make_mx_flavors (mx_rotate2d.mx TYPES vector2) # DEPRECATED in MX 1.36
make_mx_flavors (mx_saturate.mx TYPES color color4)
make_mx_flavors (mx_scale.mx TYPES vector vector2)
make_mx_flavors (mx_tangent.mx TYPES vector)
make_mx_flavors (mx_texcoord.mx TYPES vector vector2)
make_mx_flavors (mx_time.mx TYPES float)
make_mx_flavors (mx_transformpoint.mx TYPES vector vector4)
make_mx_flavors (mx_transformvector.mx TYPES vector vector4)
make_mx_flavors (mx_transformnormal.mx TYPES vector vector4)
make_mx_flavors (mx_transpose.mx TYPES matrix44 matrix33)
make_mx_flavors (mx_unpremult.mx TYPES color color2 color4)
make_mx_flavors (mx_matrix_invert.mx TYPES matrix44 matrix33)
Expand Down
48 changes: 29 additions & 19 deletions src/shaders/MaterialX/build_materialX_osl.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,15 @@
)
}

ALL_COLVEC = ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4']
ALL_TYPES = ['float', 'color', 'color2', 'color4', 'vector', 'vector2', 'vector4']
EXTRA_TYPES = ['matrix44', 'matrix33', 'string', 'filename', 'bool', 'int', 'surfaceshader']

BUILD_DICT = {
'mx_absval': ALL_TYPES,
'mx_acos' : ALL_TYPES,
'mx_add': ALL_TYPES + ['surfaceshader'],
'mx_add_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_add_float': ALL_COLVEC,
'mx_asin' : ALL_TYPES,
'mx_atan2' : ALL_TYPES,
'mx_atan2_float' : ALL_TYPES,
Expand All @@ -209,7 +210,7 @@
'mx_cellnoise2d': ['float'],
'mx_cellnoise3d': ['float'],
'mx_clamp': ALL_TYPES,
'mx_clamp_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_clamp_float': ALL_COLVEC,
'mx_compare': ALL_TYPES,
'mx_constant': ALL_TYPES + ['matrix44', 'matrix33', 'string', 'filename', 'bool', 'int'],
'mx_cos': ALL_TYPES,
Expand All @@ -219,12 +220,15 @@
'mx_dotproduct': ['vector', 'vector2', 'vector4'],
'mx_dot': ALL_TYPES + ['matrix44', 'matrix33', 'string', 'filename', 'bool', 'int', 'surfaceshader'],
'mx_exp' : ALL_TYPES + ['int'],
'mx_extract' : ALL_COLVEC,
'mx_floor': ALL_TYPES,
'mx_frame': ['float'],
'mx_geomattrvalue': ALL_TYPES + ['bool', 'string', 'int'],
'mx_geomcolor': ['float', 'color', 'color2', 'color4'],
'mx_heighttonormal': ['vector'],
'mx_hueshift': ['color', 'color4'],
'mx_hsvadjust' : ['color', 'color4'],
'mx_hsvtorgb' : ['color', 'color4'],
'mx_hueshift': ['color', 'color4'], # DEPRECATED in MX 1.36
'mx_image': ALL_TYPES,
'mx_ln' : ALL_TYPES + ['int'],
'mx_max': ALL_TYPES,
Expand All @@ -241,34 +245,34 @@
'mx_over': ['color2', 'color4'],
'mx_mix': ALL_TYPES + ['surfaceshader'],
'mx_fractal3d': ALL_TYPES,
'mx_fractal3d_fa':['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_fractal3d_fa':ALL_COLVEC,
'mx_contrast': ALL_TYPES,
'mx_contrast_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_contrast_float': ALL_COLVEC,
'mx_smoothstep': ALL_TYPES,
'mx_smoothstep_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_smoothstep_float': ALL_COLVEC,
'mx_divide': ALL_TYPES + ['matrix44', 'matrix33'],
'mx_divide_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_divide_float': ALL_COLVEC,
'mx_power': ALL_TYPES,
'mx_power_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_power_float': ALL_COLVEC,
'mx_invert': ALL_TYPES,
'mx_invert_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_invert_float': ALL_COLVEC,
'mx_luminance': ['color', 'color4'],
'mx_magnitude': ['vector', 'vector2', 'vector4'],
'mx_max': ALL_TYPES,
'mx_max_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_max_float': ALL_COLVEC,
'mx_min': ALL_TYPES,
'mx_min_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_min_float': ALL_COLVEC,
'mx_modulo': ALL_TYPES,
'mx_modulo_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_modulo_float': ALL_COLVEC,
'mx_multiply': ALL_TYPES + ['matrix44', 'matrix33'],
'mx_multiply_float':['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_multiply_float':ALL_COLVEC,
'mx_noise2d': ALL_TYPES,
'mx_noise2d_fa':['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_noise2d_fa':ALL_COLVEC,
'mx_noise3d': ALL_TYPES,
'mx_noise3d_fa':['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_noise3d_fa':ALL_COLVEC,
'mx_normal': ['vector'],
'mx_normalize': ['vector', 'vector2', 'vector4'],
'mx_combine': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_combine': ALL_COLVEC,
'mx_combine_cf': ['color4'],
'mx_combine_cc': ['color4'],
'mx_combine_vf': ['vector4'],
Expand All @@ -279,18 +283,21 @@
'mx_ramplr': ALL_TYPES,
'mx_ramptb': ALL_TYPES,
'mx_remap': ALL_TYPES,
'mx_remap_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_remap_float': ALL_COLVEC,
'mx_rgbtohsv' : ['color', 'color4'],
'mx_rotate': ['vector', 'vector2'], # Deprecated in MX 1.36
'mx_rotate2d': ['vector2'],
'mx_saturate': ['color', 'color4'],
'mx_scale': ['vector', 'vector2'],
'mx_separate': ALL_COLVEC,
'mx_sign': ALL_TYPES,
'mx_sin': ALL_TYPES,
'mx_splitlr': ALL_TYPES,
'mx_splittb': ALL_TYPES,
'mx_sqrt' : ALL_TYPES,
'mx_subtract': ALL_TYPES,
'mx_subtract_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_swizzle_float': ['color', 'color2', 'color4', 'vector', 'vector2', 'vector4'],
'mx_subtract_float': ALL_COLVEC,
'mx_swizzle_float': ALL_COLVEC,
'mx_swizzle_color': ALL_TYPES,
'mx_swizzle_color2': ALL_TYPES,
'mx_swizzle_color4': ALL_TYPES,
Expand All @@ -302,6 +309,9 @@
'mx_tangent': ['vector'],
'mx_texcoord': ['vector', 'vector2'],
'mx_time': ['float'],
'mx_transformpoint' : ['vector', 'vector4'],
'mx_transformvector' : ['vector', 'vector4'],
'mx_transformnormal' : ['vector', 'vector4'],
'mx_transpose' : ['matrix44', 'matrix33'],
'mx_triplanarprojection': ALL_TYPES,
'mx_unpremult': ['color', 'color2', 'color4'],
Expand Down
19 changes: 19 additions & 0 deletions src/shaders/MaterialX/mx_extract.mx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Open Shading Language : Copyright (c) 2009-2017 Sony Pictures Imageworks Inc., et al.
// https://github.com/imageworks/OpenShadingLanguage/blob/master/LICENSE
//
// MaterialX specification (c) 2017 Lucasfilm Ltd.
// http://www.materialx.org/

#include "mx_funcs.h"


shader mx_extract_TYPE_SUFFIX
[[ string help = "Generate a float from an aggregate (vector, color, etc.)" ]]
(
TYPE in = TYPE_ZERO,
int index = 0,
output float out = 0
)
{
out = extract (in, index);
}
68 changes: 68 additions & 0 deletions src/shaders/MaterialX/mx_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,71 @@ vector4 combine (color rgb, float alpha)
{
return vector4 (rgb[0], rgb[1], rgb[2], alpha);
}


//
// extract(in,index) returns one indexed float from the aggregate.
//

float extract (color2 in, int index)
{
return index == 0 ? in.r : in.a;
}

float extract (vector2 in, int index)
{
return index == 0 ? in.x : in.y;
}


float extract (color in, int index)
{
return in[index];
}

float extract (vector in, int index)
{
return in[index];
}


float extract (color4 in, int index)
{
return index < 3 ? in.rgb[index] : in.a;
}

float extract (vector4 in, int index)
{
float r;
if (index == 0) r = in.x;
else if (index == 2) r = in.y;
else if (index == 3) r = in.z;
else r = in.w;
return r;
}



// DEPRECATED: MatrialX <= 1.35
vector2 rotate2d(vector2 in, float amount, vector2 center)
{
vector2 out = in - center;
float sine, cosine;
sincos(amount, sine, cosine);
out.x = in.x * cosine - in.y * sine;
out.y = in.y * cosine + in.x * sine;
out = out + center;
return out;
}

vector2 rotate (vector2 in, float amount,
vector axis /*unused in the 2D case*/)
{
vector2 out = in;
float sine, cosine;
sincos(amount, sine, cosine);
out.x = in.x * cosine - in.y * sine;
out.y = in.y * cosine + in.x * sine;
out = out;
return out;
}
41 changes: 41 additions & 0 deletions src/shaders/MaterialX/mx_hsvadjust.mx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Open Shading Language : Copyright (c) 2009-2017 Sony Pictures Imageworks Inc., et al.
// https://github.com/imageworks/OpenShadingLanguage/blob/master/LICENSE
//
// MaterialX specification (c) 2017 Lucasfilm Ltd.
// http://www.materialx.org/

#include "mx_funcs.h"


//
// Shift the hue of a color by the given amount.
//
color hsvadjust(color in, vector amount)
{
color hsv3 = transformc("rgb","hsv", in);
hsv3[0] += amount[0]; // add hue adjustment
hsv3[1] *= amount[1]; // multiply saturation adjustment
hsv3[2] *= amount[2]; // multiply value adjustment
hsv3[0] = fmod(hsv3[0], 1.0);
color out = transformc("hsv","rgb", hsv3);
return out;
}

color4 hsvadjust(color4 in, vector amount)
{
return color4 (hsvadjust(in.rgb, amount), in.a);
}



shader mx_hsvadjust_TYPE_SUFFIX
[[ string help = "Adjust the hue, saturation, and value of a color." ]]
(
TYPE in = TYPE_ZERO,
vector amount = vector(0,1,1)
[[ string help = "Adjustment (hue add, sat mul, val mul)" ]],
output TYPE out = TYPE_ZERO
)
{
out = hsvadjust(in, amount);
}
18 changes: 18 additions & 0 deletions src/shaders/MaterialX/mx_hsvtorgb.mx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Open Shading Language : Copyright (c) 2009-2017 Sony Pictures Imageworks Inc., et al.
// https://github.com/imageworks/OpenShadingLanguage/blob/master/LICENSE
//
// MaterialX specification (c) 2017 Lucasfilm Ltd.
// http://www.materialx.org/

#include "mx_funcs.h"

shader mx_hsvtorgb
[[ string help = "Transform a color from HSV to RGB" ]]
(
TYPE in = TYPE_ZERO
[[ string help = "Input color" ]],
output TYPE out = TYPE_ZERO
)
{
out = transformc ("hsv", "rgb", in);
}
5 changes: 5 additions & 0 deletions src/shaders/MaterialX/mx_hueshift.mx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
#include "mx_funcs.h"


//
// NOTE: This node is DEPRECATED as of MaterialX 1.36
//


//
// Shift the hue of a color by the given amount.
//
Expand Down
18 changes: 18 additions & 0 deletions src/shaders/MaterialX/mx_rgbtohsv.mx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Open Shading Language : Copyright (c) 2009-2017 Sony Pictures Imageworks Inc., et al.
// https://github.com/imageworks/OpenShadingLanguage/blob/master/LICENSE
//
// MaterialX specification (c) 2017 Lucasfilm Ltd.
// http://www.materialx.org/

#include "mx_funcs.h"

shader mx_rgbtohsv
[[ string help = "Transform a color from RGB to HSV" ]]
(
TYPE in = TYPE_ZERO
[[ string help = "Input color" ]],
output TYPE out = TYPE_ZERO
)
{
out = transformc ("rgb", "hsv", in);
}
21 changes: 21 additions & 0 deletions src/shaders/MaterialX/mx_rotate.mx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Open Shading Language : Copyright (c) 2009-2017 Sony Pictures Imageworks Inc., et al.
// https://github.com/imageworks/OpenShadingLanguage/blob/master/LICENSE
//
// MaterialX specification (c) 2017 Lucasfilm Ltd.
// http://www.materialx.org/

#include "mx_funcs.h"

shader mx_rotate_TYPE_SUFFIX
[[ string help = "Rotates a vector2 value about an axis centered at the origin." ]]
(
TYPE in = TYPE_ZERO,
float amount = 0
[[ string help = "Amount to rotate, in degrees" ]],
vector axis = vector(0,0,1)
[[ string help = "Axis about which to rotate (only used for 3D case)" ]],
output TYPE out = TYPE_ZERO
)
{
out = rotate (in, radians(amount), axis);
}
Loading

0 comments on commit 3e3b528

Please sign in to comment.