Antony Jones
Antony Jones

Reputation: 581

Grid/Block Terrain GameObject

I am trying to learn Unity, but i have plenty of experience with C# as a professional developer so this question is solely regarding how to to represent the model in Unity.

I am trying to build a block based terrain with a more finely grained height, my model looks something like this:

[
    {
        "x":0,
        "y":0.2,
        "z":0,
        "type": "grass"
    },
    {
        "x":1,
        "y":0.3,
        "z":0,
        "type": "grass"
    },
    {
        "x":0,
        "y":0.1,
        "z":1,
        "type": "stone"
    },
    {
        "x":1,
        "y":0.4,
        "z":1,
        "type": "stone"
    }
]

I see 2 options:

  1. Create a cube for every block.
  2. Build a mesh for the exposed surfaces of all blocks combined together when viewed from above.

1 seems easy to do, but potentially slow, whereas 2 seems like the better approach, but i don't see how to go about applying different textures for each block.

Upvotes: 1

Views: 721

Answers (1)

Marco Elizondo
Marco Elizondo

Reputation: 562

Depending if you are making some kind of Minecraft world or just a simple voxel terrain you could have different solutions.

A simple solution is using quads instead of cubes: A cube is made of 6 quads, instead of creating each cube, you could create each quad for then exposed surfaces, you keep performance and is easy to implement if it's a simple terrain.

If you want more complex structures like Minecraft does, you could implement more complex algorithms where you create (and update) each polygon in an optimized way, like Greedy Meshing algorithm. Here is a Unity implementation.

Also you may take a look to Unity's DOTS where the Entity Component System promise performance while having a lot of objects.

An example video of how will look using Unity DOTS + Greedy Meshing.

EDIT

I just found a GitHub repo using Unity's DOTS to create a Minecraft style world:

Voxel Game Prototype in Unity ECS

Upvotes: 1

Related Questions