OSL Goodness

I’m very happy with the new OSL script node - here is my first exercise, an Add Shader with a factor input (I really wonder whey the normal add closure does not have it):

#include “stdosl.h”

shader node_advancedadd_closure(
float Fac = 0.5,
closure color Closure1 = background(),
closure color Closure2 = background(),
output closure color Closure = background())
{
float t = clamp(Fac, 0.0, 1.0);
Closure = Closure1 + t * Closure2;
}


1 Like

So has this been put in the lastest build now?? I really want to get playing with this.

Ok, first one was a warmup - for the next one I wanted to test wether it is possible to port one of these BI procedural texture plugins to OSL:

http://www-users.cs.umn.edu/~mein/blender/plugins/texture.html

Turns out it works pretty well and the result is way less complicated, as you can see with this basic pie texture:

#include "stdosl.h"

color pie(point p, int Divides, float Angle)
{
    float angle_new;
    angle_new = atan2(p[0],p[1]) + Angle*3.1415926/180.0;
    return color(0.5 - 0.5*sin(angle_new * Divides - 0.5));
}

shader node_magic_texture(
    int Divides = 2,
    float Angle = 5.0,
    point Vector = P,
    output color Color = color(0.0, 0.0, 0.0))
{
    Color = pie(Vector, Divides, Angle);
}

Stripped the GPL-Block of the code, copyright of the original version goes to Robert Wenzlaff.


Edit: I also wrote a version where you can set the colors of the stripes, but I think it’s not very usefull since you can easily set the colors with a colorramp node which also lets you adjust the hardness and thickness of the stripes with a very fine-grained control:

#include "stdosl.h"

color pie(point p, int Divides, float Angle)
{
    float angle_new;
    angle_new = atan2(p[0],p[1]) + Angle*3.1415926/180.0;
    return color(0.5 - 0.5*sin(angle_new * Divides - 0.5));
}

shader node_magic_texture(
    color Color1 = color(0),
    color Color2 = color(1),
    int Divides = 2,
    float Angle = 5.0,
    point Vector = P,
    output color Color = color(0.0, 0.0, 0.0))
{
    Color = mix(Color1, Color2, pie(Vector, Divides, Angle));
}

looks confusing…
art we artists or programmers?

It’s quite simple actually.
It gives enormous possibilities to those who want to dive into complex shaders.

If you can’t / don’t want to use it, don’t use it. :wink:

If you’re a good 3D artist, chances are you know a bit of programming as well. I can’t think of anyone who works in a production studio who doesn’t know at least a little bit of python or MEL script, or who can’t write a simple shader for Renderman. As artists we need to be familiar with a range of tools, sometimes tools that might not be classically associated with artists. A lumberjack and a ice sculptor both use chainsaws, but you would never call an ice sculptor anything but an artist.

Technicalities are something you just can’t escape,its the nature of computer graphics.

I don’t understand all this either.lol.

but hey,you’ll never come across a production that is made exclusively by artists or technicians,there will be a mix of both.

unfortunately some people are better technically than they are artistically (aesthetically)

there are doctors out there that know anatomy better but than anyone,but ask them draw/sculpt a human figure,lol,there’s a lot more too it than “technical knowledge”…

Gottfried has some excellent effects/tutorials on his website though,and effects simulation is somethng that requires a bit of technical know-how to get a bit more artistic control.

apologies for derailing.

This is pretty sweet, although I don’t tend to feel the urge to actually script my shaders very often, it’s useful to be able to.

As far as the artist/programmer question: to be good in this medium, you have to be both.

If you are an artist, just use the shaders others write. OSL makes the process of creating shaders and procedural textures way easier than before. Imagine a repository with hundreds of procedural textures at your fingertip, not just the ones built into Blender right now :slight_smile:

and you can port shaders from other renderers like the next vray… just for example…

So once I get this running and get my head round making shaders my 3rd year dissertation will be a comparison of OSL vs OpenGL shaders now using Blender as the test tool!

Not being a programmer I admit that this stuff seemed pretty daunting at first (It isn’t though, just read the specifications pdf on the OSL sourceforge site!). I want to use it because it looks so powerful, and like Marcatore mentions, you can port shaders from V-ray, to Arnold, to Cycles. Which, I’d imagine, will mean at some point there will be a plentiful supply of professional-grade shaders readily available on-line to either add straight to your library, or to customize to fit your own needs.

Slightly off-topic; I’ve read (or at least I understand) that in the future we will be able to do a whole lot more with OSL, going beyond shaders.

@GottfriedHofman & DingTo:
Do you think it is possible to bring with OSL the Voroni Crackle procedural to cycles?

Johannes

You can write all kinds of procedural texture patterns with OSL, Voronoi Crackle could be done too.

Great news :smiley:

Yes good news, would love to see them in a official cycles materials database that is accessed from within blender :slight_smile:

Another interesting thing I just discovered - the original Pie procedural had a turbulence-option to distort the pie-pieces - with OSL I changed it so you can simply use any type of noise for that - über-cool!

#include "stdosl.h"

color pie(point p, int Divides, float Angle, float Noise)
{
    float angle_new;
    angle_new = atan2(p[0],p[1]) + Angle*3.1415926/180.0;
    return color(0.5 - 0.5*sin(angle_new * Divides - Noise - 0.5));
}

shader node_magic_texture(
    int Divides = 2,
    float Angle = 5.0,
    float Noise = 0.0,
    point Vector = P,
    output color Color = color(0.0, 0.0, 0.0))
{
    Color = pie(Vector, Divides, Angle, Noise);
}


Nice Tests Gottfried, the latest one is really cool! :smiley:

I did some tests as well.

  1. Cell Noise
    OSL has some nice inbuilt noise patterns (perlin, cell, gabor…). Here, I simply mapped the cell noise map (which is bw) to a variety of colors using the smoothstep() function.

#include <stdosl.h>

shader node_cellnoise(
    float Saturation = 0.5,
    float Value = 1.0,
    output color Color = (0.0))
{
    float Fac = (noise("cell", P));
    Color = color("hsv", (smoothstep(0, 1, Fac)), Saturation, Value);
}


There are also some converter functions to play with.

  1. Convert a Wavelength Value (in nm) to a Color.
#include "stdosl.h"

shader node_spectrum(
    float Wavelength = 600.0,
    output color Color = (0.8))
{

    Color = wavelength_color(Wavelength);
}
  1. Convert a Blackbody (in Kelvin) to an Emission Shader
#include "stdosl.h"

shader node_blackbody(
    float Temperature = 5200.0,
    output color Color = (0.8),
    output closure color BSDF = 0)
{

    Color = blackbody(Temperature);
    BSDF = Color * emission();
}

Keep posting this stuff guys! Super interesting. Little explanations for total noob in custom shading/scripting is highly appreciated too :slight_smile:

Cheers, keep it up

is it possible to write a shader that displays the un-occluded edges of a mesh, at some thickness?