# Trying to position all chunks for distinct LoD levels

Well, my question is pretty simple to understand, I'm trying to create a grid of chunks but on each step, I want to double the distance of a chunk.

Then, this is the code I have:

using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class DrawLoDTest : MonoBehaviour
{
private List<Vector3> initialChunks;
public int chunkSize = 16;
public int lodLevels = 2;
public int width = 16;
public int height = 16;

private Color[] colorList;

// Start is called before the first frame update
private void Start()
{
}

// Update is called once per frame
private void Update()
{
}

private void Init()
{
if (initialChunks != null && initialChunks.Count > 0)
return;

colorList = new[] { Color.blue, Color.green, Color.yellow, Color.cyan, Color.magenta, Color.grey };

Debug.Log("Init test!");
initialChunks = new List<Vector3>(16 * 16);

for (var i = 0; i < width * height; i++)
{
var x = i % width;
var y = i / width;

initialChunks.Add(new Vector3(x * chunkSize - width * chunkSize / 2, 0, y * chunkSize - height * chunkSize / 2));
}

//Debug.Log(string.Join(Environment.NewLine, initialChunks.Select(v => v.ToString())));
}

private void OnDrawGizmos()
{
Init();

Gizmos.color = Color.red;
foreach (var chunk in initialChunks)
{
Gizmos.DrawWireCube(chunk, Vector3.one * chunkSize);
}

var ww = 0;
var ww2 = 0;
var sum = 0;
var halfChunkSize = chunkSize / 2;
var halfWidth = width * halfChunkSize;
var halfHeight = height * halfChunkSize;

for (var i = 1; i <= lodLevels; i++)
{
if (ww > 0)
++ww2;

var pow = (int)Mathf.Pow(2, i);

var w = width / pow;
var h = height / pow;

Gizmos.color = colorList[i - 1];

var oddSum = ww * chunkSize * pow;

for (var x = -1 - ww2; x <= w + ww2; x++)
{
for (var n = 0; n <= 1; n++)
{
var sign = n == 0 ? -1 : 1;

var chunk = new Vector3(
halfWidth - x * chunkSize * pow - halfChunkSize,
0,
halfHeight * sign - halfChunkSize - chunkSize * 2 * sign + oddSum * sign + sum * sign);

chunk.x -= pow * halfChunkSize;

for (var j = i + 1; j >= i; j--)
{
chunk.z += chunkSize * (int)Mathf.Pow(2, j) / 2 * sign;
}

Gizmos.DrawWireCube(chunk, Vector3.one * chunkSize * pow);
}
}

// TODO
if ((w + ww2 * 2 + 2) / 2 % 2 != 0)
{
Debug.Log(\$"[{i}, {ww}, {ww2}] W: {w} + {ww2} * 2 = {w + ww2 * 2} + 2 = {w + ww2 * 2 + 2} / 2 = {(w + ww2 * 2 + 2) / 2}");

if (ww == 0)
{
--i;
++ww;
}
else
{
ww = 0;
sum += oddSum;
}

}
else
{
if (ww2 > 0)
ww2 = 0;
}
}
}
}


A little bit of explanation:

• First, I load a 16x16 chunk grid (initialChunks).
• Then, I start a loop foreach lod level (I'm testing it for 6 levels).
• Then, foreach axis I iterate its position.
• Then, in a nested loop foreach axis I give a sign (a loop for 0 to 1, that converts into -1 and 1 for the sign).
• Then, and this is the more complex thing, if the following row/column has an odd number of elements then I try to match the current row/column by doubling it, in that way non of the following corner will be half of the previous one.

I have two main problems:

For some reason, sum and oddSum variables has the wrong input.

At certain levels, (for i >= 5) the logic is broken.