Jump to content

HOUDINI - VEX - OSL CONVERSION


machinainfinitum

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:unsure:

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;
}

 

Thanks in advance.

Cheers,

Scappin Matteo
www.machina-infinitum.com

Link to comment
Share on other sites

  • 5 months later...
On 10/22/2020 at 10:28 AM, machinainfinitum said:

Thanks :D 
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

not sure if you still care about this but I fixed your code

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;

 

hou_sdf.png

Link to comment
Share on other sites

14 minutes ago, varomix said:

Hi

not sure if you still care about this but I fixed your code


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;

 

hou_sdf.png

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...