Tags

, ,

I typically bundle these snippets as presets in a wrangle node as I get to use most of these functions from day to day. This generally gives me a good heads start for similar wrangle tasks and also aids my efficiency.

#ATTRIBUTE TRANSFER COLOR FROM SECOND INPUT

int handle = pcopen(@OpInput2, "P", @P, chf("rad"), chi("num"));
vector lookup_P = pcfilter(handle, "P"); //Average P from second input
vector lookup_Cd = pcfilter(handle, "Cd"); //Average Cd from second input
i@many = pcnumfound(handle);
if(i@many>0){
     @Cd = lookup_Cd;
     v@P = lerp(v@P, lookup_P, chf("mix"));
}

#CALCULATE VOLUME BASED ON BOUNDING BOX

//calculate_volume_based_on_bbox
vector two =    point(0,"P",2);
vector one =    point(0,"P",1);
vector three =  point(0,"P",3);
vector six =    point(0,"P",6);

float l = length(two-one);
float b = length(two-three);
float h = length(two-six);

f@vol = l * b * h * chf("scale"); // scale if values are too small
f@vol = abs(f@vol);

#AVERAGING STUFF

vector accum_p = set(0,0,0);
vector pos;
int count=0;
vector avg_p;
float vlen = length(v@v);
int handle = pcopen(@OpInput1, "P", @P, chf("rad"), chi("num"));
while(pciterate(handle)){
    pcimport(handle,"P",pos);
    accum_p+=pos;
    count+=1;
};
avg_p = accum_p/count;
v@P = lerp(v@P,avg_p,chf("mix"));

#CULL BASED ON RAY DIRECTION

int handle = pcopen(@OpInput1, "P", @P, chf("rad"), chi("num"));
vector up = {0,1,0};
vector dir,pos;
float angle;
i@many = pcnumfound(handle);
while(pciterate(handle)){
    if(i@many>1){
        pcimport(handle,"P",pos);
        dir = pos - @P;
        angle = dot(normalize(dir),up);
        if(angle>chf("mix"))removepoint(geoself(),@ptnum);
    };
};
pcclose(handle);

#REFLECT DOWN FACING VEL ATTRIBUTE ABOUT AN AXIS

vector ref = set(chf("refx"),chf("refy"),chf("refz"));
float dot = dot(normalize(v@v),ref);
vector axis = set(0,1,0);
if(dot<chf("thres"))v@v = reflect(v@v,axis);

#ASSIGN ROTATION MATRIX COMPONENTS TO ATTRIBUTES

assign(v@N.x, v@N.y, v@N.z, v@up.x, v@up.y, v@up.z, v@side.x, v@side.y, v@side.z,3@xform); //Get Rotation Matrix components

#EXTRACT MATRIX COMPONENTS FROM PACKED POINT

v@up=set(0,1,0);
v@v=normalize(v@v);
v@side = cross(v@up,v@v);
3@xform = set(v@v.x, v@v.y, v@v.z, v@up.x, v@up.y, v@up.z, v@side.x, v@side.y, v@side.z); //3D Rotation Matrix
4@from = set(1 , v@v.x, v@v.y, v@v.z, v@up.x, 1 , v@up.y, v@up.z, v@side.x, v@side.y, 1 , v@side.z,v@P.x, v@P.y, v@P.z, 1 ); //3D Transformation matrix

#GET CENTROID

float cex, cey, cez;
vector min, max;
getbbox(0, min, max);
cex = (max.x + min.x) * 0.5;
cey = (max.y + min.y) * 0.5;
cez = (max.z + min.z) * 0.5;
vector centroid = set(cex, cey, cez);

#APPLY SOME NOISE TO THE POSITION ATTRIBUTE

int turb = chi("turb");
float rough = chf("rough");
float atten = chf("atten");
vector mynoise = anoise(@P, turb, rough, atten);
v@P+=mynoise;

  #BLACK AND WHITE MASK AFTER PAINTING WITH THE PAINT SOP

if(@Cd.y<1)@Cd = set(1,1,1);
else (@Cd = set(0,0,0));

  #GENERAL POINT CLOUD OPEN FUNCTION

int handle = pcopen(@OpInput2, "P", @P, chf("rad"), chi("num"));
vector lookup_P = pcfilter(handle, "P");
i@many = pcnumfound(handle);

  #QUATERNION ROTATE

v@N = set(0,1,0);
matrix3 xform = ident();
float amt = chf("amt");
vector axis = set(chf("axisx"),chf("axisy"),chf("axisz"));
rotate(xform, amt, axis);
p@roti = normalize(quaternion(xform));
v@up= qrotate(p@roti,v@N);
v@N*=xform;

  #A RANDOM ROTATION MATRIX

matrix3 myrot = ident();
float amt = fit(rand(@id+chf("seed")),0,1,chf("minamt"),chf("maxamt"));
vector axis = sample_direction_uniform(rand(@id));
rotate(myrot, amt, axis);
@P*=myrot;

  #REDUCE POINTS

if(rand(@id)<ch("kill"))
    removepoint(geoself(), @ptnum);

  #SHRINK WRAP

int handle = pcopen(@OpInput2, "P", @P, chf("rad"), chi("num"));
vector lookup_P = pcfilter(handle, "P");
i@many = pcnumfound(handle);
if(i@many>=1)v@N = @P-lookup_P;
else v@N = set(0,0,0);
@P-=v@N;

  #GENERAL THRESHOLD OPERATION

if(@Cd.x<chf("thres"))removepoint(geoself(),@ptnum);

  #TIGHTEN POINTS

int handle = pcopen(@OpInput1, "P", @P, chf("rad"), chi("num"));
vector lookUp_P = pcfilter(handle, "P");
v@P = lerp(v@P,lookUp_P,chf("mix"));

  #BLEND GEO BETWEEN SUBFRAMES

v@N = v@N; //set normals
vector pp = point(@OpInput2,"P",@ptnum);
float dp = dot(normalize(v@v),normalize(v@N));
float mix = fit(dp,chf("mindot"), chf("maxdot"), chf("min"), chf("max"));
@P = lerp(@P,pp,mix);

  #CULL BY VOLUME (chi(“inv”)) is a check box.

float vsample = volumesample(1,0,@P);
if(chi("inv")==1){
 if(vsample==0) removepoint(geoself(), @ptnum);
}
else{
 if(vsample!=0) removepoint(geoself(), @ptnum);
}

  #DELETE LONE POINTS

int handle = pcopen(@OpInput1, "P", @P, chf("rad"), chi("num"));
i@many = pcnumfound(handle);
if(i@many<chi("num"))removepoint(geoself(),@ptnum);

  #DOT PRODUCT CULLING

vector ref = set(chf("refx"),chf("refy"),chf("refz"));
float dot = dot(normalize(v@v),ref);
if(dot<chf("thres")) @Cd = set(1,0,0); //Visualize
if(dot<chf("thres")) removepoint(geoself(),@ptnum); //Delete

  #LINEAR INTERPOLATIONS

//Interpolate between point positions
vector p1 = point(1,"P",@ptnum);
v@P = lerp(v@P, p1, chf("mix"));

//Interpolate between velocities
vector v1 = point(0,"v",@ptnum);
vector v2 = point(1,"v",@ptnum);
v@v = lerp(v1, v2, chf("mix"));

  #ROTATE BY MATRIX

float xrotamt = radians(chf("xrotamt"));
float yrotamt = radians(chf("yrotamt"));
float zrotamt = radians(chf("zrotamt"));
float cex, cey, cez;
vector min, max,centroid;
vector xrotaxis = set(1,0,0);
vector yrotaxis = set(0,1,0);
vector zrotaxis = set(0,0,1);

//Calculate the Centroid here

getbbox(0, min, max);

cex = (max.x + min.x) * 0.5;
cey = (max.y + min.y) * 0.5;
cez = (max.z + min.z) * 0.5;
centroid = set(cex, cey, cez); 

//Declare identity matrix here. 
3@xform = ident();

//Rotation order is x y z

rotate(3@xform , xrotamt, xrotaxis);
rotate(3@xform , yrotamt, yrotaxis);
rotate(3@xform , zrotamt, zrotaxis);

v@P-=centroid; //move points to origin
@P = @P * 3@xform; //Apply Rotation
v@P+=centroid; //move back to original position

  #TRANSFORM BY PRIMITIVE INTRINSIC

matrix xform = primintrinsic(@OpInput2, "packedfulltransform", 0);
@P*=xform;


#VOLUME FRUSTRUM CULL

vector pndc = toNDC(chs("camera_name"), @P);
// padding
float pad = .2;
if(pndc.x1+pad || pndc.y1+pad || pndc.z>=0 ){
@density=0;
}


#VELOCITY ALONG PATH

int total = npoints(0)-1; //last point
int after = total-1;//penultimate point

int ntm = @ptnum+1;
vector npos = point(0,"P",ntm);
v@v = @P-npos;

//get vel of last point
if(@ptnum==total){
 v@v = point(0,"P",after)-point(0,"P",total);
}

#MOVE POINTS TO GROUND SURFACE (GEO)

int handle = pcopen(@OpInput2, "P", @P, chf("rad"), chi("num"));
vector lp = pcfilter(handle, "P");

i@npt = nearpoint(1,@P);
vector npos = point(1,"P",i@npt);
vector off = set(0,chf("yoffset"),0);

if(@P.y<npos.y){
 vector disp = @P-lp;
 @P-=disp;
 @P+=off;
}

#ROTATE LINE ALONG SELF (TWISTER)

//uvtexture node to compute uv attrib on points
//Texture type is set to Row & Columns

//Get velocity vector along line
int total = npoints(0)-1; //last point
int after = total-1;//penultimate point

int ntm = @ptnum+1;
vector npos = point(0,"P",ntm);
v@v = @P-npos;

//get vel of last point
if(@ptnum==total){
 v@v = point(0,"P",after)-point(0,"P",total);
}


//Make orthogonal frame vectors v, up and side

v@up = set(1,0,0);
v@side = cross(normalize(v@v),normalize(v@up));
v@up = cross(normalize(v@side),normalize(v@v));
v@v = cross(normalize(v@side),normalize(v@up));


//Matrices
/*'offset' is scaled with up vector or side vector
 so the offset position is usually perpendicular to the 
angle of rotation, in the event that the line
is rotatated before applying the twist */

float offset  = chf("offset_multiplier");
float freq = chf("frequency");
float angle = @uv.x * freq;
vector offset_pos = (v@side*offset) + @P;
vector axis = v@v;
matrix3 xform  = ident();

rotate(xform,angle,axis);

//EXTRACTING TRANSFORMS
//Depending on the value of c, 
//returns the translate (c=0), rotate (c=1), or scale (c=2) 
//component of the transform (xform)

#define XFORM_SRT       0  // Scale Rotate Translate
#define XFORM_XYZ       0 // Rx Ry Rz

int trs = XFORM_XYZ; //Transform Order
int xyz = XFORM_SRT; //Rotation Order
vector p = set(0,0,0); //pivot for crack/extracting transforms
vector translate = cracktransform(trs, xyz, 0 , p, xform);
vector rotate    = cracktransform(trs, xyz, 1 , p, xform);
vector scale     = set(1,1,1);
vector pivot     = @P; //pivot for rotations

//MAKE TRANSFORM
matrix newTrans = maketransform(trs, xyz, translate, rotate, scale, pivot);



@P=offset_pos*newTrans;



#MOVE POINTS TO GEOMETRY SURFACE IF INSIDE SDF

//Input 2 is Geo
//Input3 is SDF representation of Geo
int handle = pcopen(@OpInput2, "P", @P, chf("rad"), chi("num"));
vector lp = pcfilter(handle, "P");

i@npt = nearpoint(1,@P);
vector npos = point(1,"P",i@npt);
vector off = set(0,chf("yoffset"),0);
vector disp = @P-lp;
float vsample = volumesample(2,0,@P); 

if(vsample<0)@P-=disp;

#APPLY RANDOM ROTATION TO VECTOR ON ALL AXIS (AXES)

float amt = 0;
float randx, randy, randz;
vector axis = set(0,0,0);



int npts2 = npoints(1); //Number of points in second input
int lidx2 = npts2-1; //Index of penultimate point
vector stemdir = point(1,"P",lidx2) - point(1,"P",0); //Main stem direction


amt = fit(rand(@id+chf("rotation amt_seed")),0,1,chf("min_rotation_amt"),chf("max_rotation_amt"));
randx = fit(rand(@id+chf("x_axis_seed")),0,1,chf("x_lower_limit"),chf("x_uppper_limit"));
randy = fit(rand(@id+chf("y_axis_seed")),0,1,chf("y_lower_limit"),chf("y_uppper_limit"));
randz = fit(rand(@id+chf("z_axis_seed")),0,1,chf("z_lower_limit"),chf("z_uppper_limit"));

axis = set(randx,randy,randz);

//create identity matrix to use for rotatating stem dir
matrix3 idm = ident();
rotate(idm,amt,axis); //Rotate the matrix

v@N = stemdir; //visualize through normal

@N*=idm; //Apply transformation

#MOVE POINTS TO SURFACE (SDF)

float vsample = volumesample(1,0,@P);
vector dirtosurf = volumegradient(1, 0, @P);
@P -= normalize(dirtosurf) * (vsample - chf("offset"));
Advertisements