Search Unity

Artifacts while using SetPixel32

Discussion in 'Scripting' started by Igris, Jun 14, 2018.

  1. Igris


    Apr 10, 2013
    Hello world!

    I wrote some code to draw on screen, but I get blank spaces between my drawings like the ones shown in the bottom line on the picture when moving the mouse too quickly.

    Because this is part of a larger application, I have the following constraints:
    I have to draw on a procedurally generated fully transparent texture, applied to an Unlit/Transparent material, applied to a GameObject.

    To do this, I am using the following code:
    Code (CSharp):
    1. void Update()
    2. {
    3. if (Input.GetMouseButton(0))
    4.         {
    5.             tex.SetPixels32((int)pixelUV.x, (int)pixelUV.y, brushWidth, brushHeight, colorToPaint);
    6.         }
    7. }
    The texture is a power of 2 and it's 512 x 512. It's declared and assigned here:
    Code (CSharp):
    1.  tex = new Texture2D(width, height, TextureFormat.RGBA32, true);
    2.         drawingCanvas.GetComponent<Renderer>().material.mainTexture = tex;
    And the brush (colorToPaint) is just a small array of 3 x 3 pixels.

    I am only drawing on a single object, using Color32, and using SetPixels32 instead of SetPixel. I am aware of line renderers, and I have an app for drawing with them but I cannot use those for this particular scenario.

    The FPS when running varies, but it's 74fps at its lowest. Any ideas, am I missing something obvious?

  2. Kurt-Dekker


    Mar 16, 2013
    You have to manually draw the pixels between each sample you get from the mouse.