Search Unity

Question 2D Voxel Terrain

Discussion in 'Scripting' started by tfalmeida91, Apr 29, 2021.

  1. tfalmeida91

    tfalmeida91

    Joined:
    Aug 3, 2018
    Posts:
    8
    Hi guys!

    I've been trying to mess around with Procedural 2D Terrain Generation using Voxels.
    I've followed this tutorial playlist:

    2D Voxel Terrain Generation Tutorial

    But I've hit brickwall that I'm not being able to bring down.

    Not only it gets slow when destroying or creating terrain when the therrain already has many different types of squares (like grass stone and etc), but creating the colliders is even slower (I've tried using 2D Box Coliders and Polygon Colliders, both generated using the MeshFilters Mesh), even if I do it using Coroutines.

    Is there any good tutorial out there that you can point me to? Any tip or starting point, or anything I should be looking at before starting on this journey? I'm not even interested in Marching Squares, only in 2D Voxels.

    If there is any need, I'll share the code.

    Thank you in advance!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    It's possible that tutorial doesn't make a performant implementation.

    You could use the profiler (Window -> Analysis -> Profiler) to investigate what is taking so much time.

    Alternately you could try another tutorial, but it isn't simple to see a tutorial and decide if it would be performant.
     
  3. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    hmmm I know in the past I tried around with runtime editing of 2D TileMaps (as in place/ remove/ change a tile in my world on runtime) and that worked perfectly fine as far as I remember
     
  4. tfalmeida91

    tfalmeida91

    Joined:
    Aug 3, 2018
    Posts:
    8
    I did xD

    The problem lies in the loops, they take too long to process data, and if I do them in coroutines, the collider takes a bit to be generated, so if I created a new block, the collider would appear after about 1 sec or so.
     
  5. tfalmeida91

    tfalmeida91

    Joined:
    Aug 3, 2018
    Posts:
    8
    I only wish mine would be going just as well x'D
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    As always with performance the key is to do less or to do it less often.

    This may involve cleverly caching stuff that doesn't change, or only updating stuff that is very close to the player (or other enemies), etc.
     
  7. tfalmeida91

    tfalmeida91

    Joined:
    Aug 3, 2018
    Posts:
    8
    That is something I actually thought of, of course that for now, given testing porpuses, I am inserting and removing blocks every frame.

    I think I'm going to have to rethink all my code, I do not believe it to be as efficient as it could/should.
     
  8. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    Well what I was trying to say earlier, was - i might be wrong because i only skipped through those tutorial videos but - it looked like your tutorial is essentially reimplementing a new way to make tilemaps

    though unity already has something for tilemaps, see the https://docs.unity3d.com/Manual/class-Tilemap.html

    as far as I can remember its a bit awkward coding wise to change tilemaps on runtime, but it is perfectly doable and when I did it i didnt have much performance issues

    you might be able to do what you want using thoses instead of reimplementing a tilemap system from scratch
     
  9. tfalmeida91

    tfalmeida91

    Joined:
    Aug 3, 2018
    Posts:
    8
    Yeah, my ignorance got the best of me x'D

    As soon as I heard about the Tilemap I tried something, which was basicaly 1000x more tiles than those tutorials and it worked with little to no impact on FPS. I think I'll stick to this one :p

    Thank you very much ;)!!!