Esempio n. 1
0
        public static void Initialize(BlockCollection blockCollection)
        {
            if (isInitialized)
            {
                Debug.LogWarning("Block provider is already initialized.");
                return;
            }

            blocks     = new NativeHashMap <int, Block>(0, Allocator.Persistent);
            blockIds   = new Dictionary <string, int>();
            blockNames = new Dictionary <string, string>();
            palette    = new Dictionary <int, string>();

            emptyBlocks = new NativeArray <int>(Chunk.CHUNK_SIZE * Chunk.CHUNK_SIZE * Chunk.CHUNK_SIZE, Allocator.Persistent);

            blocks.Add(AIR_TYPE_ID, new Block(AIR_TYPE_ID)
            {
                canCollide = false
            });
            blockNames.Add(AIR_TYPE, "Air");
            blockIds.Add(AIR_TYPE, AIR_TYPE_ID);
            palette.Add(AIR_TYPE_ID, AIR_TYPE);

            for (ushort i = 0; i < blockCollection.Blocks.Length; i++)
            {
                if (blockCollection.Blocks[i] == null)
                {
#if DEBUG
                    Debug.LogWarning("Element " + i + " in your block collection is null. Skipping it.");
#endif
                    continue;
                }
                ushort id = (ushort)(i + 1);

#if DEBUG
                if (blockNames.ContainsKey(blockCollection.Blocks[i].BlockID))
                {
                    Debug.LogError("Found multiple blocks with the ID '" + blockCollection.Blocks[i].BlockID + "'.");
                }
#endif

                blockIds.Add(blockCollection.Blocks[i].BlockID, id);
                blockNames.Add(blockCollection.Blocks[i].BlockID, blockCollection.Blocks[i].BlockName);
                palette.Add(id, blockCollection.Blocks[i].BlockID);

                if (blockCollection.Blocks[i] is CubeConfig cube)
                {
                    Block block = new Block(id, cube);
                    blocks.Add(id, block);
                }
                else
                {
                    Block block = new Block(id);
                    blocks.Add(id, block);
                }
            }

            isInitialized = true;
        }
Esempio n. 2
0
        public static void Initialize(BlockCollection blocks)
        {
            if (isInitialized)
            {
                Debug.LogWarning("Texture provider is already initialized.");
                return;
            }

            GenerateAtlas(blocks);

            isInitialized = true;
        }
Esempio n. 3
0
        public void Initialize(BlockCollection collection)
        {
            for (int i = 0; i < collection.Blocks.Length; i++)
            {
                if (collection.Blocks[i].IsConnectedTextures)
                {
                    m_ConnectedTextures.AddRange(collection.Blocks[i].ConnectedTextures);
                }
                else
                {
                    m_NormalTextures.AddRange(collection.Blocks[i].Textures.GetTextures());
                }
            }

            m_HasInited = true;
        }
Esempio n. 4
0
        private static void GenerateAtlas(BlockCollection blocks)
        {
            Texture2D[] uniqueTextures = blocks.UniqueTextures.ToArray();

            textures = new NativeHashMap <int, int2>(0, Allocator.Persistent);

            atlasTexture            = new Texture2D(8192, 8192, TextureFormat.ARGB32, 0, false);
            rects                   = atlasTexture.PackTextures(uniqueTextures, 0, 8192, false);
            atlasTexture.filterMode = FilterMode.Point;

            for (int i = 0; i < uniqueTextures.Length; i++)
            {
                Rect uvs = rects[i];

                if (!textures.TryGetValue(blocks.UniqueTextureIds[i], out _))
                {
                    int2 coords = new int2((int)(atlasTexture.width * uvs.x / blocks.TextureSize), (int)(atlasTexture.height * uvs.y / blocks.TextureSize));
                    textures.Add(blocks.UniqueTextureIds[i], coords);
                }
            }
        }