OSL (Open Shading Language) is a programming language designed by Sony for Arnold Renderer used for creating shaders.
1// Single-line comments start with //
2
3/* Multi line comments are preserved. */
4
5// Statements can be terminated by ;
6divide(1,2);
7
8///////////////
9// 1. Basics //
10///////////////
11
12// Declating variables
13color Blue; // Initializing a variable
14int _num = 3;
15float Num = 3.00;
16float c[3] = {0.1, 0.2, 3.14}; // Array
17
18// Math works as you would expect
193 + 1; // 4
2074 - 3; // 71
2120 * 2; // 40
2275/3; // 25.0
23
24// And modulo division only works with integers
2510 % 2; // 0
2631 % 4; // 1
27
28// Bitwise operations only works with integers
29- 0 // 1 (Unary Negation)
30~ 00100011 // 11011100 (bitwise Compliment)
311 << 2; // 4 (shift Left)
3212 >> 1; // 3 (shift Right)
331 & 0; // 0 (bitwise AND)
341 | 0; // 1 (bitwise OR)
351 ^ 1; // 0 (bitwise XOR)
36
37// We also have booleans
38true;
39false;
40
41// Booleans can't be compared to integers
42true == 1 // Error
43false == 0 // Error
44
45// Negation uses the ! symbol
46!0; // 1
47!1; // 0
48!2; // 0
49//... and so on
50
51// Relation Operators are defined like:
520 == 0 // true (equal to)
530 != 1 // true (not equal to)
545 < 3 // false (less then)
553 <= 3 // true (less than or equal to)
5669 > 69 // false (greater than)
5799 >= 52 // true (greater than or equal)
58
59
60// Functions are same as C and C++
61float sum(float a, float b){
62 return a+b;
63}
64
65int subtract(int a, int b){
66 return a-b;
67}
68
69sum(2,3); // 5
70
71////////////////
72// 2. Shaders //
73////////////////
74
75// Shaders explain the custom behavior of materials and light
76// Shader's syntax is similar to the main function in C
77// The inputs and the outputs should be initialized to default types
78shader multiply(float a = 0.0,
79 float b = 0.0,
80 output float c = 0.0){
81 c = a*b;
82}
83
84// Double brackets[[ ]] is used to classify metadata of a shader
85surface plastic
86 [[ string help = "Realistic wood shader" ]]
87(
88 color Plastic = color (0.7, 0.5, 0.3) [[ string help = "Base color" ]],
89 float Reflectivity = 0.5 [[ float min = 0, float max = 1 ]],
90){...}
91
92///////////////////////////////////////
93// Metadata Types
94///////////////////////////////////////
95
96[[ string label = "IOR" ]] // Display-name in UI of the parameter
97[[ string help = "Change Refractive Index" ]] // Info about the parameter
98[[ string help = "widget" // Gives widgets to input the parameter
99 string widget = "number" ]] // input float or int
100 string widget = "string" ]] // String input
101 string widget = "boolean" ]] // yes/no (or) 1/0
102 string widget = "popup", options = "smooth|rough" ]] // Drop-down list
103 // enum Drop-down list can also be made
104 string widget = "mapper", options = "smooth:0|rough:1" ]]
105 string widget = "filename" ]] // Input files externally
106 string widget = "null" ]] // null input
107
108[[ float min = 0.0 ]] // Minimum value of parameter
109[[ float max = 0.5 ]] // Maximum value of parameter
110[[ int slider = 3.0 // Adds a slider as an input
111 int slidermin = -1]] // minimum value of the slider
112 int slidermax = 3]] // maximum value of the slider
113 int slidercenter = 2]] // origin value of the slider
114
115[[ float sensitivity = 0.5 ]] // step size for incrementing the parameter
116[[ string URL = www.example.com/ ]] // URL of shader's documentation
117
118
119
120// There are different types of shaders
121
122/* Surface shaders determine the basic material properties of a surface and
123how it reacts to light */
124// Light shaders are a type of SURFACE shaders used for emissive objects.
125// Displacement shaders alter the geometry using position and normals.
126// Volume shaders adds a medium like air/smoke/dust into the scene.
127
128volume multiply(float a = 0.0, float b = 0.0, output float c = 0.0){
129 c = 2*a+b;
130}
131
132////////////////////////////////////////
133// 3. Data Types and Global Variables //
134////////////////////////////////////////
135
136// Data Types
137
138// 1. The void type indicates a function that doesn't return any value
139
140// 2. int (Integer)
141 int x = -12; // Minimum size of 32-bits
142 int new2 = 0x01cf; // Hexadecimal can also be specified
143
144 ///////////////////////////////////////
145 // Order of Evaluation
146 ///////////////////////////////////////
147
148 // From top to bottom, top has higher precedence
149 //--------------------------//
150 // Operators //
151 //--------------------------//
152 // int++, int-- //
153 // ++ int --int - ~ ! //
154 // * / % //
155 // + - //
156 // << >> //
157 // < <= > >= //
158 // == != //
159 // & //
160 // ^ //
161 // | //
162 // && //
163 // || //
164 // ?: //
165 // = += -= *= /= //
166 //--------------------------//
167
168// 3. float (Floating-point number)
169 float A = 2.3; // minimum IEEE 32-bit float
170 float Z = -4.1e2; // Z = -4.1 * 10^2
171
172 // Order of evaluation is similar to int.
173 // Operations like ( ~ ! % << >> ^ | & && || ) aren't available in float
174
175// 4. string
176 // The syntax is similar to C
177 string new = "Hello World";
178 // some Special characters:
179 /*
180 '\"'; // double quote
181 '\n'; // newline character
182 '\t'; // tab character (left justifies text)
183 '\v'; // vertical tab
184 '\\'; // back slash
185 '\r'; // carriage return
186 '\b'; // backspace character
187 */
188
189 // Strings are concatenated with whitespace
190 "Hello " "world!"; // "Hello world!"
191 // concat function can also be used
192 string concat ("Hello ","World!"); // "Hello world!"
193
194 // printf function is same as C
195 int i = 18;
196 printf("I am %d years old",i); // I am 18 years old
197
198 // String functions can alse be used
199 int strlen (string s); // gives the length of the string
200 int len = strlen("Hello, World!"); // len = 13
201
202 // startswith returns 1 if string starts with prefix, else returns 0
203 int starts = startswith("The quick brown fox", "The"); // starts = 1
204
205 // endswith returns 1 if string starts with suffix, else returns 0
206 int ends = endswith("The quick brown fox", "fox"); // ends will be 1
207
208// 5. color (Red, Green, Blue)
209 color p = color(0,1,2); // black
210 color q = color(1); // white ( same as color(1,1,1) )
211 color r = color("rgb", 0.23, 0.1, 0.8); // explicitly specify in RGB
212 color s = color("hsv", 0.23, 0.1, 0.8); // specify in HSV
213 // HSV stands for (Hue, Saturation, Luminance)
214 // HSL stands for (Hue, Saturation, Lightness)
215 // YIQ, XYZ and xyY formats can also be used
216 // We can also access the indivudual values of (R,G,B)
217 float Red = p[0]; // 0 (access the red component)
218 float Green = p[1]; // 1 (access the green component)
219 float Blue = p[2]; // 2 (access the blue component)
220
221 // They can also be accessed like this
222 float Red = p.r; // 0 (access the red component)
223 float Green = p.g; // 1 (access the green component)
224 float Blue = p.b; // 2 (access the blue component)
225
226 // Math operators work like this with decreasing precedence
227 color C = (3,2,3) * (1,0,0); // (3, 0, 0)
228 color D = (1,1,1) * 255; // (255, 255, 255)
229 color E = (25,5,125) / 5; // (5, 1, 25)
230 color F = (30,40,50) / (3,4,5); // (10, 10, 10)
231 color A = (1,2,3) + (1,0,0); // (2, 2, 3)
232 color B = (1,2,3) - (1,0,0); // (0, 2, 3)
233 // Operators like ( - == != ) are also used
234
235 // Color Functions
236 color blackbody (1500) // Gives color based on temperature (in Kelvin)
237 float luminance (0.5, 0.3, 0.8) // 0.37 gives luminance cd/m^2
238 // Luminance is calculated by 0.2126R+0.7152G+0.0722B
239 color wavelength color (700) // (1, 0, 0) Gives color based on wavelength
240 color transformc ("hsl", "rgb") // converts one system to another
241
242// 6. point (x,y,z) is position of a point in the 3D space
243// 7. vector (x,y,z) has length and direction but no position
244// 8. normal (x,y,z) is a special vector perpendicular to a surface
245 // These Operators are the same as color and have the same precedence
246 L = point(0.5, 0.6, 0.7);
247 M = vector(30, 100, 70);
248 N = normal(0, 0, 1);
249
250 // These 3 types can be assigned to a coordinate system
251 L = point("object", 0.5, 0.6, 0.7); // relative to local space
252 M = vector("common", 30, 100, 70); // relative to world space
253 // There's also ("shader", "world", "camera", "screen", "raster", "NDC")
254
255 float x = L[0]; // 0.5 (access the x-component)
256 float y = L[1]; // 0.6 (access the y-component)
257 float z = L[2]; // 0.7 (access the z-component)
258
259 // They can also be accessed like this
260 float x = M.x; // 30 (access the x-component)
261 float y = M.y; // 100 (access the y-component)
262 float z = M.z; // 70 (access the z-component)
263
264 float a = dot ((1,2,3), (1,2,3)); // 14 (Dot Product)
265 vector b = cross ((1,2,3), (1,2,3)); // (0,0,0) (Cross Product)
266 float l = length(L); // 1.085 (length of vector)
267 vector normalize (vector L); // (0.460, 0.552, 0.644) Normalizes the vector
268
269 point p0 = point(1, 2, 3);
270 point p1 = point(4, 5, 6);
271 point Q = point(0, 0, 0);
272
273 // Finding distance between two points
274 float len = distance(point(1, 2, 3), point(4, 5, 6)); // 5.196
275 // Perpendicular distance from Q to line joining P0 and P1
276 float distance (point P0, point P1, point Q); // 2.45
277
278
279// 9. matrix
280 // Used for transforming vectors between different coordinate systems.
281 // They are usually 4x4 (or) 16 floats
282 matrix zero = 0; // makes a 4x4 zero matrix
283 /* 0.0, 0.0, 0.0, 0.0,
284 0.0, 0.0, 0.0, 0.0,
285 0.0, 0.0, 0.0, 0.0,
286 0.0, 0.0, 0.0, 0.0 */
287
288 matrix ident = 1; // makes a 4x4 identity matrix
289 /* 1.0, 0.0, 0.0, 0.0,
290 0.0, 1.0, 0.0, 0.0,
291 0.0, 0.0, 1.0, 0.0,
292 0.0, 0.0, 0.0, 1.0 */
293
294 matrix m = 7; // Maked a 4x4 scalar matrix with scaling factor of 7
295 /* 7.0, 0.0, 0.0, 0.0,
296 0.0, 7.0, 0.0, 0.0,
297 0.0, 0.0, 7.0, 0.0,
298 0.0, 0.0, 0.0, 7.0 */
299
300 float x = m[1][1]; // 7
301
302 // matrices can be constructed using floats in row-major order
303 // matrices are usually 4x4 with 16 elements
304 matrix myMatrix = matrix(1.0, 0.0, 0.0, 0.0, // Row 1
305 0.0, 2.0, 0.0, 0.0, // Row 2
306 0.0, 0.0, 3.0, 0.0, // Row 3
307 0.0, 0.0, 0.0, 4.0); // Row 4
308
309 // matrix transformations are easy to implement
310 matrix a = matrix ("shader", 1); // converted shader to common
311 matrix m = matrix ("object", "world"); // converted object to world
312
313 // Operations that can be used with decreasing precedence are:
314 // ( - * / == !=)
315
316 float determinant (matrix M) // 24 (returns the determinant of the matrix)
317 float transpose (matrix M) // returns the transpose of the matrix
318 /* 1.0, 0.0, 0.0, 0.0,
319 0.0, 2.0, 0.0, 0.0,
320 0.0, 0.0, 3.0, 0.0,
321 0.0, 0.0, 0.0, 4.0 */
322
323// 10. array
324 // Arrays in OSL are similar to C
325 float a[5]; // initialize array a with size 5
326 int b[3] = {90,80,70}; // declare array with size 3
327 int len = arraylength(b); // 3
328 int f = b[1]; // 80
329 float anotherarray[3] = b; // arrays can be copied if same type
330
331// 11. struct (Structures)
332 // Structures in OSL are similar to C and C++.
333 struct RGBA { // Defining a structure
334 color rgb;
335 float alpha;
336 };
337
338
339 RGBA col; // Declaring a structure
340 RGBA b = { color(0.1, 0.2, 0.3), 1 }; // Can also be declared like this
341
342 r.rgb = color (1, 0, 0); // Assign to one field
343 color c = r.rgb; // Read from a structure field
344
345// 12. closure
346 // Closure is used to store data that aren't considered when it executes.
347 // It cannot be manipulated or read.
348 // A null closure can always be assigned.
349 // OSL currently only supports color as their closure.
350
351 // A few examples of closures are:
352
353 // Diffuse BSDF closures:
354 closure color oren_nayar_diffuse_bsdf(normal N, color alb, float roughness)
355 closure color burley_diffuse_bsdf(normal N, color alb, float roughness);
356
357 // Dielectric BSDF closure:
358 closure color dielectric_bsdf(normal N, vector U, color reflection_tint,
359 color transmission_tint, float roughness_x, float roughness_y,
360 float ior, string distribution);
361
362 // Conductor BSDF closure:
363 closure color conductor_bsdf(normal N, vector U, float roughness_x,
364 float roughness_y, color ior, color extinction, string distribution);
365
366 // Generalized Schlick BSDF closure:
367 closure color generalized_schlick_bsdf(normal N, vector U,
368 color reflection_tint, color transmission_tint,
369 float roughness_x, float roughness_y, color f0, color f90,
370 float exponent, string distribution);
371
372 // Translucent BSDF closure:
373 closure color translucent_bsdf(normal N, color albedo);
374
375 // Transparent BSDF closure:
376 closure color transparent_bsdf();
377
378 // Subsurface BSSRDF closure:
379 closure color subsurface_bssrdf();
380
381 // Sheen BSDF closure:
382 closure color sheen_bsdf(normal N, color albedo, float roughness);
383
384 // Anisotropic VDF closure: (Volumetric)
385 closure color anisotropic_vdf(color albedo, color extinction,
386 float anisotropy);
387
388 // Medium VDF closure: (Volumetric)
389 closure color medium_vdf(color albedo, float transmission_depth,
390 color transmission_color, float anisotropy, float ior, int priority);
391
392 closure color uniform edf(color emittance); // Emission closure
393 closure color holdout(); // Holdout Hides objects beneath it
394
395 // BSDFs can be layered using this closure
396 closure color layer (closure color top, closure color base);
397
398
399
400// Global Variables
401// Contains info that the renderer knows
402// These variables need not be declared
403
404point P // Position of the point you are shading
405vector I // Incident ray direction from viewing position to shading position
406normal N // Normal of the surface at P
407normal Ng // Normal of the surface at P irrespective of bump mapping
408float u // UV 2D x - parametric coordinate of geometry
409float v // UV 2D y - parametric coordinate of geometry
410vector dPdu // change of P with respect to u tangent to the surface
411vector dPdv // change of P with respect to v tangent to the surface
412float time // Current time
413float dtime // Time covered
414vector dPdtime // change of P with respect to time
415
416/////////////////////
417// 4. Control flow //
418/////////////////////
419
420// Conditionals in OSL are just like in C or C++.
421
422// If/Else
423if (5>2){
424 int x = s;
425 int l = x;
426}
427else{
428 int x = s + l;
429}
430
431// 'while' loop
432int i = 0;
433while (i < 5) {
434 i += 1;
435 printf("Current value of i: %d\n", i);
436}
437
438// 'do-while' loop is where test happens after the body of the loop
439int i = 0;
440do {
441 printf("Current value of i: %d\n", i);
442 i += 1;
443} while (i < 5);
444
445// 'for' loop
446for (int i = 0; i < 5; i += 1) {
447 printf("Current value of i: %d\n", i);
448}
449
450/////////////////////
451// 5. Functions //
452/////////////////////
453
454// Math Constants
455 M_PI // π
456 M_PI_35 // π/35
457 m_E // e
458 M_LN2 // ln 2
459 M_SQRT2 // √2
460 M_SQRT1_2 // √(1/2)
461
462// Geometry Functions
463 vector N = vector(0.1, 1, 0.2); // Normal vector
464 vector I = vector(-0.5, 0.2, 0.8); // Incident vector
465
466 // Faceforward tells the direction of vector
467 vector facing_dir = faceforward(N, I); // facing_dir = (-0.5, 0.2, 0.8)
468
469 // faceforward with three arguments
470 vector ref = vector(0.3, -0.7, 0.6); // Reference normal
471 facing_dir = faceforward(N, I, ref); // facing_dir = (0.5, -0.2, -0.8)
472
473 // reflect gives the reflected vector along normal
474 vector refl = reflect(I, N); // refl = (-0.7, -0.4, 1.4)\
475
476 // refract gives the refracted vector along normal
477 float ior = 1.5; // Index of refraction
478 vector refr = refract(I, N, ior); // refr = (-0.25861, 0.32814, 0.96143)
479
480 /* Fresnel computes the Reflection (R) and Transmission (T) vectors, along
481 with the scaling factors for reflected (Kr) and transmitted (Kt) light. */
482 float Kr, Kt;
483 vector R, T;
484 fresnel(I, N, ior, Kr, Kt, R, T);
485/* Kr = 0.03958, Kt = 0.96042
486 R = (-0.19278, -0.07711, 0.33854)
487 T = (-0.25861, 0.32814, 0.96143) */
488
489 // Rotating a point along a given axis
490 point Q = point(1, 0, 0);
491 float angle = radians(90); // 90 degrees
492 vector axis = vector(0, 0, 1);
493 point rotated_point = rotate(Q, angle, axis);
494 // rotated_point = point(0, 1, 0)
495
496 // Rotating a point along a line made by 2 points
497 point P0 = point(0, 0, 0);
498 point P1 = point(1, 1, 0);
499 angle = radians(45); // 45 degrees
500 Q = point(1, 0, 0);
501 rotated_point = rotate(Q, angle, P0, P1);
502 // rotated_point = point(0.707107, 0.707107, 0)
503
504 // Calculating normal of surface at point p
505 point p1 = point(1, 0, 0); // Point on the sphere of radius 1
506 vector normal1 = calculatenormal(p1);
507 // normal1 = vector(1, 0, 0)
508
509 // Transforming units is easy
510 float transformu ("cm", float x) // converts to cm
511 float transformu ("cm", "m", float y) // converts cm to m
512
513// Displacement Functions
514 void displace (float 5); // Displace by 5 amp units
515 void bump (float 10); // Bump by 10 amp units
516
517
518// Noise Generation
519
520 type noise (type noise (string noisetype, float u, float v, ...)); // noise
521 type noise (string noisetype, point p,...); // point instead of coordinates
522 /* some noises are ("perlin", "snoise", "uperlin", "noise", "cell", "hash"
523 "simplex", "usimplex", "gabor", etc) */
524
525 // Noise Names
526
527 // 1. Perlin Noise (perlin, snoise):
528 // Creates smooth, swirling noise often used for textures.
529 // Range: [-1, 1] (signed)
530 color cloud_texture = noise("perlin", P);
531
532 // 2. Simplex Noise (simplex, usimplex):
533 // Similar to Perlin noise but faster.
534 // Range: [-1, 1] (signed) for simplex, [0, 1] (unsigned) for usimplex
535 float bump_amount = 0.2 * noise("simplex", P * 5.0);
536
537 // 3. UPerlin Noise (uperlin, noise):
538 // Similar to peril
539 // Range: [0, 1] (unsigned)
540 color new_texture = noise("uperlin", P);
541
542 // 4. Cell Noise (cell):
543 // Creates a blocky, cellular and constant values within each unit block
544 // Range: [0, 1] (unsigned)
545 color new_texture = noise("cell", P);
546
547 // 5. Hash Noise (hash):
548 // Generates random, uncorrelated values at each point.
549 // Range: [0, 1] (unsigned)
550 color new_texture = noise("hash", P);
551
552 // Gabor Noise (gabor)
553 // Gabor Noise is advanced version of Perin noies and gives more control
554 // Range: [-1, 1] (signed)
555 // Gabor Noise Parameters
556
557 // Anisotropic (default: 0)
558 // Controls anisotropy:
559 // 0: Isotropic (equal frequency in all directions)
560 // 1: Anisotropic with user-defined direction vector (defaults to (1,0,0))
561 /* 2: Hybrid mode,anisotropic along direction vector but radially isotropic
562 perpendicularly. */
563
564 // Direction (default: (1,0,0))
565 // Specifies the direction of anisotropy (used only if anisotropic is 1).
566
567 // bandwidth (default: 1.0)
568 // Controls the frequency range of the noise.
569
570 // impulses (default: 16)
571 // Controls the number of impulses used per cell, affecting detail level.
572
573 // do_filter (default: 1)
574 // Enables/disables antialiasing (filtering).
575
576 result = noise(
577 "gabor",
578 P,
579 "anisotropic", anisotropic,
580 "direction", direction,
581 "bandwidth", bandwidth,
582 "impulses", impulses,
583 "do_filter", do_filter
584 );
585
586 // Specific noises can also be used instead of passing them as types
587 // pnoise is periodic noise
588 float n1 = pnoise("perlin", 0.5, 1.0);
589 // 2D periodic noise with Gabor type
590 float n2 = pnoise("gabor", 0.2, 0.3, 2.0, 3.0);
591 // 2D non-periodic simplex noise
592 float n3 = snoise(0.1, 0.7);
593 // 2D periodic simplex noise
594 type psnoise (float u, float v, float uperiod, float vperiod);
595 float n4 = psnoise(0.4, 0.6, 0.5, 0.25);
596 // 2D cellular noise
597 float n5 = cellnoise(0.2, 0.8);
598 // 2D hash noise
599 int n6 = hash(0.7, 0.3);
600
601// Step Function
602 // Step Functions are used to compare input and threshold
603
604 // The type may be of float, color, point, vector, or normal.
605 type step (type edge, type x); // Returns 1 if x ≥ edge, else 0
606 color checker = step(0.5, P); // P is a point on the surface
607 /* Pixels with P values below 0.5 will be black, those above or equal will
608 be white */
609 float visibility = step(10, distance(P, light_position));
610 // Light is fully visible within 10 units, completely invisible beyond
611
612 type linearstep (type edge0, type edge1, type x); /* Linearstep Returns 0
613 if x ≤ edge0, and 1 if x ≥ edge1, with linear interpolation */
614 color gradient = linearstep(0, 1, P);
615 // P is a point on the surface between 0 and 1
616 // Color will graduate smoothly from black to white as P moves from 0 to 1
617 float fade = linearstep(0.85, 1, N.z); // N.z is the z-component
618 // Object edges with normals close to vertical (N.z near 1) will fade out
619
620 type smoothstep (type edge0, type edge1, type x); /* smoothstep Returns 0
621 if x ≤ edge0, and 1 if x ≥ edge1, with Hermite interpolation */
622 float soft_mask = smoothstep(0.2, 0.8, noise(P)); /* noise(P) is a noisy
623 value between 0 and 1. soft_mask will vary smoothly between 0 and 1 based
624 on noise(P), with a smoother curve than linearstep */
625
626// Splines
627 // Splines are smooth curves based on a set of control points
628
629 /* The type of interpolation ranges from "catmull-rom", "bezier",
630 "bspline", "hermite", "linear", or "constant" */
631
632 // Spline with knot vector
633 float[] knots = {0, 0, 0, 0.25, 0.5, 0.75, 1, 1, 1};
634 point[] controls = {point(0),point(1, 2, 1),point(2, 1, 2),point(3, 3, 1)};
635 spline curve1 = spline("bezier", 0.5, len(knots), controls);
636 // curve1 is a Bezier spline evaluated at u = 0.5
637
638 // Spline with control points
639 spline curve2 = spline("catmull-rom", 0.25, point(0, 0, 0), point(1, 2, 1),
640 point(2, 1, 2), point(3, 3, 1));
641 // curve2 is a Catmull-Rom spline evaluated at u = 0.25
642
643 // Constant spline with a single float value
644 float value = 10;
645 u = 0.1;
646 spline curve5 = spline("constant", u, value);
647 // curve5 is a constant spline with value 10 evaluated at u = 0.1
648
649 // Hermite spline with point and vector controls
650 point q0 = point(0, 0, 0), q1 = point(3, 3, 3);
651 vector t0 = vector(1, 0, 0), t1 = vector(-1, 1, 1);
652 u = 0.75;
653 spline curve3 = spline("hermite", u, q0, t0, q1, t1);
654 // curve3 is a Hermite spline evaluated at u = 0.75
655
656 // Linear spline with float controls
657 float f0 = 0, f1 = 1, f2 = 2, f3 = 3;
658 u = 0.4;
659 spline curve4 = spline("linear", u, f0, f1, f2, f3);
660 // curve4 is a linear spline evaluated at u = 0.4
661
662 // InverseSplines also exist
663
664 // Inverse spline with control values
665 float y0 = 0, y1 = 1, y2 = 2, y3 = 3;
666 float v = 1.5;
667 float u1 = splineinverse("linear", v, y0, y1, y2, y3);
668 // u1 = 0.5 (linear interpolation between y1 and y2)
669
670 // Inverse spline with knot vector
671 float[] knots = {0, 0, 0, 0.25, 0.5, 0.75, 1, 1, 1};
672 float[] values = {0, 1, 4, 9};
673 v = 6;
674 float u2 = splineinverse("bezier", v, len(knots), values);
675 // u2 = 0.75 (Bezier spline inverse evaluated at v = 6)
676
677 // Inverse spline with constant value
678 v = 10;
679 float u3 = splineinverse("constant", v, 10);
680 // u3 = 0 (since the constant spline always returns 10)
681
682 // Inverse spline with periodic values
683 float y4 = 0, y5 = 1, y6 = 0;
684 v = 0.5;
685 float u4 = splineinverse("periodic", v, y4, y5, y6);
686 // u4 = 0.75 (periodic spline inverse evaluated at v = 0.5)
687
688
689
690// Calculus Operators
691 // Partial derivative of f with respect to x, y and z using Dx, Dy, Dz
692 float a = 3.14;
693 float dx = Dx(a); // partial derivative of a with respect to x
694
695 point p = point(1.0, 2.0, 3.0);
696 vector dp_dx = Dx(p); // partial derivative of p with respect to x
697
698 vector dv_dy = Dy(N); // partial derivative of normal with respect to y
699
700 color c = color(0.5, 0.2, 0.8);
701 color dc_dz = Dz(c); // partial derivative of c with respect to z
702
703
704 float area (point p) // gives the surface area at the position p
705
706 float filterwidth (float x) // gives the changes of x in adjacent samples
707
708// Texture Functions
709 // lookup for a texture at coordinates (x,y)
710 color col1 = texture("texture.png", 0.5, 0.2);
711 // Lookup color at (0.5, 0.2) in texture.png
712
713 // 3D lookup for a texture at coordinates (x,y)
714 color col3 = texture3d("texture3d.vdb", point(0.25, 0.5, 0.75));
715
716 // parameters are ("blur","width","wrap","fill","alpha","interp", ...)
717 color col2 = texture("texture.png",1.0,0.75,"blur",0.1,"wrap", "periodic");
718 // Lookup color at (1.0, 0.75) with blur 0.1 and periodic wrap mode
719
720// Light Functions
721
722 float surfacearea (); // Returns the surface area of area light covers
723 int backfacing (); // Outputs 1 if the normals are backfaced, else 0
724 int raytype (string name); // returns 1 if the ray is a particular raytype
725
726 // Tracing a ray from a position in a direction
727 point pos = point(0, 0, 0); // Starting position of the ray
728 vector dir = vector(0, 0, 1); // Direction of the ray
729 int hit = trace(pos, dir); // returns 1 if it hits, else 0
Further reading ¶
- Blender Docs for OSL
- C4D Docs for OSL
- Open Shading Language on GitHub
- Official OSL Documentation