Exemplo n.º 1
0
        public Rect GetTexture(Chunk chunk, ref Vector3Int localPos, Direction direction)
        {
            if (usesConnectedTextures)
            {
                ChunkBlocks blocks    = chunk.blocks;
                int         index     = Helpers.GetChunkIndex1DFrom3D(localPos.x, localPos.y, localPos.z);
                ushort      blockType = blocks.Get(index).Type;

                bool nw = ConnectedTextures.IsSame(blocks, index, -1, 1, direction, blockType);
                bool n  = ConnectedTextures.IsSame(blocks, index, 0, 1, direction, blockType);
                bool ne = ConnectedTextures.IsSame(blocks, index, 1, 1, direction, blockType);
                bool w  = ConnectedTextures.IsSame(blocks, index, -1, 0, direction, blockType);
                bool e  = ConnectedTextures.IsSame(blocks, index, 1, 0, direction, blockType);
                bool se = ConnectedTextures.IsSame(blocks, index, 1, -1, direction, blockType);
                bool s  = ConnectedTextures.IsSame(blocks, index, 0, -1, direction, blockType);
                bool sw = ConnectedTextures.IsSame(blocks, index, -1, -1, direction, blockType);

                return(connectedTextures[ConnectedTextures.GetTexture(n, e, s, w, nw, ne, se, sw)]);
            }

            if (textures.Count == 1)
            {
                return(textures[0]);
            }

            if (textures.Count > 1)
            {
                int hash = localPos.GetHashCode();
                if (hash < 0)
                {
                    hash *= -1;
                }

                float randomNumber = (hash % 100) / 100f;
                randomNumber *= textures.Count;

                return(textures[(int)randomNumber]);
            }


            Debug.LogError("There were no textures for " + textureName);
            return(new Rect());
        }
Exemplo n.º 2
0
        public Rect GetTexture(Chunk chunk, Vector3Int localPos, Vector3Int globalPos, Direction direction)
        {
            if (usesConnectedTextures)
            {
                int blockType = chunk.world.blocks.Get(globalPos).Type;

                bool wn = ConnectedTextures.IsSame(chunk, globalPos, -1, 1, direction, blockType);
                bool n  = ConnectedTextures.IsSame(chunk, globalPos, 0, 1, direction, blockType);
                bool ne = ConnectedTextures.IsSame(chunk, globalPos, 1, 1, direction, blockType);
                bool w  = ConnectedTextures.IsSame(chunk, globalPos, -1, 0, direction, blockType);
                bool e  = ConnectedTextures.IsSame(chunk, globalPos, 1, 0, direction, blockType);
                bool es = ConnectedTextures.IsSame(chunk, globalPos, 1, -1, direction, blockType);
                bool s  = ConnectedTextures.IsSame(chunk, globalPos, 0, -1, direction, blockType);
                bool sw = ConnectedTextures.IsSame(chunk, globalPos, -1, -1, direction, blockType);

                return(connectedTextures[ConnectedTextures.GetTexture(n, e, s, w, wn, ne, es, sw)]);
            }

            if (textures.Count == 1)
            {
                return(textures[0]);
            }

            if (textures.Count > 1)
            {
                int hash = localPos.GetHashCode();
                if (hash < 0)
                {
                    hash *= -1;
                }

                float randomNumber = (hash % 100) / 100f;
                randomNumber *= textures.Count;

                return(textures[(int)randomNumber]);
            }


            Debug.LogError("There were no textures for " + textureName);
            return(new Rect());
        }
Exemplo n.º 3
0
        private TextureConfig[] LoadAllTextures()
        {
            TextureConfig[] allConfigs = new ConfigLoader <TextureConfig>(new[] { config.textureFolder }).AllConfigs();

            // Load all files in Textures folder
            Texture2D[] sourceTextures = Resources.LoadAll <Texture2D>(config.textureFolder);

            Dictionary <string, Texture2D> sourceTexturesLookup = new Dictionary <string, Texture2D>();

            foreach (var texture in sourceTextures)
            {
                sourceTexturesLookup.Add(texture.name, texture);
            }

            for (int i = 0; i < allConfigs.Length; i++)
            {
                var cfg = allConfigs[i];

                for (int n = 0; n < cfg.textures.Length; n++)
                {
                    cfg.textures[n].texture2d = Texture2DFromConfig(cfg.textures[n], sourceTexturesLookup);
                }

                if (cfg.type == TextureConfigType.Connected)
                {
                    // Create all 48 possibilities from the 5 supplied textures
                    Texture2D[]             newTextures       = ConnectedTextures.ConnectedTexturesFromBaseTextures(cfg.textures);
                    TextureConfig.Texture[] connectedTextures = new TextureConfig.Texture[48];

                    for (int x = 0; x < newTextures.Length; x++)
                    {
                        connectedTextures[x].index     = x;
                        connectedTextures[x].texture2d = newTextures[x];
                    }

                    cfg.textures = connectedTextures;
                }
            }

            return(allConfigs);
        }
Exemplo n.º 4
0
        public Rect GetTexture(Chunk chunk, ref Vector3Int localPos, Direction direction)
        {
            if (m_uvs.Count == 1)
            {
                return(m_uvs[0]);
            }

            if (m_textureType == TextureConfigType.Connected)
            {
                ChunkBlocks blocks        = chunk.Blocks;
                int         localPosIndex = Helpers.GetChunkIndex1DFrom3D(localPos.x, localPos.y, localPos.z);
                ushort      blockType     = blocks.Get(localPosIndex).Type;

                // Side blocks
                bool n_, _e, s_, _w;
                // Corner blocks
                bool nw, ne, se, sw;

                int index1, index2, index3;
                int sizeWithPadding     = chunk.SideSize + Env.ChunkPadding2;
                int sizeWithPaddingPow2 = sizeWithPadding * sizeWithPadding;

                switch (direction)
                {
                case Direction.up:
                    index1 = localPosIndex + sizeWithPaddingPow2;       // + (0,1,0)
                    index2 = index1 - sizeWithPadding;                  // - (0,0,1)
                    index3 = index1 + sizeWithPadding;                  // + (0,0,1)

                    sw = blocks.Get(index2 - 1).Type == blockType;      // -1,1,-1
                    s_ = blocks.Get(index2).Type == blockType;          //  0,1,-1
                    se = blocks.Get(index2 + 1).Type == blockType;      //  1,1,-1
                    _w = blocks.Get(index1 - 1).Type == blockType;      // -1,1, 0
                    _e = blocks.Get(index1 + 1).Type == blockType;      //  1,1, 0
                    nw = blocks.Get(index3 - 1).Type == blockType;      // -1,1, 1
                    n_ = blocks.Get(index3).Type == blockType;          //  0,1, 1
                    ne = blocks.Get(index3 + 1).Type == blockType;      //  1,1, 1
                    break;

                case Direction.down:
                    index1 = localPosIndex - sizeWithPaddingPow2;      // - (0,1,0)
                    index2 = index1 - sizeWithPadding;                 // - (0,0,1)
                    index3 = index1 + sizeWithPadding;                 // + (0,0,1)

                    sw = blocks.Get(index2 - 1).Type == blockType;     // -1,-1,-1
                    s_ = blocks.Get(index2).Type == blockType;         //  0,-1,-1
                    se = blocks.Get(index2 + 1).Type == blockType;     //  1,-1,-1
                    _w = blocks.Get(index1 - 1).Type == blockType;     // -1,-1, 0
                    _e = blocks.Get(index1 + 1).Type == blockType;     //  1,-1, 0
                    nw = blocks.Get(index3 - 1).Type == blockType;     // -1,-1, 1
                    n_ = blocks.Get(index3).Type == blockType;         //  0,-1, 1
                    ne = blocks.Get(index3 + 1).Type == blockType;     //  1,-1, 1
                    break;

                case Direction.north:
                    index1 = localPosIndex + sizeWithPadding;       // + (0,0,1)
                    index2 = index1 - sizeWithPaddingPow2;          // - (0,1,0)
                    index3 = index1 + sizeWithPaddingPow2;          // + (0,1,0)

                    sw = blocks.Get(index2 - 1).Type == blockType;  // -1,-1,1
                    se = blocks.Get(index2 + 1).Type == blockType;  //  1,-1,1
                    _w = blocks.Get(index1 - 1).Type == blockType;  // -1, 0,1
                    _e = blocks.Get(index1 + 1).Type == blockType;  //  1, 0,1
                    nw = blocks.Get(index3 - 1).Type == blockType;  // -1, 1,1
                    s_ = blocks.Get(index2).Type == blockType;      //  0,-1,1
                    n_ = blocks.Get(index3).Type == blockType;      //  0, 1,1
                    ne = blocks.Get(index3 + 1).Type == blockType;  //  1, 1,1
                    break;

                case Direction.south:
                    index1 = localPosIndex - sizeWithPadding;       // - (0,0,1)
                    index2 = index1 - sizeWithPaddingPow2;          // - (0,1,0)
                    index3 = index1 + sizeWithPaddingPow2;          // + (0,1,0)

                    sw = blocks.Get(index2 - 1).Type == blockType;  // -1,-1,-1
                    se = blocks.Get(index2 + 1).Type == blockType;  //  1,-1,-1
                    _w = blocks.Get(index1 - 1).Type == blockType;  // -1, 0,-1
                    _e = blocks.Get(index1 + 1).Type == blockType;  //  1, 0,-1
                    nw = blocks.Get(index3 - 1).Type == blockType;  // -1, 1,-1
                    s_ = blocks.Get(index2).Type == blockType;      //  0,-1,-1
                    n_ = blocks.Get(index3).Type == blockType;      //  0, 1,-1
                    ne = blocks.Get(index3 + 1).Type == blockType;  //  1, 1,-1
                    break;

                case Direction.east:
                    index1 = localPosIndex + 1;                                  // + (1,0,0)
                    index2 = index1 - sizeWithPaddingPow2;                       // - (0,1,0)
                    index3 = index1 + sizeWithPaddingPow2;                       // + (0,1,0)

                    sw = blocks.Get(index2 - sizeWithPadding).Type == blockType; // 1,-1,-1
                    s_ = blocks.Get(index2).Type == blockType;                   // 1,-1, 0
                    se = blocks.Get(index2 + sizeWithPadding).Type == blockType; // 1,-1, 1
                    _w = blocks.Get(index1 - sizeWithPadding).Type == blockType; // 1, 0,-1
                    _e = blocks.Get(index1 + sizeWithPadding).Type == blockType; // 1, 0, 1
                    nw = blocks.Get(index3 - sizeWithPadding).Type == blockType; // 1, 1,-1
                    n_ = blocks.Get(index3).Type == blockType;                   // 1, 1, 0
                    ne = blocks.Get(index3 + sizeWithPadding).Type == blockType; // 1, 1, 1
                    break;

                default:                                                         //case Direction.west:
                    index1 = localPosIndex - 1;                                  // - (1,0,0)
                    index2 = index1 - sizeWithPaddingPow2;                       // - (0,1,0)
                    index3 = index1 + sizeWithPaddingPow2;                       // + (0,1,0)

                    sw = blocks.Get(index2 - sizeWithPadding).Type == blockType; // -1,-1,-1
                    s_ = blocks.Get(index2).Type == blockType;                   // -1,-1, 0
                    se = blocks.Get(index2 + sizeWithPadding).Type == blockType; // -1,-1, 1
                    _w = blocks.Get(index1 - sizeWithPadding).Type == blockType; // -1, 0,-1
                    _e = blocks.Get(index1 + sizeWithPadding).Type == blockType; // -1, 0, 1
                    nw = blocks.Get(index3 - sizeWithPadding).Type == blockType; // -1, 1,-1
                    n_ = blocks.Get(index3).Type == blockType;                   // -1, 1, 0
                    ne = blocks.Get(index3 + sizeWithPadding).Type == blockType; // -1, 1, 1
                    break;
                }

                int uvIndex = ConnectedTextures.GetTexture(n_, _e, s_, _w, nw, ne, se, sw);
                return(m_uvs[uvIndex]);
            }

            if (m_uvs.Count > 1)
            {
                int hash = localPos.GetHashCode();
                if (hash < 0)
                {
                    hash *= -1;
                }

                float randomNumber = (hash % 100) / 100f;
                randomNumber *= m_uvs.Count;

                return(m_uvs[(int)randomNumber]);
            }

            Debug.LogError("There were no textures for " + m_textureName);
            return(new Rect());
        }