Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Drawing lines with pixels (like MSPaint)

Discussion in '2D' started by yboris, Jan 10, 2015.

  1. yboris

    yboris

    Joined:
    Jan 8, 2015
    Posts:
    1
    I'm trying to replicate the Windows game I made 6 years ago Gravity Wars this time for iOS
    Coding it was simple: I painted a pixel where I wanted on screen and thus created trails for my flying projectiles. I thought I could use "Trail Renderer " in unity, but when I destroy the object that creates the trail, the trail goes away. Also, It looks like it will be a mess trying to dim each trail by some amount over time (in the original game I just drew a black rectangle across the screen at low opacity and was done).

    Could anyone suggest what method would be best? I saw Textur2D.SetPixel option, but there's a warning that it's an "expensive operation".
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    SetPixel isn't expensive; the warning is about uploading the texture, which is expensive—the bigger the texture, the slower it is, which makes it unfeasible for realtime updating of screen-size textures. Instead, you can divide the screen into many smaller textures and just update the ones that actually need to change.

    If you want a simpler, more performant way, there's an asset for line-drawing. Can't remember the name...starts with "V" or something....

    --Eric
     
    yboris likes this.
  3. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    When dividing into smaller textures, beware the obvious fact that draw calls become much higher which in itself can slow things down. Would be so much better for Unity to offer uploads to a) only a portion of an existing texture, not the whole thing, and b) zapping a byte array to a portion of an existing texture without having to go through any conversion or internal storage or whatever. It should be so much faster than it is.
     
  4. QuantumFlin

    QuantumFlin

    Joined:
    Dec 4, 2014
    Posts:
    1
    The lines you are drawing on the screen seem relatively smooth for the most part, so you might be able to replace the "drawing to texture" thing by leaving behind 1x10 or so Sprite objects that would make up the line. You would do that until you have too many Sprites, then you would basically do one huge draw call on the texture and delete the sprites, then start over again.
    That way you could avoid uploading a texture to GPU for most of the frames, although it is more complicated to implement...
     
  5. SpaceMammoth

    SpaceMammoth

    Joined:
    Jan 2, 2013
    Posts:
    220
    Sorry for answering an old post, but I can't resist. I would advice not to do this using pixels there is no need in Unity. For Mammoth Gravity Battles (a gravity wars update, would you believe), I use Unity's trail renderer, and it is perfect. Your particles trace a path and a beautiful line is left behind.

     
  6. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    There's Vectrosity and also a free script called "Drawing.cs" here in the forums that lets you draw vector-based lines.