VEX Carve

Thanks a lot to Andrew for the initial script. I have rewritten it to work on open curves. I found the setpointattrib to be slow. UV texture is faster to get the uv.x, instead of computing the arclength in the prim wrangle.

vector getlinepos(int primnum; float u) {
    vector pos = primuv(0, "P", primnum, set(u,0,0));
    return pos;
}

int    createpoint(int primnum; vector pos) {
    int ptnum = addpoint(primnum, pos+{0.5,0,0});
    int vertex = addvertex(0, primnum, ptnum);
    return vertex;
}


int oldprim = @primnum;
int count = primvertexcount(0,@primnum)-1;

if(!(hasprimattrib(0, "start"))) f@start = ch("start");
if(!(hasprimattrib(0, "end"))) f@end = ch("end");


if (@start<@end){
    int newprim = addprim(0, "polyline");
    
    //start
    createpoint(newprim, getlinepos(oldprim, @start));
            
    //from prim
    int startvert, endvert;
    startvert  = 1 + floor(@start * count);
    endvert    = 1 + floor(@end * count);
    endvert    = min(endvert, count);
    
    for (int i = startvert; i < endvert; i++)
    {
        createpoint(newprim, point(0, "P", i));
    }
    //end
    createpoint(newprim, getlinepos(oldprim, @end));
} else {
    int newprim;
    newprim = addprim(0, "polyline");

    //from prim
    int startvert, endvert;
    startvert  = 1 + floor(@start * count);
    endvert    = 1 + floor(@end * count);
    endvert    = min(endvert, count);
    
    for (int i = 0; i < endvert; i++)
    {
        createpoint(newprim, point(0, "P", i));
    }
    //end
    createpoint(newprim, getlinepos(oldprim, @end));


    //start
    newprim = addprim(0, "polyline");
    createpoint(newprim, getlinepos(oldprim, @start));

    for (int i = startvert; i <= count; i++)
    {
        createpoint(newprim, point(0, "P", i));
    }

}

removeprim(0, oldprim, 1);

 

Leave a Reply

Your email address will not be published. Required fields are marked *