### Learn the basics of OSL scripting in Blender to create your own Cycles shaders

One of the big new features of Blender 2.65 is the inclusion of the Open Shading Language (OSL) for the Cycles render engine. OSL has been developed by Sony Pictures Imageworks as a shading language for modern path-tracing render engines like Arnold or Cycles. The testbed for OSL, until now, has been a special version of the Arnold render engine used on the films The Amazing Spiderman, Men in Black 3, and Hotel Transylvania.

With Blender 2.65, Cycles has become the first publicly available render engine to support OSL.

### Introduction to OSL in Blender 2.65 – Part 3 – Integrating the Pie

This third installment of the Blender Cookie OSL introduction explains the internals of the Pie texture and how to improve the integration into the Cycles node system.

Basically an OSL shader (just like GLSL shaders and Renderman shaders) evaluates a point on a surface. The point is defined by a ray hitting the surface. It then performs a mathematical calculation on a property, like in our case the color. Consider a simple plane with object coordinates:

This is why you need texture coordinates to start a procedural texture. The shader needs to know relative to where to run the computation. The cool thing is that the shader is working like it’s doing the computations for all points simultaneously thus it works like some kind of 2D plotter. That essentially means you can use a large pool of mathematical functions to create patterns using OSL. A good tool to preview many of those functions is the Blender add-on 3D Function Surfaces: http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Add_Mesh/Add_3d_Function_Surface

But there is another implication. Remember how we left the turbulence-part out of the function for the final pattern? There was a good reason for this. We simply don’t need to calculate any kind of turbulence inside the shader when there is a vast variety of turbulence patterns right at our fingertips in Cycles. For example, the noise texture. If you use an external texture instead of a turbulence function from inside the shader for each point, the shader will look at the value of the external texture at that point and use it for the computation. So let’s remove one of the turbulence inputs, rename the other one to simply “Noise”. This way the user of the shader knows that they have to plug some kind of noise texture into that slot. Next, add the turb-part back to the output line:

You can make the effect even stronger by adding a math node set to multiply:

Or change the appearance completely by using a different noise texture as input:

As you can see, OSL isn’t just making the writing of new shaders and textures easier, the fact that it is completely fit for data propagation through nodes is also making things a lot more versatile!

As a final brush-up, let’s remove the “Hardness” Input and the “– 0.5” at the end of the final line. The latter is simply rotating the pattern a little and not really needed:

As a conclusion to this chapter for the Blender Cookie OSL Introduction here’s a comparison between the two textures in Blender Internal and OSL:

Lines of code for Blender Internal: 98

Lines of code for OSL: 15

Because OSL makes writing shaders really easy, we can expect a wealth of new shaders ported to OSL in the near future. Great times ahead for the Blender community!

There are literally thousands of shaders out there written in different shading languages that are just waiting to be ported to OSL. If you have a GLSL shader at hand and want to port it to OSL I recommend this short video tutorial by Thomas Dinges: http://www.openshading.com/tutorial-2-glsl-to-osl/

A nice resource of Renderman shaders that you can try to port to OSL can be found at http://www.renderman.org/RMR/RMRShaders.html

You must be logged in to upload images. Register

### Discussion

5 Responses to “Introduction to OSL in Blender Cycles – part 03”
1. Posts: 8

First, Good job Gottfried.
-Riley

#
1
Jan 28, 2013 at 11:33 am
2. Posts: 41

Hi Gottfried, this series has been rather useful.

I’ve been tinkering around with some shaders, trying to get a halfway decent fire shader out of it. I realise it is not simply a shader problem, and that geometry etc plays a big part in the overall result, but do you have any hints or ideas as to what I would want to consider trying in such a shader?

I currently use a blackbody radiation function with a radial fall-off (from a selected centre) to give the colour distribution in the flames. Feeding the particle position node into this centre vector seems to give a reasonable result.

#
2
Jan 28, 2013 at 10:20 pm