I've finally found a good example on the internet for a problem that I had in Blender, but it is made by the text window that looks like this - enter image description here

And then it makes a result like this - enter image description here

And it is using this node:
enter image description here.

Please give me an explanation of this node, and how it is used.

  • 1
    $\begingroup$ Ok, I researched the internet and it says its an open shading language. But, how do I use it. Is it for righting your own shaders? What are some good places to look for information about it? Is every shader written in this language like the Diffuse, Glossy, Glass, Transparent etc.? $\endgroup$ – Vladimir Jun 3 '14 at 18:15
  • $\begingroup$ And are there some examples of blender files that I can download and check them out? $\endgroup$ – Vladimir Jun 3 '14 at 18:18
  • $\begingroup$ And on the second picture, what do those values mean? - "color_diffuse = 0.5", "color_specular = 0.1" etc. $\endgroup$ – Vladimir Jun 3 '14 at 18:24
  • $\begingroup$ Related: blender.stackexchange.com/q/2238/599 $\endgroup$ – gandalf3 Jun 3 '14 at 18:54
  • $\begingroup$ @Vladimir Setting color_diffuse = 0.5 is the same as assigning color(0.5, 0.5, 0.5) , gray R,G and B set to .5, have you tried my answer here blender.stackexchange.com/questions/10915/… at all? $\endgroup$ – stacker Jun 3 '14 at 19:03

The cycles script node connects OSL scripts into cycles, pretty much like an addon for the render engine. While you can use an OSL script to define the final appearance of the material, you can also use them as small steps that add custom noise or pattern inputs or custom math functions.

There is a self published e-book by Michel Anders called Open Shading Language for Blender: A practical primer. You will want some programming knowledge before reading it at it doesn't teach the core language.

You will also find that OSL is very similar to RenderMan scripts. Which is not surprising as it was created by Larry Gritz who worked on RenderMan at Pixar for several years before starting OSL. You will find that many RenderMan scripts are easily converted to OSL and any tutorials should also easily translate.

I have a github repository that has over 100 OSL scripts, including preview renders and node tree snapshots used to create them. Some of these scripts come from various websites including Michel Anders blog and the Blender Artists forum while others are conversions of older RenderMan scripts and a couple are OSL versions of quite complex node setups.

Some samples of scripts that define the entire material and connect to the output node -

enter image description hereenter image description here

These two samples use the same hexagonal pattern script within different node trees -

enter image description hereenter image description here


Apart from the predefined shaders you can write your own shaders for Cycles. They only work for CPU rendering though.
The scripts you see in the text editor are shaders written in the Open Shading Language (OSL). These shaders are small programs written in a C-like language. They are usually used to tell Cycles how to calculate the material color, but can also perform other computations.

The following is a short description of how OSL shaders work. It is not intendet to be a complete tutorial, and you should have at least basic programming skills. Also it has been a while since I played around with OSL shaders, so if anyone finds a mistake in my description please let me know.

Using an OSL Shader:
To use a OSL shader, you have to add a script node to your Cycles material which is set to the .osl script. You can add a script node by pressing Shift+A and selecting Script->Script from the menu. This node can be set to use an internal shader (one from Blender's text editor) or an external shader file (an .osl file on your hard drive). The inputs and outputs of this node are generated automatically from the shader code.

Structure of an OSL Shader:
An OSL shader can contain typical C-style functions and preprocessor directives. The actual shader program in the .osl script is the function that begins with the keyword shader. The name of the shader function is arbitrary, but only one shader function is allowed per file.
Between the opening and closing bracket after the function's name comes a list of input and output variables. Each variable is declared using a type (e.g. color), its name (e.g. Color_Diffuse) and a default value (e.g. 0.5) which is used when no input is connected to this slot of the script node . In addition to that, a variable declaration can be preceded by the keyword output. These variables will appear as output sockets on the right side of the script node, all other variables are inputs. Outputs can be written to, inputs can only be read. Each OSL shader should have at least one output, otherwise its computations can not be used.
Apart from the user-supplied inputs an OSL shader can also use some functions and variables provided by Blender, most notably closures. Closures allow you to access the functionality of the normal Cycles shader nodes. See the documentation for a list of these functions. In the screenshot you posted the diffuse_toon, specular_toon, transparent and emission closure are used to calculate the output closure named BSDF, which determines the color of your material when it is connected to the 'Surface' input of the 'Material Output' node.

If you want to see some other shader examples click on the 'Templates' button in Blender's text editor, then select 'Open Shading Language' and select a template that sounds interesting.
enter image description here
Unfortunatly I do not know any good beginner tutorials, but you can have a look at this one which describes how to translate a GLSL shader into a OSL shader and this one which describes how to create an absorption shader.
Otherwise I advise you to just try it out. Learning by doing usualy gives you the most benefit when programming.

  • $\begingroup$ bytheway, on the first picture, on top of the text code where it says "shader toon", does that refer to the toon shader in the shaders list in the node editor - Shift+A/Shader/Toon? $\endgroup$ – Vladimir Jun 3 '14 at 20:21
  • $\begingroup$ No, the name of the shader function is arbitrary. You can name it what you want. The diffuse_toon(N, Size_Diffuse, Smooth_Diffuse) function referres to the cycles toon shader node. $\endgroup$ – maddin45 Jun 3 '14 at 20:50
  • $\begingroup$ What does the "float" and the "output closure" refer to? $\endgroup$ – Vladimir Jun 4 '14 at 0:17
  • $\begingroup$ @Vladimir floatis short for floating point, a data type computers use to represent real numbers. As I wrote in my answer, after the name of the shader function (here toon) follows a list of input and output variables. These variables are declared with a type (for example float), a name and a default value. The type tells Blender what kind of data this input expects. A float input expects a real number as input and will be translated into a spin box (the number field with the small arrows left and right) in the script node. $\endgroup$ – maddin45 Jun 4 '14 at 6:23
  • $\begingroup$ output closure are two so-called qualifiers that give Blender additional information about the variable that follows (color BSDF in your case). output obviously tells Blender that this variable will be used as an output socket in the Script node. This is why the output of the Script node has the name BSDF. closure on the other hand stands for data that can be written to, but nor read, during the shader execution, since it will only be evaluated later. Currently the only closure type is color and this is used to create the green outputs like in the normal Cycles shader nodes. $\endgroup$ – maddin45 Jun 4 '14 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.