kunz VEX Wrangle Snippets

VEX Wrangle Snippets

From kunz
Revision as of 16:41, 26 October 2021 by Admin (talk | contribs) (Created page with "== Iterative Face Insetting == Triangular recursive edge splitting https://twitter.com/d_gfx/status/1452718124579520512 <syntaxhighlight lang='C'> int pts[] = primpoints(0,@pr...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Iterative Face Insetting

Triangular recursive edge splitting https://twitter.com/d_gfx/status/1452718124579520512

int pts[] = primpoints(0,@primnum); vector pos[];
int edge_div_pts[]; vector edge_div_pos[];
foreach( int pt; pts ) 
    append(pos, vector(point(0,'P',pt)));
for( int i = 0; i < chi('iterations'); i++ )    {
    resize(edge_div_pts,0); // empty
    resize(edge_div_pos,0); // empty
    for( int j = 0; j < len(pts); j++ ) {
        vector pt_pos = lerp( pos[j], pos[(j+1)%len(pts)], chf('div_ratio') );
        append(edge_div_pts, addpoint(0, pt_pos));
        append(edge_div_pos, pt_pos);
    }
    for (int k = 0; k < len(pts); k++ ) {
        addprim(0, 'poly', pts[k], edge_div_pts[k], edge_div_pts[(k+2)%len(pts)]);
    }
    pts = edge_div_pts;
    pos = edge_div_pos;
}
addprim(0, 'poly', edge_div_pts);
removeprim(0,@primnum,1);

Boxify

Distorts the geometry into a box shape

vector centroid = getbbox_center(0);
vector size = getbbox_size(0);
size = min(size); // Largest component
@P -= centroid;
@P *= (1.0/size);
@P = lerp(@P, @P+clamp(normalize(@P)*1.75,vector(-1),vector(1)) * (1.0-length(max(abs(@P)))), chf('blend'));
@P *= size;
@P += centroid;

Spherify

Distorts the geometry into a sphere shape Spherify.gif

vector centroid = getbbox_center(0);
vector size = getbbox_size(0);
size = min(size); // Largest component
@P -= centroid;
@P *= (1.0/size);
@P = lerp(@P, normalize(@P), chf('blend'));
@P *= size;
@P += centroid;

Snap to Grid

Snap point positions to a grid

float grid_scale = chf('grid_scale'); // try 8
@P = rint(@P*grid_scale)/grid_scale;

Swizzle Coordinate System

Switch between Y up and Z up coordinates

@P = @P.xzy * set(-1,1,1);

Twist

Twirl the geometry around the Y axis

float angle = chf('angle');
vector origin = set(0,0,0);

vector d = v@P*set(1,0,1) - origin;
float r = length(d);
r = pow(r, chf('pow'));
float beta = atan(d.x, d.z) + radians(angle) * r;
v@P = origin + r * set(sin(beta), 0, cos(beta)) + set(0,@P.y,0);

alt

float angle = @P.y*900;
vector origin = set(0,0,0);

vector d = v@P*set(1,0,1) - origin;
float r = length(d);
float beta = atan(d.x, d.z) + radians(angle);
v@P = origin + r * set(sin(beta), 0, cos(beta)) + set(0,@P.y,0);

Peak

Push mesh along the surface normals

@P += normalize(@P) * chf('scale');

Exploded View

Jitter each point by a random spherical direction

@P += normalize(@P) * chf('scale');


Point Jitter

Jitter each point by a random spherical direction

@P += sample_sphere_uniform(rand(@elemnum+chf('seed'))) * chf('scale');

Plexus Effect

Connect nearby points

foreach(int pt; nearpoints(0, @P, 0.5, 250))    {
    if(pt > @ptnum)
        addprim(0, 'polyline', @ptnum, pt);
}

Connect to Nearest Point

Draw a line to the closest point of the second input

addprim(0, 'polyline', @ptnum, addpoint(0, vector(point(1,'P',nearpoint(1,@P)))) );

Connect to Closest Surface Position

Draw a line to the closest surface position

addprim(0, 'polyline', @ptnum, addpoint(0, minpos(1,@P) );

Randomize the Rotation of Packed Primitives

Update the transform intrinsic to a random orientation

vector r = sample_direction_uniform(rand(@primnum));
matrix3 x = primintrinsic(0,'transform',i@primnum);
rotate(x, PI*pow(rand(@primnum-666),0.5), r);
setprimintrinsic(0,'transform',i@primnum,x);

Randomize the Scale of Packed Primitives

Update the transform intrinsic to a apply a random scale

vector s = rand(i@primnum);
s = s.yyy;  // Uniform Scale
matrix3 x = primintrinsic(0,'transform',i@primnum);
scale(x, s);
setprimintrinsic(0,'transform',i@primnum,x);

Random Color from Normal Direction

Generate random colors based on the surface normal

v@Cd = rand(rint(v@N*8));