Thursday, June 16, 2022
HomeGame Developmentunity - Attempting to place all chunks for distinct LoD ranges

unity – Attempting to place all chunks for distinct LoD ranges


Effectively, my query is fairly easy to grasp, I am making an attempt to create a grid of chunks however on every step, I need to double the gap of a bit.

Then, that is the code I’ve:

utilizing System.Collections.Generic;
utilizing UnityEngine;

[ExecuteInEditMode]
public class DrawLoDTest : MonoBehaviour
{
    non-public Listing<Vector3> initialChunks;
    public int chunkSize = 16;
    public int lodLevels = 2;
    public int width = 16;
    public int peak = 16;

    non-public Coloration[] colorList;

    // Begin is known as earlier than the primary body replace
    non-public void Begin()
    {
    }

    // Replace is known as as soon as per body
    non-public void Replace()
    {
    }

    non-public void Init()
    {
        if (initialChunks != null && initialChunks.Depend > 0)
            return;

        colorList = new[] { Coloration.blue, Coloration.inexperienced, Coloration.yellow, Coloration.cyan, Coloration.magenta, Coloration.gray };

        Debug.Log("Init take a look at!");
        initialChunks = new Listing<Vector3>(16 * 16);

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

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

        //Debug.Log(string.Be part of(Atmosphere.NewLine, initialChunks.Choose(v => v.ToString())));
    }

    non-public void OnDrawGizmos()
    {
        Init();

        Gizmos.coloration = Coloration.pink;
        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 = peak * 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 = peak / pow;

            Gizmos.coloration = 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 signal = n == 0 ? -1 : 1;

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

                    chunk.x -= pow * halfChunkSize;

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

                    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;
            }
        }
    }
}

Slightly little bit of rationalization:

  • First, I load a 16×16 chunk grid (initialChunks).
  • Then, I begin a loop foreach lod stage (I am testing it for six ranges).
  • Then, foreach axis I iterate its place.
  • Then, in a nested loop foreach axis I give an indication (a loop for 0 to 1, that converts into -1 and 1 for the signal).
  • Then, and that is the extra advanced factor, if the next row/column has an odd variety of components then I attempt to match the present row/column by doubling it, in that method non of the next nook will probably be half of the earlier one.

I’ve two fundamental issues:

lod1

For some purpose, sum and oddSum variables has the improper enter.

lod2

At sure ranges, (for i >= 5) the logic is damaged.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments