From d6a6fea8c08caf30d8ac295befb69df3953d834b Mon Sep 17 00:00:00 2001 From: Preetham Pemmasani <75422607+Preetham-ai@users.noreply.github.com> Date: Fri, 8 Mar 2024 00:17:07 +0530 Subject: [PATCH 01/10] added OSL language Added most of the basics --- osl.html.markdown | 138 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 osl.html.markdown diff --git a/osl.html.markdown b/osl.html.markdown new file mode 100644 index 0000000000..d78598d3ec --- /dev/null +++ b/osl.html.markdown @@ -0,0 +1,138 @@ +--- +language: osl +filename: learnosl.osl +contributors: + - ["Preetham Pemmasani", "https://github.com/Preetham-ai"] +--- + +OSL (Open Shading Language) is a programming language designed for creating shaders to describe how surfaces react to light in computer graphics. + +[Read more here.](https://raw.githubusercontent.com/imageworks/OpenShadingLanguage/master/src/doc/osl-languagespec.pdf) + +```c + + +// Single-line comments start with // + +/* Multi line comments are preserved. */ + +// Statements can be terminated by ; +dothis(); + +/////////////// +// 1. Basics // +/////////////// + +float num = 3.00; // Scalar floating-point data (numbers) +int num = 3; // Integer data + +// Math works as you would expect +3 + 1; // 4 +74 - 3; // 71 +20 * 2; // 40 +75/3; // 25.0 + +// And modulo division only works with integers +10 % 2; // 0 +31 % 4; // 1 + +// Bitwise operations only works with integers +- 0 // 1 (Negation) +~ 00100011 // 11011100 (bitwise Compliment) +1 << 2; // 4 (shift Left) +12 >> 1; // 3 (shift Right) +1 & 0; // 0 (bitwise AND) +1 | 0; // 1 (bitwise OR) +1 ^ 1; // 0 (bitwise XOR) + +// We also have booleans +true; +false; + +// Negation uses the ~ symbol +~true; // false +~false; // true + +// Relation Operators are defined like: +0 == 0 // true (equal to) +0 != 1 // true (not equal to) +5 < 3 // false (less then) +3 <= 3 // true (less than or equal to) +69 > 69 // false (greater than) +99 >= 52 // true (greater than or equal) + + +// Functions are same as C and C++ +float sum(float a, float b){ + return a+b; +} + +int subtract(int a, int b){ + return a-b; +} + +sum(2,3); // 5 + +// Shader (similar to main function in C) +// The inputs should be initialized +shader multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ + c = a*b; +} + +// Functions can be used inside shader. + +///////////////////////// +// 2. Global Variables // +///////////////////////// + +// Contains information that the renderer knows about the point being shaded +//These variables need not be declared + +point P // Position of the point you are shading +vector I // Incident ray direction from viewing position to shading position +normal N +normal Ng +float u +float v +vector dPdu +vector dPdv +point Ps +float time +float dtime +vector dPdtime +closure color Ci + +///////////////////// +// 4. Control flow // +///////////////////// + +// Conditionals in OSL are just like in C or C++. + +// If/Else +if (5>2){ + x = s; + l = x; +} +else{ + x = s + l; +} + +// 'while' loop +int i = 0; +while (i < 5) { + i += 1; + printf("Current value of i: %d\n", i); +} + +// 'do-while' loop is where test happens after the body of the loop +int i = 0; +do { + printf("Current value of i: %d\n", i); + i += 1; +} while (i < 5); + +// 'for' loop +for (int i = 0; i < 5; i += 1) { + printf("Current value of i: %d\n", i); +} +``` \ No newline at end of file From 822c39b9a05a76dad50f5dd97d23331bfef4cdcf Mon Sep 17 00:00:00 2001 From: Preetham Pemmasani <75422607+Preetham-ai@users.noreply.github.com> Date: Fri, 8 Mar 2024 02:13:28 +0530 Subject: [PATCH 02/10] Update osl.html.markdown --- osl.html.markdown | 170 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 138 insertions(+), 32 deletions(-) diff --git a/osl.html.markdown b/osl.html.markdown index d78598d3ec..557eac3b91 100644 --- a/osl.html.markdown +++ b/osl.html.markdown @@ -17,14 +17,16 @@ OSL (Open Shading Language) is a programming language designed for creating shad /* Multi line comments are preserved. */ // Statements can be terminated by ; -dothis(); +divide(1,2); /////////////// // 1. Basics // /////////////// - -float num = 3.00; // Scalar floating-point data (numbers) -int num = 3; // Integer data + +// Declating variables +float Num = 3.00; // Scalar floating-point data (numbers) +int _num = 3; // Integer data +float c[3] = {0.1, 0.2, 3.14}; // Array // Math works as you would expect 3 + 1; // 4 @@ -37,7 +39,7 @@ int num = 3; // Integer data 31 % 4; // 1 // Bitwise operations only works with integers -- 0 // 1 (Negation) +- 0 // 1 (Unary Negation) ~ 00100011 // 11011100 (bitwise Compliment) 1 << 2; // 4 (shift Left) 12 >> 1; // 3 (shift Right) @@ -49,9 +51,15 @@ int num = 3; // Integer data true; false; -// Negation uses the ~ symbol -~true; // false -~false; // true +// Booleans can't be compared to integers +true = 1 // Error +false = 0 // Error + +// Negation uses the ! symbol +!0; // 1 +!1; // 0 +!2; // 0 +//... and so on // Relation Operators are defined like: 0 == 0 // true (equal to) @@ -73,34 +81,132 @@ int subtract(int a, int b){ sum(2,3); // 5 -// Shader (similar to main function in C) -// The inputs should be initialized -shader multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ +//////////////// +// 2. Shaders // +//////////////// + +// Shaders extend the functionality of a renderer with custom behavior of materials and light +// Shader's syntax is similar to the main function in C +// The inputs and the outputs should be initialized to default types +shader multiply(float a = 0.0, + float b = 0.0, + output float c = 0.0){ c = a*b; } -// Functions can be used inside shader. - -///////////////////////// -// 2. Global Variables // -///////////////////////// +// Double brackets[[ ]] is used to classify metadata of a shader +surface plastic + [[ string help = "Realistic wood shader" ]] +( + color Plastic = color (0.7, 0.5, 0.3) [[ string help = "Base color" ]], + float Reflectivity = 0.5 [[ float min = 0, float max = 1 ]], +){...} + +/////////////////////////////////////// +// Metadata Types +/////////////////////////////////////// + +[[ string label = "IOR" ]] // Display-name in UI of the parameter +[[ string help = "Change Refractive Index" ]] // Info about the parameter +[[ string help = "widget" // Gives widgets to input the parameter + string widget = "number" ]] // input float or int + string widget = "string" ]] // String input + string widget = "boolean" ]] // yes/no (or) 1/0 + string widget = "popup", options = "smooth|rough" ]] // Drop-down list + // enum Drop-down list can also be made + string widget = "mapper", options = "smooth:0|rough:1" ]] + string widget = "filename" ]] // Input files externally + string widget = "null" ]] // null input + +[[ float min = 0.0 ]] // Minimum value of parameter +[[ float max = 0.5 ]] // Maximum value of parameter +[[ int slider = 3.0 // Adds a slider as an input + int slidermin = -1]] // minimum value of the slider + int slidermax = 3]] // maximum value of the slider + int slidercenter = 2]] // origin value of the slider + +[[ float sensitivity = 0.5 ]] // step size for incrementing the parameter +[[ string URL = www.example.com/ ]] // URL of shader's documentation + + + +// There are different types of shaders + +/* Surface shaders determine the basic material properties of a surface and +how it reacts to light */ +// Light shaders are a type of SURFACE shaders used for emissive objects. +// Displacement shaders alter the geometry using position and normals. +// Volume shaders adds a medium like air/smoke/dust into the scene. + +volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ + c = 2*a+b; +} -// Contains information that the renderer knows about the point being shaded -//These variables need not be declared +//////////////////////////////////////// +// 3. Data Types and Global Variables // +//////////////////////////////////////// + +// Data Types + +// 1. int (Integer) +int x = -12; // Minimum size of 32-bits +int new2 = 0x01cf // Hexadecimal can also be specified + +/////////////////////////////////////// +// Order of Evaluation +/////////////////////////////////////// + +// From top to bottom, top has higher precedence +//-----------------------------------// +// Operators // +//-----------------------------------// +// int++, int-- // +// ++ int --int - ~ ! // +// * / % // +// + - // +// << >> // +// < <= > >= // +// == != // +// & // +// ^ // +// | // +// && // +// || // +// ?: // +// = += -= *= /= // +//-----------------------------------// + +// 2. float (Floating-point number) +float A = 2.3; // minimum IEEE 32-bit float +float Z = -4.1e2 + +// Order of evaluation is similar to int. +// Operations like ( ~ ! % << >> ^ | & && || ) aren't available in float + +// 3. color (Red, Green, Blue) +color p = color(0,0,0) // black +color q = color(1) // white ( same as color(1,1,1) ) +color r = color("rgb", 0.23, 0.1, 0.8) // explicitly specify in RGB +color s = color("hsv", 0.23, 0.1, 0.8) // specify in HSV +// HSV stands for (Hue, Saturation, Luminance) +// HSL stands for (Hue, Saturation, Lightness) +// YIQ, XYZ and xyY formats can also be used + +// Global Variables +// Contains info that the renderer knows +// These variables need not be declared point P // Position of the point you are shading vector I // Incident ray direction from viewing position to shading position -normal N -normal Ng -float u -float v -vector dPdu -vector dPdv -point Ps -float time -float dtime -vector dPdtime -closure color Ci +normal N // Normal of the surface at P +normal Ng // Normal of the surface at P irrespective of bump mapping +float u // UV 2D x - parametric coordinate of geometry +float v // UV 2D y - parametric coordinate of geometry +vector dPdu // change of P with respect to u tangent to the surface +vector dPdv // change of P with respect to v tangent to the surface +float time // Current time +float dtime // Time covered +vector dPdtime // change of P with respect to time ///////////////////// // 4. Control flow // @@ -110,11 +216,11 @@ closure color Ci // If/Else if (5>2){ - x = s; - l = x; + int x = s; + int l = x; } else{ - x = s + l; + int x = s + l; } // 'while' loop From a6678fadf264a387094bf64ab155ce022ae73c53 Mon Sep 17 00:00:00 2001 From: Preetham Pemmasani <75422607+Preetham-ai@users.noreply.github.com> Date: Fri, 8 Mar 2024 13:24:13 +0530 Subject: [PATCH 03/10] Updated Datatypes --- osl.html.markdown | 201 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 158 insertions(+), 43 deletions(-) diff --git a/osl.html.markdown b/osl.html.markdown index 557eac3b91..64d2d15dbf 100644 --- a/osl.html.markdown +++ b/osl.html.markdown @@ -25,6 +25,7 @@ divide(1,2); // Declating variables float Num = 3.00; // Scalar floating-point data (numbers) +color Blue; // Initializing a variable int _num = 3; // Integer data float c[3] = {0.1, 0.2, 3.14}; // Array @@ -85,7 +86,7 @@ sum(2,3); // 5 // 2. Shaders // //////////////// -// Shaders extend the functionality of a renderer with custom behavior of materials and light +// Shaders explain the custom behavior of materials and light // Shader's syntax is similar to the main function in C // The inputs and the outputs should be initialized to default types shader multiply(float a = 0.0, @@ -132,7 +133,7 @@ surface plastic // There are different types of shaders -/* Surface shaders determine the basic material properties of a surface and +/* Surface shaders determine the basic material properties of a surface and how it reacts to light */ // Light shaders are a type of SURFACE shaders used for emissive objects. // Displacement shaders alter the geometry using position and normals. @@ -148,49 +149,163 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ // Data Types -// 1. int (Integer) -int x = -12; // Minimum size of 32-bits -int new2 = 0x01cf // Hexadecimal can also be specified +// 1. The void type indicates a function that doesn't return any value + +// 2. int (Integer) + int x = -12; // Minimum size of 32-bits + int new2 = 0x01cf; // Hexadecimal can also be specified + + /////////////////////////////////////// + // Order of Evaluation + /////////////////////////////////////// + + // From top to bottom, top has higher precedence + //-----------------------------------// + // Operators // + //-----------------------------------// + // int++, int-- // + // ++ int --int - ~ ! // + // * / % // + // + - // + // << >> // + // < <= > >= // + // == != // + // & // + // ^ // + // | // + // && // + // || // + // ?: // + // = += -= *= /= // + //-----------------------------------// + +// 3. float (Floating-point number) + float A = 2.3; // minimum IEEE 32-bit float + float Z = -4.1e2; + + // Order of evaluation is similar to int. + // Operations like ( ~ ! % << >> ^ | & && || ) aren't available in float + +// 4. string + // The syntax is similar to C + string new = "Hello World"; + // some Special characters: + /* + '\"'; // double quote + '\n'; // newline character + '\t'; // tab character (left justifies text) + '\v'; // vertical tab + '\\'; // back slash + '\r'; // carriage return + '\b'; // backspace character + */ + + // Strings are concatenated with whitespace + "Hello " "world!"; // "Hello world!" + // concat function can also be used + string concat ("Hello ","World!"); + + // printf function is same as C + int i = 18 + printf("I am %d years old",i); + + // String functions can alse be used + int strlen (string s) // gives the length of the string + int startswith (string s, "the") // gives 1 if string starts with suffix + int endswith (string s, "the") // gives 1 if string ends with suffix + +// 5. color (Red, Green, Blue) + color p = color(0,0,0); // black + color q = color(1); // white ( same as color(1,1,1) ) + color r = color("rgb", 0.23, 0.1, 0.8); // explicitly specify in RGB + color s = color("hsv", 0.23, 0.1, 0.8); // specify in HSV + // HSV stands for (Hue, Saturation, Luminance) + // HSL stands for (Hue, Saturation, Lightness) + // YIQ, XYZ and xyY formats can also be used + // We can also access the indivudual values of (R,G,B) + float Red = p[0]; // access the red component + float Green = p[1]; // access the green component + float Blue = p[2]; // access the blue component + + // They can also be accessed like this + float Red = p.r; // access the red component + float Green = p.g; // access the green component + float Blue = p.b; // access the blue component + + // Math operators work like this with decreasing precedence + color C = (3,2,3) * (1,0,0); // (3, 0, 0) + color D = (1,1,1) * 255; // (255, 255, 255) + color E = (25,5,125) / 5; // (5, 1, 25) + color F = (30,40,50) / (3,4,5); // (10, 10, 10) + color A = (1,2,3) + (1,0,0); // (2, 2, 3) + color B = (1,2,3) - (1,0,0); // (0, 2, 3) + // Operators like ( - == != ) are also used + + // Color Functions + color blackbody (float 1500) // Gives color based on temperature + float luminance (color A) // gives luminance as 0.2126R+0.7152G+0.0722B + color wavelength color (float 700) // Gives color based on wavelength + color transformc ("hsl", "rgb") //converts rgb to hsl and etc + +// 6. point (x,y,z) is position of a point in the 3D space +// 7. vector (x,y,z) has length and direction but no position +// 8. normal (x,y,z) is a special vector perpendicular to a surface + L = point(0.5, 0.5, 0.5); + M = vector(1, 1, 1); + N = normal(0, 0, 1); + + // These 3 types can be assigned to a coordinate system + L = point("object", 0.5, 0.5, 0.5); // relative to local space + M = vector("common", 0.5, 0.5, 0.5); // relative to world space + // There's also ("shader", "world", "camera", "screen", "raster", "NDC") + + float x = L[0]; // access the x-component + float y = L[1]; // access the y-component + float z = L[2]; // access the z-component + + // They can also be accessed like this + float x = M.x; // access the x-component + float y = M.y; // access the y-component + float z = M.z; // access the z-component + + // Operators are the same as color and have the same precedence + +// 9. matrix + // Used for transforming vectors between different coordinate systems. + // They are usually 4x4 (or) 16 floats + matrix zero = 0; // makes a 4x4 zero matrix + matrix ident = 1; // makes a 4x4 identity matrix + matrix m = 7; // Maked a 4x4 scalar matrix with scaling factor of 7 + float x = m[1][1]; // 7 + + // matrices can be constructed using floats in row-major order + matrix new = matrix (m00, m01, m02, m03, m10, m11, m12, m13, + m20, m21, m22, m23, m30, m31, m32, m33); + + +// 10. array + // Arrays in OSL are similar to C + float a[5]; // initialize array a with size 5 + int b[3] = {90,80,70}; // declare array with size 3 + int len = arraylength(b); // 3 + int f = b[1]; // 80 + float anotherarray[3] = b; // arrays can be copied if same type + +// 11. struct (Structures) + // Structures in OSL are similar to C and C++. + struct RGBA { // Defining a structure + color rgb; + float alpha; + }; + + + RGBA col; // Declare a structure + RGBA b = { color(.1,.2,.3), 1 }; // Can also be declared like this + + r.rgb = color (1, 0, 0); // Assign to one field + color c = r.rgb; // Read from a structure field -/////////////////////////////////////// -// Order of Evaluation -/////////////////////////////////////// -// From top to bottom, top has higher precedence -//-----------------------------------// -// Operators // -//-----------------------------------// -// int++, int-- // -// ++ int --int - ~ ! // -// * / % // -// + - // -// << >> // -// < <= > >= // -// == != // -// & // -// ^ // -// | // -// && // -// || // -// ?: // -// = += -= *= /= // -//-----------------------------------// - -// 2. float (Floating-point number) -float A = 2.3; // minimum IEEE 32-bit float -float Z = -4.1e2 - -// Order of evaluation is similar to int. -// Operations like ( ~ ! % << >> ^ | & && || ) aren't available in float - -// 3. color (Red, Green, Blue) -color p = color(0,0,0) // black -color q = color(1) // white ( same as color(1,1,1) ) -color r = color("rgb", 0.23, 0.1, 0.8) // explicitly specify in RGB -color s = color("hsv", 0.23, 0.1, 0.8) // specify in HSV -// HSV stands for (Hue, Saturation, Luminance) -// HSL stands for (Hue, Saturation, Lightness) -// YIQ, XYZ and xyY formats can also be used // Global Variables // Contains info that the renderer knows From ce2db25b7e3b9a464f1bdb2b0165142ce2ef8ba3 Mon Sep 17 00:00:00 2001 From: Preetham Pemmasani <75422607+Preetham-ai@users.noreply.github.com> Date: Sat, 9 Mar 2024 16:33:07 +0530 Subject: [PATCH 04/10] Added some functions --- osl.html.markdown | 51 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/osl.html.markdown b/osl.html.markdown index 64d2d15dbf..7c21d13733 100644 --- a/osl.html.markdown +++ b/osl.html.markdown @@ -268,6 +268,17 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ float y = M.y; // access the y-component float z = M.z; // access the z-component + float a = dot ((1,2,3), (1,2,3)) // Dot Product + vector b = cross ((1,2,3), (1,2,3)) // Cross Product + float l = length(b) // length of vector + vector normalize (vector b) // Normalizes the vector + + float distance (point P0, point P1) //Finds the distance between two points + float distance (point P0, point P1, point Q) /* Perpendicular distance from + Q to line joining P0 and P1 */ + + + // Operators are the same as color and have the same precedence // 9. matrix @@ -282,6 +293,15 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ matrix new = matrix (m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33); + // matrix transformations are easy to implement + matrix a = matrix ("shader", 1); // converted shader to common + matrix m = matrix ("object", "world"); // converted object to world + + // Operations that can be used with decreasing precedence are: + // ( - * / == !=) + + float determinant (matrix M) // returns the determinant of the matrix + float transpose (matrix M) // returns the transpose of the matrix // 10. array // Arrays in OSL are similar to C @@ -301,11 +321,11 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ RGBA col; // Declare a structure RGBA b = { color(.1,.2,.3), 1 }; // Can also be declared like this - + r.rgb = color (1, 0, 0); // Assign to one field color c = r.rgb; // Read from a structure field - +// 12. closure // Global Variables // Contains info that the renderer knows @@ -356,4 +376,31 @@ do { for (int i = 0; i < 5; i += 1) { printf("Current value of i: %d\n", i); } + +///////////////////// +// 5. Functions // +///////////////////// + +// Math Functions + M_PI // π + M_PI_35 // π/35 + m_E // e + M_LN2 // ln 2 + M_SQRT2 // √2 + M_SQRT1_2 // √(1/2) + +// Geometry Functions + + vector reflect (vector I, vector N) + vector faceforward (vector N, vector I) // Tells the direction of vector + vector faceforward (vector N, vector I, vector Nref) // Using a reference + vector reflect (vector I, vector N) // gives Reflection vector along normal + vector refract (vector I, vector N, float IOR) // gives refracted vector + void fresnel (vector I, normal N, float eta, + output float Kr, output float Kt, + output vector R, output vector T); + /* Computes the Reflection (R) and Transmission (T) vectors, along with the + scaling factors for reflected (Kr) and transmitted (Kt) light. */ + + ``` \ No newline at end of file From 12c079a1220d96347d251851150f066355fae4c7 Mon Sep 17 00:00:00 2001 From: Preetham Pemmasani <75422607+Preetham-ai@users.noreply.github.com> Date: Sat, 9 Mar 2024 17:39:25 +0530 Subject: [PATCH 05/10] Update osl.html.markdown --- osl.html.markdown | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/osl.html.markdown b/osl.html.markdown index 7c21d13733..1959b2515a 100644 --- a/osl.html.markdown +++ b/osl.html.markdown @@ -326,6 +326,10 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ color c = r.rgb; // Read from a structure field // 12. closure + // Used to store data that aren't considered during Shader's execution + // Can't be manipulated, nor read + // A null closure can always be assigned + closure color(255,255,255); // Global Variables // Contains info that the renderer knows @@ -402,5 +406,18 @@ for (int i = 0; i < 5; i += 1) { /* Computes the Reflection (R) and Transmission (T) vectors, along with the scaling factors for reflected (Kr) and transmitted (Kt) light. */ + // Rotating a point along a given axis + point rotate (point Q, float angle, vector axis) + // Rotating a point along a line made by 2 points + point rotate (point Q, float angle, point P0, point P1) + // Transforming units is easy + float transformu ("cm", float x) // converts to cm + float transformu ("cm", "m", float y) // converts cm to m + +// Displacement Functions + void displace (float 5); // Displace by 5 amp units + void bump (float 10); // Bump by 10 amp units + + ``` \ No newline at end of file From 2551785ef65f0a915e3910d30798c261d52bd4fc Mon Sep 17 00:00:00 2001 From: Preetham Pemmasani <75422607+Preetham-ai@users.noreply.github.com> Date: Sun, 10 Mar 2024 13:05:21 +0530 Subject: [PATCH 06/10] Added Pattern Generations, Calculus, Texture and Light functions --- osl.html.markdown | 118 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 108 insertions(+), 10 deletions(-) diff --git a/osl.html.markdown b/osl.html.markdown index 1959b2515a..34019ada09 100644 --- a/osl.html.markdown +++ b/osl.html.markdown @@ -5,7 +5,7 @@ contributors: - ["Preetham Pemmasani", "https://github.com/Preetham-ai"] --- -OSL (Open Shading Language) is a programming language designed for creating shaders to describe how surfaces react to light in computer graphics. +OSL (Open Shading Language) is a programming language designed by Sony for Arnold Renderer used for creating shaders. [Read more here.](https://raw.githubusercontent.com/imageworks/OpenShadingLanguage/master/src/doc/osl-languagespec.pdf) @@ -24,9 +24,9 @@ divide(1,2); /////////////// // Declating variables -float Num = 3.00; // Scalar floating-point data (numbers) color Blue; // Initializing a variable -int _num = 3; // Integer data +int _num = 3; +float Num = 3.00; float c[3] = {0.1, 0.2, 3.14}; // Array // Math works as you would expect @@ -53,8 +53,8 @@ true; false; // Booleans can't be compared to integers -true = 1 // Error -false = 0 // Error +true == 1 // Error +false == 0 // Error // Negation uses the ! symbol !0; // 1 @@ -329,7 +329,37 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ // Used to store data that aren't considered during Shader's execution // Can't be manipulated, nor read // A null closure can always be assigned - closure color(255,255,255); + + closure color oren nayar diffuse_bsdf(normal N, color alb, float roughness) + closure color burley diffuse_bsdf (normal N, color alb, float roughness) + closure color dielectric_bsdf (normal N, vector U, color reflection tint, + color transmission tint, float roughness x, float roughness y, + float ior, string distribution) + closure color conductor_bsdf (normal N, vector U, float roughness x, + float roughness y, color ior, color extinction, string distribution) + closure color generalized schlick_bsdf (normal N, vector U, + color reflection tint, color transmission tint, + float roughness x, float roughness y, color f0, color f90, + float exponent, string distribution) + closure color translucent_bsdf (normal N, color albedo) + closure color transparent_bsdf () + closure color subsurface bssrdf () + closure color sheen_bsdf (normal N, color albedo, float roughness) + + // Also exist for Volumetrics + + closure color anisotropic_vdf(color albedo, color extinction, + float anisotropy) + closure color medium vdf (color albedo, float transmission depth, + color transmission color, float anisotropy, float ior, int priority) + + closure color uniform edf (color emittance) // Emission closure + closure color holdout () // Hides objects beneath it + + // BSDFs can be layered using this closure + closure color layer (closure color top, closure color base) + + // Global Variables // Contains info that the renderer knows @@ -385,7 +415,7 @@ for (int i = 0; i < 5; i += 1) { // 5. Functions // ///////////////////// -// Math Functions +// Math Constants M_PI // π M_PI_35 // π/35 m_E // e @@ -410,6 +440,8 @@ for (int i = 0; i < 5; i += 1) { point rotate (point Q, float angle, vector axis) // Rotating a point along a line made by 2 points point rotate (point Q, float angle, point P0, point P1) + vector calculatenormal (point p) // Calculates normal of surface at point p + // Transforming units is easy float transformu ("cm", float x) // converts to cm float transformu ("cm", "m", float y) // converts cm to m @@ -418,6 +450,72 @@ for (int i = 0; i < 5; i += 1) { void displace (float 5); // Displace by 5 amp units void bump (float 10); // Bump by 10 amp units - - -``` \ No newline at end of file +// Pattern Generations + type step (type edge, type x) // Returns 1 if x ≥ edge, else 0 + type linearstep (type edge0, type edge1, type x) /* Linearstep Returns 0 if + x ≤ edge0, and 1 if x ≥ edge1, with linear interpolation */ + type linearstep (type edge0, type edge1, type x) /* smoothstep Returns 0 if + x ≤ edge0, and 1 if x ≥ edge1, with Hermite interpolation */ + + type noise (type noise (string noisetype, float u, float v, ...)) // noise + /* some noises are ("perlin", "snoise", "uperlin", "noise", "cell", "hash" + "simplex", "usimplex", "gabor", etc) */ + + type pnoise (string noisetype, float u, float uperiod) // periodic noise + type pnoise (string noisetype, float u, float v, float uperiod, float vperiod) + type snoise (float u, float v) + type psnoise (float u, float v, float uperiod, float vperiod) + type cellnoise (float u, float v) + type hashnoise (float u, float v) + // The type may be of float, color, point, vector, or normal. + int hash (float u, float v) + + // Splines can be created in two ways + type spline (string basis, float x, int nknots, type y[]) + type spline (string basis, float x, type y0, type y1, ... type y(n−1)) + /* basis is the type of interpolation ranges from "catmull-rom", "bezier", + "bspline", "hermite", "linear", or "constant" */ + + // InverseSplines also exist + float splineinverse (string basis, float v, float y0, ... float yn−1) + float splineinverse (string basis, float v, int nknots, float y[]) + +// Calculus Operators + type Dx (type f), Dy (type f), Dz (type f) + // The type can be of float, vector and color + // Returns partial derivative of f with respect to x, y and z + vector Dx (point a), Dy (point a), Dz (point a) + + float area (point p) // gives the surface area at the position p + + float filterwidth (float x) // gives the changes of x in adjacent samples + +// Texture Functions + // lookup for a texture at coordinates (x,y) + type texture (string filename, float x, float y, ...params...) + // 3D lookup for a texture at coordinates (x,y) + type texture3d (string filename, point p, ...params...) + // parameters are ("blur","width","wrap","fill","alpha","interp", ...) + +// Light Functions + + float surfacearea () // Returns the surface area of area light covers + int backfacing () // Outputs 1 if the normals are backfaced, else 0 + int raytype (string name) // returns 1 if the ray is a particular raytype + int trace (point pos, vector dir, ...) // Trace ray from pos in a direction + // Parameters are ("mindist", "mindist", "shade", "traceset") + +// Lookup Functions + // Regex can be used to search inside strings + int regex search (string subject, string re) // search for subject in re + int regex match (string subject, string re) + + // Dictionaries exist either as a string or a file + int dict find (string dictionary, string query) +``` +### Further reading + +* [Blender Docs for OSL](https://docs.blender.org/manual/en/latest/render/shader_nodes/osl.html) +* [C4D Docs for OSL](https://docs.otoy.com/cinema4d//OpenShadingLanguageOSL.html) +* Open Shading Language on [Github](https://github.com/AcademySoftwareFoundation/OpenShadingLanguage) +* [Official OSL Documentation](https://open-shading-language.readthedocs.io/en/main/) \ No newline at end of file From c4ad68182670dc942e7aa5889371da66aaf87c02 Mon Sep 17 00:00:00 2001 From: Preetham Pemmasani <75422607+Preetham-ai@users.noreply.github.com> Date: Sun, 10 Mar 2024 17:58:14 +0530 Subject: [PATCH 07/10] Final --- osl.html.markdown | 333 +++++++++++++++++++++++++++++++--------------- 1 file changed, 226 insertions(+), 107 deletions(-) diff --git a/osl.html.markdown b/osl.html.markdown index 34019ada09..d8cf69231c 100644 --- a/osl.html.markdown +++ b/osl.html.markdown @@ -160,28 +160,28 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ /////////////////////////////////////// // From top to bottom, top has higher precedence - //-----------------------------------// - // Operators // - //-----------------------------------// - // int++, int-- // - // ++ int --int - ~ ! // - // * / % // - // + - // - // << >> // - // < <= > >= // - // == != // - // & // - // ^ // - // | // - // && // - // || // - // ?: // - // = += -= *= /= // - //-----------------------------------// + //--------------------------// + // Operators // + //--------------------------// + // int++, int-- // + // ++ int --int - ~ ! // + // * / % // + // + - // + // << >> // + // < <= > >= // + // == != // + // & // + // ^ // + // | // + // && // + // || // + // ?: // + // = += -= *= /= // + //--------------------------// // 3. float (Floating-point number) float A = 2.3; // minimum IEEE 32-bit float - float Z = -4.1e2; + float Z = -4.1e2; // Z = -4.1 * 10^2 // Order of evaluation is similar to int. // Operations like ( ~ ! % << >> ^ | & && || ) aren't available in float @@ -203,16 +203,21 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ // Strings are concatenated with whitespace "Hello " "world!"; // "Hello world!" // concat function can also be used - string concat ("Hello ","World!"); + string concat ("Hello ","World!"); // "Hello world!" // printf function is same as C - int i = 18 - printf("I am %d years old",i); + int i = 18; + printf("I am %d years old",i); // I am 18 years old // String functions can alse be used - int strlen (string s) // gives the length of the string - int startswith (string s, "the") // gives 1 if string starts with suffix - int endswith (string s, "the") // gives 1 if string ends with suffix + int strlen (string s); // gives the length of the string + int len = strlen("Hello, World!"); // len = 13 + + int startswith (string s, "the"); // gives 1 if string starts with suffix + int starts = startswith("The quick brown fox", "The"); // starts = 1 + + int endswith (string s, "the"); // gives 1 if string ends with suffix + int ends = endswith("The quick brown fox", "fox"); // ends will be 1 // 5. color (Red, Green, Blue) color p = color(0,0,0); // black @@ -250,36 +255,36 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ // 6. point (x,y,z) is position of a point in the 3D space // 7. vector (x,y,z) has length and direction but no position // 8. normal (x,y,z) is a special vector perpendicular to a surface - L = point(0.5, 0.5, 0.5); - M = vector(1, 1, 1); + // These Operators are the same as color and have the same precedence + L = point(0.5, 0.6, 0.7); + M = vector(30, 100, 70); N = normal(0, 0, 1); // These 3 types can be assigned to a coordinate system - L = point("object", 0.5, 0.5, 0.5); // relative to local space - M = vector("common", 0.5, 0.5, 0.5); // relative to world space + L = point("object", 0.5, 0.6, 0.7); // relative to local space + M = vector("common", 30, 100, 70); // relative to world space // There's also ("shader", "world", "camera", "screen", "raster", "NDC") - float x = L[0]; // access the x-component - float y = L[1]; // access the y-component - float z = L[2]; // access the z-component + float x = L[0]; // 0.5 (access the x-component) + float y = L[1]; // 0.6 (access the y-component) + float z = L[2]; // 0.7 (access the z-component) // They can also be accessed like this - float x = M.x; // access the x-component - float y = M.y; // access the y-component - float z = M.z; // access the z-component - - float a = dot ((1,2,3), (1,2,3)) // Dot Product - vector b = cross ((1,2,3), (1,2,3)) // Cross Product - float l = length(b) // length of vector - vector normalize (vector b) // Normalizes the vector + float x = M.x; // 30 (access the x-component) + float y = M.y; // 100 (access the y-component) + float z = M.z; // 70 (access the z-component) - float distance (point P0, point P1) //Finds the distance between two points - float distance (point P0, point P1, point Q) /* Perpendicular distance from - Q to line joining P0 and P1 */ + float a = dot ((1,2,3), (1,2,3)); // 14 (Dot Product) + vector b = cross ((1,2,3), (1,2,3)); // (0,0,0) (Cross Product) + float l = length(L); // 1.085 (length of vector) + vector normalize (vector L); // (0.460, 0.552, 0.644) Normalizes the vector + float distance (point P0, point P1); // Finds distance between two points + float len = distance(point(1, 2, 3), point(4, 5, 6)); //5.196 + float distance (point P0, point P1, point Q); /* Perpendicular distance + from Q to line joining P0 and P1 */ - // Operators are the same as color and have the same precedence // 9. matrix // Used for transforming vectors between different coordinate systems. @@ -326,38 +331,57 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ color c = r.rgb; // Read from a structure field // 12. closure - // Used to store data that aren't considered during Shader's execution - // Can't be manipulated, nor read - // A null closure can always be assigned - - closure color oren nayar diffuse_bsdf(normal N, color alb, float roughness) - closure color burley diffuse_bsdf (normal N, color alb, float roughness) - closure color dielectric_bsdf (normal N, vector U, color reflection tint, - color transmission tint, float roughness x, float roughness y, - float ior, string distribution) - closure color conductor_bsdf (normal N, vector U, float roughness x, - float roughness y, color ior, color extinction, string distribution) - closure color generalized schlick_bsdf (normal N, vector U, - color reflection tint, color transmission tint, - float roughness x, float roughness y, color f0, color f90, - float exponent, string distribution) - closure color translucent_bsdf (normal N, color albedo) - closure color transparent_bsdf () - closure color subsurface bssrdf () - closure color sheen_bsdf (normal N, color albedo, float roughness) - - // Also exist for Volumetrics + // Closure is used to store data that aren't considered during Shader's execution. + // It cannot be manipulated or read. + // A null closure can always be assigned. + // OSL currently only supports color as their closure. + + // A few examples of closures are: + + // Diffuse BSDF closures: + closure color oren_nayar_diffuse_bsdf(normal N, color alb, float roughness) + closure color burley_diffuse_bsdf(normal N, color alb, float roughness); + + // Dielectric BSDF closure: + closure color dielectric_bsdf(normal N, vector U, color reflection_tint, + color transmission_tint, float roughness_x, float roughness_y, + float ior, string distribution); + + // Conductor BSDF closure: + closure color conductor_bsdf(normal N, vector U, float roughness_x, + float roughness_y, color ior, color extinction, string distribution); + + // Generalized Schlick BSDF closure: + closure color generalized_schlick_bsdf(normal N, vector U, + color reflection_tint, color transmission_tint, + float roughness_x, float roughness_y, color f0, color f90, + float exponent, string distribution); + + // Translucent BSDF closure: + closure color translucent_bsdf(normal N, color albedo); + + // Transparent BSDF closure: + closure color transparent_bsdf(); + // Subsurface BSSRDF closure: + closure color subsurface_bssrdf(); + + // Sheen BSDF closure: + closure color sheen_bsdf(normal N, color albedo, float roughness); + + // Anisotropic VDF closure: (Volumetric) closure color anisotropic_vdf(color albedo, color extinction, - float anisotropy) - closure color medium vdf (color albedo, float transmission depth, - color transmission color, float anisotropy, float ior, int priority) + float anisotropy); + + // Medium VDF closure: (Volumetric) + closure color medium_vdf(color albedo, float transmission_depth, + color transmission_color, float anisotropy, float ior, int priority); - closure color uniform edf (color emittance) // Emission closure - closure color holdout () // Hides objects beneath it + closure color uniform edf(color emittance); // Emission closure + closure color holdout(); // Holdout Hides objects beneath it // BSDFs can be layered using this closure - closure color layer (closure color top, closure color base) + closure color layer (closure color top, closure color base); @@ -424,12 +448,11 @@ for (int i = 0; i < 5; i += 1) { M_SQRT1_2 // √(1/2) // Geometry Functions - - vector reflect (vector I, vector N) - vector faceforward (vector N, vector I) // Tells the direction of vector - vector faceforward (vector N, vector I, vector Nref) // Using a reference - vector reflect (vector I, vector N) // gives Reflection vector along normal - vector refract (vector I, vector N, float IOR) // gives refracted vector + + vector faceforward (vector N, vector I); // Tells the direction of vector + vector faceforward (vector N, vector I, vector Nref); // Using a reference + vector reflect (vector I, vector N); // gives Reflection vector along normal + vector refract (vector I, vector N, float IOR); // gives refracted vector void fresnel (vector I, normal N, float eta, output float Kr, output float Kt, output vector R, output vector T); @@ -437,9 +460,16 @@ for (int i = 0; i < 5; i += 1) { scaling factors for reflected (Kr) and transmitted (Kt) light. */ // Rotating a point along a given axis - point rotate (point Q, float angle, vector axis) + point rotate (point Q, float angle, vector axis); + point rotated_point = rotate(point(1, 0, 0), radians(90), vector(0, 0, 1)); + // (0, 1, 0) + // Rotating a point along a line made by 2 points - point rotate (point Q, float angle, point P0, point P1) + point rotate (point Q, float angle, point P0, point P1); + point rotated_point = rotate(point(1, 0, 0), radians(45), point(0, 0, 0), + point(1, 1, 0)); + // (0.707, 0.707, 0) + vector calculatenormal (point p) // Calculates normal of surface at point p // Transforming units is easy @@ -451,34 +481,123 @@ for (int i = 0; i < 5; i += 1) { void bump (float 10); // Bump by 10 amp units // Pattern Generations - type step (type edge, type x) // Returns 1 if x ≥ edge, else 0 - type linearstep (type edge0, type edge1, type x) /* Linearstep Returns 0 if - x ≤ edge0, and 1 if x ≥ edge1, with linear interpolation */ - type linearstep (type edge0, type edge1, type x) /* smoothstep Returns 0 if - x ≤ edge0, and 1 if x ≥ edge1, with Hermite interpolation */ + // Noise Generation - type noise (type noise (string noisetype, float u, float v, ...)) // noise + type noise (type noise (string noisetype, float u, float v, ...)); // noise + type noise (string noisetype, point p,...); // point instead of coordinates /* some noises are ("perlin", "snoise", "uperlin", "noise", "cell", "hash" "simplex", "usimplex", "gabor", etc) */ + + // Noise Names + + // 1. Perlin Noise (perlin, snoise): + // Creates smooth, swirling noise often used for textures. + // Range: [-1, 1] (signed) + color cloud_texture = noise("perlin", P); + + // 2. Simplex Noise (simplex, usimplex): + // Similar to Perlin noise but faster. + // Range: [-1, 1] (signed) for simplex, [0, 1] (unsigned) for usimplex + float bump_amount = 0.2 * noise("simplex", P * 5.0); + + // 3. UPerlin Noise (uperlin, noise): + // Similar to peril + // Range: [0, 1] (unsigned) + color new_texture = noise("uperlin", P); + + // 4. Cell Noise (cell): + // Creates a blocky, cellular and constant values within each unit block + // Range: [0, 1] (unsigned) + color new_texture = noise("cell", P); + + // 5. Hash Noise (hash): + // Generates random, uncorrelated values at each point. + // Range: [0, 1] (unsigned) + color new_texture = noise("hash", P); + + // Gabor Noise (gabor) + // Gabor Noise is advanced version of Perin noies and gives more control + // Range: [-1, 1] (signed) + // Gabor Noise Parameters + + // Anisotropic (default: 0) + // Controls anisotropy: + // 0: Isotropic (equal frequency in all directions) + // 1: Anisotropic with user-defined direction vector (defaults to (1,0,0)) + /* 2: Hybrid mode, anisotropic along direction vector but radially isotropic + perpendicularly. */ + + // Direction (default: (1,0,0)) + // Specifies the direction of anisotropy (used only if anisotropic is 1). + + // bandwidth (default: 1.0) + // Controls the frequency range of the noise. + + // impulses (default: 16) + // Controls the number of impulses used per cell, affecting detail level. + + // do_filter (default: 1) + // Enables/disables antialiasing (filtering). Filtering is generally recommended. + + result = noise( + "gabor", + P, + "anisotropic", anisotropic, + "direction", direction, + "bandwidth", bandwidth, + "impulses", impulses, + "do_filter", do_filter + ); + + // Specific noises can also be used instead of passing them as types + // pnoise is periodic noise + float n1 = pnoise("perlin", 0.5, 1.0); + // 2D periodic noise with Gabor type + float n2 = pnoise("gabor", 0.2, 0.3, 2.0, 3.0); + // 2D non-periodic simplex noise + float n3 = snoise(0.1, 0.7); + // 2D periodic simplex noise + type psnoise (float u, float v, float uperiod, float vperiod); + float n4 = psnoise(0.4, 0.6, 0.5, 0.25); + // 2D cellular noise + float n5 = cellnoise(0.2, 0.8); + // 2D hash noise + int n6 = hash(0.7, 0.3); + + // Step Functions are used to compare input and threshold - type pnoise (string noisetype, float u, float uperiod) // periodic noise - type pnoise (string noisetype, float u, float v, float uperiod, float vperiod) - type snoise (float u, float v) - type psnoise (float u, float v, float uperiod, float vperiod) - type cellnoise (float u, float v) - type hashnoise (float u, float v) // The type may be of float, color, point, vector, or normal. - int hash (float u, float v) - - // Splines can be created in two ways - type spline (string basis, float x, int nknots, type y[]) - type spline (string basis, float x, type y0, type y1, ... type y(n−1)) + type step (type edge, type x); // Returns 1 if x ≥ edge, else 0 + color checker = step(0.5, P); // P is a point on the surface + /* Pixels with P values below 0.5 will be black, those above or equal will + be white */ + float visibility = step(10, distance(P, light_position)); + // Light is fully visible within 10 units, completely invisible beyond + + type linearstep (type edge0, type edge1, type x); /* Linearstep Returns 0 + if x ≤ edge0, and 1 if x ≥ edge1, with linear interpolation */ + color gradient = linearstep(0, 1, P); + // P is a point on the surface between 0 and 1 + // Color will graduate smoothly from black to white as P moves from 0 to 1 + float fade = linearstep(0.85, 1, N.z); // N.z is the z-component + // Object edges with normals close to vertical (N.z near 1) will fade out + + type smoothstep (type edge0, type edge1, type x); /* smoothstep Returns 0 + if x ≤ edge0, and 1 if x ≥ edge1, with Hermite interpolation */ + float soft_mask = smoothstep(0.2, 0.8, noise(P)); /* noise(P) is a noisy + value between 0 and 1. soft_mask will vary smoothly between 0 and 1 based + on noise(P), with a smoother curve than linearstep */ + + // Splines are smooth curves based on a set of control points + // Splines are created in two ways + type spline (string basis, float x, int nknots, type y[]); + type spline (string basis, float x, type y0, type y1, ... type yn−1); /* basis is the type of interpolation ranges from "catmull-rom", "bezier", "bspline", "hermite", "linear", or "constant" */ // InverseSplines also exist - float splineinverse (string basis, float v, float y0, ... float yn−1) - float splineinverse (string basis, float v, int nknots, float y[]) + float splineinverse (string basis, float v, float y0, ... float yn−1); + float splineinverse (string basis, float v, int nknots, float y[]); // Calculus Operators type Dx (type f), Dy (type f), Dz (type f) @@ -492,26 +611,26 @@ for (int i = 0; i < 5; i += 1) { // Texture Functions // lookup for a texture at coordinates (x,y) - type texture (string filename, float x, float y, ...params...) + type texture (string filename, float x, float y, ...params...); // 3D lookup for a texture at coordinates (x,y) - type texture3d (string filename, point p, ...params...) + type texture3d (string filename, point p, ...params...); // parameters are ("blur","width","wrap","fill","alpha","interp", ...) // Light Functions - float surfacearea () // Returns the surface area of area light covers - int backfacing () // Outputs 1 if the normals are backfaced, else 0 - int raytype (string name) // returns 1 if the ray is a particular raytype - int trace (point pos, vector dir, ...) // Trace ray from pos in a direction + float surfacearea (); // Returns the surface area of area light covers + int backfacing (); // Outputs 1 if the normals are backfaced, else 0 + int raytype (string name); // returns 1 if the ray is a particular raytype + int trace (point pos, vector dir, ...); // Trace ray from pos in a direction // Parameters are ("mindist", "mindist", "shade", "traceset") // Lookup Functions // Regex can be used to search inside strings - int regex search (string subject, string re) // search for subject in re - int regex match (string subject, string re) + int regex search (string subject, string re); // search for subject in re + int regex match (string subject, string re); // Dictionaries exist either as a string or a file - int dict find (string dictionary, string query) + int dict find (string dictionary, string query); ``` ### Further reading From eaf3e590e541468e338001592b269cb9d0dc8111 Mon Sep 17 00:00:00 2001 From: Preetham Pemmasani <75422607+Preetham-ai@users.noreply.github.com> Date: Sun, 10 Mar 2024 18:07:17 +0530 Subject: [PATCH 08/10] fixed 80 words --- osl.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/osl.html.markdown b/osl.html.markdown index d8cf69231c..c91932c758 100644 --- a/osl.html.markdown +++ b/osl.html.markdown @@ -331,7 +331,7 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ color c = r.rgb; // Read from a structure field // 12. closure - // Closure is used to store data that aren't considered during Shader's execution. + // Closure is used to store data that aren't considered when it executes. // It cannot be manipulated or read. // A null closure can always be assigned. // OSL currently only supports color as their closure. @@ -451,7 +451,7 @@ for (int i = 0; i < 5; i += 1) { vector faceforward (vector N, vector I); // Tells the direction of vector vector faceforward (vector N, vector I, vector Nref); // Using a reference - vector reflect (vector I, vector N); // gives Reflection vector along normal + vector reflect (vector I, vector N); // gives Reflected vector along normal vector refract (vector I, vector N, float IOR); // gives refracted vector void fresnel (vector I, normal N, float eta, output float Kr, output float Kt, @@ -524,7 +524,7 @@ for (int i = 0; i < 5; i += 1) { // Controls anisotropy: // 0: Isotropic (equal frequency in all directions) // 1: Anisotropic with user-defined direction vector (defaults to (1,0,0)) - /* 2: Hybrid mode, anisotropic along direction vector but radially isotropic + /* 2: Hybrid mode,anisotropic along direction vector but radially isotropic perpendicularly. */ // Direction (default: (1,0,0)) @@ -537,7 +537,7 @@ for (int i = 0; i < 5; i += 1) { // Controls the number of impulses used per cell, affecting detail level. // do_filter (default: 1) - // Enables/disables antialiasing (filtering). Filtering is generally recommended. + // Enables/disables antialiasing (filtering). result = noise( "gabor", @@ -621,7 +621,7 @@ for (int i = 0; i < 5; i += 1) { float surfacearea (); // Returns the surface area of area light covers int backfacing (); // Outputs 1 if the normals are backfaced, else 0 int raytype (string name); // returns 1 if the ray is a particular raytype - int trace (point pos, vector dir, ...); // Trace ray from pos in a direction + int trace (point pos, vector dir, ...) // Trace ray from pos in a direction // Parameters are ("mindist", "mindist", "shade", "traceset") // Lookup Functions From 8ed44a9deadba0f6c1bac50cf1b2614742732254 Mon Sep 17 00:00:00 2001 From: Preetham Pemmasani <75422607+Preetham-ai@users.noreply.github.com> Date: Sun, 10 Mar 2024 21:48:27 +0530 Subject: [PATCH 09/10] Added Examples --- osl.html.markdown | 245 +++++++++++++++++++++++++++++++++------------- 1 file changed, 178 insertions(+), 67 deletions(-) diff --git a/osl.html.markdown b/osl.html.markdown index c91932c758..af5f83bcc3 100644 --- a/osl.html.markdown +++ b/osl.html.markdown @@ -213,14 +213,14 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ int strlen (string s); // gives the length of the string int len = strlen("Hello, World!"); // len = 13 - int startswith (string s, "the"); // gives 1 if string starts with suffix + // startswith returns 1 if string starts with prefix, else returns 0 int starts = startswith("The quick brown fox", "The"); // starts = 1 - int endswith (string s, "the"); // gives 1 if string ends with suffix + // endswith returns 1 if string starts with suffix, else returns 0 int ends = endswith("The quick brown fox", "fox"); // ends will be 1 // 5. color (Red, Green, Blue) - color p = color(0,0,0); // black + color p = color(0,1,2); // black color q = color(1); // white ( same as color(1,1,1) ) color r = color("rgb", 0.23, 0.1, 0.8); // explicitly specify in RGB color s = color("hsv", 0.23, 0.1, 0.8); // specify in HSV @@ -228,14 +228,14 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ // HSL stands for (Hue, Saturation, Lightness) // YIQ, XYZ and xyY formats can also be used // We can also access the indivudual values of (R,G,B) - float Red = p[0]; // access the red component - float Green = p[1]; // access the green component - float Blue = p[2]; // access the blue component + float Red = p[0]; // 0 (access the red component) + float Green = p[1]; // 1 (access the green component) + float Blue = p[2]; // 2 (access the blue component) // They can also be accessed like this - float Red = p.r; // access the red component - float Green = p.g; // access the green component - float Blue = p.b; // access the blue component + float Red = p.r; // 0 (access the red component) + float Green = p.g; // 1 (access the green component) + float Blue = p.b; // 2 (access the blue component) // Math operators work like this with decreasing precedence color C = (3,2,3) * (1,0,0); // (3, 0, 0) @@ -247,10 +247,11 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ // Operators like ( - == != ) are also used // Color Functions - color blackbody (float 1500) // Gives color based on temperature - float luminance (color A) // gives luminance as 0.2126R+0.7152G+0.0722B - color wavelength color (float 700) // Gives color based on wavelength - color transformc ("hsl", "rgb") //converts rgb to hsl and etc + color blackbody (1500) // Gives color based on temperature (in Kelvin) + float luminance (0.5, 0.3, 0.8) // 0.37 gives luminance cd/m^2 + // Luminance is calculated by 0.2126R+0.7152G+0.0722B + color wavelength color (700) // (1, 0, 0) Gives color based on wavelength + color transformc ("hsl", "rgb") // converts one system to another // 6. point (x,y,z) is position of a point in the 3D space // 7. vector (x,y,z) has length and direction but no position @@ -279,24 +280,45 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ float l = length(L); // 1.085 (length of vector) vector normalize (vector L); // (0.460, 0.552, 0.644) Normalizes the vector + point p0 = point(1, 2, 3); + point p1 = point(4, 5, 6); + point Q = point(0, 0, 0); - float distance (point P0, point P1); // Finds distance between two points - float len = distance(point(1, 2, 3), point(4, 5, 6)); //5.196 - float distance (point P0, point P1, point Q); /* Perpendicular distance - from Q to line joining P0 and P1 */ + // Finding distance between two points + float len = distance(point(1, 2, 3), point(4, 5, 6)); // 5.196 + // Perpendicular distance from Q to line joining P0 and P1 + float distance (point P0, point P1, point Q); // 2.45 // 9. matrix // Used for transforming vectors between different coordinate systems. // They are usually 4x4 (or) 16 floats matrix zero = 0; // makes a 4x4 zero matrix + /* 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0 */ + matrix ident = 1; // makes a 4x4 identity matrix + /* 1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0 */ + matrix m = 7; // Maked a 4x4 scalar matrix with scaling factor of 7 + /* 7.0, 0.0, 0.0, 0.0, + 0.0, 7.0, 0.0, 0.0, + 0.0, 0.0, 7.0, 0.0, + 0.0, 0.0, 0.0, 7.0 */ + float x = m[1][1]; // 7 // matrices can be constructed using floats in row-major order - matrix new = matrix (m00, m01, m02, m03, m10, m11, m12, m13, - m20, m21, m22, m23, m30, m31, m32, m33); + // matrices are usually 4x4 with 16 elements + matrix myMatrix = matrix(1.0, 0.0, 0.0, 0.0, // Row 1 + 0.0, 2.0, 0.0, 0.0, // Row 2 + 0.0, 0.0, 3.0, 0.0, // Row 3 + 0.0, 0.0, 0.0, 4.0); // Row 4 // matrix transformations are easy to implement matrix a = matrix ("shader", 1); // converted shader to common @@ -305,8 +327,12 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ // Operations that can be used with decreasing precedence are: // ( - * / == !=) - float determinant (matrix M) // returns the determinant of the matrix + float determinant (matrix M) // 24 (returns the determinant of the matrix) float transpose (matrix M) // returns the transpose of the matrix + /* 1.0, 0.0, 0.0, 0.0, + 0.0, 2.0, 0.0, 0.0, + 0.0, 0.0, 3.0, 0.0, + 0.0, 0.0, 0.0, 4.0 */ // 10. array // Arrays in OSL are similar to C @@ -324,8 +350,8 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ }; - RGBA col; // Declare a structure - RGBA b = { color(.1,.2,.3), 1 }; // Can also be declared like this + RGBA col; // Declaring a structure + RGBA b = { color(0.1, 0.2, 0.3), 1 }; // Can also be declared like this r.rgb = color (1, 0, 0); // Assign to one field color c = r.rgb; // Read from a structure field @@ -448,29 +474,51 @@ for (int i = 0; i < 5; i += 1) { M_SQRT1_2 // √(1/2) // Geometry Functions - - vector faceforward (vector N, vector I); // Tells the direction of vector - vector faceforward (vector N, vector I, vector Nref); // Using a reference - vector reflect (vector I, vector N); // gives Reflected vector along normal - vector refract (vector I, vector N, float IOR); // gives refracted vector - void fresnel (vector I, normal N, float eta, - output float Kr, output float Kt, - output vector R, output vector T); - /* Computes the Reflection (R) and Transmission (T) vectors, along with the - scaling factors for reflected (Kr) and transmitted (Kt) light. */ - - // Rotating a point along a given axis - point rotate (point Q, float angle, vector axis); - point rotated_point = rotate(point(1, 0, 0), radians(90), vector(0, 0, 1)); - // (0, 1, 0) + vector N = vector(0.1, 1, 0.2); // Normal vector + vector I = vector(-0.5, 0.2, 0.8); // Incident vector + + // Faceforward tells the direction of vector + vector facing_dir = faceforward(N, I); // facing_dir = (-0.5, 0.2, 0.8) + + // faceforward with three arguments + vector ref = vector(0.3, -0.7, 0.6); // Reference normal + facing_dir = faceforward(N, I, ref); // facing_dir = (0.5, -0.2, -0.8) + + // reflect gives the reflected vector along normal + vector refl = reflect(I, N); // refl = (-0.7, -0.4, 1.4)\ + + // refract gives the refracted vector along normal + float ior = 1.5; // Index of refraction + vector refr = refract(I, N, ior); // refr = (-0.25861, 0.32814, 0.96143) + + /* Fresnel computes the Reflection (R) and Transmission (T) vectors, along + with the scaling factors for reflected (Kr) and transmitted (Kt) light. */ + float Kr, Kt; + vector R, T; + fresnel(I, N, ior, Kr, Kt, R, T); +/* Kr = 0.03958, Kt = 0.96042 + R = (-0.19278, -0.07711, 0.33854) + T = (-0.25861, 0.32814, 0.96143) */ + + // Rotating a point along a given axis + point Q = point(1, 0, 0); + float angle = radians(90); // 90 degrees + vector axis = vector(0, 0, 1); + point rotated_point = rotate(Q, angle, axis); + // rotated_point = point(0, 1, 0) // Rotating a point along a line made by 2 points - point rotate (point Q, float angle, point P0, point P1); - point rotated_point = rotate(point(1, 0, 0), radians(45), point(0, 0, 0), - point(1, 1, 0)); - // (0.707, 0.707, 0) - - vector calculatenormal (point p) // Calculates normal of surface at point p + point P0 = point(0, 0, 0); + point P1 = point(1, 1, 0); + angle = radians(45); // 45 degrees + Q = point(1, 0, 0); + rotated_point = rotate(Q, angle, P0, P1); + // rotated_point = point(0.707107, 0.707107, 0) + + // Calculating normal of surface at point p + point p1 = point(1, 0, 0); // Point on the sphere of radius 1 + vector normal1 = calculatenormal(p1); + // normal1 = vector(1, 0, 0) // Transforming units is easy float transformu ("cm", float x) // converts to cm @@ -480,8 +528,8 @@ for (int i = 0; i < 5; i += 1) { void displace (float 5); // Displace by 5 amp units void bump (float 10); // Bump by 10 amp units -// Pattern Generations - // Noise Generation + +// Noise Generation type noise (type noise (string noisetype, float u, float v, ...)); // noise type noise (string noisetype, point p,...); // point instead of coordinates @@ -564,6 +612,7 @@ for (int i = 0; i < 5; i += 1) { // 2D hash noise int n6 = hash(0.7, 0.3); +// Step Function // Step Functions are used to compare input and threshold // The type may be of float, color, point, vector, or normal. @@ -576,7 +625,7 @@ for (int i = 0; i < 5; i += 1) { type linearstep (type edge0, type edge1, type x); /* Linearstep Returns 0 if x ≤ edge0, and 1 if x ≥ edge1, with linear interpolation */ - color gradient = linearstep(0, 1, P); + color gradient = linearstep(0, 1, P); // P is a point on the surface between 0 and 1 // Color will graduate smoothly from black to white as P moves from 0 to 1 float fade = linearstep(0.85, 1, N.z); // N.z is the z-component @@ -588,22 +637,83 @@ for (int i = 0; i < 5; i += 1) { value between 0 and 1. soft_mask will vary smoothly between 0 and 1 based on noise(P), with a smoother curve than linearstep */ +// Splines // Splines are smooth curves based on a set of control points - // Splines are created in two ways - type spline (string basis, float x, int nknots, type y[]); - type spline (string basis, float x, type y0, type y1, ... type yn−1); - /* basis is the type of interpolation ranges from "catmull-rom", "bezier", + + /* The type of interpolation ranges from "catmull-rom", "bezier", "bspline", "hermite", "linear", or "constant" */ + // Spline with knot vector + float[] knots = {0, 0, 0, 0.25, 0.5, 0.75, 1, 1, 1}; + point[] controls = {point(0),point(1, 2, 1),point(2, 1, 2),point(3, 3, 1)}; + spline curve1 = spline("bezier", 0.5, len(knots), controls); + // curve1 is a Bezier spline evaluated at u = 0.5 + + // Spline with control points + spline curve2 = spline("catmull-rom", 0.25, point(0, 0, 0), point(1, 2, 1), + point(2, 1, 2), point(3, 3, 1)); + // curve2 is a Catmull-Rom spline evaluated at u = 0.25 + + // Constant spline with a single float value + float value = 10; + u = 0.1; + spline curve5 = spline("constant", u, value); + // curve5 is a constant spline with value 10 evaluated at u = 0.1 + + // Hermite spline with point and vector controls + point q0 = point(0, 0, 0), q1 = point(3, 3, 3); + vector t0 = vector(1, 0, 0), t1 = vector(-1, 1, 1); + u = 0.75; + spline curve3 = spline("hermite", u, q0, t0, q1, t1); + // curve3 is a Hermite spline evaluated at u = 0.75 + + // Linear spline with float controls + float f0 = 0, f1 = 1, f2 = 2, f3 = 3; + u = 0.4; + spline curve4 = spline("linear", u, f0, f1, f2, f3); + // curve4 is a linear spline evaluated at u = 0.4 + // InverseSplines also exist - float splineinverse (string basis, float v, float y0, ... float yn−1); - float splineinverse (string basis, float v, int nknots, float y[]); + + // Inverse spline with control values + float y0 = 0, y1 = 1, y2 = 2, y3 = 3; + float v = 1.5; + float u1 = splineinverse("linear", v, y0, y1, y2, y3); + // u1 = 0.5 (linear interpolation between y1 and y2) + + // Inverse spline with knot vector + float[] knots = {0, 0, 0, 0.25, 0.5, 0.75, 1, 1, 1}; + float[] values = {0, 1, 4, 9}; + v = 6; + float u2 = splineinverse("bezier", v, len(knots), values); + // u2 = 0.75 (Bezier spline inverse evaluated at v = 6) + + // Inverse spline with constant value + v = 10; + float u3 = splineinverse("constant", v, 10); + // u3 = 0 (since the constant spline always returns 10) + + // Inverse spline with periodic values + float y4 = 0, y5 = 1, y6 = 0; + v = 0.5; + float u4 = splineinverse("periodic", v, y4, y5, y6); + // u4 = 0.75 (periodic spline inverse evaluated at v = 0.5) + + // Calculus Operators - type Dx (type f), Dy (type f), Dz (type f) - // The type can be of float, vector and color - // Returns partial derivative of f with respect to x, y and z - vector Dx (point a), Dy (point a), Dz (point a) + // Partial derivative of f with respect to x, y and z using Dx, Dy, Dz + float a = 3.14; + float dx = Dx(a); // partial derivative of a with respect to x + + point p = point(1.0, 2.0, 3.0); + vector dp_dx = Dx(p); // partial derivative of p with respect to x + + vector dv_dy = Dy(N); // partial derivative of normal with respect to y + + color c = color(0.5, 0.2, 0.8); + color dc_dz = Dz(c); // partial derivative of c with respect to z + float area (point p) // gives the surface area at the position p @@ -611,26 +721,27 @@ for (int i = 0; i < 5; i += 1) { // Texture Functions // lookup for a texture at coordinates (x,y) - type texture (string filename, float x, float y, ...params...); + color col1 = texture("texture.png", 0.5, 0.2); + // Lookup color at (0.5, 0.2) in texture.png + // 3D lookup for a texture at coordinates (x,y) - type texture3d (string filename, point p, ...params...); + color col3 = texture3d("texture3d.vdb", point(0.25, 0.5, 0.75)); + // parameters are ("blur","width","wrap","fill","alpha","interp", ...) + color col2 = texture("texture.png",1.0,0.75,"blur",0.1,"wrap", "periodic"); + // Lookup color at (1.0, 0.75) with blur 0.1 and periodic wrap mode // Light Functions float surfacearea (); // Returns the surface area of area light covers int backfacing (); // Outputs 1 if the normals are backfaced, else 0 - int raytype (string name); // returns 1 if the ray is a particular raytype - int trace (point pos, vector dir, ...) // Trace ray from pos in a direction - // Parameters are ("mindist", "mindist", "shade", "traceset") + int raytype (string name); // returns 1 if the ray is a particular raytype -// Lookup Functions - // Regex can be used to search inside strings - int regex search (string subject, string re); // search for subject in re - int regex match (string subject, string re); + // Tracing a ray from a position in a direction + point pos = point(0, 0, 0); // Starting position of the ray + vector dir = vector(0, 0, 1); // Direction of the ray + int hit = trace(pos, dir); // returns 1 if it hits, else 0 - // Dictionaries exist either as a string or a file - int dict find (string dictionary, string query); ``` ### Further reading From 9d49bf38d73b23dcd658b4b805b52fefac4fe437 Mon Sep 17 00:00:00 2001 From: Preetham Pemmasani <75422607+ppmpreetham@users.noreply.github.com> Date: Sat, 31 Aug 2024 16:57:06 +0530 Subject: [PATCH 10/10] fixed username and conciseness Fixed my github username and added a few changes for conciseness --- osl.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/osl.html.markdown b/osl.html.markdown index af5f83bcc3..46f295deba 100644 --- a/osl.html.markdown +++ b/osl.html.markdown @@ -2,7 +2,7 @@ language: osl filename: learnosl.osl contributors: - - ["Preetham Pemmasani", "https://github.com/Preetham-ai"] + - ["Preetham Pemmasani", "https://github.com/ppmpreetham"] --- OSL (Open Shading Language) is a programming language designed by Sony for Arnold Renderer used for creating shaders. @@ -311,6 +311,7 @@ volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){ 0.0, 0.0, 7.0, 0.0, 0.0, 0.0, 0.0, 7.0 */ + // Multi-Dimensional Arrays can be accessed like this float x = m[1][1]; // 7 // matrices can be constructed using floats in row-major order @@ -433,7 +434,7 @@ vector dPdtime // change of P with respect to time // Conditionals in OSL are just like in C or C++. -// If/Else +// If / Else if (5>2){ int x = s; int l = x; @@ -674,6 +675,7 @@ for (int i = 0; i < 5; i += 1) { // curve4 is a linear spline evaluated at u = 0.4 // InverseSplines also exist + // Parameter to Point Mapping and Texture Mapping and Sampling // Inverse spline with control values float y0 = 0, y1 = 1, y2 = 2, y3 = 3;