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:

ProceduralDirt

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

8 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?

    Thanx!

    • 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!

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s