Search Unity

Voxel terrain generation performance problems

Discussion in 'General Graphics' started by SidiaDev, Mar 15, 2019.

  1. SidiaDev

    SidiaDev

    Joined:
    Mar 15, 2019
    Posts:
    4
    I am trying to create a minecraft clone to practice some stuff in Unity including Raycasting, terrain generation and player controlls.

    Terrain is generating without any problem with a perlinNoise.

    My problem is the then following performance problems.

    The terrain is only 1 block high with no underlying blocks. Each block constist out of 6 planes with a 64x64 texture. I generate terrain at 200 x 200 size.

    I can't imagine why 4000 voxels with such simple textures can lag out whilst my pc runs objects with 50k tris and complex textures without any problem.

    My code:


        [...]

    public int xVoxels = 200;
    public int zVoxels = 200;

    [...]

    for (int x = 0; x < xVoxels; x++){
    for (int z = 0; z < zVoxels; z++){

    // voxel = script parameter
    GameObject newVoxel = GameObject.Instantiate(voxel);
    Vector3 position = new Vector3();

    position.x = xVoxels/2 + x;
    position.z = zVoxels/2 + z;
    // PerlinNoise stripped out for testing
    position.y = 0f;

    newVoxel.transform.position = position;
    }
    }


    Solutions I tried:

    Combining meshes

    Combining all meshes to a single one per material worked wonderfully... until I wanted to interact with each voxel determined by Raycasting from the Camera, I had a raycast hit to the combined mesh and not to my determined voxel

    Generating the planes myself

    Instead of using the unity plane object I generated all vertecies myself, same performance problems
     
  2. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612