Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What's the best way to save a player's drawing on my server?

Discussion in 'General Graphics' started by ez06, Jan 13, 2021.

  1. ez06

    ez06

    Joined:
    Feb 18, 2014
    Posts:
    45
    In my game, the player has to draw something with black lines only.

    I will upload this drawing to my server and the player will be able to download it again next time he launches the game.

    So I could either:

    -generate a black-and-white PNG file and upload it to my server

    -create an array of waypoints for my lineRenderer, send it to my database, and recreate the drawing from that array.

    My main concern is speed/bandwidth.

    Which method is the best option for efficiency?
     
  2. ez06

    ez06

    Joined:
    Feb 18, 2014
    Posts:
    45
    Anyone?
     
  3. ZombieTFK

    ZombieTFK

    Joined:
    Sep 6, 2016
    Posts:
    55
    That's just 1-bit raster vs vector graphics. They're not really comparable besides the fact they can output an image. So it will depend entirely upon your requirements. Typically vector graphics are smaller in size.
     
  4. ez06

    ez06

    Joined:
    Feb 18, 2014
    Posts:
    45
    Thanks for your response.
    Basically I'm wondering whether I'm better off storing:

    A) a B&W png file on my server
    B) an array of Vector2's (containing probably 200-300 points) in my database

    Important: There will be a larger number of images and requests to my web server: every time a player launches the game, it will fetch new images on my server.

    If (hopefully) there are lots of requests, this will make a massive difference on bandwidth and speed.
     
  5. HarryPigskin

    HarryPigskin

    Joined:
    Jan 14, 2021
    Posts:
    6
    Doing the math is quite easy if you're interested.

    A Vector2 is 2 floats, each float is 4 bytes each, and you said between 200 and 300 points.
    So if we plug the numbers in:
    2 * 4 bytes * 300 = 2400 bytes (300 to be on the safe side)
    2400 bytes / 1024 = 2.34375 kilobytes

    A rather small amount of data, probably not something too concerning if players aren't saving the image multiple times a second.
    If this is a multiplayer game the amount of bandwidth used to update two players positions will likely exceed that rather quickly.

    If you think of position and orientation saved as a two Vector3s, two players, a tickrate of 20hz you wind up with just two people playing together exceeding the bandwidth in:
    Vector3 = 3 floats, position and orientation = 2 Vector3s, player count = 2.
    3 * 2 * 2 * 4 bytes = 48 bytes a tick.
    48 * 20 = 960 bytes a second.
    2400/960 = 2.5 seconds

    So two players will use an image represented by Vector2 array of bandwidth every 2.5 seconds just to see each other.
    I think tickrates of games are often higher than 20hz also so times may be even smaller.

    Now the PNG is harder for me to tell because I do not know the dimensions.
    In optimal circumstances a greyscale image should be storable using only a single byte per pixel, so an image of roughly 50x50 pixels should take up the same amount of space pre-compression, assuming PNG's compression can decipher that an image is greyscale and not waste any bits in compression.

    I'm having a hard time finding a good source for the compression level of PNGs, so your range may be a little larger than 50x50.

    However if the image is purely Black or White with no inbetween you could simply save every pixel as an individual bit, which would be rather small and easy to develop compression methods for.

    For example: If you're saving a 64x64 image as 128 integers, you could have 4 ints which represent every integer which is 0, saving up to 124 integers of space on an empty image and taking up 132 ints of space in a case where every integer has at least one black and white pixel in them.


    Something else to consider is that rendering a complex line renderer of 200+ points will likely be far more taxing on your player's PCs than simply rendering an image onto a texture, if you have a scene with a lot of photos as you've described them it may cause issues on lower end machines if they were all represented in game as line renderers.


    Personally, I wouldn't worry too much about what technique you are using right now, if you run into problems later down the line simply swap to the other technique. There's a programmer saying I quite like in these situations:
    Premature optimization is the root of all evil.


    All the best,
    Harry.
     
    Last edited: Jan 14, 2021
    ez06 likes this.
  6. ez06

    ez06

    Joined:
    Feb 18, 2014
    Posts:
    45
    Hi there,
    Thanks a lot for that.
    I forgot to mention it's for a mobile game.

    Basically, there will be about 100 images, and each of them will be some kind of tag, drawn by each player. And it will be visible to all other players. The tags will always have the same position, within the map.

    Every time a player launches the game, it fill fetch new images on my server, and the rendering and positioning will happen ONCE per game session.

    So the game will only go only once through the 300 vector2's, of all 100 images.

    Also, this will happen during the launch of the game, so I believe it won't have any incidence on the in-game performance.

    I'm tempted to go for the vector2's instead of png. That would save me the hassle of dealing with uploading files to my server.

    Please let me know if you have any suggestions
    Thanks again