# U coordinate on closed prim

## Recommended Posts

How can I read the U coordinate (from 0 to 1) of closed primitives in VEX?

I tried xyzdist and primuv ...

int prim;
vector uv;
xyzdist(0, @P, prim, uv);
v@uv_profile = set(uv.x, 0, 0);

... but I only got results that did not represent the relative distance between the vertices.

Edited by konstantin magnus

##### Share on other sites

Hi, would a non-vex approach fit your needs?

1. Convert SOP: Convert to Nurbs; U Order 2.
2. UV Texture SOP: Type Arc Length; Attrib as Point.
3. Use the resulting uv.x point attribute.

u_coord.hiplc - H16.0.689 Indie

##### Share on other sites

Thank you! But yes, I am more looking for a VEX solution..

##### Share on other sites

No worries, I tried to have go at it in VEX, but I'm not entirely sure if the results are correct or accurate.

In the attached file, as it turns out, the initial SOPs method (in red) I suggested does seem to appear incorrect when laid out as a straight line. The VEX method (in green) seems to visually appear more correct.

Compare the points 4 to 8..

If I turn off the Closed toggle of the Curve SOP, the RED line will match the GREEN line.

Have a look at the setup, and see if it anywhere near the direction you wanted.
u_coord2.hiplc - H16.0.689 Indie

My head is kinda twirling at the moment, I may need to sleep this through for now :))

Cheers!
Jeff

• 2

##### Share on other sites

@galagast, thanks! I updated my own snippet with closure check to work like yours.

Here is what I got:

// Detail wrangle.
float length = primintrinsic(0, "measuredperimeter", 0);

// Set to false if value of 1.0 is unwanted for the last point
// of closed prims and instead need to be treated as if point with
// u=1 is coincident with the first point (like in unrolled prims).
int as_open = true;

// Compensate for closing auto-edge length.
if (as_open && primintrinsic(0, "closed", 0))
{
vector auto_edge = point(0, "P", 0) - point(0, "P", @numpt-1);
length -= length(auto_edge);
}

// Compute curve u.
float passed = 0;
for (int i = 1; i < @numpt; i++)
{
vector d = point(0, "P", i) - point(0, "P", i-1);
passed += length(d);
setpointattrib(0, "u", i, passed/length);
}

Difference between behaviors "as open" and "as unrolled":

• 2
• 1

##### Share on other sites

Alright, thank you both! I mainly wanted to make sure I am not missing something obvious. Like there is no intrinsic U attribute or a dedicated function.

Edited by konstantin magnus

##### Share on other sites

I think primuvconvert function will be helpful for intrinsic uv attribute.

An example of code to get relative distance between the vertices.

// Run Over -> Points
int prim;
vector uv;
xyzdist(0, @P, prim, uv);

int mode = 4; // PRIMUV_UNIT_TO_UNITLEN
u@uv_profile = primuvconvert(@OpInput1, set(uv.x, 0.0), prim, mode);

Edited by satoru
• 3

##### Share on other sites
8 hours ago, satoru said:

I think primuvconvert function will be helpful for intrinsic uv attribute.

Really great! Thank you!

## 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