Fractal Noise Generator Shader

Hello there!

So, the title says all! I coded this 2D fractal noise generator into the OSL language, i hope you enjoy and can improve on it :).


Fractal_Noise_OSL.zip (813 Bytes)

Cheers!

I’ve made some upgrades to the shader, also i want to share another rendering of it!



float fract(float x){

    return (x - floor(x));

}
float rand(point co){

    return fract(sin(dot(co,point(12.9898,78.233,0))) * 43758.5453);
}
float noise2f(point p){

    point ip = point(floor(p[0]),floor(p[1]),0);
    point u = point(fract(p[0]),fract(p[1]),0);
    u = u*u*(3.0-2.0*u);
    float res = mix(
        mix(rand(ip),rand(ip+point(1.0,0.0,0.0)),u[0]),
        mix(rand(ip+point(0.0,1.0,0.0)),rand(ip+point(1.0,1.0,0.0)),u[0]),
        u[1]);
    return res*res;

}

point cMul(point a, point b){   

    return point(a[0] * b[0] - a[1] * b[1], a[0] * b[1] + a[1] * b[0], 0);

}

shader NoiseFract(
    float Mul = 5,
    float Decay = 0.5,
    color color1 = color(0,1,0),
    color color2 = color(1,0,0),
    color color3 = color(0,1,0),
    output closure color CL = background()
    )
{
    //
    float fbm(point c){
    // Iterations [0,1,19] -- Mul [0,2,10] -- Decay [0,0.5,2]
    float f = 0.0;
    float w = 1.0;
    for(int i = 0; i < 19; i++){
        f+= w*noise2f(c);
        c*= Mul;
        w*= Decay; 
    }
    return f;    
    }
    point p = P;
    point q = 0;
    point r = 0;
    q[0] = fbm(p +0.01*1);
    q[1] = fbm(p +point(1));   
    r[0] = fbm(p +1.0*q + point(1.7, 9.2, 0)+0.15*1);
    r[1] = fbm(p +1.0*q + point(8.3, 2.8, 0)+0.126*1);   
    float f = fbm(P +1.0*r + 0.0*1);
    color Cl_temp = mix(color1,color2,clamp((f*f)*4.0,0.0,1.0));
    Cl_temp += color2;
    Cl_temp += color(mix(Cl_temp,color3,clamp(length(q),0,1)));
    normal n = calculatenormal(P + length(vector(pow(Cl_temp,6)))/10000000*Ng);
    CL = diffuse(n)*clamp(Cl_temp,0.0,1.0);
    return;
}

This is cool!
When I saw the thread title I thought “hmmm, you mean like a clouds texture?” but it’s different and unique.

Thats beautifule noise !!!
Thank you.
Any chance to control offset, scale, coordinates?
For now its planar, is there a way to have it as volume kind?
(Until now I used for this kind of dirt planet.osl, but it always looked more as planet anyway.)

I’m not frequent user of OSL material, but first I thought something is wrong.
It took me a time to discover that I have to connect your script directly to output.
(Without shader node).

I’m a surprised what a low feedback here :slight_smile:

BTW what about generated dust and scratch texture :slight_smile:

Edit: second version crash with 2.72a

Thanks for the reply @Quantum_Anomaly!

@vklidu:

I’ve found interesting to convert this noise shader from GLSL to OSL, it was originally created by the Great Igno Quilez, but!
GLSL shaders are all about working with a 2D plane to get a 3D effect while OSL brings stuff more directly into 3D space. Also OSL comes with functions that help alot getting stuff done in a more simple way. That’s why i’ve been focusing more on creating stuff from scratch with OSL rather than converting scripts from other languages that in long or short term, with newer versions of blender may have greater compatibility issues.

Actually i have in mind to create a new noise shader from scractch using only OSL, i will share it for sure then! :slight_smile:

Thanks for answer…
So if I understood you right, you converted GLSL shader to OSL so you can’t create 3D version.
To do that, you need to start from scratch, right?

To control offset, scale, coordinates is also task of the new script done from scratch, right?

To control offset, scale, coordinates is also task of the new script done from scratch, right?

Well… Yes and no, i guess one could work with this code to costumize it but it’s a great deal of work when you could do it from scratch with a few lines of code with the given functions!