x


Painting on a texture without crossing mesh triangle.

I am painting onto a mesh with the mouse. Wherever the user clicks, the current colour will be painted in a circular shape of about 32 pixels, centered around the click position. To find the position I have been using a Raycase:

RaycastHit hitInfo;
Ray ray = Camera.main.ScreenPointToRay (inputPos);
if (Physics.Raycast (ray, out hitInfo, Mathf.Infinity, planeMask)) {
    Vector2 hit = hitInfo.textureCoord;

which can be combined with texture.SetPixels() to change the texture to the correct colour.

My problem is this:

I want to only change the colour for the pixels which lie on the triangle I have hit by the raycast. I realize I can get the triangle index from the RaycastHit using hitInfo.triangleIndex which in turn can be used to calculate the pixel positions of each of the vertices in the triangle. But how do I restrict which pixels I want to update and send back to 'texture.SetPixels()'?

more ▼

asked Feb 08 '11 at 10:18 PM

Lemon gravatar image

Lemon
194 5 5 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

I just wrote my own barycentric calculation routine. It's not optimised and don't have error checking/handling, but it works great in my tests.

Vector3 GetBarycentric (Vector2 v1,Vector2 v2,Vector2 v3,Vector2 p)
{
    Vector3 B = new Vector3();
    B.x = ((v2.y - v3.y)*(p.x-v3.x) + (v3.x - v2.x)*(p.y - v3.y)) /
        ((v2.y-v3.y)*(v1.x-v3.x) + (v3.x-v2.x)*(v1.y -v3.y));
    B.y = ((v3.y - v1.y)*(p.x-v3.x) + (v1.x - v3.x)*(p.y - v3.y)) /
        ((v3.y-v1.y)*(v2.x-v3.x) + (v1.x-v3.x)*(v2.y -v3.y));
    B.z = 1 - B.x - B.y;
    return B;
}

bool InTriangle(Vector3 barycentric)
{
    return (barycentric.x >= 0.0f) && (barycentric.x <= 1.0f)
        && (barycentric.y >= 0.0f) && (barycentric.y <= 1.0f)
        && (barycentric.z >= 0.0f); //(barycentric.z <= 1.0f)
}

GetBarycentric just returns the same value you get from hit.barycentricCoordinate, but for any point you like. The point you offer have to be in uv coordinates. Just check every "pixel" you want to draw. If your "circular shape" is generated in pixels i guess that should work too but you have to use the pixelcoordinates instead of uvs for the 3 vertex positions.

Mesh m = hit.collider.GetComponent<MeshFilter>().mesh;
int index = hit.triangleIndex*3;            
Vector2 uv1 = m.uv[m.triangles[index + 0]];
Vector2 uv2 = m.uv[m.triangles[index + 1]];
Vector2 uv3 = m.uv[m.triangles[index + 2]];

Vector2 P = hit.textureCoord;

Vector3 B = GetBarycentric(uv1,uv2,uv3,P);

Debug.Log("hit:" + hit.barycentricCoordinate);
Debug.Log("calculated:" + B);
Debug.Log("inside triangle:" + InTriangle(B));

InTriangle is not tested, but it should work. z don't have to be tested for lower 1 since it's calculated out of x and y.

I hope that helps a bit ;) It's a pity that Unity don't offers a barycentric conversion routine. I just adapted the wikipedia formular

more ▼

answered Feb 09 '11 at 02:15 AM

Bunny83 gravatar image

Bunny83
45.4k 11 49 207

Thanks heaps, especially for the detailed example!

Feb 10 '11 at 01:43 AM Lemon

I've mostly finished the implementation and the concept works. Just wanted to note that due to rounding errors it is possible that a hit coordinate ends up with a barycentric coordinate that is very slightly under 0.0f or over 1.0f. In this case a pixel may not be associated with any triangle. A quick fix which works in my situation was to simply add a alight error tollerance: (barycentric.x >= 0.0f - errorTol) && (barycentric.x <= 1.0f + errorTol) etc.

Feb 11 '11 at 01:02 AM Lemon

Well, the float class have a special const that represents the smallest float value. float.Epsilon can be used to extend the range to reduce the precision problem. In the end i'm sure that one epsilon isn't enough. So setting up your own error tolerance is the best way ;)

Feb 11 '11 at 12:37 PM Bunny83

how do you convert raycast hit coordinates to uv coordinates:

Nov 25 '11 at 08:31 AM ina

@ina: You don't have to convert anything. RaycastHit contains the texture coordinates of the point you've hit. This question was about testing if the hit coordinate is within a certain triangle.

You just need:

hit.textureCoord

which holds the uv-coordinate of your hit point.

Nov 25 '11 at 11:55 AM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2203
x1531
x37
x28
x7

asked: Feb 08 '11 at 10:18 PM

Seen: 1682 times

Last Updated: Nov 25 '11 at 11:55 AM