Esempio n. 1
0
        void random()
        {
            map = new byte[width, height, width];

            TerrainType terrainType = GetTerrainFor(transform.position.x, transform.position.z);
            BiomeType   biomeType   = GetBiomeFor(transform.position.x, transform.position.z);

            float myHeight   = GetHeightOf(terrainType);
            byte  dirt       = (byte)ItemDatabase.GetItemByName("Dirt").id;
            byte  grassydirt = (byte)ItemDatabase.GetItemByName("Grass").id;
            byte  ice        = (byte)ItemDatabase.GetItemByName("Stone").id;
            byte  sand       = (byte)ItemDatabase.GetItemByName("Bedrock").id;


            for (int x = 0; x < width; x++)
            {
                float percent  = (float)x / (float)(width - 1);
                float xBalance = CurvePoint(
                    percent,
                    (GetHeightOf(transform.position.x - width, transform.position.z) + myHeight) / 2,
                    myHeight,
                    (GetHeightOf(transform.position.x + width, transform.position.z) + myHeight) / 2);

                for (int z = 0; z < width; z++)
                {
                    percent = (float)z / (float)(width - 1);
                    float zBalance = CurvePoint(
                        percent,
                        (GetHeightOf(transform.position.x, transform.position.z - width) + myHeight) / 2,
                        myHeight,
                        (GetHeightOf(transform.position.x, transform.position.z + width) + myHeight) / 2);
                    float finalHeight = (xBalance + zBalance) / 2;
                    for (int y = 0; y < finalHeight; y++)
                    {
                        Vector3 offset0       = new Vector3(Random.value * 10000, Random.value * 10000, Random.value * 10000);
                        Vector3 offset1       = new Vector3(Random.value * 10000, Random.value * 10000, Random.value * 10000);
                        Vector3 offset2       = new Vector3(Random.value * 10000, Random.value * 10000, Random.value * 10000);
                        float   clusterValue  = CalculateNoiseValue(new Vector3(x, y, z) + transform.position, offset0, 0.02f);
                        float   blobValue     = CalculateNoiseValue(new Vector3(x, y, z) + transform.position, offset1, 0.05f);
                        float   mountainValue = CalculateNoiseValue(new Vector3(x, y, z) + transform.position, offset2, 0.009f);
                        switch (terrainType)
                        {
                        case TerrainType.Lowlands:
                            finalHeight = blobValue;
                            break;

                        case TerrainType.Highlands:
                            finalHeight = clusterValue;
                            break;

                        case TerrainType.Mountains:
                            finalHeight = mountainValue;
                            break;
                        }
                        switch (biomeType)
                        {
                        default:
                            if (y >= finalHeight - 1)
                            {
                                map[x, y, z] = grassydirt;
                            }
                            else
                            {
                                map[x, y, z] = dirt;
                            }
                            break;

                        case BiomeType.Ice:
                            if (y >= finalHeight - 2)
                            {
                                map[x, y, z] = ice;
                            }
                            else
                            {
                                map[x, y, z] = dirt;
                            }
                            break;

                        case BiomeType.Desert:
                            if (y >= finalHeight - 7)
                            {
                                map[x, y, z] = sand;
                            }
                            else
                            {
                                map[x, y, z] = dirt;
                            }
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public virtual void BuildFace(byte brick, Vector3 corner, Vector3 normal, List <Vector3> verts, List <Vector2> uvs, List <int> tris)
        {
            Vector3 right    = Vector3.zero;
            Vector3 up       = Vector3.zero;
            bool    reversed = false;

            if (normal == Vector3.down || normal == Vector3.right || normal == Vector3.forward)
            {
                reversed = true;
            }
            int index = verts.Count;

            if (normal == Vector3.up || normal == Vector3.down)
            {
                up    = Vector3.forward;
                right = Vector3.right;
            }
            else if (normal == Vector3.left || normal == Vector3.right)
            {
                up    = Vector3.up;
                right = Vector3.back;
            }
            else if (normal == Vector3.forward || normal == Vector3.back)
            {
                up    = Vector3.up;
                right = Vector3.right;
            }
            verts.Add(corner);
            verts.Add(corner + up);
            verts.Add(corner + up + right);
            verts.Add(corner + right);
            if (reversed)
            {
                tris.Add(index + 0);
                tris.Add(index + 2);
                tris.Add(index + 1);

                tris.Add(index + 0);
                tris.Add(index + 3);
                tris.Add(index + 2);
            }
            else
            {
                tris.Add(index + 0);
                tris.Add(index + 1);
                tris.Add(index + 2);

                tris.Add(index + 0);
                tris.Add(index + 2);
                tris.Add(index + 3);
            }
            Vector2 offset     = Vector3.zero;
            float   resolution = 0.0625f;

            Vector2 _00 = new Vector2(0, 0) * resolution;
            Vector2 _01 = new Vector2(0, 1) * resolution;
            Vector2 _11 = new Vector2(1, 1) * resolution;
            Vector2 _10 = new Vector2(1, 0) * resolution;

            ItemTexture texture = ItemDatabase.GetItemById(brick).texture;

            if (normal == Vector3.up)
            {
                offset = texture.top;
            }
            else if (normal == Vector3.down)
            {
                offset = texture.bottom;
            }
            else if (normal == Vector3.left)
            {
                offset = texture.left;
            }
            else if (normal == Vector3.right)
            {
                offset = texture.right;
            }
            else if (normal == Vector3.forward)
            {
                offset = texture.front;
            }
            else if (normal == Vector3.back)
            {
                offset = texture.back;
            }

            uvs.Add(_00 + offset);
            uvs.Add(_01 + offset);
            uvs.Add(_11 + offset);
            uvs.Add(_10 + offset);
        }