Search Unity

All texture pixels within a radius

Discussion in 'Scripting' started by GeodeLX, Mar 12, 2015.

  1. GeodeLX

    GeodeLX

    Joined:
    Nov 28, 2013
    Posts:
    7
    I've got a little project I'm working on in Unity. My problem involves a point, a mesh, and a texture... and math.

    The texture is mapped onto the mesh (all triangles, of course), and the user selects a point somewhere on the mesh. Of course, this corresponds to a point on the texture. Imagine that there is a circle on the mesh (radius <R>) whose origin/center is at the selected point, and that the circle is planar (not a sphere). This circle maps to a group of points on the texture, and I want to color/shade all those points. How do I quickly calculate which pixels in the texture image correspond to the area covered by the circle?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    - Raycast against the mesh to get the UV coordinate associated with where the user clicked
    - Get the heigh/width of the texture being modified
    - Multiply height/width by UV x/y to get pixel location on the texture itself
    - Add/subtract half your "brush size" to the pixel coordinates and clamp to 0/bounds on texture
     
  3. GeodeLX

    GeodeLX

    Joined:
    Nov 28, 2013
    Posts:
    7
    KelsoMRK,
    Thanks.

    One problem I'm concerned about is that while triangles on a mesh are connected (assuming they have common vertices), the corresponding triangles on the texture map may be disconnected. The equation has to consider discontiguous texture mapping... I think.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That's true and becomes much trickier to negotiate.

    One thing worth mentioning is that there are a fair number of mesh painting solutions on the Asset Store already. It might be worthwhile to investigate those before proceeding further.
     
  5. pranav_suresh

    pranav_suresh

    Joined:
    Jun 19, 2022
    Posts:
    8
    Hi, I didn't get why we multiply the raycasthit texturecordinates with width and height of texture2d to set position of exact click. Could you please exlpain the maths? .
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    UV coordinates are in a range from 0 to 1. Think of it as a "percentage" of how much the pixel is left/right/top/bottom. So if your UV coordinate is (0.5, 0.5) then you're exactly 50% of the texture in both dimensions. So if your texture is 100x100 then (0.5, 0.5) is at (50, 50). If you're texture is 200x200 then you're at (100, 100) but the UV coordinate is still (0.5, 0.5) in both cases.
     
    pranav_suresh likes this.
  7. pranav_suresh

    pranav_suresh

    Joined:
    Jun 19, 2022
    Posts:
    8
    Thanks a lot, got it cleared.
     
  8. strikeric11

    strikeric11

    Joined:
    Sep 7, 2016
    Posts:
    13

    I can't figure out what do you mean clamp to 0/bounds on texture. Below is my code how can I know if that pixel is not in radius



    1. int pixelX = (int)(textureCoord.x * _templateDirtMask.width);
    2. int pixelY = (int)(textureCoord.y * _templateDirtMask.height);

    3. int pixelXOffset = pixelX - (_brush.width / 2);
    4. int pixelYOffset = pixelY - (_brush.height / 2);

    5. for (int x = 0; x < _brush.width; x++)
    6. {
    7. for (int y = 0; y < _brush.height; y++)
    8. {
    9. Color pixelDirt = _brush.GetPixel(x, y);
    10. Color pixelDirtMask = _templateDirtMask.GetPixel(pixelXOffset + x, pixelYOffset + y);

    11. float removedAmount = pixelDirtMask.g - (pixelDirtMask.g * pixelDirt.g);

    12. dirtAmount -= removedAmount;

    13. _templateDirtMask.SetPixel(
    14. pixelXOffset + x,
    15. pixelYOffset + y,
    16. new Color(0, pixelDirtMask.g * pixelDirt.g, 0)
    17. );
    18. }
    19. }

    20. _templateDirtMask.Apply();
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If, after subtracting half your brush size, the value is less than 0, make it 0. If, after adding half your brush size, the value is greater than the max dimension of your texture, make it the max dimension of your texture.