jueves, 11 de abril de 2013

Blender Cycles OSL: Mandelbrot Fractal (Interior Colors)

Hi. Today I'm going to show you an OSL code to get a Mandelbrot Fractal with its interior colorized, like this:




You can see a HiRes version in this link.

This is the code:

#include <stdosl.h>

shader node_fractal(
    float CenterX = 0.0,
    float CenterY = 0.0,
    point Vector = P,
    float Zoom = 1.0,
    int MaxIterations = 50,
    color Foreground = color(1.0),
    color Background = color(0.0),
    output float Fac = 0.0,
    output color Color = 0.0)
{
    point p = Vector / (2* Zoom);
    float auxiliar = 0.0;
		
	p[0] += CenterX;	
	p[1] += CenterY;	
	
    point pa = p;
    point pb = point(0.0);
    int i = 0;
    for(i=1; i<MaxIterations; i++)
    {
        pb[0] = pa[0]*pa[0] - pa[1]*pa[1];
        pb[1] = pa[0]*pa[1] + pa[1]*pa[0];
        pa = pb + p;
    }
	
    auxiliar = sqrt((pa[0]*pa[0]) + (pa[1]*pa[1]));

    Fac = clamp(auxiliar,0.0,1.0);

    if (Fac == 0.0)
        Color = Background;
    else
        Color = mix(Foreground, Background, Fac);       
}

And this is the node setup to get the image before:





Finally, the osl code is prepared to get a colorized image without using a color ramp node. If you connect the "Color" output socket with the diffuse node, you get this:






Bye

3 comentarios:

Javi dijo...

Got this error:
"/home/javi/Documentos/blend files/mandelbrot.osl:1:9: error: #include espera "NOMBRE_ARCHIVO" ó
Error: OSL script compilation failed, see console for errors"

Javi dijo...

Solved. After that problem I got another one of sintax.
The code that worked for me:

#include "stdosl.h"

shader node_mandelbrot_interior(
float CenterX = 0.0,
float CenterY = 0.0,
point Vector = P,
float Zoom = 1.0,
int Iterations = 50,
color Foreground = color(1.0),
color Background = color(0.0),
output float Fac = 0.0,
output color Color = 0.0)
{
// Scale position
point p = Vector / (2* Zoom);
float s = 0.0;
float auxiliar = 0.0;
float factor = 0.0;

// Move center of the image
p[0] += CenterX;
p[1] += CenterY;

point pa = p;
point pb = point(0.0);
int i = 0;
for(i=1; i < 100; i++)
{
pb[0] = pa[0]*pa[0] - pa[1]*pa[1];
pb[1] = pa[0]*pa[1] + pa[1]*pa[0];
pa = pb + p;
}

auxiliar = sqrt((pa[0]*pa[0]) + (pa[1]*pa[1]));

// Colorize
Fac = clamp(auxiliar,0.0,1.0);

// Return variables
if (Fac == 0.0)
Color = Background;
else
Color = mix(Foreground, Background, Fac);
}

Eduardo Maldonado dijo...

Thank you for your comment Javi, probably a copy / paste error while formatting the article.

Este sitio emplea cookies de Google para prestar sus servicios, para personalizar anuncios y para analizar el tráfico. Google recibe información sobre tu uso de este sitio web. Si utilizas este sitio web, se sobreentiende que aceptas el uso de cookies. Más información Entendido