Search Unity

Drawing on 2D Textures using SetPixel: Drawing on left edge - also appears on right edge

Discussion in '2D' started by ClaasM, Nov 15, 2014.

  1. ClaasM

    ClaasM

    Joined:
    Oct 4, 2014
    Posts:
    26
    Hey,

    In my Project, I am trying to draw (inGame) on a Plane with this method:

    Code (CSharp):
    1. public void Circle(Texture2D tex, int cx, int cy, int r, Color col)
    2.     {
    3.         int x, y, px, nx, py, ny, d;
    4.  
    5.         for (x = 0; x <= r; x++)
    6.         {
    7.             d = (int)Mathf.Ceil(Mathf.Sqrt(r * r - x * x));
    8.             for (y = 0; y <= d; y++)
    9.             {
    10.                 px = cx + x;
    11.                 nx = cx - x;
    12.                 py = cy + y;
    13.                 ny = cy - y;
    14.                 try
    15.                 {
    16.                     pixelArray[py * paper_width + px] = col;
    17.                     pixelArray[py * paper_width + nx] = col;
    18.                 }
    19.                 catch (Exception e) { }
    20.                 try
    21.                 {
    22.                     pixelArray[ny * paper_width + px] = col;
    23.                     pixelArray[ny * paper_width + nx] = col;
    24.                 }
    25.                 catch (Exception e) { }
    26.             }
    27.         }
    28.         tex.SetPixels32(pixelArray);
    29.         tex.Apply();
    30.     }
    (with pixelArray = paper.getPixels32)

    Now, when I draw a line (of Circles) to the left or right edge of the paper, the bit of the Circle that doesnt fit on the Paper appears on the right/left edge of the paper.
    How can I stop that from happening?



    When I draw to the upper/lower edges, it just gives an IndexOutOfBoundsException which is why I used try/catch and which is what I also want to happen for the left/right edges.

    Any help is appreciated.

    Regards, Claas M.
     
  2. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    have you tried changing the clamping?
     
  3. ClaasM

    ClaasM

    Joined:
    Oct 4, 2014
    Posts:
    26
    I tried
    Code (CSharp):
    1. paper.wrapMode = TextureWrapMode.Clamp;
    , with no success.
     
  4. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    well if there is no other easy solution suggested by others then I guess you are going to have to update your code which builds up the pixel array to perform this clamping
     
    ClaasM likes this.
  5. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    i briefly looked at you code and just noticed you redraw the entire texture array every time? Why are you not drawing only the area required for updating? ( unless its something ive overlooked and proves to be way to difficult or less optimal?)
     
  6. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    i would think its better to cache the brush textures array and simply blit it onto the area concerned of the paper texture
     
  7. ClaasM

    ClaasM

    Joined:
    Oct 4, 2014
    Posts:
    26
    I solved it using
    Code (CSharp):
    1. cx = Mathf.Clamp(cx, r, paper_width - r);
    in line 3 of the method.
    The problem is that the radius of the circle differs a lot so that wouldn't bring much performance. But maybe I will take a look at that. Anyway thanks for your help :)
     
  8. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    glad you came right ;)
     
  9. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Yah was going to say, because you are drwing to main memory, you were just drawing past the right edge of a row and the memory simply wraps around to the start of the next row, so you just needed to not draw outside the bounds of the image.