Open Shading Language Experiment

I’ve been trying to learn a bit about writing OSL shaders the past couple of days, and I’m finding it pretty interesting. So far I’ve just learned a bit about what osl can do and how to put together a basic shader. Here’s one of my first attempts – a procedural dirt shader based on the trace function. It’s pretty much just an ambient occlusion pass that you can use as a mask for other materials, but what’s nice about it is that it’s completely dynamic: no baking, no vertex paint etc.

Here’s what it looks like:


And here’s the shader code:

shader tracetest(
    normal Normal = N,
    float Distance = 10,
    float NoiseFactor = 0.5,
    float NoiseScale = 100,
    output float TraceRandom = 0.0)

    normal NewNorm = Normal;
    // Create a randomised normal to trace with.
    vector RandomVec = vector(0,0,0);
    vector NormalVec = transform("common", N);
    RandomVec = NormalVec + (NoiseFactor * noise("perlin", P * NoiseScale));
    RandomVec = normalize(RandomVec);
    int Hit2 = trace(P, RandomVec, "maxdist", Distance);
    if (Hit2) {
        float Dist = 0.0;
        // Get the hit distance:
        int HitTrace = getmessage("trace", "hitdist", Dist);
        // Turn Hit Distance into output:
        float Grad = 1 - Dist/Distance;
        TraceRandom = pow(Grad,2);

Edit: Here’s a screenshot of the node setup.

Screen Shot 2014-01-07 at 19.01.34

11 responses to “Open Shading Language Experiment

  1. Really impresses with all of your work so far Ben. I am thinking of buying a new computer. Can you tell me what are your system specs and what do you recommend?


    • Hi Playway

      I’m using a 2011 iMac for my work. 8GB Ram, Quad Core processor. The most important hardware specs for blender are RAM (for handling large scenes) and CPU power (for rendering). GPU is obviously usefull as well, particularly if you want to use it for rendering. I’d advise having a quad core machine with at least 8GB of ram but the rest I’m not too up to date on…

      • Ah, I see. Thanx for helping me out! A little professional advice is always helpful:)

        Good luck for your future projects and have a nice day!

  2. Script works perfect, but it takes ages to render. Am i doing something wrong or is just normal when using OSL

  3. Pingback: Open Shading Language from Scratch (Ubuntu) – Digital Box·

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s