Find the corresponding point index of primitives' vectice

   2126   1   0
User Avatar
Member
1 posts
Joined: Sept. 2021
Offline
Hi guys, I am trying to use the average color of each vertex to recolor the primitives, I know use the attribute promote node can convert the point attribute to the primitive attribute. But I want to use VEX to achieve the same result, and I have some problem now.

My logic is as following:
1. First, use the primvertices to get linear vertex indices for each primitive.
2. Then, convert these vertex indices into point index by using vertexpoint.
3. Finally, access the "Cd" with the point index and calculate the average color for each primitive.

Here is my Vex code that run over primitive:

int verticeIndex[] = primvertices(0, @ptnum);
vector colorArray[] = array();
foreach(int vertexIndex; verticeIndex)
{
    int posIndex = vertexpoint(1, vertexIndex);
    
    vector pointColor = point(1, "Cd", posIndex);
    append(colorArray, pointColor);
}

vector newcolor = sum(colorArray) / len(colorArray);
setprimattrib(0, "Cd", @ptnum, newcolor);

But my code does't work, can some one help me to check my code?
User Avatar
Member
9384 posts
Joined: July 2007
Offline
- in your lines 1 and 12 you need to use @primnum instead of @ptnum
- lines 5 and 7 you are sampling geo from second input (index 1), use 0 to sample 1st unless this is on purpose and you have appropriate correct geo connected to 2nd niput
- you will need to append Attribute Delete afterwards and delete point Cd to see newly created primitive Cd in the viewport

purely with these change this is the code
int verticeIndex[] = primvertices(0, @primnum);
vector colorArray[] = array();
foreach(int vertexIndex; verticeIndex)
{
    int posIndex = vertexpoint(0, vertexIndex);
    
    vector pointColor = point(0, "Cd", posIndex);
    append(colorArray, pointColor);
}

vector newcolor = sum(colorArray) / len(colorArray);
setprimattrib(0, "Cd", @primnum, newcolor);


however you can access points directly using primpoints()
and also since you are iterating over primitives you can use v@Cd directly to write to current prims Cd attrib
so the code would be something like:
v@Cd = 0;
int pts[] = primpoints(0, @primnum);
foreach(int pt; pts){
    v@Cd += point(0, "Cd", pt);
}
v@Cd /= len(pts);
Tomas Slancik
CG Supervisor
Framestore, NY
  • Quick Links