For each vertex you want to color, to find the face it’s attached to you’ll have to iterate over the triangle list to find a reference to the vertex index. The triangle list is actually a list of vertex indices where each 3 vertex indices is one triangle. So once you find a match it’ll either be the previous two indices, next two indices, or surrounding indices that make up the triangle depending on the current triangle list index.
for (int t=0; t<tris.Length; t++)
{
if (tris[t] == i) // closest vertex i
{
int subIndex = t % 3;
if (subIndex == 0)
// apply color to t+1 & t+2
else if (subIndex == 1)
// apply color to t-1 and t+1
else
// apply color to t-2 & t-1
}
}
Thanks for the reply much appreciated, I used your code and implemented but … It doesn’t work.
It paints the wrong ones. Gosh I’m missing something obvious here but I don’t get why.
Your original example code was getting a vertex index. My example code took that vertex index, and finds the appropriate range in the tri array.
The new code you posted is getting the triangle index from a raycast, not the vertex index. To get the vertex indices for that triangle it’s just that tri index value * 3, +1, +2. No need to search though anything at that point.
Yes you’re right … then I want to paint the triangles that are within a radius of my brush; the question is then : “how to find the neighbors from that triangle index” ?
If you just want all triangles within the radius, then you’ll want to iterate over all of the vertex positions like you were before, then iterate over the triangle list to find the vertices of those triangles.
If you want to only color triangles that are both in range, and “connected” to the first triangle you touched, that’s a lot harder. That requires iterating over all of the triangles to find those that have shared edges, or at least shared vertex positions. Since your mesh is using hard edged normals, each triangle has unique vertices after being imported into Unity, so you have to compare each vertex’s position against all other vertices and match ones that are approximately the same. (Note, the built in == comparison for Vector3 values is already an “approximate equal” which should be good enough).
For the connected triangles? It’s a lot of recursive nested loops. I would suggest not trying to do the “connected” test at all. No point if your mesh is terrain-like mesh like you have as all of the triangles are guaranteed to be connected, so the test would be a very expensive function that would always return true.
If you want to find all triangles that have a vertex in range, and are connected to the raycasted triangle in a continuous way, then you could go about it in this way.
Get the vertices of the triangle you hit:
v0 = tris[triangleIndex * 3]
v1 = tris[triangleIndex * 3 +1]
v2 = tris[triangleIndex * 3 + 2]
For each vertex, iterate over the vertex positions to find other vertices with the same position:
for (i=0; i<vertexCount; i++)
if (i != v0 && i != v1) && i != v2) && (vertices == vertices[v0] || vertices == vertices[v1] || vertices[v2]) // is connected vertex Then find all of the vertex indices for that triangle using the first example I gave you, and set their color. Then, for all of those vertices that aren’t the original triangle’s, if they’re in range, find the vertices & triangles that are attached to that, color, repeat until you run out.
I guess you didn’t see my reply; 3 mins before yours; so I got it almost right; just missing the last triangles; if you have a trick for that I owe you one beer
I compare the vertex from where the brush hit (index “i”) to the one I parse index “z” but at the end I’m always missing the last triangles that form a quad; if we could figure out this that would be super.
You’re comparing the 3 vertices from triangle “i” to the 3 vertices in triangle “z”, but that’s completely arbitrary. You have no idea how the first vertex of triangle “i” relates to the first vertex of triangle “z”, so that comparison is likely causing some of saw toothed the weirdness you’re seeing.
Here’s a basic example. Let’s take 4 triangles in a strip like this:
Now, you have a brush with a radius that’s slightly less than a single triangle wide, and your brush’s center hits the red triangle.
The red triangle’s vertices are in this order: EBC.
Your code now iterates over the triangles one by one. Let’s say the blue triangle is triangle 0, and its vertices are DAB. Let’s compare each vertex in that order, D-E fails, as does A-B, as does B-C. Blue is not colored.
Now let’s say green is triangle 1, with the order DBE. D-E fails again, but the next vertex is B-B, and that succeeds! So green is colored.
Triangle 2 is the red triangle, that is already colored, but it gets colored again since obviously each vertex is close enough to itself. Triangle 3 is the yellow triangle, with the order ECF. The first vertex succeeds, so it too is colored.
So now we have this:
Notice the saw tooth shape forming? This is why you shouldn’t arbitrarily compare vertices in a specific order. You do not know how that order relates to other meshes, or how that might impact the results. Do the distance comparison against the hit position, not the hit triangle or its vertices.
The quad isn’t really a thing though. It’s all just triangles by the time you’re looking at it from code. There are no quads though, only triangles. Any concept of quads innate to the mesh was lost when you imported or exported, or whenever the mesh was triangulated. Sure there’s an implicit quad there, but you’d have to figure out which edge is the “diagonal” edge of the triangle, which two vertices make up that edge, and then find another triangle that has vertices in both of those locations.
Thank you very much for taking the time to draw and explain; I understood why I should compare arbitrary vertices but I still don’t understand what distance you compare to what
for (i=0; i<vertexCount; i++)
if (i != v0 && i != v1) && i != v2) && (vertices == vertices[v0] || vertices == vertices[v1] || vertices[v2])
If that’s helping I have written some code that turn any mesh into a half edge structure by collapsing split vertex, it might help you iterate through vertices more easily (it keep a reference to the triangle index of the mesh)?
If you’re looking to color all triangles within range of the brush, then you need to be doing a distance check between each triangles’ vertex positions and the brush’s center position to see if it is within the brush size. The curHit.point value in the function that does the raycast would be that brush position, so you should be passing that along too.
If you’re looking to find triangles that are “next” to each other, you need to compare each of the 3 vertices of a triangle against all 3 vertices of the next, individually. And you need to do it recursively, so once you check if a triangle is attached, you need to find triangles that attach to that triangle, and triangles that attach to that triangle, etc. But if you do it naively, you’ll run into infinite loops, so you’d probably want to keep a list of triangles you’ve already found in a list and skip those triangles when they’re found as a neighbor. You might also only want to look for neighbors of one or two vertices of a triangle at a time rather than all 3 after the first triangle depending on how many matched the previous triangle to limit the search space since the previous search for “that vertex” presumably already found all its neighbors.
But it’s not clear to me that you need to do neighbor searches since all of the example meshes you’ve shown have been terrain-like. Hence my previous suggestion you skip this entirely and only use distance to the brush position.
Finding “quads” is similar, but since it’s not recursive you can be a little dirtier there. Like I said before, you need to find the “diagonal” edge. Assuming your meshes are nicely axis aligned, and y is up, you can compare a triangles 3 vertices to find the two that are not aligned along an axis. Those will be the two vertices that make the “diagonal” edge. Then you’d search through all of the triangles to find another that also has two vertices that match those positions. You could also only do this check on triangles where only that right angle corner vertex is the one within the brush range, as if any other vertex is in range the matching triangle to make the quad will have already been found.
pseudo code:
foreach triangle
{
int inRangeCount = 0;
bool triangleInRange = false;
foreach triangle vertex
{
if ((vertexPos - brushPos).sqrMag < sqrBrushRadius)
{
triangleInRange = true;
inRangeCount++;
}
}
if triangleInRange
foreach triangle vertex
colors[vertex index] = brushColor;
if (inRangeCount == 1)
{
Vector3 diagVertPos0, diagVertPos1;
// find "corner" vertex, if in range, set positions from other two vertices
// otherwise continue loop, skipping the rest of this code
foreach triangle
{
if (triangle == current triangle) // don't check itself
continue
foreach triangle vertex
if (vertexPos == diagVertPos0)
foreach triangle vertex
if (vertexPos == diagVertPos1)
// this is the matching tri to make the "quad", color all vertices, and break out of loop
}
}
}
The triangles array is in sets of 3 vertices, so it should be < triangles.Length / 3 for both triangle loops. With out that you’re going to be going outside the bounds of the triangles array.
The “diagonal” edge is not guaranteed to be the longest edge in 3D space. If you have a decently steep slope, the right angle’s edges can be longer than the diagonal edge. You need to compare the distance (or square magnitude) in 2D space ignoring the height offset.
float f0f1 = (f0.x - f1.x) * (f0.x - f1.x) + (f0.z - f1.z) * (f0.z - f1.z); // assuming your mesh is y up
I’d also strongly recommend checking if the one vertex that is in range was the right angle corner vertex and skipping the second iteration if it is not.
bool cornerInRange = false;
if (f0f1 > f0f2 && f0f1 > f1f2) {
diagVertPos0 = f0;
diagVertPos1 = f1;
cornerInRange = (f2 - curHit.point).sqrMagnitude < brushSize;
}
// other two checks
if (!cornerInRange)
continue;
for (int x = 0; x < triangles.Length; x++) {
// etc
Also, I’m assuming the “brushSize” value is actually brushRadius * brushRadius?
Now the only issue is that the painting doesn’t exactly occur on the hit point; moving the brush around i see it only paints on the ‘edges’ of the brush; i can’t figure out why …
This shape is when i put the brush to the edge of the mesh.
[edit: nevermind it doesn’t work, something is f***ed up]
Here’s a hint. The diagVertPos0 and diagVertPos1 values should only be needed inside the scope of the if (inRangeCount == 1) condition. If you move the lines where they’re defined to be just after that if statement, nothing should break (but it will with how you have your code).
I also don’t know if your mesh is Y up or not, so the diagonal & “right angle” vertices might not be correct.