Skip to content

Commit c0cd53b

Browse files
committed
Updated Chunk Generation Loop
1 parent db952d3 commit c0cd53b

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

Assets/Voxels/Scripts/Chunk.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ public void Remesh()
6060
});
6161
}
6262

63-
public Chunk(Vector3 chunkPos)
63+
public Chunk(Vector2 chunkPos)
6464
{
6565
float blockSize = Generation.BLOCK_SIZE;
6666
// Remember, our voxels are smaller than 1 unit. Using "chunkPos.x * CHUNK_WIDTH" would give us spacing as if each block was 1 unit. Multiply to get correct world space
67-
this.chunkPos = new Vector3Int((int)(chunkPos.x * (CHUNK_WIDTH * blockSize)), 0, (int)(chunkPos.z * (CHUNK_LENGTH * blockSize)));
67+
this.chunkPos = new Vector3Int((int)(chunkPos.x * (CHUNK_WIDTH * blockSize)), 0, (int)(chunkPos.y * (CHUNK_LENGTH * blockSize)));
6868

6969
this.chunkObj = new GameObject("Chunk", typeof(MeshFilter), typeof(MeshRenderer));
7070
this.chunkObj.GetComponent<Renderer>().material = Generation.instance.terrainMat;

Assets/Voxels/Scripts/Generation.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System;
2-
using System.Collections;
3-
using System.Collections.Concurrent;
4-
using System.Threading;
5-
using System.Threading.Tasks;
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using Unity.VisualScripting;
64
using UnityEngine;
75
using Voxels.Scripts.Utils;
86

@@ -64,14 +62,25 @@ private void Start()
6462
private IEnumerator InititalizeChunks()
6563
{
6664
Performance.Reset();
67-
for (int x = 0; x < 16; x++)
68-
{
69-
for (int z = 0; z < 16; z++)
65+
66+
Vector2 chunkPos = Vector2.zero;
67+
for(int i = 0; i < 256; i++)
68+
{
69+
// i % 16 gives you the x coordinate, which cycles from 0 --> 15.
70+
// i / 16 gives you the z coordinate, which increases every 16 steps.
71+
int x = i % 16;
72+
int z = i / 16;
73+
74+
chunkPos.x = x;
75+
chunkPos.y = z;
76+
new Chunk(chunkPos);
77+
78+
if (i % 2 == 0) // Generates 2 chunks per frame, i.e. we don't "yeild" every single time a chunk is loaded, we yeild when 2 chunks are loaded
7079
{
71-
Chunk chunk = new Chunk(new Vector3(x, 0, z));
72-
if (z % 2 == 0) yield return null;
80+
yield return null;
7381
}
7482
}
83+
7584
Debug.Log("Gathering Metrics.");
7685

7786
yield return new WaitForSeconds(30);

0 commit comments

Comments
 (0)