|
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:
which can be combined with 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
(comments are locked)
|
|
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.
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.
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 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: which holds the uv-coordinate of your hit point.
Nov 25 '11 at 11:55 AM
Bunny83
(comments are locked)
|
