Пример #1
0
    private void Awake()
    {
        int renderDistance = Snap(this.renderDistance, Chunk.size.x / 2) * 8;

        if (seed == "")
        {
            seed = UnityEngine.Random.Range(int.MinValue, int.MaxValue).ToString();
        }
        wseed  = GetRawSeed(seed);
        random = new System.Random(wseed);
        Debug.Log("Your Generated Seed: " + GetRawSeed(seed));
        ChunkTextureController.Initialize(Application.streamingAssetsPath + "/firstpass-texture.png", material);
        player         = Camera.main.transform;
        worldGenerator = new Thread(new ThreadStart(delegate()
        {
            while (true)
            {
                try
                {
                    while (pendingDeletions.Count > 0)
                    {
                        Thread.Sleep(15);
                    }
                    Vector3Int Player = new Vector3Int(Snap(playerPos.x, Chunk.size.x), 0, Snap(playerPos.z, Chunk.size.z));
                    int minX          = Player.x - renderDistance;
                    int maxX          = Player.x + renderDistance;
                    int minZ          = Player.z - renderDistance;
                    int maxZ          = Player.z + renderDistance;
                    for (int z = minZ; z < maxZ; z += Chunk.size.z)
                    {
                        for (int x = minX; x < maxX; x += Chunk.size.x)
                        {
                            Vector3Int vector = new Vector3Int(x, 0, z);
                            Chunk chunk       = null;
                            chunks.TryGetValue(vector, out chunk);
                            if (chunk == null)
                            {
                                Chunk nchunk = new Chunk(vector);
                                nchunk.GenerateBlockArray_Normal(wseed, random);
                                nchunk.GenerateMesh();
                                chunks.Add(vector, nchunk);
                            }
                        }
                    }

                    foreach (var chunk in chunks.Values)
                    {
                        if (chunk != null)
                        {
                            if (chunk.isReady)
                            {
                                Vector3Int vector = chunk.position;
                                if (vector.x > maxX ||
                                    vector.x < minX ||
                                    vector.z > maxZ ||
                                    vector.z < minZ)
                                {
                                    pendingDeletions.Add(vector);
                                }
                            }
                        }
                    }
                }
                catch (Exception) { }
                Thread.Sleep(1);
            }
        }));
        worldGenerator.Start();
    }
Пример #2
0
    public void Execute()
    {
        int index = 0;

        for (int x = 0; x < Chunk.size.x; x++)
        {
            for (int y = 0; y < Chunk.size.y; y++)
            {
                for (int z = 0; z < Chunk.size.z; z++)
                {
                    if (blocks[index] == Block.air /* || x == 0 || y == 0 || z == 0
                                                   || x == Chunk.size.x - 1
                                                   || z == Chunk.size.z - 1*/)
                    {
                        faces[index] = 0;
                        index++;
                        continue;
                    }

                    if (z == 0)
                    {
                        faces[index] |= (byte)Direction.South;
                        sizeEstimate += 4;
                    }
                    else if (z > 0 && blocks[index - 1] == Block.air)
                    {
                        faces[index] |= (byte)Direction.South;
                        sizeEstimate += 4;
                    }

                    if (z == Chunk.size.z - 1)
                    {
                        faces[index] |= (byte)Direction.North;
                        sizeEstimate += 4;
                    }
                    else if (z < Chunk.size.y - 1 && blocks[index + 1] == Block.air)
                    {
                        faces[index] |= (byte)Direction.North;
                        sizeEstimate += 4;
                    }

                    if (y == 0)
                    {
                        faces[index] |= (byte)Direction.Down;
                        sizeEstimate += 4;
                    }
                    else if (y > 0 && blocks[index - Chunk.size.z] == Block.air)
                    {
                        faces[index] |= (byte)Direction.Down;
                        sizeEstimate += 4;
                    }

                    if (y == Chunk.size.y - 1)
                    {
                        faces[index] |= (byte)Direction.Up;
                        sizeEstimate += 4;
                    }
                    else if (y < Chunk.size.y - 1 && blocks[index + Chunk.size.z] == Block.air)
                    {
                        faces[index] |= (byte)Direction.Up;
                        sizeEstimate += 4;
                    }

                    if (x == 0)
                    {
                        faces[index] |= (byte)Direction.West;
                        sizeEstimate += 4;
                    }
                    else if (x > 0 && blocks[index - Chunk.size.z * Chunk.size.y] == Block.air)
                    {
                        faces[index] |= (byte)Direction.West;
                        sizeEstimate += 4;
                    }

                    if (x == Chunk.size.x - 1)
                    {
                        faces[index] |= (byte)Direction.East;
                        sizeEstimate += 4;
                    }
                    else if (x < Chunk.size.x - 1 && blocks[index + Chunk.size.z * Chunk.size.y] == Block.air)
                    {
                        faces[index] |= (byte)Direction.East;
                        sizeEstimate += 4;
                    }

                    isVisible = true;

                    index++;
                }
            }
        }

        if (!isVisible)
        {
            return;
        }

        vertices  = new Vector3[sizeEstimate];
        uvs       = new Vector2[sizeEstimate];
        triangles = new int[(int)(sizeEstimate * 1.5f)];

        index = 0;
        for (int x = 0; x < Chunk.size.x; x++)
        {
            for (int y = 0; y < Chunk.size.y; y++)
            {
                for (int z = 0; z < Chunk.size.z; z++)
                {
                    if (faces[index] == 0)
                    {
                        index++;
                        continue;
                    }

                    if ((faces[index] & (byte)Direction.North) != 0)
                    {
                        vertices[vertexIndex]     = new Vector3(x + position.x, y + position.y, z + position.z + 1);
                        vertices[vertexIndex + 2] = new Vector3(x + position.x, y + position.y + 1, z + position.z + 1);
                        vertices[vertexIndex + 1] = new Vector3(x + position.x + 1, y + position.y, z + position.z + 1);
                        vertices[vertexIndex + 3] = new Vector3(x + position.x + 1, y + position.y + 1, z + position.z + 1);

                        triangles[triangleIndex]     = vertexIndex + 1;
                        triangles[triangleIndex + 1] = vertexIndex + 2;
                        triangles[triangleIndex + 2] = vertexIndex;

                        triangles[triangleIndex + 3] = vertexIndex + 1;
                        triangles[triangleIndex + 4] = vertexIndex + 3;
                        triangles[triangleIndex + 5] = vertexIndex + 2;

                        ChunkTextureController.AddTexture(blocks[index], Direction.North, vertexIndex, uvs);

                        vertexIndex   += 4;
                        triangleIndex += 6;
                    }

                    if ((faces[index] & (byte)Direction.East) != 0)
                    {
                        vertices[vertexIndex]     = new Vector3(x + position.x + 1, y + position.y, z + position.z);
                        vertices[vertexIndex + 1] = new Vector3(x + position.x + 1, y + position.y, z + position.z + 1);
                        vertices[vertexIndex + 2] = new Vector3(x + position.x + 1, y + position.y + 1, z + position.z);
                        vertices[vertexIndex + 3] = new Vector3(x + position.x + 1, y + position.y + 1, z + position.z + 1);

                        triangles[triangleIndex]     = vertexIndex;
                        triangles[triangleIndex + 1] = vertexIndex + 2;
                        triangles[triangleIndex + 2] = vertexIndex + 1;

                        triangles[triangleIndex + 3] = vertexIndex + 2;
                        triangles[triangleIndex + 4] = vertexIndex + 3;
                        triangles[triangleIndex + 5] = vertexIndex + 1;

                        ChunkTextureController.AddTexture(blocks[index], Direction.East, vertexIndex, uvs);

                        vertexIndex   += 4;
                        triangleIndex += 6;
                    }

                    if ((faces[index] & (byte)Direction.South) != 0)
                    {
                        vertices[vertexIndex]     = new Vector3(x + position.x, y + position.y, z + position.z);
                        vertices[vertexIndex + 1] = new Vector3(x + position.x + 1, y + position.y, z + position.z);
                        vertices[vertexIndex + 2] = new Vector3(x + position.x, y + position.y + 1, z + position.z);
                        vertices[vertexIndex + 3] = new Vector3(x + position.x + 1, y + position.y + 1, z + position.z);

                        triangles[triangleIndex]     = vertexIndex;
                        triangles[triangleIndex + 1] = vertexIndex + 2;
                        triangles[triangleIndex + 2] = vertexIndex + 1;

                        triangles[triangleIndex + 3] = vertexIndex + 2;
                        triangles[triangleIndex + 4] = vertexIndex + 3;
                        triangles[triangleIndex + 5] = vertexIndex + 1;

                        ChunkTextureController.AddTexture(blocks[index], Direction.South, vertexIndex, uvs);

                        vertexIndex   += 4;
                        triangleIndex += 6;
                    }

                    if ((faces[index] & (byte)Direction.West) != 0)
                    {
                        vertices[vertexIndex]     = new Vector3(x + position.x, y + position.y, z + position.z);
                        vertices[vertexIndex + 2] = new Vector3(x + position.x, y + position.y + 1, z + position.z);
                        vertices[vertexIndex + 1] = new Vector3(x + position.x, y + position.y, z + position.z + 1);
                        vertices[vertexIndex + 3] = new Vector3(x + position.x, y + position.y + 1, z + position.z + 1);

                        triangles[triangleIndex]     = vertexIndex + 1;
                        triangles[triangleIndex + 1] = vertexIndex + 2;
                        triangles[triangleIndex + 2] = vertexIndex;

                        triangles[triangleIndex + 3] = vertexIndex + 1;
                        triangles[triangleIndex + 4] = vertexIndex + 3;
                        triangles[triangleIndex + 5] = vertexIndex + 2;

                        ChunkTextureController.AddTexture(blocks[index], Direction.West, vertexIndex, uvs);

                        vertexIndex   += 4;
                        triangleIndex += 6;
                    }

                    if ((faces[index] & (byte)Direction.Up) != 0)
                    {
                        vertices[vertexIndex]     = new Vector3(x + position.x, y + position.y + 1, z + position.z);
                        vertices[vertexIndex + 1] = new Vector3(x + position.x + 1, y + position.y + 1, z + position.z);
                        vertices[vertexIndex + 2] = new Vector3(x + position.x, y + position.y + 1, z + position.z + 1);
                        vertices[vertexIndex + 3] = new Vector3(x + position.x + 1, y + position.y + 1, z + position.z + 1);

                        triangles[triangleIndex]     = vertexIndex;
                        triangles[triangleIndex + 1] = vertexIndex + 2;
                        triangles[triangleIndex + 2] = vertexIndex + 1;

                        triangles[triangleIndex + 3] = vertexIndex + 2;
                        triangles[triangleIndex + 4] = vertexIndex + 3;
                        triangles[triangleIndex + 5] = vertexIndex + 1;

                        ChunkTextureController.AddTexture(blocks[index], Direction.Up, vertexIndex, uvs);

                        vertexIndex   += 4;
                        triangleIndex += 6;
                    }

                    if ((faces[index] & (byte)Direction.Down) != 0)
                    {
                        vertices[vertexIndex]     = new Vector3(x + position.x, y + position.y, z + position.z);
                        vertices[vertexIndex + 1] = new Vector3(x + position.x, y + position.y, z + position.z + 1);
                        vertices[vertexIndex + 2] = new Vector3(x + position.x + 1, y + position.y, z + position.z);
                        vertices[vertexIndex + 3] = new Vector3(x + position.x + 1, y + position.y, z + position.z + 1);

                        triangles[triangleIndex]     = vertexIndex;
                        triangles[triangleIndex + 1] = vertexIndex + 2;
                        triangles[triangleIndex + 2] = vertexIndex + 1;

                        triangles[triangleIndex + 3] = vertexIndex + 2;
                        triangles[triangleIndex + 4] = vertexIndex + 3;
                        triangles[triangleIndex + 5] = vertexIndex + 1;

                        ChunkTextureController.AddTexture(blocks[index], Direction.Down, vertexIndex, uvs);

                        vertexIndex   += 4;
                        triangleIndex += 6;
                    }

                    index++;
                }
            }
        }
    }