Skip to main content
copy-paste error
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

Your code shown here doesn't actually depend on GameObjects - they're just serving as an expensive way for you to store a set of positions. So you could get something equivalent as follows, just using transformation matrices instead:

// You can assign the default Unity cube here so you have a template mesh to stamp-out.
public Mesh cubeMesh;

public static Mesh GenerateChunkMesh(int[,,] map, int chunkSize, Vector3Int chunkPosition)
{
    // Prep a list of mesh instances to stitch together.
    List<CombineInstance> instances = new List<CombineInstance>();

    // Prepare a "template" object with the attributes we want to apply to each block.
    CombineInstance instance;
    instance.mesh = cubeMesh;

    for (int x = 0; x < chunkSize; x++)
        for (int z = 0; z < chunkSize; z++)
            for (int y = 0; y < chunkSize; y++) {                    
                Vector3Int cubePosition = chunkPosition + new Vector3Int(x, y, z);

                // If this part of the map is empty, skip it without making a block.
                if(map[cubePosition.x, cubePosition.y, cubePosition.z] == 0)
                    continue;

                // Make a transformation matrix that positions the cube at this location.
                instance.transform = Matrix4x4.Translate(cubePosition);

                // Record "I want a cube at this position" in our work list.
                instances.Add(instance);
            }
        }
    }

    // Build a mesh that combines all of the requested cubes in our work list.
    var mesh = new Mesh();
    mesh.CombineMeshes(combineinstances.ToArray(), true, true);

    return mesh;
}

But note that this is still rendering more geometry than you need, because you're not eliminating hidden faces. This answer outlines how you can do that with your own mesh generation routine.

Your code shown here doesn't actually depend on GameObjects - they're just serving as an expensive way for you to store a set of positions. So you could get something equivalent as follows, just using transformation matrices instead:

// You can assign the default Unity cube here so you have a template mesh to stamp-out.
public Mesh cubeMesh;

public static Mesh GenerateChunkMesh(int[,,] map, int chunkSize, Vector3Int chunkPosition)
{
    // Prep a list of mesh instances to stitch together.
    List<CombineInstance> instances = new List<CombineInstance>();

    // Prepare a "template" object with the attributes we want to apply to each block.
    CombineInstance instance;
    instance.mesh = cubeMesh;

    for (int x = 0; x < chunkSize; x++)
        for (int z = 0; z < chunkSize; z++)
            for (int y = 0; y < chunkSize; y++) {                    
                Vector3Int cubePosition = chunkPosition + new Vector3Int(x, y, z);

                // If this part of the map is empty, skip it without making a block.
                if(map[cubePosition.x, cubePosition.y, cubePosition.z] == 0)
                    continue;

                // Make a transformation matrix that positions the cube at this location.
                instance.transform = Matrix4x4.Translate(cubePosition);

                // Record "I want a cube at this position" in our work list.
                instances.Add(instance);
            }
        }
    }

    // Build a mesh that combines all of the requested cubes in our work list.
    var mesh = new Mesh();
    mesh.CombineMeshes(combine.ToArray(), true, true);

    return mesh;
}

But note that this is still rendering more geometry than you need, because you're not eliminating hidden faces. This answer outlines how you can do that with your own mesh generation routine.

Your code shown here doesn't actually depend on GameObjects - they're just serving as an expensive way for you to store a set of positions. So you could get something equivalent as follows, just using transformation matrices instead:

// You can assign the default Unity cube here so you have a template mesh to stamp-out.
public Mesh cubeMesh;

public static Mesh GenerateChunkMesh(int[,,] map, int chunkSize, Vector3Int chunkPosition)
{
    // Prep a list of mesh instances to stitch together.
    List<CombineInstance> instances = new List<CombineInstance>();

    // Prepare a "template" object with the attributes we want to apply to each block.
    CombineInstance instance;
    instance.mesh = cubeMesh;

    for (int x = 0; x < chunkSize; x++)
        for (int z = 0; z < chunkSize; z++)
            for (int y = 0; y < chunkSize; y++) {                    
                Vector3Int cubePosition = chunkPosition + new Vector3Int(x, y, z);

                // If this part of the map is empty, skip it without making a block.
                if(map[cubePosition.x, cubePosition.y, cubePosition.z] == 0)
                    continue;

                // Make a transformation matrix that positions the cube at this location.
                instance.transform = Matrix4x4.Translate(cubePosition);

                // Record "I want a cube at this position" in our work list.
                instances.Add(instance);
            }
        }
    }

    // Build a mesh that combines all of the requested cubes in our work list.
    var mesh = new Mesh();
    mesh.CombineMeshes(instances.ToArray(), true, true);

    return mesh;
}

But note that this is still rendering more geometry than you need, because you're not eliminating hidden faces. This answer outlines how you can do that with your own mesh generation routine.

Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

Your code shown here doesn't actually depend on GameObjects - they're just serving as an expensive way for you to store a set of positions. So you could get something equivalent as follows, just using transformation matrices instead:

// You can assign the default Unity cube here so you have a template mesh to stamp-out.
public Mesh cubeMesh;

public static Mesh GenerateChunkMesh(int[,,] map, int chunkSize, Vector3Int chunkPosition)
{
    // Prep a list of mesh instances to stitch together.
    List<CombineInstance> instances = new List<CombineInstance>();

    // Prepare a "template" object with the attributes we want to apply to each block.
    CombineInstance instance;
    instance.mesh = cubeMesh;

    for (int x = 0; x < chunkSize; x++)
        for (int z = 0; z < chunkSize; z++)
            for (int y = 0; y < chunkSize; y++) {                    
                Vector3Int cubePosition = chunkPosition + new Vector3Int(x, y, z);

                // If this part of the map is empty, skip it without making a block.
                if(map[cubePosition.x, cubePosition.y, cubePosition.z] == 0)
                    continue;

                // Make a transformation matrix that positions the cube at this location.
                instance.transform = Matrix4x4.Translate(cubePosition);

                // Record "I want a cube at this position" in our work list.
                instances.Add(instance);
            }
        }
    }

    // Build a mesh that combines all of the requested cubes in our work list.
    var mesh = new Mesh();
    mesh.CombineMeshes(combine.ToArray(), true, true);

    return mesh;
}

But note that this is still rendering more geometry than you need, because you're not eliminating hidden faces. This answer outlines how you can do that with your own mesh generation routine.