HOUDINI - VEX - OSL CONVERSION

Recommended Posts

I’m looking for someone who can convert some OSL code to VEX code.
I have a fractal formula written in OSL that needs to be volume density output VEX.
For now the simple mandelbulb fractal conversion is enough but I'm looking for someone that could convert many other complex formulas.(200-300 lines of code).

New to Houdini, and can't find a way to get this working...after days of looking around, this forum is my last option.

OSL CODE TO CONVERT:

```shader OslGeometry(
int Iterations = 10,
float Power = 2.0,
float Bailout = 20,
output _sdf c = _SDFDEF)
{
vector pos = P;

vector z = P;
float dr = 1.0;
float r = 0.0;
for (int i = 0; i < Iterations ; i++) {
r = length(z);
if (r>Bailout) break;

// convert to polar coordinates
float theta = acos(z[2]/r);
float phi = atan2(z[1],z[0]);
dr =  pow( r, Power-1.0)*Power*dr + 1.0;

// scale and rotate the point
float zr = pow( r,Power);
theta = theta*Power;
phi = phi*Power;

// convert back to cartesian coordinates
z = zr*vector(sin(theta)*cos(phi), sin(phi)*sin(theta), cos(theta));
z+=pos;
}
c.dist = 0.5*log(r)*r/dr;
}```

OSL CODE THAT WORKS BUT NEED IT TO BE LIKE THE OSL:

```
function int Mandel(float x0, y0, z0; int Iterations){
float x, y, z, xnew, ynew, znew, n=ch('n'), r, theta, phi;
int i;

x = x0;
y = y0;
z = z0;

for(i=0; i < Iterations; i++){

r = sqrt(x*x + y*y + z*z);
theta = atan2(sqrt(x*x + y*y) , z);
phi = atan2(y,x);

xnew = pow(r, n) * sin(theta*n) * cos(phi*n) + x0;
ynew = pow(r, n) * sin(theta*n) * sin(phi*n) + y0;
znew = pow(r, n) * cos(theta*n) + z0;

if(xnew*xnew + ynew*ynew + znew*znew > 8){
return(i);
}

x = xnew;
y = ynew;
z = znew;
}
return(Iterations);
}
//--- Main ---

int maxiter = 8;

if(Mandel(@P.x, @P.y, @P.z, maxiter) < maxiter){
@density = 0.0;
}
else {
@density = 1.0;
}```

AAND MY CONVERSION THAT DOES NOT WORK

```function int Fractal(int Iterations, float Power, float Bailout, vector pos, vector z, float dr, float r){

for (int i = 0; i < Iterations ; i++) {
r = length(z);
if (r>Bailout) break;

// convert to polar coordinates
float theta = acos(z.z/r);
float phi = atan2(z.y,z.x);
dr =  pow( r, Power-1.0)*Power*dr + 1.0;

// scale and rotate the point
float zr = pow( r,Power);
theta = theta*Power;
phi = phi*Power;

// convert back to cartesian coordinates
z = zr*vector(sin(theta)*cos(phi), sin(phi)*sin(theta), cos(theta));
z+=pos;
}
return 0.5*log(r)*r/dr;
}

int Iterations = 10;
float Power = 2.0;
float Bailout = 20;
vector pos = @P;
vector z = @P;
float dr = 1.0;
float r = 0.0;
if(Fractal(Iterations,Power,Bailout,@P,@P,dr,r) < Iterations){
@density = 0.0;
}
else {
@density = 1.0;
}```

Cheers,

Scappin Matteo
www.machina-infinitum.com

Share on other sites

Edited by Librarian

Share on other sites

Thanks
I need to convert all my existing OSL fractals code to VEX.
Than I can mesh the VEX fractal and use it as dummy obj on top of Octane Vectron fractals

Share on other sites
On 10/22/2020 at 10:28 AM, machinainfinitum said:

Thanks
I need to convert all my existing OSL fractals code to VEX.
Than I can mesh the VEX fractal and use it as dummy obj on top of Octane Vectron fractals

Hi

```float fractal(int iterations; float power; float bailout; vector pos){

float r = 0.0;
float dr = 0.0;
vector z = pos;

for (int i = 0; i < iterations ; i++) {
r = length(z);
if (r>bailout) break;

// convert to polar coordinates
float theta = acos(z.z/r);
float phi = atan2(z.y,z.x);
dr =  pow( r, power-1.0)*power*dr + 1.0;

// scale and rotate the point
float zr = pow( r,power);
theta = theta*power;
phi = phi*power;

// convert back to cartesian coordinates
z = zr*set(sin(theta)*cos(phi), sin(phi)*sin(theta), cos(theta));
z+=pos;
}
return 0.5*log(r)*r/dr;
}

int iterations = 5;
float power = 5.0;
float bailout = 20.0;
vector pos = @P;

float map = 0.0;
map = fractal(iterations, power, bailout, pos);

@surface = map;```

Share on other sites
14 minutes ago, varomix said:

Hi

```
float fractal(int iterations; float power; float bailout; vector pos){

float r = 0.0;
float dr = 0.0;
vector z = pos;

for (int i = 0; i < iterations ; i++) {
r = length(z);
if (r>bailout) break;

// convert to polar coordinates
float theta = acos(z.z/r);
float phi = atan2(z.y,z.x);
dr =  pow( r, power-1.0)*power*dr + 1.0;

// scale and rotate the point
float zr = pow( r,power);
theta = theta*power;
phi = phi*power;

// convert back to cartesian coordinates
z = zr*set(sin(theta)*cos(phi), sin(phi)*sin(theta), cos(theta));
z+=pos;
}
return 0.5*log(r)*r/dr;
}

int iterations = 5;
float power = 5.0;
float bailout = 20.0;
vector pos = @P;

float map = 0.0;
map = fractal(iterations, power, bailout, pos);

@surface = map;```

This works perfectly ! Thanks so much!
Now I'll try to convert all the formulas I have

Share on other sites

let me know if you need any help

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account