Example #1
0
    void Update()
    {
        currentChunkBounds = new Vector4(((int)debugInsert.position.x / insertSize) * insertSize, ((int)debugInsert.position.y / insertSize) * insertSize, ((int)debugInsert.position.z / insertSize) * insertSize, insertSize);
        if (Input.GetKeyDown(KeyCode.R))
        {
            print("Inserting that cube into the octree...");
            Chunk c = new Chunk();
            c.position = currentChunkBounds;
            c.size     = (int)currentChunkBounds.w;
            octree.AddChunk(c);
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            print("Deleting chunk from octree...");
            Chunk c = octree.FindChunk(currentChunkBounds);
            octree.RemoveChunk(c);
        }
    }
Example #2
0
    public void DoChunkUpdate()
    {
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();

        Vector3 pos        = transform.position;
        float   updateTime = Time.realtimeSinceStartup;

        int numChunksAdded = 0;
        int minCoord       = -radius + 1;
        int maxCoord       = radius + 1;

        Vector3Int prevMin        = Vector3Int.zero;
        Vector3Int prevMax        = Vector3Int.zero;
        int        chunkArraySize = 4 * radius;

        for (int lod = 0; lod < lods; lod++)
        {
            int scale     = (int)Mathf.Pow(2, lod);
            int chunkSize = minimumChunkSize * scale;
            int snapSize  = lod == (lods - 1) ? chunkSize : chunkSize * 2;

            Vector3Int snapped = new Vector3Int(
                (int)Mathf.Floor((pos.x + chunkSize / 2) / snapSize) * snapSize,
                (int)Mathf.Floor((pos.y + chunkSize / 2) / snapSize) * snapSize,
                (int)Mathf.Floor((pos.z + chunkSize / 2) / snapSize) * snapSize);

            if (lod == 0)
            {
                if (initialized && snapped == previousPosition)
                {
                    return;                     // snapped position didn't change, so no new chunks will be added
                }
                previousPosition = snapped;
                initialized      = true;
            }

            Vector3Int min = Vector3Int.one * ((minCoord - 1) * chunkSize) + snapped;
            Vector3Int max = Vector3Int.one * ((maxCoord - 2) * chunkSize) + snapped;

            Vector3Int chunkPos = Vector3Int.zero;
            for (int x = minCoord; x < maxCoord; x++)
            {
                chunkPos.x = ((x - 1) * chunkSize) + snapped.x;
                bool xInBounds = chunkPos.x >= prevMin.x && chunkPos.x <= prevMax.x;

                for (int y = minCoord; y < maxCoord; y++)
                {
                    chunkPos.y = ((y - 1) * chunkSize) + snapped.y;
                    bool yInBounds = chunkPos.y >= prevMin.y && chunkPos.y <= prevMax.y;

                    for (int z = minCoord; z < maxCoord; z++)
                    {
                        chunkPos.z = ((z - 1) * chunkSize) + snapped.z;
                        bool zInBounds = chunkPos.z >= prevMin.z && chunkPos.z <= prevMax.z;

                        if (lod != 0 && (xInBounds && yInBounds && zInBounds))
                        {
                            continue;
                        }

                        int arrx = ((chunkPos.x / chunkSize) % chunkArraySize + chunkArraySize) % chunkArraySize;
                        int arry = ((chunkPos.y / chunkSize) % chunkArraySize + chunkArraySize) % chunkArraySize;
                        int arrz = ((chunkPos.z / chunkSize) % chunkArraySize + chunkArraySize) % chunkArraySize;

                        Vector4Int key = new Vector4Int(arrx, arry, arrz, lod);

                        if (chunks[lod][arrx, arry, arrz] == null)
                        {
                            Chunk chunk = new Chunk();
                            chunk.position     = chunkPos;
                            chunk.key          = key;
                            chunk.lod          = lod;
                            chunk.creationTime = updateTime;
                            chunk.index        = arrayMax++;
                            chunk.size         = chunkSize;

                            numChunksAdded++;
                            chunks[lod][arrx, arry, arrz] = chunk;
                            chunkArray[chunk.index]       = chunk;
                            octree.AddChunk(chunk);
                        }
                        else
                        {
                            chunks[lod][arrx, arry, arrz].creationTime = updateTime;
                        }
                    }
                }
            }

            prevMax = max;
            prevMin = min;
        }

        double ElapsedMilliseconds1 = sw.Elapsed.TotalMilliseconds;

        sw.Restart();

        RemoveOldChunks(updateTime);
        UpdateArrayMin();

        double ElapsedMilliseconds2 = sw.Elapsed.TotalMilliseconds;

        Debug.LogFormat("Chunk Update: S1 {0} ms, S2 {1} ms, Total {2}ms ({3} chunks added)",
                        ElapsedMilliseconds1, ElapsedMilliseconds2, (ElapsedMilliseconds1 + ElapsedMilliseconds2), numChunksAdded);
        sw.Stop();
    }