diff options
Diffstat (limited to 'intern/cycles/kernel/shaders')
63 files changed, 4122 insertions, 0 deletions
diff --git a/intern/cycles/kernel/shaders/CMakeLists.txt b/intern/cycles/kernel/shaders/CMakeLists.txt new file mode 100644 index 00000000000..00ab52a1d13 --- /dev/null +++ b/intern/cycles/kernel/shaders/CMakeLists.txt @@ -0,0 +1,98 @@ + +# OSL node shaders + +set(SRC_OSL + node_add_closure.osl + node_attribute.osl + node_background.osl + node_brick_texture.osl + node_bump.osl + node_camera.osl + node_checker_texture.osl + node_combine_rgb.osl + node_convert_from_color.osl + node_convert_from_float.osl + node_convert_from_int.osl + node_convert_from_normal.osl + node_convert_from_point.osl + node_convert_from_vector.osl + node_diffuse_bsdf.osl + node_emission.osl + node_environment_texture.osl + node_fresnel.osl + node_gamma.osl + node_gradient_texture.osl + node_brightness.osl + node_geometry.osl + node_glass_bsdf.osl + node_glossy_bsdf.osl + node_holdout.osl + node_hsv.osl + node_image_texture.osl + node_invert.osl + node_layer_weight.osl + node_light_path.osl + node_light_falloff.osl + node_magic_texture.osl + node_mapping.osl + node_math.osl + node_mix.osl + node_mix_closure.osl + node_musgrave_texture.osl + node_normal.osl + node_noise_texture.osl + node_object_info.osl + node_output_displacement.osl + node_output_surface.osl + node_output_volume.osl + node_particle_info.osl + node_rgb_ramp.osl + node_separate_rgb.osl + node_set_normal.osl + node_sky_texture.osl + node_texture_coordinate.osl + node_translucent_bsdf.osl + node_transparent_bsdf.osl + node_value.osl + node_vector_math.osl + node_velvet_bsdf.osl + node_voronoi_texture.osl + node_ward_bsdf.osl + node_wave_texture.osl +) + +set(SRC_OSL_HEADERS + node_texture.h + node_color.h + node_fresnel.h + stdosl.h + oslutil.h +) + +set(SRC_OSO + +) + +# TODO, add a module to compile OSL +foreach(_file ${SRC_OSL}) + set(_OSL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${_file}) + string(REPLACE ".osl" ".oso" _OSO_FILE ${_OSL_FILE}) + string(REPLACE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} _OSO_FILE ${_OSO_FILE}) + add_custom_command( + OUTPUT ${_OSO_FILE} + COMMAND ${OSL_COMPILER} -O2 -I"${CMAKE_CURRENT_SOURCE_DIR}" ${_OSL_FILE} + DEPENDS ${_OSL_FILE} ${SRC_OSL_HEADERS}) + list(APPEND SRC_OSO + ${_OSO_FILE} + ) + + unset(_OSL_FILE) + unset(_OSO_FILE) +endforeach() + +add_custom_target(cycles_osl_shaders ALL DEPENDS ${SRC_OSO} ${SRC_OSL_HEADERS}) + +# CMAKE_CURRENT_SOURCE_DIR is already included in OSO paths +delayed_install("" "${SRC_OSO}" ${CYCLES_INSTALL_PATH}/shader) +delayed_install("${CMAKE_CURRENT_SOURCE_DIR}" "${SRC_OSL_HEADERS}" ${CYCLES_INSTALL_PATH}/shader) + diff --git a/intern/cycles/kernel/shaders/node_add_closure.osl b/intern/cycles/kernel/shaders/node_add_closure.osl new file mode 100644 index 00000000000..ecf6bf5912e --- /dev/null +++ b/intern/cycles/kernel/shaders/node_add_closure.osl @@ -0,0 +1,28 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_add_closure( + closure color Closure1 = background(), + closure color Closure2 = background(), + output closure color Closure = background()) +{ + Closure = Closure1 + Closure2; +} + diff --git a/intern/cycles/kernel/shaders/node_attribute.osl b/intern/cycles/kernel/shaders/node_attribute.osl new file mode 100644 index 00000000000..8e7c846d1a3 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_attribute.osl @@ -0,0 +1,43 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_attribute( + string bump_offset = "center", + string name = "", + output point Vector = point(0.0, 0.0, 0.0), + output color Color = color(0.0, 0.0, 0.0), + output float Fac = 0.0) +{ + getattribute(name, Color); + Vector = point(Color); + getattribute(name, Fac); + + if (bump_offset == "dx") { + Color += Dx(Color); + Vector += Dx(Vector); + Fac += Dx(Fac); + } + else if (bump_offset == "dy") { + Color += Dy(Color); + Vector += Dy(Vector); + Fac += Dy(Fac); + } +} + diff --git a/intern/cycles/kernel/shaders/node_background.osl b/intern/cycles/kernel/shaders/node_background.osl new file mode 100644 index 00000000000..b51a1685294 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_background.osl @@ -0,0 +1,28 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_background( + color Color = color(0.8, 0.8, 0.8), + float Strength = 1.0, + output closure color Background = background()) +{ + Background = Color * Strength * background(); +} + diff --git a/intern/cycles/kernel/shaders/node_brick_texture.osl b/intern/cycles/kernel/shaders/node_brick_texture.osl new file mode 100644 index 00000000000..478d9457001 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_brick_texture.osl @@ -0,0 +1,93 @@ +/* + * Copyright 2012, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_texture.h" + +/* Brick */ + +float brick_noise(int n) /* fast integer noise */ +{ + int nn; + n = (n >> 13) ^ n; + nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 2147483647; + return 0.5 * ((float)nn / 1073741824.0); +} + +float brick(point p, float mortar_size, float bias, + float BrickWidth, float row_height, float offset_amount, int offset_frequency, + float squash_amount, int squash_frequency, float tint) +{ + int bricknum, rownum; + float offset = 0.0; + float brick_width = BrickWidth; + float x, y; + + rownum = (int)floor(p[1] / row_height); + + if (offset_frequency && squash_frequency) { + brick_width *= ((int)(rownum) % squash_frequency) ? 1.0 : squash_amount; /* squash */ + offset = ((int)(rownum) % offset_frequency) ? 0 : (brick_width * offset_amount); /* offset */ + } + + bricknum = (int)floor((p[0] + offset) / brick_width); + + x = (p[0] + offset) - brick_width * bricknum; + y = p[1] - row_height * rownum; + + tint = clamp((brick_noise((rownum << 16) + (bricknum & 65535)) + bias), 0.0, 1.0); + + return (x < mortar_size || y < mortar_size || + x > (brick_width - mortar_size) || + y > (row_height - mortar_size)) ? 1.0 : 0.0; +} + +shader node_brick_texture( + float Offset = 0.5, + int OffsetFrequency = 2, + float Squash = 1.0, + int SquashFrequency = 1, + point Vector = P, + color Color1 = color(0.2, 0.2, 0.2), + color Color2 = color(0.8, 0.8, 0.8), + color Mortar = color(0.0, 0.0, 0.0), + float Scale = 5.0, + float MortarSize = 0.02, + float Bias = 0.0, + float BrickWidth = 0.5, + float RowHeight = 0.25, + output float Fac = 0.0, + output color Color = color(0.2, 0.2, 0.2)) +{ + float tint = 0.0; + color Col = Color1; + + Fac = brick(Vector * Scale, MortarSize, Bias, BrickWidth, RowHeight, + Offset, OffsetFrequency, Squash, SquashFrequency, tint); + + if (Fac != 1.0) { + float facm = 1.0 - tint; + + Col[0] = facm * (Color1[0]) + tint * Color2[0]; + Col[1] = facm * (Color1[1]) + tint * Color2[1]; + Col[2] = facm * (Color1[2]) + tint * Color2[2]; + } + + Color = (Fac == 1.0) ? Mortar: Col; +} + diff --git a/intern/cycles/kernel/shaders/node_brightness.osl b/intern/cycles/kernel/shaders/node_brightness.osl new file mode 100644 index 00000000000..8e9f5c9c796 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_brightness.osl @@ -0,0 +1,51 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_brightness( + color ColorIn = color(0.8, 0.8, 0.8), + float Brightness = 0.0, + float Contrast = 0.0, + output color ColorOut = color(0.8, 0.8, 0.8)) +{ + float delta = Contrast * (1.0 / 200.0); + float a = 1.0 - delta * 2.0; + float b; + + /* input value is a percentage */ + float bright_factor = Brightness / 100.0; + + /* + * The algorithm is by Werner D. Streidt + * (http://visca.com/ffactory/archives/5-99/msg00021.html) + * Extracted of OpenCV demhist.c + */ + + if (Contrast > 0.0) { + a = (a < 0.0 ? 1.0 / a : 0.0); + b = a * (bright_factor - delta); + } + else { + delta *= -1.0; + b = a * (bright_factor + delta); + } + + ColorOut = a * ColorIn + b; +} + diff --git a/intern/cycles/kernel/shaders/node_bump.osl b/intern/cycles/kernel/shaders/node_bump.osl new file mode 100644 index 00000000000..24db1b24458 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_bump.osl @@ -0,0 +1,49 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +/* "Bump Mapping Unparameterized Surfaces on the GPU" + * Morten S. Mikkelsen, 2010 */ + +surface node_bump( + normal NormalIn = N, + float Strength = 0.0, + float SampleCenter = 0.0, + float SampleX = 0.0, + float SampleY = 0.0, + output normal Normal = N) +{ + /* get surface tangents from normal */ + vector dPdx = Dx(P); + vector dPdy = Dy(P); + + vector Rx = cross(dPdy, NormalIn); + vector Ry = cross(NormalIn, dPdx); + + /* compute surface gradient and determinant */ + float det = dot(dPdx, Rx); + vector surfgrad = (SampleX - SampleCenter) * Rx + (SampleY - SampleCenter) * Ry; + + surfgrad *= Strength; + float absdet = fabs(det); + + /* compute and output perturbed normal */ + Normal = normalize(absdet * NormalIn - sign(det) * surfgrad); +} + diff --git a/intern/cycles/kernel/shaders/node_camera.osl b/intern/cycles/kernel/shaders/node_camera.osl new file mode 100644 index 00000000000..898ebd2bbb2 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_camera.osl @@ -0,0 +1,33 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_camera( + output vector ViewVector = vector(0.0, 0.0, 0.0), + output float ViewZDepth = 0.0, + output float ViewDistance = 0.0) +{ + ViewVector = (vector)transform("world", "camera", P); + + ViewZDepth = fabs(ViewVector[2]); + ViewDistance = length(ViewVector); + + ViewVector = normalize(ViewVector); +} + diff --git a/intern/cycles/kernel/shaders/node_checker_texture.osl b/intern/cycles/kernel/shaders/node_checker_texture.osl new file mode 100644 index 00000000000..577caf308ff --- /dev/null +++ b/intern/cycles/kernel/shaders/node_checker_texture.osl @@ -0,0 +1,58 @@ +/* + * Copyright 2012, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_texture.h" + +/* Checker */ + +float checker(point p) +{ + p[0] = (p[0] + 0.00001) * 0.9999; + p[1] = (p[1] + 0.00001) * 0.9999; + p[2] = (p[2] + 0.00001) * 0.9999; + + int xi = (int)fabs(floor(p[0])); + int yi = (int)fabs(floor(p[1])); + int zi = (int)fabs(floor(p[2])); + + if ((xi % 2 == yi % 2) == (zi % 2)) { + return 1.0; + } + else { + return 0.0; + } +} + +shader node_checker_texture( + float Scale = 5.0, + point Vector = P, + color Color1 = color(0.8, 0.8, 0.8), + color Color2 = color(0.2, 0.2, 0.2), + output float Fac = 0.0, + output color Color = color(0.0, 0.0, 0.0)) +{ + Fac = checker(Vector * Scale); + if (Fac == 1.0) { + Color = Color1; + } + else { + Color = Color2; + } +} + diff --git a/intern/cycles/kernel/shaders/node_color.h b/intern/cycles/kernel/shaders/node_color.h new file mode 100644 index 00000000000..80786e4e369 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_color.h @@ -0,0 +1,125 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +/* Color Management */ + +float color_srgb_to_scene_linear(float c) +{ + if (c < 0.04045) + return (c < 0.0) ? 0.0 : c * (1.0 / 12.92); + else + return pow((c + 0.055) * (1.0 / 1.055), 2.4); +} + +float color_scene_linear_to_srgb(float c) +{ + if (c < 0.0031308) + return (c < 0.0) ? 0.0 : c * 12.92; + else + return 1.055 * pow(c, 1.0 / 2.4) - 0.055; +} + +color color_srgb_to_scene_linear(color c) +{ + return color( + color_srgb_to_scene_linear(c[0]), + color_srgb_to_scene_linear(c[1]), + color_srgb_to_scene_linear(c[2])); +} + +color color_scene_linear_to_srgb(color c) +{ + return color( + color_scene_linear_to_srgb(c[0]), + color_scene_linear_to_srgb(c[1]), + color_scene_linear_to_srgb(c[2])); +} + +/* Color Operations */ + +color rgb_to_hsv(color rgb) +{ + float cmax, cmin, h, s, v, cdelta; + color c; + + cmax = max(rgb[0], max(rgb[1], rgb[2])); + cmin = min(rgb[0], min(rgb[1], rgb[2])); + cdelta = cmax - cmin; + + v = cmax; + + if (cmax != 0.0) { + s = cdelta / cmax; + } + else { + s = 0.0; + h = 0.0; + } + + if (s == 0.0) { + h = 0.0; + } + else { + c = (color(cmax, cmax, cmax) - rgb) / cdelta; + + if (rgb[0] == cmax) h = c[2] - c[1]; + else if (rgb[1] == cmax) h = 2.0 + c[0] - c[2]; + else h = 4.0 + c[1] - c[0]; + + h /= 6.0; + + if (h < 0.0) + h += 1.0; + } + + return color(h, s, v); +} + +color hsv_to_rgb(color hsv) +{ + float i, f, p, q, t, h, s, v; + color rgb; + + h = hsv[0]; + s = hsv[1]; + v = hsv[2]; + + if (s == 0.0) { + rgb = color(v, v, v); + } + else { + if (h == 1.0) + h = 0.0; + + h *= 6.0; + i = floor(h); + f = h - i; + rgb = color(f, f, f); + p = v * (1.0 - s); + q = v * (1.0 - (s * f)); + t = v * (1.0 - (s * (1.0 - f))); + + if (i == 0.0) rgb = color(v, t, p); + else if (i == 1.0) rgb = color(q, v, p); + else if (i == 2.0) rgb = color(p, v, t); + else if (i == 3.0) rgb = color(p, q, v); + else if (i == 4.0) rgb = color(t, p, v); + else rgb = color(v, p, q); + } + + return rgb; +} + diff --git a/intern/cycles/kernel/shaders/node_combine_rgb.osl b/intern/cycles/kernel/shaders/node_combine_rgb.osl new file mode 100644 index 00000000000..546369f660e --- /dev/null +++ b/intern/cycles/kernel/shaders/node_combine_rgb.osl @@ -0,0 +1,29 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_combine_rgb( + float R = 0.0, + float G = 0.0, + float B = 0.0, + output color Image = color(0.8, 0.8, 0.8)) +{ + Image = color(R, G, B); +} + diff --git a/intern/cycles/kernel/shaders/node_convert_from_color.osl b/intern/cycles/kernel/shaders/node_convert_from_color.osl new file mode 100644 index 00000000000..2884c772414 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_convert_from_color.osl @@ -0,0 +1,35 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_convert_from_color( + color Color = color(0.0, 0.0, 0.0), + output float Val = 0.0, + output int ValInt = 0, + output vector Vector = vector(0.0, 0.0, 0.0), + output point Point = point(0.0, 0.0, 0.0), + output normal Normal = normal(0.0, 0.0, 0.0)) +{ + Val = Color[0] * 0.2126 + Color[1] * 0.7152 + Color[2] * 0.0722; + ValInt = (int)(Color[0]*0.2126 + Color[1]*0.7152 + Color[2]*0.0722); + Vector = vector(Color[0], Color[1], Color[2]); + Point = point(Color[0], Color[1], Color[2]); + Normal = normal(Color[0], Color[1], Color[2]); +} + diff --git a/intern/cycles/kernel/shaders/node_convert_from_float.osl b/intern/cycles/kernel/shaders/node_convert_from_float.osl new file mode 100644 index 00000000000..4466fbae3a6 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_convert_from_float.osl @@ -0,0 +1,35 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_convert_from_float( + float Val = 0.0, + output int ValInt = 0, + output color Color = color(0.0, 0.0, 0.0), + output vector Vector = vector(0.0, 0.0, 0.0), + output point Point = point(0.0, 0.0, 0.0), + output normal Normal = normal(0.0, 0.0, 0.0)) +{ + ValInt = (int)Val; + Color = color(Val, Val, Val); + Vector = vector(Val, Val, Val); + Point = point(Val, Val, Val); + Normal = normal(Val, Val, Val); +} + diff --git a/intern/cycles/kernel/shaders/node_convert_from_int.osl b/intern/cycles/kernel/shaders/node_convert_from_int.osl new file mode 100644 index 00000000000..060d4184fa6 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_convert_from_int.osl @@ -0,0 +1,36 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_convert_from_int( + int ValInt = 0, + output float Val = 0.0, + output color Color = color(0.0, 0.0, 0.0), + output vector Vector = vector(0.0, 0.0, 0.0), + output point Point = point(0.0, 0.0, 0.0), + output normal Normal = normal(0.0, 0.0, 0.0)) +{ + float f = (float)ValInt; + Val = f; + Color = color(f, f, f); + Vector = vector(f, f, f); + Point = point(f, f, f); + Normal = normal(f, f, f); +} + diff --git a/intern/cycles/kernel/shaders/node_convert_from_normal.osl b/intern/cycles/kernel/shaders/node_convert_from_normal.osl new file mode 100644 index 00000000000..32ef430d93b --- /dev/null +++ b/intern/cycles/kernel/shaders/node_convert_from_normal.osl @@ -0,0 +1,35 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_convert_from_normal( + normal Normal = normal(0.0, 0.0, 0.0), + output float Val = 0.0, + output int ValInt = 0, + output vector Vector = vector(0.0, 0.0, 0.0), + output color Color = color(0.0, 0.0, 0.0), + output point Point = point(0.0, 0.0, 0.0)) +{ + Val = (Normal[0] + Normal[1] + Normal[2]) * (1.0 / 3.0); + ValInt = (int)((Normal[0] + Normal[1] + Normal[2]) * (1.0 / 3.0)); + Vector = vector(Normal[0], Normal[1], Normal[2]); + Color = color(Normal[0], Normal[1], Normal[2]); + Point = point(Normal[0], Normal[1], Normal[2]); +} + diff --git a/intern/cycles/kernel/shaders/node_convert_from_point.osl b/intern/cycles/kernel/shaders/node_convert_from_point.osl new file mode 100644 index 00000000000..a9435c8abf4 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_convert_from_point.osl @@ -0,0 +1,35 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_convert_from_point( + point Point = point(0.0, 0.0, 0.0), + output float Val = 0.0, + output int ValInt = 0, + output vector Vector = vector(0.0, 0.0, 0.0), + output color Color = color(0.0, 0.0, 0.0), + output normal Normal = normal(0.0, 0.0, 0.0)) +{ + Val = (Point[0] + Point[1] + Point[2]) * (1.0 / 3.0); + ValInt = (int)((Normal[0] + Normal[1] + Normal[2]) * (1.0 / 3.0)); + Vector = vector(Point[0], Point[1], Point[2]); + Color = color(Point[0], Point[1], Point[2]); + Normal = normal(Point[0], Point[1], Point[2]); +} + diff --git a/intern/cycles/kernel/shaders/node_convert_from_vector.osl b/intern/cycles/kernel/shaders/node_convert_from_vector.osl new file mode 100644 index 00000000000..4516f92c753 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_convert_from_vector.osl @@ -0,0 +1,35 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_convert_from_vector( + vector Vector = vector(0.0, 0.0, 0.0), + output float Val = 0.0, + output int ValInt = 0, + output color Color = color(0.0, 0.0, 0.0), + output point Point = point(0.0, 0.0, 0.0), + output normal Normal = normal(0.0, 0.0, 0.0)) +{ + Val = (Vector[0] + Vector[1] + Vector[2]) * (1.0 / 3.0); + ValInt = (int)((Normal[0] + Normal[1] + Normal[2]) * (1.0 / 3.0)); + Color = color(Vector[0], Vector[1], Vector[2]); + Point = point(Vector[0], Vector[1], Vector[2]); + Normal = normal(Vector[0], Vector[1], Vector[2]); +} + diff --git a/intern/cycles/kernel/shaders/node_diffuse_bsdf.osl b/intern/cycles/kernel/shaders/node_diffuse_bsdf.osl new file mode 100644 index 00000000000..d6dc17316e8 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_diffuse_bsdf.osl @@ -0,0 +1,32 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_diffuse_bsdf( + color Color = color(0.8, 0.8, 0.8), + float Roughness = 0.0, + normal Normal = N, + output closure color BSDF = diffuse(Normal)) +{ + if (Roughness == 0.0) + BSDF = Color * diffuse(Normal); + else + BSDF = Color * oren_nayar(Normal, Roughness); +} + diff --git a/intern/cycles/kernel/shaders/node_emission.osl b/intern/cycles/kernel/shaders/node_emission.osl new file mode 100644 index 00000000000..7ad0f9f7760 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_emission.osl @@ -0,0 +1,32 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_emission( + int TotalPower = 0, + color Color = color(0.8, 0.8, 0.8), + float Strength = 1.0, + output closure color Emission = emission()) +{ + if (TotalPower) + Emission = ((Strength / surfacearea()) * Color) * emission(); + else + Emission = (Strength * Color) * emission(); +} + diff --git a/intern/cycles/kernel/shaders/node_environment_texture.osl b/intern/cycles/kernel/shaders/node_environment_texture.osl new file mode 100644 index 00000000000..bad62e56ab4 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_environment_texture.osl @@ -0,0 +1,34 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_color.h" + +shader node_environment_texture( + vector Vector = P, + string filename = "", + string color_space = "sRGB", + output color Color = color(0.0, 0.0, 0.0), + output float Alpha = 1.0) +{ + Color = (color)environment(filename, Vector, "alpha", Alpha); + + if (color_space == "sRGB") + Color = color_srgb_to_scene_linear(Color); +} + diff --git a/intern/cycles/kernel/shaders/node_fresnel.h b/intern/cycles/kernel/shaders/node_fresnel.h new file mode 100644 index 00000000000..dfd0a23fe1e --- /dev/null +++ b/intern/cycles/kernel/shaders/node_fresnel.h @@ -0,0 +1,36 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +float fresnel_dielectric(vector Incoming, normal Normal, float eta) +{ + /* compute fresnel reflectance without explicitly computing + * the refracted direction */ + float c = fabs(dot(Incoming, Normal)); + float g = eta * eta - 1 + c * c; + float result; + + if (g > 0) { + g = sqrt(g); + float A = (g - c) / (g + c); + float B = (c * (g + c) - 1) / (c * (g - c) + 1); + result = 0.5 * A * A * (1 + B * B); + } + else + result = 1.0; /* TIR (no refracted component) */ + + return result; +} + diff --git a/intern/cycles/kernel/shaders/node_fresnel.osl b/intern/cycles/kernel/shaders/node_fresnel.osl new file mode 100644 index 00000000000..e8d8e945f98 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_fresnel.osl @@ -0,0 +1,31 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_fresnel.h" + +shader node_fresnel( + float IOR = 1.45, + normal Normal = N, + output float Fac = 0.0) +{ + float f = max(IOR, 1.0 + 1e-5); + float eta = backfacing() ? 1.0 / f: f; + Fac = fresnel_dielectric(I, Normal, eta); +} + diff --git a/intern/cycles/kernel/shaders/node_gamma.osl b/intern/cycles/kernel/shaders/node_gamma.osl new file mode 100644 index 00000000000..d55e908b0b7 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_gamma.osl @@ -0,0 +1,27 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_gamma( + color ColorIn = color(0.8, 0.8, 0.8), + float Gamma = 1.0, + output color ColorOut = color(0.0, 0.0, 0.0)) +{ + ColorOut = pow(ColorIn, Gamma); +} diff --git a/intern/cycles/kernel/shaders/node_geometry.osl b/intern/cycles/kernel/shaders/node_geometry.osl new file mode 100644 index 00000000000..a3831cbec9c --- /dev/null +++ b/intern/cycles/kernel/shaders/node_geometry.osl @@ -0,0 +1,50 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_geometry( + normal NormalIn = N, + string bump_offset = "center", + + output point Position = point(0.0, 0.0, 0.0), + output normal Normal = normal(0.0, 0.0, 0.0), + output normal Tangent = normal(0.0, 0.0, 0.0), + output normal TrueNormal = normal(0.0, 0.0, 0.0), + output vector Incoming = vector(0.0, 0.0, 0.0), + output point Parametric = point(0.0, 0.0, 0.0), + output float Backfacing = 0.0) +{ + Position = P; + Normal = NormalIn; + Tangent = normalize(dPdu); + TrueNormal = Ng; + Incoming = I; + Parametric = point(u, v, 0.0); + Backfacing = backfacing(); + + if (bump_offset == "dx") { + Position += Dx(Position); + Parametric += Dx(Parametric); + } + else if (bump_offset == "dy") { + Position += Dy(Position); + Parametric += Dy(Parametric); + } +} + diff --git a/intern/cycles/kernel/shaders/node_glass_bsdf.osl b/intern/cycles/kernel/shaders/node_glass_bsdf.osl new file mode 100644 index 00000000000..30b9d301f32 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_glass_bsdf.osl @@ -0,0 +1,43 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_fresnel.h" + +shader node_glass_bsdf( + color Color = color(0.8, 0.8, 0.8), + string distribution = "Sharp", + float Roughness = 0.2, + float IOR = 1.45, + normal Normal = N, + output closure color BSDF = diffuse(Normal)) +{ + float f = max(IOR, 1.0 + 1e-5); + float eta = backfacing() ? 1.0 / f: f; + float Fr = fresnel_dielectric(I, Normal, eta); + + if (distribution == "Sharp") + BSDF = Color * (Fr * reflection(Normal) + (1.0 - Fr) * refraction(Normal, eta)); + else if (distribution == "Beckmann") + BSDF = Color * (Fr * microfacet_beckmann(Normal, Roughness) + + (1.0 - Fr) * microfacet_beckmann_refraction(Normal, Roughness, eta)); + else if (distribution == "GGX") + BSDF = Color * (Fr * microfacet_ggx(Normal, Roughness) + + (1.0 - Fr) * microfacet_ggx_refraction(Normal, Roughness, eta)); +} + diff --git a/intern/cycles/kernel/shaders/node_glossy_bsdf.osl b/intern/cycles/kernel/shaders/node_glossy_bsdf.osl new file mode 100644 index 00000000000..03340c74af5 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_glossy_bsdf.osl @@ -0,0 +1,37 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_fresnel.h" + +shader node_glossy_bsdf( + color Color = color(0.8, 0.8, 0.8), + string distribution = "Beckmann", + float Roughness = 0.2, + normal Normal = N, + output closure color BSDF = diffuse(Normal)) +{ + if (distribution == "Sharp") + BSDF = Color * reflection(Normal); + else if (distribution == "Beckmann") + BSDF = Color * microfacet_beckmann(Normal, Roughness); + else if (distribution == "GGX") + BSDF = Color * microfacet_ggx(Normal, Roughness); + +} + diff --git a/intern/cycles/kernel/shaders/node_gradient_texture.osl b/intern/cycles/kernel/shaders/node_gradient_texture.osl new file mode 100644 index 00000000000..ae7cfa51f59 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_gradient_texture.osl @@ -0,0 +1,74 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_texture.h" + +/* Gradient */ + +float gradient(point p, string type) +{ + float x, y, z; + + x = p[0]; + y = p[1]; + z = p[2]; + + float result = 0.0; + + if (type == "Linear") { + result = x; + } + else if (type == "Quadratic") { + float r = max(x, 0.0); + result = r * r; + } + else if (type == "Easing") { + float r = min(max(x, 0.0), 1.0); + float t = r * r; + + result = (3.0 * t - 2.0 * t * r); + } + else if (type == "Diagonal") { + result = (x + y) / 2.0; + } + else if (type == "Radial") { + result = atan2(y, x) / (2.0 * M_PI) + 0.5; + } + else { + float r = max(1.0 - sqrt(x * x + y * y + z * z), 0.0); + + if (type == "Quadratic Sphere") + result = r * r; + else if (type == "Spherical") + result = r; + } + + return result; +} + +shader node_gradient_texture( + string Type = "Linear", + point Vector = P, + output float Fac = 0.0, + output color Color = color(0.0, 0.0, 0.0)) +{ + Fac = gradient(Vector, Type); + Color = color(Fac, Fac, Fac); +} + diff --git a/intern/cycles/kernel/shaders/node_holdout.osl b/intern/cycles/kernel/shaders/node_holdout.osl new file mode 100644 index 00000000000..aede50c7ca0 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_holdout.osl @@ -0,0 +1,26 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_holdout( + output closure color Holdout = holdout()) +{ + +} + diff --git a/intern/cycles/kernel/shaders/node_hsv.osl b/intern/cycles/kernel/shaders/node_hsv.osl new file mode 100644 index 00000000000..8fd7a1612e8 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_hsv.osl @@ -0,0 +1,43 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_color.h" + +shader node_hsv( + float Hue = 0.5, + float Saturation = 1.0, + float Value = 1.0, + float Fac = 0.5, + color ColorIn = color(0.0, 0.0, 0.0), + output color ColorOut = color(0.0, 0.0, 0.0)) +{ + float t = clamp(Fac, 0.0, 1.0); + color Color = rgb_to_hsv(ColorIn); + + // remember: fmod doesn't work for negative numbers + Color[0] += Hue + 0.5; + Color[0] = fmod(Color[0], 1.0); + Color[1] *= Saturation; + Color[2] *= Value; + + Color = hsv_to_rgb(Color); + + ColorOut = mix(Color, ColorIn, t); +} + diff --git a/intern/cycles/kernel/shaders/node_image_texture.osl b/intern/cycles/kernel/shaders/node_image_texture.osl new file mode 100644 index 00000000000..6393605e6b5 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_image_texture.osl @@ -0,0 +1,120 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_color.h" + +color image_texture_lookup(string filename, string color_space, float u, float v, output float Alpha) +{ + color rgb = (color)texture(filename, u, 1.0 - v, "wrap", "periodic", "alpha", Alpha); + + if (color_space == "sRGB") + rgb = color_srgb_to_scene_linear(rgb); + + return rgb; +} + +shader node_image_texture( + point Vector = P, + string filename = "", + string color_space = "sRGB", + string projection = "Flat", + float projection_blend = 0.0, + output color Color = color(0.0, 0.0, 0.0), + output float Alpha = 1.0) +{ + if (projection == "Flat") { + Color = image_texture_lookup(filename, color_space, Vector[0], Vector[1], Alpha); + } + else if (projection == "Box") { + /* object space normal */ + vector Nob = transform("world", "object", N); + + /* project from direction vector to barycentric coordinates in triangles */ + Nob = vector(fabs(Nob[0]), fabs(Nob[1]), fabs(Nob[2])); + Nob /= (Nob[0] + Nob[1] + Nob[2]); + + /* basic idea is to think of this as a triangle, each corner representing + * one of the 3 faces of the cube. in the corners we have single textures, + * in between we blend between two textures, and in the middle we a blend + * between three textures. + * + * the Nxyz values are the barycentric coordinates in an equilateral + * triangle, which in case of blending, in the middle has a smaller + * equilateral triangle where 3 textures blend. this divides things into + * 7 zones, with an if () test for each zone */ + + vector weight = vector(0.0, 0.0, 0.0); + float blend = projection_blend; + float limit = 0.5*(1.0 + blend); + + /* first test for corners with single texture */ + if (Nob[0] > limit*(Nob[0] + Nob[1]) && Nob[0] > limit*(Nob[0] + Nob[2])) { + weight[0] = 1.0; + } + else if (Nob[1] > limit*(Nob[0] + Nob[1]) && Nob[1] > limit*(Nob[1] + Nob[2])) { + weight[1] = 1.0; + } + else if (Nob[2] > limit*(Nob[0] + Nob[2]) && Nob[2] > limit*(Nob[1] + Nob[2])) { + weight[2] = 1.0; + } + else if (blend > 0.0) { + /* in case of blending, test for mixes between two textures */ + if (Nob[2] < (1.0 - limit)*(Nob[1] + Nob[0])) { + weight[0] = Nob[0] / (Nob[0] + Nob[1]); + weight[0] = clamp((weight[0] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0); + weight[1] = 1.0 - weight[0]; + } + else if (Nob[0] < (1.0 - limit)*(Nob[1] + Nob[2])) { + weight[1] = Nob[1] / (Nob[1] + Nob[2]); + weight[1] = clamp((weight[1] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0); + weight[2] = 1.0 - weight[1]; + } + else if (Nob[1] < (1.0 - limit) * (Nob[0] + Nob[2])) { + weight[0] = Nob[0] / (Nob[0] + Nob[2]); + weight[0] = clamp((weight[0] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0); + weight[2] = 1.0 - weight[0]; + } + else { + /* last case, we have a mix between three */ + weight[0] = ((2.0 - limit) * Nob[0] + (limit - 1.0)) / (2.0 * limit - 1.0); + weight[1] = ((2.0 - limit) * Nob[1] + (limit - 1.0)) / (2.0 * limit - 1.0); + weight[2] = ((2.0 - limit) * Nob[2] + (limit - 1.0)) / (2.0 * limit - 1.0); + } + } + + Color = color(0.0, 0.0, 0.0); + Alpha = 0.0; + + float tmp_alpha; + + if (weight[0] > 0.0) { + Color += weight[0]*image_texture_lookup(filename, color_space, Vector[1], Vector[2], tmp_alpha); + Alpha += weight[0]*tmp_alpha; + } + if (weight[1] > 0.0) { + Color += weight[1]*image_texture_lookup(filename, color_space, Vector[0], Vector[2], tmp_alpha); + Alpha += weight[1]*tmp_alpha; + } + if (weight[2] > 0.0) { + Color += weight[2]*image_texture_lookup(filename, color_space, Vector[1], Vector[0], tmp_alpha); + Alpha += weight[2]*tmp_alpha; + } + } +} + diff --git a/intern/cycles/kernel/shaders/node_invert.osl b/intern/cycles/kernel/shaders/node_invert.osl new file mode 100644 index 00000000000..27021942558 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_invert.osl @@ -0,0 +1,29 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_invert( + float Fac = 1.0, + color ColorIn = color(0.8, 0.8, 0.8), + output color ColorOut = color(0.8, 0.8, 0.8)) +{ + color ColorInv = color(1.0) - ColorIn; + ColorOut = mix(ColorIn, ColorInv, Fac); +} + diff --git a/intern/cycles/kernel/shaders/node_layer_weight.osl b/intern/cycles/kernel/shaders/node_layer_weight.osl new file mode 100644 index 00000000000..3ea57f71786 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_layer_weight.osl @@ -0,0 +1,46 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_fresnel.h" + +shader node_layer_weight( + float Blend = 0.5, + normal Normal = N, + output float Fresnel = 0.0, + output float Facing = 0.0) +{ + float blend = Blend; + + /* Fresnel */ + float eta = max(1.0 - Blend, 1e-5); + eta = backfacing() ? eta : 1.0 / eta; + Fresnel = fresnel_dielectric(I, Normal, eta); + + /* Facing */ + Facing = abs(dot(I, Normal)); + + if (blend != 0.5) { + blend = clamp(blend, 0.0, 1.0 - 1e-5); + blend = (blend < 0.5) ? 2.0 * blend : 0.5 / (1.0 - blend); + + Facing = pow(Facing, blend); + } + + Facing = 1.0 - Facing; +} diff --git a/intern/cycles/kernel/shaders/node_light_falloff.osl b/intern/cycles/kernel/shaders/node_light_falloff.osl new file mode 100644 index 00000000000..7ffa6fe0ffb --- /dev/null +++ b/intern/cycles/kernel/shaders/node_light_falloff.osl @@ -0,0 +1,46 @@ +/* + * Copyright 2012, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_light_falloff( + float Strength = 0.0, + float Smooth = 0.0, + output float Quadratic = 0.0, + output float Linear = 0.0, + output float Constant = 0.0) +{ + float ray_length = 0.0; + float strength = Strength; + getattribute("path:ray_length", ray_length); + + if (Smooth > 0.0) { + float squared = ray_length*ray_length; + strength *= squared / (Smooth + squared); + } + + /* Quadratic */ + Quadratic = strength; + + /* Linear */ + Linear = (strength*ray_length); + + /* Constant */ + Constant = (strength*ray_length*ray_length); +} + diff --git a/intern/cycles/kernel/shaders/node_light_path.osl b/intern/cycles/kernel/shaders/node_light_path.osl new file mode 100644 index 00000000000..9e3f6c7b4a9 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_light_path.osl @@ -0,0 +1,41 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_light_path( + output float IsCameraRay = 0.0, + output float IsShadowRay = 0.0, + output float IsDiffuseRay = 0.0, + output float IsGlossyRay = 0.0, + output float IsSingularRay = 0.0, + output float IsReflectionRay = 0.0, + output float IsTransmissionRay = 0.0, + output float RayLength = 0.0) +{ + IsCameraRay = raytype("camera"); + IsShadowRay = raytype("shadow"); + IsDiffuseRay = raytype("diffuse"); + IsGlossyRay = raytype("glossy"); + IsSingularRay = raytype("singular"); + IsReflectionRay = raytype("reflection"); + IsTransmissionRay = raytype("refraction"); + + getattribute("path:ray_length", RayLength); +} + diff --git a/intern/cycles/kernel/shaders/node_magic_texture.osl b/intern/cycles/kernel/shaders/node_magic_texture.osl new file mode 100644 index 00000000000..e464b83bc9e --- /dev/null +++ b/intern/cycles/kernel/shaders/node_magic_texture.osl @@ -0,0 +1,104 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_texture.h" + +/* Magic */ + +color magic(point p, int n, float distortion) +{ + float dist = distortion; + + float x = sin(( p[0] + p[1] + p[2]) * 5.0); + float y = cos((-p[0] + p[1] - p[2]) * 5.0); + float z = -cos((-p[0] - p[1] + p[2]) * 5.0); + + if (n > 0) { + x *= dist; + y *= dist; + z *= dist; + y = -cos(x - y + z); + y *= dist; + + if (n > 1) { + x = cos(x - y - z); + x *= dist; + + if (n > 2) { + z = sin(-x - y - z); + z *= dist; + + if (n > 3) { + x = -cos(-x + y - z); + x *= dist; + + if (n > 4) { + y = -sin(-x + y + z); + y *= dist; + + if (n > 5) { + y = -cos(-x + y + z); + y *= dist; + + if (n > 6) { + x = cos(x + y + z); + x *= dist; + + if (n > 7) { + z = sin(x + y - z); + z *= dist; + + if (n > 8) { + x = -cos(-x - y + z); + x *= dist; + + if (n > 9) { + y = -sin(x - y + z); + y *= dist; + } + } + } + } + } + } + } + } + } + } + + if (dist != 0.0) { + dist *= 2.0; + x /= dist; + y /= dist; + z /= dist; + } + + return color(0.5 - x, 0.5 - y, 0.5 - z); +} + +shader node_magic_texture( + int Depth = 2, + float Distortion = 5.0, + float Scale = 5.0, + point Vector = P, + output color Color = color(0.0, 0.0, 0.0)) +{ + Color = magic(Vector * Scale, Depth, Distortion); +} + diff --git a/intern/cycles/kernel/shaders/node_mapping.osl b/intern/cycles/kernel/shaders/node_mapping.osl new file mode 100644 index 00000000000..2e720edfc7e --- /dev/null +++ b/intern/cycles/kernel/shaders/node_mapping.osl @@ -0,0 +1,28 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_mapping( + matrix Matrix = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), + point VectorIn = point(0.0, 0.0, 0.0), + output point VectorOut = point(0.0, 0.0, 0.0)) +{ + VectorOut = transform(Matrix, VectorIn); +} + diff --git a/intern/cycles/kernel/shaders/node_math.osl b/intern/cycles/kernel/shaders/node_math.osl new file mode 100644 index 00000000000..24dce898fd2 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_math.osl @@ -0,0 +1,88 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +float safe_divide(float a, float b) +{ + float result; + + if (b == 0.0) + result = 0.0; + else + result = a / b; + + return result; +} + +float safe_log(float a, float b) +{ + if (a < 0.0 || b < 0.0) + return 0.0; + + return log(a) / log(b); +} + +shader node_math( + string type = "Add", + int Clamp = 0, + float Value1 = 0.0, + float Value2 = 0.0, + output float Value = 0.0) +{ + /* OSL asin, acos, pow check for values that could give rise to nan */ + + if (type == "Add") + Value = Value1 + Value2; + if (type == "Subtract") + Value = Value1 - Value2; + if (type == "Multiply") + Value = Value1 * Value2; + if (type == "Divide") + Value = safe_divide(Value1, Value2); + if (type == "Sine") + Value = sin(Value1); + if (type == "Cosine") + Value = cos(Value1); + if (type == "Tangent") + Value = tan(Value1); + if (type == "Arcsine") + Value = asin(Value1); + if (type == "Arccosine") + Value = acos(Value1); + if (type == "Arctangent") + Value = atan(Value1); + if (type == "Power") + Value = pow(Value1, Value2); + if (type == "Logarithm") + Value = safe_log(Value1, Value2); + if (type == "Minimum") + Value = min(Value1, Value2); + if (type == "Maximum") + Value = max(Value1, Value2); + if (type == "Round") + Value = floor(Value1 + 0.5); + if (type == "Less Than") + Value = Value1 < Value2; + if (type == "Greater Than") + Value = Value1 > Value2; + + if (Clamp) + Value = clamp(Value1, 0.0, 1.0); +} + diff --git a/intern/cycles/kernel/shaders/node_mix.osl b/intern/cycles/kernel/shaders/node_mix.osl new file mode 100644 index 00000000000..69e68e5ed15 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_mix.osl @@ -0,0 +1,331 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_color.h" + +color node_mix_blend(float t, color col1, color col2) +{ + return mix(col1, col2, t); +} + +color node_mix_add(float t, color col1, color col2) +{ + return mix(col1, col1 + col2, t); +} + +color node_mix_mul(float t, color col1, color col2) +{ + return mix(col1, col1 * col2, t); +} + +color node_mix_screen(float t, color col1, color col2) +{ + float tm = 1.0 - t; + + return color(1.0) - (color(tm) + t * (color(1.0) - col2)) * (color(1.0) - col1); +} + +color node_mix_overlay(float t, color col1, color col2) +{ + float tm = 1.0 - t; + + color outcol = col1; + + if (outcol[0] < 0.5) + outcol[0] *= tm + 2.0 * t * col2[0]; + else + outcol[0] = 1.0 - (tm + 2.0 * t * (1.0 - col2[0])) * (1.0 - outcol[0]); + + if (outcol[1] < 0.5) + outcol[1] *= tm + 2.0 * t * col2[1]; + else + outcol[1] = 1.0 - (tm + 2.0 * t * (1.0 - col2[1])) * (1.0 - outcol[1]); + + if (outcol[2] < 0.5) + outcol[2] *= tm + 2.0 * t * col2[2]; + else + outcol[2] = 1.0 - (tm + 2.0 * t * (1.0 - col2[2])) * (1.0 - outcol[2]); + + return outcol; +} + +color node_mix_sub(float t, color col1, color col2) +{ + return mix(col1, col1 - col2, t); +} + +color node_mix_div(float t, color col1, color col2) +{ + float tm = 1.0 - t; + + color outcol = col1; + + if (col2[0] != 0.0) outcol[0] = tm * outcol[0] + t * outcol[0] / col2[0]; + if (col2[1] != 0.0) outcol[1] = tm * outcol[1] + t * outcol[1] / col2[1]; + if (col2[2] != 0.0) outcol[2] = tm * outcol[2] + t * outcol[2] / col2[2]; + + return outcol; +} + +color node_mix_diff(float t, color col1, color col2) +{ + return mix(col1, abs(col1 - col2), t); +} + +color node_mix_dark(float t, color col1, color col2) +{ + return min(col1, col2 * t); +} + +color node_mix_light(float t, color col1, color col2) +{ + return max(col1, col2 * t); +} + +color node_mix_dodge(float t, color col1, color col2) +{ + color outcol = col1; + + if (outcol[0] != 0.0) { + float tmp = 1.0 - t * col2[0]; + if (tmp <= 0.0) + outcol[0] = 1.0; + else if ((tmp = outcol[0] / tmp) > 1.0) + outcol[0] = 1.0; + else + outcol[0] = tmp; + } + if (outcol[1] != 0.0) { + float tmp = 1.0 - t * col2[1]; + if (tmp <= 0.0) + outcol[1] = 1.0; + else if ((tmp = outcol[1] / tmp) > 1.0) + outcol[1] = 1.0; + else + outcol[1] = tmp; + } + if (outcol[2] != 0.0) { + float tmp = 1.0 - t * col2[2]; + if (tmp <= 0.0) + outcol[2] = 1.0; + else if ((tmp = outcol[2] / tmp) > 1.0) + outcol[2] = 1.0; + else + outcol[2] = tmp; + } + + return outcol; +} + +color node_mix_burn(float t, color col1, color col2) +{ + float tmp, tm = 1.0 - t; + + color outcol = col1; + + tmp = tm + t * col2[0]; + if (tmp <= 0.0) + outcol[0] = 0.0; + else if ((tmp = (1.0 - (1.0 - outcol[0]) / tmp)) < 0.0) + outcol[0] = 0.0; + else if (tmp > 1.0) + outcol[0] = 1.0; + else + outcol[0] = tmp; + + tmp = tm + t * col2[1]; + if (tmp <= 0.0) + outcol[1] = 0.0; + else if ((tmp = (1.0 - (1.0 - outcol[1]) / tmp)) < 0.0) + outcol[1] = 0.0; + else if (tmp > 1.0) + outcol[1] = 1.0; + else + outcol[1] = tmp; + + tmp = tm + t * col2[2]; + if (tmp <= 0.0) + outcol[2] = 0.0; + else if ((tmp = (1.0 - (1.0 - outcol[2]) / tmp)) < 0.0) + outcol[2] = 0.0; + else if (tmp > 1.0) + outcol[2] = 1.0; + else + outcol[2] = tmp; + + return outcol; +} + +color node_mix_hue(float t, color col1, color col2) +{ + color outcol = col1; + color hsv2 = rgb_to_hsv(col2); + + if (hsv2[1] != 0.0) { + color hsv = rgb_to_hsv(outcol); + hsv[0] = hsv2[0]; + color tmp = hsv_to_rgb(hsv); + + outcol = mix(outcol, tmp, t); + } + + return outcol; +} + +color node_mix_sat(float t, color col1, color col2) +{ + float tm = 1.0 - t; + + color outcol = col1; + + color hsv = rgb_to_hsv(outcol); + + if (hsv[1] != 0.0) { + color hsv2 = rgb_to_hsv(col2); + + hsv[1] = tm * hsv[1] + t * hsv2[1]; + outcol = hsv_to_rgb(hsv); + } + + return outcol; +} + +color node_mix_val(float t, color col1, color col2) +{ + float tm = 1.0 - t; + + color hsv = rgb_to_hsv(col1); + color hsv2 = rgb_to_hsv(col2); + + hsv[2] = tm * hsv[2] + t * hsv2[2]; + + return hsv_to_rgb(hsv); +} + +color node_mix_color(float t, color col1, color col2) +{ + color outcol = col1; + color hsv2 = rgb_to_hsv(col2); + + if (hsv2[1] != 0.0) { + color hsv = rgb_to_hsv(outcol); + hsv[0] = hsv2[0]; + hsv[1] = hsv2[1]; + color tmp = hsv_to_rgb(hsv); + + outcol = mix(outcol, tmp, t); + } + + return outcol; +} + +color node_mix_soft(float t, color col1, color col2) +{ + float tm = 1.0 - t; + + color one = color(1.0); + color scr = one - (one - col2) * (one - col1); + + return tm * col1 + t * ((one - col1) * col2 * col1 + col1 * scr); +} + +color node_mix_linear(float t, color col1, color col2) +{ + color outcol = col1; + + if (col2[0] > 0.5) + outcol[0] = col1[0] + t * (2.0 * (col2[0] - 0.5)); + else + outcol[0] = col1[0] + t * (2.0 * (col2[0]) - 1.0); + + if (col2[1] > 0.5) + outcol[1] = col1[1] + t * (2.0 * (col2[1] - 0.5)); + else + outcol[1] = col1[1] + t * (2.0 * (col2[1]) - 1.0); + + if (col2[2] > 0.5) + outcol[2] = col1[2] + t * (2.0 * (col2[2] - 0.5)); + else + outcol[2] = col1[2] + t * (2.0 * (col2[2]) - 1.0); + + return outcol; +} + +color node_mix_clamp(color col) +{ + color outcol = col; + + outcol[0] = clamp(col[0], 0.0, 1.0); + outcol[1] = clamp(col[2], 0.0, 1.0); + outcol[2] = clamp(col[2], 0.0, 1.0); + + return outcol; +} + +shader node_mix( + string type = "Mix", + int Clamp = 0, + float Fac = 0.5, + color Color1 = color(0.0, 0.0, 0.0), + color Color2 = color(0.0, 0.0, 0.0), + output color Color = color(0.0, 0.0, 0.0)) +{ + float t = clamp(Fac, 0.0, 1.0); + + if (type == "Mix") + Color = node_mix_blend(t, Color1, Color2); + if (type == "Add") + Color = node_mix_add(t, Color1, Color2); + if (type == "Multiply") + Color = node_mix_mul(t, Color1, Color2); + if (type == "Screen") + Color = node_mix_screen(t, Color1, Color2); + if (type == "Overlay") + Color = node_mix_overlay(t, Color1, Color2); + if (type == "Subtract") + Color = node_mix_sub(t, Color1, Color2); + if (type == "Divide") + Color = node_mix_div(t, Color1, Color2); + if (type == "Difference") + Color = node_mix_diff(t, Color1, Color2); + if (type == "Darken") + Color = node_mix_dark(t, Color1, Color2); + if (type == "Lighten") + Color = node_mix_light(t, Color1, Color2); + if (type == "Dodge") + Color = node_mix_dodge(t, Color1, Color2); + if (type == "Burn") + Color = node_mix_burn(t, Color1, Color2); + if (type == "Hue") + Color = node_mix_hue(t, Color1, Color2); + if (type == "Saturation") + Color = node_mix_sat(t, Color1, Color2); + if (type == "Value") + Color = node_mix_val (t, Color1, Color2); + if (type == "Color") + Color = node_mix_color(t, Color1, Color2); + if (type == "Soft Light") + Color = node_mix_soft(t, Color1, Color2); + if (type == "Linear Light") + Color = node_mix_linear(t, Color1, Color2); + + if (Clamp) + Color = node_mix_clamp(Color); +} + diff --git a/intern/cycles/kernel/shaders/node_mix_closure.osl b/intern/cycles/kernel/shaders/node_mix_closure.osl new file mode 100644 index 00000000000..e28dd1fc436 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_mix_closure.osl @@ -0,0 +1,30 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_mix_closure( + float Fac = 0.5, + closure color Closure1 = background(), + closure color Closure2 = background(), + output closure color Closure = background()) +{ + float t = clamp(Fac, 0.0, 1.0); + Closure = (1.0 - t) * Closure1 + t * Closure2; +} + diff --git a/intern/cycles/kernel/shaders/node_musgrave_texture.osl b/intern/cycles/kernel/shaders/node_musgrave_texture.osl new file mode 100644 index 00000000000..71461b8fd79 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_musgrave_texture.osl @@ -0,0 +1,222 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_texture.h" + +/* Musgrave fBm + * + * H: fractal increment parameter + * lacunarity: gap between successive frequencies + * octaves: number of frequencies in the fBm + * + * from "Texturing and Modelling: A procedural approach" + */ + +float noise_musgrave_fBm(point p, string basis, float H, float lacunarity, float octaves) +{ + float rmd; + float value = 0.0; + float pwr = 1.0; + float pwHL = pow(lacunarity, -H); + int i; + + for (i = 0; i < (int)octaves; i++) { + value += noise("perlin", p) * pwr; + pwr *= pwHL; + p *= lacunarity; + } + + rmd = octaves - floor(octaves); + if (rmd != 0.0) + value += rmd * noise("perlin", p) * pwr; + + return value; +} + +/* Musgrave Multifractal + * + * H: highest fractal dimension + * lacunarity: gap between successive frequencies + * octaves: number of frequencies in the fBm + */ + +float noise_musgrave_multi_fractal(point p, string basis, float H, float lacunarity, float octaves) +{ + float rmd; + float value = 1.0; + float pwr = 1.0; + float pwHL = pow(lacunarity, -H); + int i; + + for (i = 0; i < (int)octaves; i++) { + value *= (pwr * noise("perlin", p) + 1.0); + pwr *= pwHL; + p *= lacunarity; + } + + rmd = octaves - floor(octaves); + if (rmd != 0.0) + value *= (rmd * pwr * noise("perlin", p) + 1.0); /* correct? */ + + return value; +} + +/* Musgrave Heterogeneous Terrain + * + * H: fractal dimension of the roughest area + * lacunarity: gap between successive frequencies + * octaves: number of frequencies in the fBm + * offset: raises the terrain from `sea level' + */ + +float noise_musgrave_hetero_terrain(point p, string basis, float H, float lacunarity, float octaves, float offset) +{ + float value, increment, rmd; + float pwHL = pow(lacunarity, -H); + float pwr = pwHL; + int i; + + /* first unscaled octave of function; later octaves are scaled */ + value = offset + noise("perlin", p); + p *= lacunarity; + + for (i = 1; i < (int)octaves; i++) { + increment = (noise("perlin", p) + offset) * pwr * value; + value += increment; + pwr *= pwHL; + p *= lacunarity; + } + + rmd = octaves - floor(octaves); + if (rmd != 0.0) { + increment = (noise("perlin", p) + offset) * pwr * value; + value += rmd * increment; + } + + return value; +} + +/* Hybrid Additive/Multiplicative Multifractal Terrain + * + * H: fractal dimension of the roughest area + * lacunarity: gap between successive frequencies + * octaves: number of frequencies in the fBm + * offset: raises the terrain from `sea level' + */ + +float noise_musgrave_hybrid_multi_fractal(point p, string basis, float H, + float lacunarity, float octaves, float offset, float gain) +{ + float result, signal, weight, rmd; + float pwHL = pow(lacunarity, -H); + float pwr = pwHL; + int i; + + result = noise("perlin", p) + offset; + weight = gain * result; + p *= lacunarity; + + for (i = 1; (weight > 0.001) && (i < (int)octaves); i++) { + if (weight > 1.0) + weight = 1.0; + + signal = (noise("perlin", p) + offset) * pwr; + pwr *= pwHL; + result += weight * signal; + weight *= gain * signal; + p *= lacunarity; + } + + rmd = octaves - floor(octaves); + if (rmd != 0.0) + result += rmd * ((noise("perlin", p) + offset) * pwr); + + return result; +} + +/* Ridged Multifractal Terrain + * + * H: fractal dimension of the roughest area + * lacunarity: gap between successive frequencies + * octaves: number of frequencies in the fBm + * offset: raises the terrain from `sea level' + */ + +float noise_musgrave_ridged_multi_fractal(point p, string basis, float H, + float lacunarity, float octaves, float offset, float gain) +{ + float result, signal, weight; + float pwHL = pow(lacunarity, -H); + float pwr = pwHL; + int i; + + signal = offset - fabs(noise("perlin", p)); + signal *= signal; + result = signal; + weight = 1.0; + + for (i = 1; i < (int)octaves; i++) { + p *= lacunarity; + weight = clamp(signal * gain, 0.0, 1.0); + signal = offset - fabs(noise("perlin", p)); + signal *= signal; + signal *= weight; + result += signal * pwr; + pwr *= pwHL; + } + + return result; +} + +/* Shader */ + +shader node_musgrave_texture( + string Type = "fBM", + float Dimension = 2.0, + float Lacunarity = 1.0, + float Detail = 2.0, + float Offset = 0.0, + float Gain = 1.0, + float Scale = 5.0, + point Vector = P, + output float Fac = 0.0, + output color Color = color(0.0, 0.0, 0.0)) +{ + float dimension = max(Dimension, 1e-5); + float octaves = clamp(Detail, 0.0, 16.0); + float lacunarity = max(Lacunarity, 1e-5); + string Basis = "Perlin"; + float intensity = 1.0; + + point p = Vector * Scale; + + if (Type == "Multifractal") + Fac = intensity * noise_musgrave_multi_fractal(p, Basis, dimension, lacunarity, octaves); + else if (Type == "fBM") + Fac = intensity * noise_musgrave_fBm(p, Basis, dimension, lacunarity, octaves); + else if (Type == "Hybrid Multifractal") + Fac = intensity * noise_musgrave_hybrid_multi_fractal(p, Basis, dimension, lacunarity, octaves, Offset, Gain); + else if (Type == "Ridged Multifractal") + Fac = intensity * noise_musgrave_ridged_multi_fractal(p, Basis, dimension, lacunarity, octaves, Offset, Gain); + else if (Type == "Hetero Terrain") + Fac = intensity * noise_musgrave_hetero_terrain(p, Basis, dimension, lacunarity, octaves, Offset); + + Color = color(Fac, Fac, Fac); +} + diff --git a/intern/cycles/kernel/shaders/node_noise_texture.osl b/intern/cycles/kernel/shaders/node_noise_texture.osl new file mode 100644 index 00000000000..227b2bf8cea --- /dev/null +++ b/intern/cycles/kernel/shaders/node_noise_texture.osl @@ -0,0 +1,56 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_texture.h" + +/* Noise */ + +float noise(point p, string basis, float distortion, float detail, float fac, color Color) +{ + point r; + int hard = 0; + + if (distortion != 0.0) { + r[0] = noise_basis(p + point(13.5), basis) * distortion; + r[1] = noise_basis(p, basis) * distortion; + r[2] = noise_basis(p - point(13.5), basis) * distortion; + + p += r; + } + + fac = noise_turbulence(p, basis, detail, hard); + + Color = color(fac, noise_turbulence(point(p[1], p[0], p[2]), basis, detail, hard), + noise_turbulence(point(p[1], p[2], p[0]), basis, detail, hard)); + + return fac; +} + +shader node_noise_texture( + float Distortion = 0.0, + float Scale = 5.0, + float Detail = 2.0, + point Vector = P, + output float Fac = 0.0, + output color Color = color(0.2, 0.2, 0.2)) +{ + string Basis = "Perlin"; + Fac = noise(Vector * Scale, Basis, Distortion, Detail, Fac, Color); +} + diff --git a/intern/cycles/kernel/shaders/node_normal.osl b/intern/cycles/kernel/shaders/node_normal.osl new file mode 100644 index 00000000000..d5f16acb88c --- /dev/null +++ b/intern/cycles/kernel/shaders/node_normal.osl @@ -0,0 +1,30 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_normal( + normal Direction = normal(0.0, 0.0, 0.0), + normal NormalIn = normal(0.0, 0.0, 0.0), + output normal NormalOut = normal(0.0, 0.0, 0.0), + output float Dot = 1.0) +{ + NormalOut = normalize(Direction); + Dot = dot(NormalOut, NormalIn); +} + diff --git a/intern/cycles/kernel/shaders/node_object_info.osl b/intern/cycles/kernel/shaders/node_object_info.osl new file mode 100644 index 00000000000..c3b1ff29f09 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_object_info.osl @@ -0,0 +1,32 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_object_info( + output point Location = point(0.0, 0.0, 0.0), + output float ObjectIndex = 0.0, + output float MaterialIndex = 0.0, + output float Random = 0.0) +{ + getattribute("object:location", Location); + getattribute("object:index", ObjectIndex); + getattribute("material:index", MaterialIndex); + getattribute("object:random", Random); +} + diff --git a/intern/cycles/kernel/shaders/node_output_displacement.osl b/intern/cycles/kernel/shaders/node_output_displacement.osl new file mode 100644 index 00000000000..5649b879c5b --- /dev/null +++ b/intern/cycles/kernel/shaders/node_output_displacement.osl @@ -0,0 +1,25 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +displacement node_output_displacement(float Displacement = 0.0) +{ + P += N * Displacement * 0.1; /* todo: get rid of this factor */ +} + diff --git a/intern/cycles/kernel/shaders/node_output_surface.osl b/intern/cycles/kernel/shaders/node_output_surface.osl new file mode 100644 index 00000000000..6efaf91121b --- /dev/null +++ b/intern/cycles/kernel/shaders/node_output_surface.osl @@ -0,0 +1,25 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +surface node_output_surface(closure color Surface = background()) +{ + Ci = Surface; +} + diff --git a/intern/cycles/kernel/shaders/node_output_volume.osl b/intern/cycles/kernel/shaders/node_output_volume.osl new file mode 100644 index 00000000000..18094242dc7 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_output_volume.osl @@ -0,0 +1,25 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +volume node_output_volume(closure color Volume = background()) +{ + Ci = Volume; +} + diff --git a/intern/cycles/kernel/shaders/node_particle_info.osl b/intern/cycles/kernel/shaders/node_particle_info.osl new file mode 100644 index 00000000000..5e59ad1a990 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_particle_info.osl @@ -0,0 +1,38 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_particle_info( + output float Index = 0.0, + output float Age = 0.0, + output float Lifetime = 0.0, + output point Location = point(0.0, 0.0, 0.0), + output float Size = 0.0, + output vector Velocity = point(0.0, 0.0, 0.0), + output vector AngularVelocity = point(0.0, 0.0, 0.0)) +{ + getattribute("particle:index", Index); + getattribute("particle:age", Age); + getattribute("particle:lifetime", Lifetime); + getattribute("particle:location", Location); + getattribute("particle:size", Size); + getattribute("particle:velocity", Velocity); + getattribute("particle:angular_velocity", AngularVelocity); +} + diff --git a/intern/cycles/kernel/shaders/node_rgb_ramp.osl b/intern/cycles/kernel/shaders/node_rgb_ramp.osl new file mode 100644 index 00000000000..a128ebbd1cf --- /dev/null +++ b/intern/cycles/kernel/shaders/node_rgb_ramp.osl @@ -0,0 +1,43 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "oslutil.h" + +shader node_rgb_ramp( + color ramp_color[RAMP_TABLE_SIZE] = {0.0}, + float ramp_alpha[RAMP_TABLE_SIZE] = {0.0}, + + float Fac = 0.0, + output color Color = color(0.0, 0.0, 0.0), + output float Alpha = 1.0) +{ + float f = clamp(Fac, 0.0, 1.0) * (RAMP_TABLE_SIZE - 1); + + int i = (int)f; + float t = f - (float)i; + + Color = ramp_color[i]; + Alpha = ramp_alpha[i]; + + if (t > 0.0) { + Color = (1.0 - t) * Color + t * ramp_color[i + 1]; + Alpha = (1.0 - t) * Alpha + t * ramp_alpha[i + 1]; + } +} + diff --git a/intern/cycles/kernel/shaders/node_separate_rgb.osl b/intern/cycles/kernel/shaders/node_separate_rgb.osl new file mode 100644 index 00000000000..b48bd7e59d6 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_separate_rgb.osl @@ -0,0 +1,30 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_separate_rgb( + color Image = color(0.8, 0.8, 0.8), + output float R = 0.0, + output float G = 0.0, + output float B = 0.0) +{ + R = Image[0]; + G = Image[1]; + B = Image[2]; +} diff --git a/intern/cycles/kernel/shaders/node_set_normal.osl b/intern/cycles/kernel/shaders/node_set_normal.osl new file mode 100644 index 00000000000..27a4b2f5b8b --- /dev/null +++ b/intern/cycles/kernel/shaders/node_set_normal.osl @@ -0,0 +1,28 @@ +/* + * Copyright 2012, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +surface node_set_normal( + normal Direction = N, + output normal Normal = N) +{ + N = Direction; + Normal = Direction; +} + diff --git a/intern/cycles/kernel/shaders/node_sky_texture.osl b/intern/cycles/kernel/shaders/node_sky_texture.osl new file mode 100644 index 00000000000..932fb1e2f17 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_sky_texture.osl @@ -0,0 +1,162 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +struct KernelSunSky { + /* sun direction in spherical and cartesian */ + float theta, phi; + vector dir; + + /* perez function parameters */ + float zenith_Y, zenith_x, zenith_y; + float perez_Y[5], perez_x[5], perez_y[5]; +}; + +color xyY_to_xyz(float x, float y, float Y) +{ + float X, Z; + + if (y != 0.0) X = (x / y) * Y; + else X = 0.0; + + if (y != 0.0 && Y != 0.0) Z = ((1.0 - x - y) / y) * Y; + else Z = 0.0; + + return color(X, Y, Z); +} + +color xyz_to_rgb(float x, float y, float z) +{ + return color(3.240479 * x + -1.537150 * y + -0.498535 * z, + -0.969256 * x + 1.875991 * y + 0.041556 * z, + 0.055648 * x + -0.204043 * y + 1.057311 * z); +} + +float sky_angle_between(float thetav, float phiv, float theta, float phi) +{ + float cospsi = sin(thetav) * sin(theta) * cos(phi - phiv) + cos(thetav) * cos(theta); + + if (cospsi > 1.0) + return 0.0; + if (cospsi < -1.0) + return M_PI; + + return acos(cospsi); +} + +vector sky_spherical_coordinates(vector dir) +{ + return vector(acos(dir[2]), atan2(dir[0], dir[1]), 0); +} + +float sky_perez_function(float lam[5], float theta, float gamma) +{ + float ctheta = cos(theta); + float cgamma = cos(gamma); + + return (1.0 + lam[0] * exp(lam[1] / ctheta)) * (1.0 + lam[2] * exp(lam[3] * gamma) + lam[4] * cgamma * cgamma); +} + +color sky_xyz_radiance(KernelSunSky sunsky, vector dir) +{ + /* convert vector to spherical coordinates */ + vector spherical = sky_spherical_coordinates(dir); + float theta = spherical[0]; + float phi = spherical[1]; + + /* angle between sun direction and dir */ + float gamma = sky_angle_between(theta, phi, sunsky.theta, sunsky.phi); + + /* clamp theta to horizon */ + theta = min(theta, M_PI_2 - 0.001); + + /* compute xyY color space values */ + float x = sunsky.zenith_x * sky_perez_function(sunsky.perez_x, theta, gamma); + float y = sunsky.zenith_y * sky_perez_function(sunsky.perez_y, theta, gamma); + float Y = sunsky.zenith_Y * sky_perez_function(sunsky.perez_Y, theta, gamma); + + /* convert to RGB */ + color xyz = xyY_to_xyz(x, y, Y); + return xyz_to_rgb(xyz[0], xyz[1], xyz[2]); +} + +void precompute_sunsky(vector dir, float turbidity, output KernelSunSky sunsky) +{ + vector spherical = sky_spherical_coordinates(dir); + float theta = spherical[0]; + float phi = spherical[1]; + + sunsky.theta = theta; + sunsky.phi = phi; + sunsky.dir = dir; + + float theta2 = theta * theta; + float theta3 = theta * theta * theta; + float T = turbidity; + float T2 = T * T; + + float chi = (4.0 / 9.0 - T / 120.0) * (M_PI - 2.0 * theta); + sunsky.zenith_Y = (4.0453 * T - 4.9710) * tan(chi) - 0.2155 * T + 2.4192; + sunsky.zenith_Y *= 0.06; + + sunsky.zenith_x = + ( 0.00166 * theta3 - 0.00375 * theta2 + 0.00209 * theta) * T2 + + (-0.02903 * theta3 + 0.06377 * theta2 - 0.03202 * theta + 0.00394) * T + + ( 0.11693 * theta3 - 0.21196 * theta2 + 0.06052 * theta + 0.25886); + + sunsky.zenith_y = + ( 0.00275 * theta3 - 0.00610 * theta2 + 0.00317 * theta) * T2 + + (-0.04214 * theta3 + 0.08970 * theta2 - 0.04153 * theta + 0.00516) * T + + ( 0.15346 * theta3 - 0.26756 * theta2 + 0.06670 * theta + 0.26688); + + sunsky.perez_Y[0] = ( 0.1787 * T - 1.4630); + sunsky.perez_Y[1] = (-0.3554 * T + 0.4275); + sunsky.perez_Y[2] = (-0.0227 * T + 5.3251); + sunsky.perez_Y[3] = ( 0.1206 * T - 2.5771); + sunsky.perez_Y[4] = (-0.0670 * T + 0.3703); + + sunsky.perez_x[0] = (-0.0193 * T - 0.2592); + sunsky.perez_x[1] = (-0.0665 * T + 0.0008); + sunsky.perez_x[2] = (-0.0004 * T + 0.2125); + sunsky.perez_x[3] = (-0.0641 * T - 0.8989); + sunsky.perez_x[4] = (-0.0033 * T + 0.0452); + + sunsky.perez_y[0] = (-0.0167 * T - 0.2608); + sunsky.perez_y[1] = (-0.0950 * T + 0.0092); + sunsky.perez_y[2] = (-0.0079 * T + 0.2102); + sunsky.perez_y[3] = (-0.0441 * T - 1.6537); + sunsky.perez_y[4] = (-0.0109 * T + 0.0529); + + sunsky.zenith_Y /= sky_perez_function(sunsky.perez_Y, 0, theta); + sunsky.zenith_x /= sky_perez_function(sunsky.perez_x, 0, theta); + sunsky.zenith_y /= sky_perez_function(sunsky.perez_y, 0, theta); +} + +shader node_sky_texture( + vector Vector = P, + vector sun_direction = vector(0, 0, 1), + float turbidity = 2.2, + output color Color = color(0.0, 0.0, 0.0)) +{ + KernelSunSky sunsky; + + precompute_sunsky(sun_direction, turbidity, sunsky); + Color = sky_xyz_radiance(sunsky, Vector); +} + diff --git a/intern/cycles/kernel/shaders/node_texture.h b/intern/cycles/kernel/shaders/node_texture.h new file mode 100644 index 00000000000..1b3ba8207ab --- /dev/null +++ b/intern/cycles/kernel/shaders/node_texture.h @@ -0,0 +1,270 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +/* Voronoi Distances */ + +float voronoi_distance(string distance_metric, vector d, float e) +{ + float result = 0.0; + + if (distance_metric == "Distance Squared") + result = dot(d, d); + if (distance_metric == "Actual Distance") + result = length(d); + if (distance_metric == "Manhattan") + result = fabs(d[0]) + fabs(d[1]) + fabs(d[2]); + if (distance_metric == "Chebychev") + result = max(fabs(d[0]), max(fabs(d[1]), fabs(d[2]))); + if (distance_metric == "Minkovsky 1/2") + result = sqrt(fabs(d[0])) + sqrt(fabs(d[1])) + sqrt(fabs(d[1])); + if (distance_metric == "Minkovsky 4") + result = sqrt(sqrt(dot(d * d, d * d))); + if (distance_metric == "Minkovsky") + result = pow(pow(fabs(d[0]), e) + pow(fabs(d[1]), e) + pow(fabs(d[2]), e), 1.0 / e); + + return result; +} + +/* Voronoi / Worley like */ + +color cellnoise_color(point p) +{ + float r = cellnoise(p); + float g = cellnoise(point(p[1], p[0], p[2])); + float b = cellnoise(point(p[1], p[2], p[0])); + + return color(r, g, b); +} + +void voronoi(point p, string distance_metric, float e, float da[4], point pa[4]) +{ + /* returns distances in da and point coords in pa */ + int xx, yy, zz, xi, yi, zi; + + xi = (int)floor(p[0]); + yi = (int)floor(p[1]); + zi = (int)floor(p[2]); + + da[0] = 1e10; + da[1] = 1e10; + da[2] = 1e10; + da[3] = 1e10; + + for (xx = xi - 1; xx <= xi + 1; xx++) { + for (yy = yi - 1; yy <= yi + 1; yy++) { + for (zz = zi - 1; zz <= zi + 1; zz++) { + point ip = point(xx, yy, zz); + point vp = (point)cellnoise_color(ip); + point pd = p - (vp + ip); + float d = voronoi_distance(distance_metric, pd, e); + + vp += point(xx, yy, zz); + + if (d < da[0]) { + da[3] = da[2]; + da[2] = da[1]; + da[1] = da[0]; + da[0] = d; + + pa[3] = pa[2]; + pa[2] = pa[1]; + pa[1] = pa[0]; + pa[0] = vp; + } + else if (d < da[1]) { + da[3] = da[2]; + da[2] = da[1]; + da[1] = d; + + pa[3] = pa[2]; + pa[2] = pa[1]; + pa[1] = vp; + } + else if (d < da[2]) { + da[3] = da[2]; + da[2] = d; + + pa[3] = pa[2]; + pa[2] = vp; + } + else if (d < da[3]) { + da[3] = d; + pa[3] = vp; + } + } + } + } +} + +float voronoi_Fn(point p, int n) +{ + float da[4]; + point pa[4]; + + voronoi(p, "Distance Squared", 0, da, pa); + + return da[n]; +} + +float voronoi_FnFn(point p, int n1, int n2) +{ + float da[4]; + point pa[4]; + + voronoi(p, "Distance Squared", 0, da, pa); + + return da[n2] - da[n1]; +} + +float voronoi_F1(point p) { return voronoi_Fn(p, 0); } +float voronoi_F2(point p) { return voronoi_Fn(p, 1); } +float voronoi_F3(point p) { return voronoi_Fn(p, 2); } +float voronoi_F4(point p) { return voronoi_Fn(p, 3); } +float voronoi_F1F2(point p) { return voronoi_FnFn(p, 0, 1); } + +float voronoi_Cr(point p) +{ + /* crackle type pattern, just a scale/clamp of F2-F1 */ + float t = 10.0 * voronoi_F1F2(p); + return (t > 1.0) ? 1.0 : t; +} + +float voronoi_F1S(point p) { return 2.0 * voronoi_F1(p) - 1.0; } +float voronoi_F2S(point p) { return 2.0 * voronoi_F2(p) - 1.0; } +float voronoi_F3S(point p) { return 2.0 * voronoi_F3(p) - 1.0; } +float voronoi_F4S(point p) { return 2.0 * voronoi_F4(p) - 1.0; } +float voronoi_F1F2S(point p) { return 2.0 * voronoi_F1F2(p) - 1.0; } +float voronoi_CrS(point p) { return 2.0 * voronoi_Cr(p) - 1.0; } + +/* Noise Bases */ + +float noise_basis(point p, string basis) +{ + float result = 0.0; + + if (basis == "Perlin") + result = noise(p); /* returns perlin noise in range 0..1 */ + if (basis == "Voronoi F1") + result = voronoi_F1S(p); + if (basis == "Voronoi F2") + result = voronoi_F2S(p); + if (basis == "Voronoi F3") + result = voronoi_F3S(p); + if (basis == "Voronoi F4") + result = voronoi_F4S(p); + if (basis == "Voronoi F2-F1") + result = voronoi_F1F2S(p); + if (basis == "Voronoi Crackle") + result = voronoi_CrS(p); + if (basis == "Cell Noise") + result = cellnoise(p); + + return result; +} + +/* Soft/Hard Noise */ + +float noise_basis_hard(point p, string basis, int hard) +{ + float t = noise_basis(p, basis); + return (hard) ? fabs(2.0 * t - 1.0) : t; +} + +/* Waves */ + +float noise_wave(string wave, float a) +{ + float result = 0.0; + + if (wave == "Sine") { + result = 0.5 + 0.5 * sin(a); + } + else if (wave == "Saw") { + float b = 2 * M_PI; + int n = (int)(a / b); + a -= n * b; + if (a < 0) a += b; + + result = a / b; + } + else if (wave == "Tri") { + float b = 2 * M_PI; + float rmax = 1.0; + + result = rmax - 2.0 * fabs(floor((a * (1.0 / b)) + 0.5) - (a * (1.0 / b))); + } + + return result; +} + +/* Turbulence */ + +float noise_turbulence(point p, string basis, float details, int hard) +{ + float fscale = 1.0; + float amp = 1.0; + float sum = 0.0; + int i, n; + + float octaves = clamp(details, 0.0, 16.0); + n = (int)octaves; + + for (i = 0; i <= n; i++) { + float t = noise_basis(fscale * p, basis); + + if (hard) + t = fabs(2.0 * t - 1.0); + + sum += t * amp; + amp *= 0.5; + fscale *= 2.0; + } + + float rmd = octaves - floor(octaves); + + if (rmd != 0.0) { + float t = noise_basis(fscale * p, basis); + + if (hard) + t = fabs(2.0 * t - 1.0); + + float sum2 = sum + t*amp; + + sum *= ((float)(1 << n) / (float)((1 << (n + 1)) - 1)); + sum2 *= ((float)(1 << (n + 1)) / (float)((1 << (n + 2)) - 1)); + + return (1.0 - rmd)*sum + rmd*sum2; + } + else { + sum *= ((float)(1 << n) / (float)((1 << (n + 1)) - 1)); + return sum; + } +} + +/* Utility */ + +float nonzero(float f, float eps) +{ + float r; + + if (abs(f) < eps) + r = sign(f) * eps; + else + r = f; + + return r; +} + diff --git a/intern/cycles/kernel/shaders/node_texture_coordinate.osl b/intern/cycles/kernel/shaders/node_texture_coordinate.osl new file mode 100644 index 00000000000..791838dfffe --- /dev/null +++ b/intern/cycles/kernel/shaders/node_texture_coordinate.osl @@ -0,0 +1,81 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_texture_coordinate( + normal NormalIn = N, + int is_background = 0, + int from_dupli = 0, + string bump_offset = "center", + + output point Generated = point(0.0, 0.0, 0.0), + output point UV = point(0.0, 0.0, 0.0), + output point Object = point(0.0, 0.0, 0.0), + output point Camera = point(0.0, 0.0, 0.0), + output point Window = point(0.0, 0.0, 0.0), + output normal Normal = normal(0.0, 0.0, 0.0), + output point Reflection = point(0.0, 0.0, 0.0)) +{ + if (is_background) { + Generated = P; + UV = point(0.0, 0.0, 0.0); + Object = P; + point Pcam = transform("camera", "world", point(0, 0, 0)); + Camera = transform("camera", P + Pcam); + Window = transform("NDC", P + Pcam); + Normal = NormalIn; + Reflection = I; + } + else { + if (from_dupli) { + getattribute("geom:dupli_generated", Generated); + getattribute("geom:dupli_uv", UV); + } + else { + getattribute("geom:generated", Generated); + getattribute("geom:uv", UV); + } + + Object = transform("object", P); + Camera = transform("camera", P); + Window = transform("NDC", P); + Normal = transform("world", "object", NormalIn); + Reflection = reflect(I, NormalIn); + } + + if (bump_offset == "dx") { + if (!from_dupli) { + Generated += Dx(Generated); + UV += Dx(UV); + } + Object += Dx(Object); + Camera += Dx(Camera); + Window += Dx(Window); + } + else if (bump_offset == "dy") { + if (!from_dupli) { + Generated += Dy(Generated); + UV += Dy(UV); + } + Object += Dy(Object); + Camera += Dy(Camera); + Window += Dy(Window); + } +} + diff --git a/intern/cycles/kernel/shaders/node_translucent_bsdf.osl b/intern/cycles/kernel/shaders/node_translucent_bsdf.osl new file mode 100644 index 00000000000..e7efe73700c --- /dev/null +++ b/intern/cycles/kernel/shaders/node_translucent_bsdf.osl @@ -0,0 +1,28 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_translucent_bsdf( + color Color = color(0.8, 0.8, 0.8), + normal Normal = N, + output closure color BSDF = diffuse(Normal)) +{ + BSDF = Color * translucent(Normal); +} + diff --git a/intern/cycles/kernel/shaders/node_transparent_bsdf.osl b/intern/cycles/kernel/shaders/node_transparent_bsdf.osl new file mode 100644 index 00000000000..875bce3f16c --- /dev/null +++ b/intern/cycles/kernel/shaders/node_transparent_bsdf.osl @@ -0,0 +1,28 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_transparent_bsdf( + color Color = color(0.8, 0.8, 0.8), + normal Normal = N, + output closure color BSDF = diffuse(Normal)) +{ + BSDF = Color * transparent(); +} + diff --git a/intern/cycles/kernel/shaders/node_value.osl b/intern/cycles/kernel/shaders/node_value.osl new file mode 100644 index 00000000000..bee6f39f2bc --- /dev/null +++ b/intern/cycles/kernel/shaders/node_value.osl @@ -0,0 +1,33 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_value( + float value_value = 0.0, + vector vector_value = vector(0.0, 0.0, 0.0), + color color_value = color(0.0, 0.0, 0.0), + output float Value = 0.0, + output vector Vector = vector(0.0, 0.0, 0.0), + output color Color = color(0.0, 0.0, 0.0)) +{ + Value = value_value; + Vector = vector_value; + Color = color_value; +} + diff --git a/intern/cycles/kernel/shaders/node_vector_math.osl b/intern/cycles/kernel/shaders/node_vector_math.osl new file mode 100644 index 00000000000..f22a6e8441a --- /dev/null +++ b/intern/cycles/kernel/shaders/node_vector_math.osl @@ -0,0 +1,53 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_vector_math( + string type = "Add", + vector Vector1 = vector(0.0, 0.0, 0.0), + vector Vector2 = vector(0.0, 0.0, 0.0), + output float Value = 0.0, + output vector Vector = vector(0.0, 0.0, 0.0)) +{ + if (type == "Add") { + Vector = Vector1 + Vector2; + Value = (abs(Vector[0]) + abs(Vector[1]) + abs(Vector[2])) / 3.0; + } + if (type == "Subtract") { + Vector = Vector1 - Vector2; + Value = (abs(Vector[0]) + abs(Vector[1]) + abs(Vector[2])) / 3.0; + } + if (type == "Average") { + Value = length(Vector1 + Vector2); + Vector = normalize(Vector1 + Vector2); + } + if (type == "Dot Product") { + Value = dot(Vector1, Vector2); + } + if (type == "Cross Product") { + vector c = cross(Vector1, Vector2); + Value = length(c); + Vector = normalize(c); + } + if (type == "Normalize") { + Value = length(Vector1); + Vector = normalize(Vector1); + } +} + diff --git a/intern/cycles/kernel/shaders/node_velvet_bsdf.osl b/intern/cycles/kernel/shaders/node_velvet_bsdf.osl new file mode 100644 index 00000000000..3aa662bdd08 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_velvet_bsdf.osl @@ -0,0 +1,32 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_fresnel.h" + +shader node_velvet_bsdf( + color Color = color(0.8, 0.8, 0.8), + float Sigma = 0.0, + normal Normal = N, + output closure color BSDF = diffuse(Normal)) +{ + float sigma = clamp(Sigma, 0.0, 1.0); + + BSDF = Color * ashikhmin_velvet(Normal, sigma); +} + diff --git a/intern/cycles/kernel/shaders/node_voronoi_texture.osl b/intern/cycles/kernel/shaders/node_voronoi_texture.osl new file mode 100644 index 00000000000..a44df00a267 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_voronoi_texture.osl @@ -0,0 +1,48 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_texture.h" + +/* Voronoi */ + +shader node_voronoi_texture( + string Coloring = "Intensity", + float Scale = 5.0, + point Vector = P, + output float Fac = 0.0, + output color Color = color(0.0, 0.0, 0.0)) +{ + /* compute distance and point coordinate of 4 nearest neighbours */ + float da[4]; + point pa[4]; + + voronoi(Vector * Scale, "Distance Squared", 1.0, da, pa); + + /* Colored output */ + if (Coloring == "Intensity") { + Fac = fabs(da[0]); + Color = color(Fac); + } + else { + Color = cellnoise_color(pa[0]); + Fac = (Color[0] + Color[1] + Color[2]) * (1.0 / 3.0); + + } +} + diff --git a/intern/cycles/kernel/shaders/node_ward_bsdf.osl b/intern/cycles/kernel/shaders/node_ward_bsdf.osl new file mode 100644 index 00000000000..e204be123b8 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_ward_bsdf.osl @@ -0,0 +1,30 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_ward_bsdf( + color Color = color(0.8, 0.8, 0.8), + float RoughnessU = 0.0, + float RoughnessV = 0.0, + normal Normal = N, + output closure color BSDF = diffuse(Normal)) +{ + BSDF = Color * ward(Normal, normalize(dPdu), RoughnessU, RoughnessV); +} + diff --git a/intern/cycles/kernel/shaders/node_wave_texture.osl b/intern/cycles/kernel/shaders/node_wave_texture.osl new file mode 100644 index 00000000000..79b8a8885d1 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_wave_texture.osl @@ -0,0 +1,61 @@ +/* + * Copyright 2012, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" +#include "node_texture.h" + +/* Wave */ + +float wave(point p, float scale, string type, float detail, float distortion, float dscale) +{ + float x = p[0] * scale; + float y = p[1] * scale; + float z = p[2] * scale; + + float result = 0.0; + float n = 0.0; + + if (type == "Bands") { + n = (x + y + z) * 10.0; + } + else if (type == "Rings") { + n = (sqrt(x * x + y * y + z * z) * 20.0); + } + + if (distortion != 0.0) { + n = n + (distortion * noise_turbulence(p * dscale, "Perlin", detail, 0)); + } + result = noise_wave("Sine", n); + + return result; +} + +shader node_wave_texture( + string Type = "Bands", + float Scale = 5.0, + float Distortion = 0.0, + float Detail = 2.0, + float DetailScale = 1.0, + point Vector = P, + output float Fac = 0.0, + output color Color = color (0.0, 0.0, 0.0)) +{ + Fac = wave(Vector, Scale, Type, Detail, Distortion, DetailScale); + Color = color(Fac, Fac, Fac); +} + diff --git a/intern/cycles/kernel/shaders/oslutil.h b/intern/cycles/kernel/shaders/oslutil.h new file mode 100644 index 00000000000..6c91684d9a6 --- /dev/null +++ b/intern/cycles/kernel/shaders/oslutil.h @@ -0,0 +1,94 @@ +///////////////////////////////////////////////////////////////////////////// +// Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al. All Rights Reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// * Neither the name of Sony Pictures Imageworks nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +///////////////////////////////////////////////////////////////////////////// + + +#ifndef CCL_OSLUTIL_H +#define CCL_OSLUTIL_H + +/* NB: must match the value in kernel_types.h */ +#define RAMP_TABLE_SIZE 256 + +// Return wireframe opacity factor [0, 1] given a geometry type in +// ("triangles", "polygons" or "patches"), and a line_width in raster +// or world space depending on the last (raster) boolean argument. +// +float wireframe(string edge_type, float line_width, int raster) +{ + // ray differentials are so big in diffuse context that this function would always return "wire" + if (raytype("path:diffuse")) return 0.0; + + int np = 0; + point p[64]; + float pixelWidth = 1; + + if (edge_type == "triangles") + { + np = 3; + if (!getattribute("geom:trianglevertices", p)) + return 0.0; + } + else if (edge_type == "polygons" || edge_type == "patches") + { + getattribute("geom:numpolyvertices", np); + if (np < 3 || !getattribute("geom:polyvertices", p)) + return 0.0; + } + + if (raster) + { + // Project the derivatives of P to the viewing plane defined + // by I so we have a measure of how big is a pixel at this point + float pixelWidthX = length(Dx(P) - dot(Dx(P), I) * I); + float pixelWidthY = length(Dy(P) - dot(Dy(P), I) * I); + // Take the average of both axis' length + pixelWidth = (pixelWidthX + pixelWidthY) / 2; + } + + // Use half the width as the neighbor face will render the + // other half. And take the square for fast comparison + pixelWidth *= 0.5 * line_width; + pixelWidth *= pixelWidth; + for (int i = 0; i < np; i++) + { + int i2 = i ? i - 1 : np - 1; + vector dir = P - p[i]; + vector edge = p[i] - p[i2]; + vector crs = cross(edge, dir); + // At this point dot(crs, crs) / dot(edge, edge) is + // the square of area / length(edge) == square of the + // distance to the edge. + if (dot(crs, crs) < (dot(edge, edge) * pixelWidth)) + return 1; + } + return 0; +} + +float wireframe(string edge_type, float line_width) { return wireframe(edge_type, line_width, 1); } +float wireframe(string edge_type) { return wireframe(edge_type, 1.0, 1); } +float wireframe() { return wireframe("polygons", 1.0, 1); } + +#endif /* CCL_OSLUTIL_H */ diff --git a/intern/cycles/kernel/shaders/stdosl.h b/intern/cycles/kernel/shaders/stdosl.h new file mode 100644 index 00000000000..4237291c7a4 --- /dev/null +++ b/intern/cycles/kernel/shaders/stdosl.h @@ -0,0 +1,474 @@ +///////////////////////////////////////////////////////////////////////////// +// Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al. All Rights Reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// * Neither the name of Sony Pictures Imageworks nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +///////////////////////////////////////////////////////////////////////////// + + +#ifndef CCL_STDOSL_H +#define CCL_STDOSL_H + + +#ifndef M_PI +#define M_PI 3.1415926535897932 /* pi */ +#define M_PI_2 1.5707963267948966 /* pi/2 */ +#define M_PI_4 0.7853981633974483 /* pi/4 */ +#define M_2_PI 0.6366197723675813 /* 2/pi */ +#define M_2PI 6.2831853071795865 /* 2*pi */ +#define M_4PI 12.566370614359173 /* 4*pi */ +#define M_2_SQRTPI 1.1283791670955126 /* 2/sqrt(pi) */ +#define M_E 2.7182818284590452 /* e (Euler's number) */ +#define M_LN2 0.6931471805599453 /* ln(2) */ +#define M_LN10 2.3025850929940457 /* ln(10) */ +#define M_LOG2E 1.4426950408889634 /* log_2(e) */ +#define M_LOG10E 0.4342944819032518 /* log_10(e) */ +#define M_SQRT2 1.4142135623730950 /* sqrt(2) */ +#define M_SQRT1_2 0.7071067811865475 /* 1/sqrt(2) */ +#endif + + + +// Declaration of built-in functions and closures +#define BUILTIN [[ int builtin = 1 ]] +#define BUILTIN_DERIV [[ int builtin = 1, int deriv = 1 ]] + +#define PERCOMP1(name) \ + normal name (normal x) BUILTIN; \ + vector name (vector x) BUILTIN; \ + point name (point x) BUILTIN; \ + color name (color x) BUILTIN; \ + float name (float x) BUILTIN; + +#define PERCOMP2(name) \ + normal name (normal x, normal y) BUILTIN; \ + vector name (vector x, vector y) BUILTIN; \ + point name (point x, point y) BUILTIN; \ + color name (color x, color y) BUILTIN; \ + float name (float x, float y) BUILTIN; + +#define PERCOMP2F(name) \ + normal name (normal x, float y) BUILTIN; \ + vector name (vector x, float y) BUILTIN; \ + point name (point x, float y) BUILTIN; \ + color name (color x, float y) BUILTIN; \ + float name (float x, float y) BUILTIN; + + +// Basic math +normal degrees (normal x) { return x*(180.0/M_PI); } +vector degrees (vector x) { return x*(180.0/M_PI); } +point degrees (point x) { return x*(180.0/M_PI); } +color degrees (color x) { return x*(180.0/M_PI); } +float degrees (float x) { return x*(180.0/M_PI); } +normal radians (normal x) { return x*(M_PI/180.0); } +vector radians (vector x) { return x*(M_PI/180.0); } +point radians (point x) { return x*(M_PI/180.0); } +color radians (color x) { return x*(M_PI/180.0); } +float radians (float x) { return x*(M_PI/180.0); } +PERCOMP1 (cos) +PERCOMP1 (sin) +PERCOMP1 (tan) +PERCOMP1 (acos) +PERCOMP1 (asin) +PERCOMP1 (atan) +PERCOMP2 (atan2) +PERCOMP1 (cosh) +PERCOMP1 (sinh) +PERCOMP1 (tanh) +PERCOMP2F (pow) +PERCOMP1 (exp) +PERCOMP1 (exp2) +PERCOMP1 (expm1) +PERCOMP1 (log) +point log (point a, float b) { return log(a)/log(b); } +vector log (vector a, float b) { return log(a)/log(b); } +color log (color a, float b) { return log(a)/log(b); } +float log (float a, float b) { return log(a)/log(b); } +PERCOMP1 (log2) +PERCOMP1 (log10) +PERCOMP1 (logb) +PERCOMP1 (sqrt) +PERCOMP1 (inversesqrt) +float hypot (float a, float b) { return sqrt (a*a + b*b); } +float hypot (float a, float b, float c) { return sqrt (a*a + b*b + c*c); } +PERCOMP1 (abs) +int abs (int x) BUILTIN; +PERCOMP1 (fabs) +int fabs (int x) BUILTIN; +PERCOMP1 (sign) +PERCOMP1 (floor) +PERCOMP1 (ceil) +PERCOMP1 (round) +PERCOMP1 (trunc) +PERCOMP2 (fmod) +PERCOMP2F (fmod) +int mod (int a, int b) { return a - b*(int)floor(a/b); } +point mod (point a, point b) { return a - b*floor(a/b); } +vector mod (vector a, vector b) { return a - b*floor(a/b); } +normal mod (normal a, normal b) { return a - b*floor(a/b); } +color mod (color a, color b) { return a - b*floor(a/b); } +point mod (point a, float b) { return a - b*floor(a/b); } +vector mod (vector a, float b) { return a - b*floor(a/b); } +normal mod (normal a, float b) { return a - b*floor(a/b); } +color mod (color a, float b) { return a - b*floor(a/b); } +float mod (float a, float b) { return a - b*floor(a/b); } +PERCOMP2 (min) +PERCOMP2 (max) +normal clamp (normal x, normal minval, normal maxval) { return max(min(x,maxval),minval); } +vector clamp (vector x, vector minval, vector maxval) { return max(min(x,maxval),minval); } +point clamp (point x, point minval, point maxval) { return max(min(x,maxval),minval); } +color clamp (color x, color minval, color maxval) { return max(min(x,maxval),minval); } +float clamp (float x, float minval, float maxval) { return max(min(x,maxval),minval); } +normal mix (normal x, normal y, normal a) { return x*(1-a) + y*a; } +normal mix (normal x, normal y, float a) { return x*(1-a) + y*a; } +vector mix (vector x, vector y, vector a) { return x*(1-a) + y*a; } +vector mix (vector x, vector y, float a) { return x*(1-a) + y*a; } +point mix (point x, point y, point a) { return x*(1-a) + y*a; } +point mix (point x, point y, float a) { return x*(1-a) + y*a; } +color mix (color x, color y, color a) { return x*(1-a) + y*a; } +color mix (color x, color y, float a) { return x*(1-a) + y*a; } +float mix (float x, float y, float a) { return x*(1-a) + y*a; } +int isnan (float x) BUILTIN; +int isinf (float x) BUILTIN; +int isfinite (float x) BUILTIN; +float erf (float x) BUILTIN; +float erfc (float x) BUILTIN; + +// Vector functions + +vector cross (vector a, vector b) BUILTIN; +float dot (vector a, vector b) BUILTIN; +float length (vector v) BUILTIN; +float distance (point a, point b) BUILTIN; +float distance (point a, point b, point q) BUILTIN; +normal normalize (normal v) BUILTIN; +vector normalize (vector v) BUILTIN; +vector faceforward (vector N, vector I, vector Nref) BUILTIN; +vector faceforward (vector N, vector I) BUILTIN; +vector reflect (vector I, vector N) { return I - 2*dot(N,I)*N; } +vector refract (vector I, vector N, float eta) { + float IdotN = dot (I, N); + float k = 1 - eta*eta * (1 - IdotN*IdotN); + return (k < 0) ? vector(0,0,0) : (eta*I - N * (eta*IdotN + sqrt(k))); +} +void fresnel (vector I, normal N, float eta, + output float Kr, output float Kt, + output vector R, output vector T) +{ + float sqr(float x) { return x*x; } + float c = dot(I, N); + if (c < 0) + c = -c; + R = reflect(I, N); + float g = 1.0 / sqr(eta) - 1.0 + c * c; + if (g >= 0.0) { + g = sqrt (g); + float beta = g - c; + float F = (c * (g+c) - 1.0) / (c * beta + 1.0); + F = 0.5 * (1.0 + sqr(F)); + F *= sqr (beta / (g+c)); + Kr = F; + Kt = (1.0 - Kr) * eta*eta; + // OPT: the following recomputes some of the above values, but it + // gives us the same result as if the shader-writer called refract() + T = refract(I, N, eta); + } else { + // total internal reflection + Kr = 1.0; + Kt = 0.0; + T = vector (0,0,0); + } +} + +void fresnel (vector I, normal N, float eta, + output float Kr, output float Kt) +{ + vector R, T; + fresnel(I, N, eta, Kr, Kt, R, T); +} + + +normal transform (matrix Mto, normal p) BUILTIN; +vector transform (matrix Mto, vector p) BUILTIN; +point transform (matrix Mto, point p) BUILTIN; +normal transform (string from, string to, normal p) BUILTIN; +vector transform (string from, string to, vector p) BUILTIN; +point transform (string from, string to, point p) BUILTIN; +normal transform (string to, normal p) { return transform("common",to,p); } +vector transform (string to, vector p) { return transform("common",to,p); } +point transform (string to, point p) { return transform("common",to,p); } + +float transformu (string tounits, float x) BUILTIN; +float transformu (string fromunits, string tounits, float x) BUILTIN; + +point rotate (point p, float angle, point a, point b) +{ + vector axis = normalize (b - a); + float cosang, sinang; + sincos (angle, sinang, cosang); + float cosang1 = 1.0 - cosang; + float x = axis[0], y = axis[1], z = axis[2]; + matrix M = matrix (x * x + (1.0 - x * x) * cosang, + x * y * cosang1 + z * sinang, + x * z * cosang1 - y * sinang, + 0.0, + x * y * cosang1 - z * sinang, + y * y + (1.0 - y * y) * cosang, + y * z * cosang1 + x * sinang, + 0.0, + x * z * cosang1 + y * sinang, + y * z * cosang1 - x * sinang, + z * z + (1.0 - z * z) * cosang, + 0.0, + 0.0, 0.0, 0.0, 1.0); + return transform (M, p-a) + a; +} + + + +// Color functions + +float luminance (color c) BUILTIN; +color blackbody (float temperatureK) BUILTIN; +color wavelength_color (float wavelength_nm) BUILTIN; + + +color transformc (string to, color x) +{ + color rgb_to_hsv (color rgb) { // See Foley & van Dam + float r = rgb[0], g = rgb[1], b = rgb[2]; + float mincomp = min (r, min (g, b)); + float maxcomp = max (r, max (g, b)); + float delta = maxcomp - mincomp; // chroma + float h, s, v; + v = maxcomp; + if (maxcomp > 0) + s = delta / maxcomp; + else s = 0; + if (s <= 0) + h = 0; + else { + if (r >= maxcomp) h = (g-b) / delta; + else if (g >= maxcomp) h = 2 + (b-r) / delta; + else h = 4 + (r-g) / delta; + h /= 6; + if (h < 0) + h += 1; + } + return color (h, s, v); + } + + color rgb_to_hsl (color rgb) { // See Foley & van Dam + // First convert rgb to hsv, then to hsl + float minval = min (rgb[0], min (rgb[1], rgb[2])); + color hsv = rgb_to_hsv (rgb); + float maxval = hsv[2]; // v == maxval + float h = hsv[0], s, l = (minval+maxval) / 2; + if (minval == maxval) + s = 0; // special 'achromatic' case, hue is 0 + else if (l <= 0.5) + s = (maxval - minval) / (maxval + minval); + else + s = (maxval - minval) / (2 - maxval - minval); + return color (h, s, l); + } + + color r; + if (to == "rgb" || to == "RGB") + r = x; + else if (to == "hsv") + r = rgb_to_hsv (x); + else if (to == "hsl") + r = rgb_to_hsl (x); + else if (to == "YIQ") + r = color (dot (vector(0.299, 0.587, 0.114), (vector)x), + dot (vector(0.596, -0.275, -0.321), (vector)x), + dot (vector(0.212, -0.523, 0.311), (vector)x)); + else if (to == "xyz") + r = color (dot (vector(0.412453, 0.357580, 0.180423), (vector)x), + dot (vector(0.212671, 0.715160, 0.072169), (vector)x), + dot (vector(0.019334, 0.119193, 0.950227), (vector)x)); + else { + error ("Unknown color space \"%s\"", to); + r = x; + } + return r; +} + + +color transformc (string from, string to, color x) +{ + color hsv_to_rgb (color c) { // Reference: Foley & van Dam + float h = c[0], s = c[1], v = c[2]; + color r; + if (s < 0.0001) { + r = v; + } else { + h = 6 * (h - floor(h)); // expand to [0..6) + int hi = (int)h; + float f = h - hi; + float p = v * (1-s); + float q = v * (1-s*f); + float t = v * (1-s*(1-f)); + if (hi == 0) r = color (v, t, p); + else if (hi == 1) r = color (q, v, p); + else if (hi == 2) r = color (p, v, t); + else if (hi == 3) r = color (p, q, v); + else if (hi == 4) r = color (t, p, v); + else r = color (v, p, q); + } + return r; + } + + color hsl_to_rgb (color c) { + float h = c[0], s = c[1], l = c[2]; + // Easiest to convert hsl -> hsv, then hsv -> RGB (per Foley & van Dam) + float v = (l <= 0.5) ? (l * (1 + s)) : (l * (1 - s) + s); + color r; + if (v <= 0) { + r = 0; + } else { + float min = 2 * l - v; + s = (v - min) / v; + r = hsv_to_rgb (color (h, s, v)); + } + return r; + } + + color r; + if (from == "rgb" || from == "RGB") + r = x; + else if (from == "hsv") + r = hsv_to_rgb (x); + else if (from == "hsl") + r = hsl_to_rgb (x); + else if (from == "YIQ") + r = color (dot (vector(1, 0.9557, 0.6199), (vector)x), + dot (vector(1, -0.2716, -0.6469), (vector)x), + dot (vector(1, -1.1082, 1.7051), (vector)x)); + else if (from == "xyz") + r = color (dot (vector( 3.240479, -1.537150, -0.498535), (vector)x), + dot (vector(-0.969256, 1.875991, 0.041556), (vector)x), + dot (vector( 0.055648, -0.204043, 1.057311), (vector)x)); + else { + error ("Unknown color space \"%s\"", to); + r = x; + } + return transformc (to, r); +} + + + +// Matrix functions + +float determinant (matrix m) BUILTIN; +matrix transpose (matrix m) BUILTIN; + + + +// Pattern generation + +float step (float edge, float x) BUILTIN; +color step (color edge, color x) BUILTIN; +point step (point edge, point x) BUILTIN; +vector step (vector edge, vector x) BUILTIN; +normal step (normal edge, normal x) BUILTIN; +float smoothstep (float edge0, float edge1, float x) BUILTIN; + + +// Derivatives and area operators + + +// Displacement functions + + +// String functions + +int strlen (string s) BUILTIN; +int startswith (string s, string prefix) BUILTIN; +int endswith (string s, string suffix) BUILTIN; +string substr (string s, int start, int len) BUILTIN; +string substr (string s, int start) { return substr (s, start, strlen(s)); } + +// Define concat in terms of shorter concat +string concat (string a, string b, string c) { + return concat(concat(a,b), c); +} +string concat (string a, string b, string c, string d) { + return concat(concat(a,b,c), d); +} +string concat (string a, string b, string c, string d, string e) { + return concat(concat(a,b,c,d), e); +} +string concat (string a, string b, string c, string d, string e, string f) { + return concat(concat(a,b,c,d,e), f); +} + + +// Texture + + +// Closures + +closure color diffuse(normal N) BUILTIN; +closure color oren_nayar(normal N, float sigma) BUILTIN; +closure color translucent(normal N) BUILTIN; +closure color reflection(normal N) BUILTIN; +closure color refraction(normal N, float eta) BUILTIN; +closure color transparent() BUILTIN; +closure color microfacet_ggx(normal N, float ag) BUILTIN; +closure color microfacet_ggx_refraction(normal N, float ag, float eta) BUILTIN; +closure color microfacet_beckmann(normal N, float ab) BUILTIN; +closure color microfacet_beckmann_refraction(normal N, float ab, float eta) BUILTIN; +closure color ward(normal N, vector T,float ax, float ay) BUILTIN; +closure color ashikhmin_velvet(normal N, float sigma) BUILTIN; +closure color emission() BUILTIN; +closure color background() BUILTIN; +closure color holdout() BUILTIN; + +// Renderer state +int raytype (string typename) BUILTIN; +// the individual 'isFOOray' functions are deprecated +int iscameraray () { return raytype("camera"); } +int isdiffuseray () { return raytype("diffuse"); } +int isglossyray () { return raytype("glossy"); } +int isshadowray () { return raytype("shadow"); } +int getmatrix (string fromspace, string tospace, output matrix M) BUILTIN; +int getmatrix (string fromspace, output matrix M) { + return getmatrix (fromspace, "common", M); +} + + +// Miscellaneous + + + + +#undef BUILTIN +#undef BUILTIN_DERIV +#undef PERCOMP1 +#undef PERCOMP2 +#undef PERCOMP2F + +#endif /* CCL_STDOSL_H */ |