#Encoding Barycentric coordinates into vertex colors
1 messages · Page 1 of 1 (latest)
// This applies the barycentric coordinates to each vertex in a triangle.
[maxvertexcount(3)]
void geom(triangle v2f IN[3], inout TriangleStream<g2f> triStream) {
float edgeLengthX = length(IN[1].vertex - IN[2].vertex);
float edgeLengthY = length(IN[0].vertex - IN[2].vertex);
float edgeLengthZ = length(IN[0].vertex - IN[1].vertex);
float3 modifier = float3(0.0, 0.0, 0.0);
// We're fine using if statments it's a trivial function.
if ((edgeLengthX > edgeLengthY) && (edgeLengthX > edgeLengthZ)) {
modifier = float3(1.0, 0.0, 0.0);
}
else if ((edgeLengthY > edgeLengthX) && (edgeLengthY > edgeLengthZ)) {
modifier = float3(0.0, 1.0, 0.0);
}
else if ((edgeLengthZ > edgeLengthX) && (edgeLengthZ > edgeLengthY)) {
modifier = float3(0.0, 0.0, 1.0);
}
g2f o;
o.pos = UnityObjectToClipPos(IN[0].vertex);
o.barycentric = float3(1.0, 0.0, 0.0) + modifier;
triStream.Append(o);
o.pos = UnityObjectToClipPos(IN[1].vertex);
o.barycentric = float3(0.0, 1.0, 0.0) + modifier;
triStream.Append(o);
o.pos = UnityObjectToClipPos(IN[2].vertex);
o.barycentric = float3(0.0, 0.0, 1.0) + modifier;
triStream.Append(o);
}```
This is the shader code I am attempting to move over into a C# method
I found someone's cs online that does the triangles correctly, but I haven't had much success refactoring their code to include the additional longest side culling
I've tried 3-4 attempts to write the whole thing from scratch (eg. iterate through each triangle, assign the vertex colors correctly, test longest, add that extra, assign vert colors)
but none of my attempts to write it from scratch functioned as desired, and only theirs even does the triangles correctly
here is my various attempts, ultimately none of them improved upon the original code
@native linden The issue might be with shared vertices? I think with the shader it's at the point when it's dealing with the triangles so it never has to care about the value of a vertex affecting other triangles since it's already past that stage, while in vertex color you would probably need every single triangle to have unique vertices (you might be able to get around it with some very careful mesh but idk)
Not sure though
I thought the same but currently the mesh is flat shaded to solve that, there are no shared vertexes
I almost have it working (probably not actually?) but I am running into a wall
private List<int[]> _GetSortedTriangles(int[] triangles)
{
List<int[]> result = new List<int[]>();
for (int i = 0; i < triangles.Length; i += 3)
{
List<int> t = new List<int> { triangles[i], triangles[i + 1], triangles[i + 2] };
t.Sort();
result.Add(t.ToArray());
}
return result;
}
List<int[]> triangles = _GetSortedTriangles(mesh.triangles);
triangles.Sort((int[] t1, int[] t2) =>
{
int i = 0;
while (i < t1.Length && i < t2.Length)
{
if (t1[i] < t2[i]) return -1;
if (t1[i] > t2[i]) return 1;
i += 1;
}
if (t1.Length < t2.Length) return -1;
if (t1.Length > t2.Length) return 1;
return 0;
});
Color[] colors = new Color[vertexCount];
for (int i = 0; i < vertexCount; i++)
colors[i] = vertexLables[i] > 0 ? _COLORS[vertexLables[i] - 1] : _COLORS[0];```
their code (across different methods) fucks around with sorting orders and rearranging
I can't just GET the three world positions of the vertexes per triangle from this
I've tried dozens of ways to get the right vertexes in the right order, but its buried under so much shit
shit that I dont understand but is critical for the code to work to do it in this exact way
this is the most recent cleaned up code
they repeatedly do all kinds of shit to the vertex order
I can't figure out how to extract from this the exact final vertex order to use the exact same order in my method to test longest edge
private Color[] ComputeLongestEdge(List<int[]> triangles, int[] vertexLables, int vertexCount)
{
Color[] modColors = new Color[vertexCount];
foreach (int[] triangle in triangles)
{
int vertexIndex0 = triangle[0];
int vertexIndex1 = triangle[1];
int vertexIndex2 = triangle[2];
// this is the problem: HOW DO I FIND THE VORRECT VERTEXES.
Vector3 vertex0 = vertexLables[vertexIndex0].vertex;
Vector3 vertex1 = vertexLables[vertexIndex1].vertex;
Vector3 vertex2 = vertexLables[vertexIndex2].vertex;
float edgeLengthX = Vector3.Distance(vertex1, vertex2);
float edgeLengthY = Vector3.Distance(vertex0, vertex2);
float edgeLengthZ = Vector3.Distance(vertex0, vertex1);
Vector3 modifier = Vector3.zero;
if ((edgeLengthX > edgeLengthY) && (edgeLengthX > edgeLengthZ))
{
modifier = new Vector3(1.0f, 0.0f, 0.0f);
}
else if ((edgeLengthY > edgeLengthX) && (edgeLengthY > edgeLengthZ))
{
modifier = new Vector3(0.0f, 1.0f, 0.0f);
}
else if ((edgeLengthZ > edgeLengthX) && (edgeLengthZ > edgeLengthY))
{
modifier = new Vector3(0.0f, 0.0f, 1.0f);
}
Color modColor = new Color(modifier.x, modifier.y, modifier.z);
modColors[vertexIndex0] = modColor;
modColors[vertexIndex1] = modColor;
modColors[vertexIndex2] = modColor;
}
return modColors;
}```
this is my method for determining the longest edge
but it needs reference to the vector3 positions, which dont exist, because its passing all kinds of wtf sorted vertex labels around
Are they sorting the vertex array or just the triangles array?
I have no idea, thats part of the problem, they sort like 3 times across 5 different lists. Relvant code:
none of this makes any sense to me, all I want from this is the world positions of each triangles vertexes, in the exact same order as this
the reason I need it in the exact same order is because I need to add colors to each vertex, but the colors being added must line up exactly with the colors they're adding
and every time I try to do this, it fails, because I am not adding the color to the correct vertexes bcause I cant find the order theyre using
theirs working on the left, mine not working because I cant find the exact same order theirs uses
It looks to me like they don't reorder the vertices array, so the 3 ints in each triangle should still point to the same (correct) vertices?
then I dont know what the problem is, only that there is a problem and I cant solve it
the result is obviously completely fucked and unusuable
but I cant figure out how to fix it because I'm just too stupid
their code works, mine doesnt, ergo I am the problem, but knowing that doesnt help me fix my mistakes to make it work, make it go
im starting to get distressed and go into crisis so im just going to quit again for now
if I change the order in the longest edge code
you can see more obviously how it keeps getting the order wrong
I need the right order
but the order is wrong
and I cant figure out the order because theyve hidden it behind 20 layers of stupid bullshit
and im just too stupid to disentangle it
wrong order, again
its clear the wrong colors are being assigned to the wrong vertexes
but I cant parse the correct
yeah im stumped