Пример #1
0
        IEnumerator GenChunks()
        {
            chunks   = new CVM_Chunk[RadCount(radius)];
            instMap  = new Dictionary <Vector2Int, CombinedVoxelMesh>(chunks.Length);
            voxelMap = new Dictionary <Vector2Int, Voxel[]>(chunks.Length);

            Vector3Int chunkSize = chunkPrefab.GetComponent <CombinedVoxelMesh>().settings.size;

            csx = chunkSize.x;
            csy = chunkSize.y;
            csz = chunkSize.z;

            viewPos = GetViewpos();

            posStack = new Stack <Vector2Int>(chunks.Length);

            int i = 0;

            for (float y = 0.5f - radius, rsq = (radius + bias) * (radius + bias); y < radius; y++)
            {
                for (float x = 0.5f - radius; x < radius; x++)
                {
                    if (x * x + y * y <= rsq)
                    {
                        Vector2Int xz = viewPos + new Vector2Int((int)(x - 0.5f), (int)(y - 0.5f));
                        GameObject o  = Instantiate(chunkPrefab, new Vector3(xz.x * chunkSize.x, 0f, xz.y * chunkSize.z), Quaternion.identity, transform);

                        CombinedVoxelMesh CVM = o.GetComponent <CombinedVoxelMesh>();
                        chunks[i++]  = new CVM_Chunk(xz, viewPos, CVM);
                        instMap[xz]  = CVM;
                        voxelMap[xz] = CVM.voxels;

                        o.name = $"Chunk {i}";
                        if (i % 256 == 0)
                        {
                            print($"{i} Chunks Generated");
                        }
                        yield return(null);
                    }
                }
            }

            print($"{i} Chunks Generated");
            generated = true;
        }
Пример #2
0
        IEnumerator UpdateChunks(Vector2Int vpos, int msLimit = 5)
        {
            viewPos = vpos;

            if (coro != null)
            {
                StopCoroutine(coro);
                coro = null;
            }

            sw.Restart();

            for (float y = 0.5f - radius, rsq = (radius + bias) * (radius + bias); y < radius; y++)
            {
                for (float x = 0.5f - radius; x < radius; x++)
                {
                    if (x * x + y * y <= rsq)
                    {
                        Vector2Int p = vpos + new Vector2Int((int)(x - 0.5f), (int)(y - 0.5f));
                        if (!instMap.ContainsKey(p))
                        {
                            posStack.Push(p);
                        }
                    }
                }
            }

            for (int i = 0; i < chunks.Length; i++)
            {
                CVM_Chunk c   = chunks[i];
                Vector2   dif = c.pos + new Vector2(0.5f, 0.5f) - vpos;

                if (dif.x * dif.x + dif.y * dif.y > (radius + bias) * (radius + bias))
                {
                    Vector2Int nPos = posStack.Pop();

                    if (instMap.ContainsKey(nPos))
                    {
                        print("taken!");
                    }

                    CombinedVoxelMesh cvm = c.CVM;
                    instMap.Remove(c.pos);
                    instMap[nPos] = cvm;
                    chunks[i].pos = nPos;

                    cvm.gameObject.transform.position = new Vector3(nPos.x * csx, 0f, nPos.y * csz);

                    if (voxelMap.ContainsKey(nPos))
                    {
                        cvm.voxels = voxelMap[nPos];
                    }
                    else
                    {
                        cvm.voxels = voxelMap[nPos] = new Voxel[cvm.voxels.Length];
                        cvm.FillVoxels();
                    }
                    cvm.Regenerate();

                    if (sw.ElapsedMilliseconds >= msLimit)
                    {
                        sw.Restart();
                        yield return(null);
                    }
                }

                chunks[i].center = vpos;
            }

            posStack.Clear();

            /*for (int i = 0; i < chunks.Length; i++) {
             *      CVM_Chunk c = chunks[i];
             *      Vector2 dif = c.pos + new Vector2(0.5f, 0.5f) - vpos;
             *
             *      if (dif.x * dif.x + dif.y * dif.y > (radius + bias) * (radius + bias)) {
             *              Vector2Int nPos = c.center + vpos - c.pos - new Vector2Int(1, 1);
             *
             *              if (instMap.ContainsKey(nPos)) print("taken!");
             *
             *              CombinedVoxelMesh cvm = c.CVM;
             *              instMap.Remove(c.pos);
             *              instMap[nPos] = cvm;
             *              chunks[i].pos = nPos;
             *
             *              cvm.gameObject.transform.position = new Vector3(nPos.x * csx, 0f, nPos.y * csz);
             *
             *              if (voxelMap.ContainsKey(nPos))
             *                      cvm.voxels = voxelMap[nPos];
             *              else {
             *                      cvm.voxels = voxelMap[nPos] = new Voxel[cvm.voxels.Length];
             *                      cvm.FillVoxels();
             *              }
             *              cvm.Regenerate();
             *
             *              if (sw.ElapsedMilliseconds >= msLimit) {
             *                      sw.Restart();
             *                      yield return null;
             *              }
             *      }
             *
             *      chunks[i].center = vpos;
             * }*/
        }