Beispiel #1
0
        public Unity_TileSet LoadTileSet(byte[] tileSet, RGBA5551Color[] palette, bool is2bpp, bool flipX, SNES_Proto_AnimatedTileEntry[] animatedTiles = null, bool shadow = false)
        {
            var pal = is2bpp ? Util.ConvertAndSplitGBCPalette(palette) : Util.ConvertAndSplitGBAPalette(palette);

            int       numPalettes         = pal.Length;
            const int wrap                = 64;
            int       bpp                 = is2bpp ? 2 : 4;
            const int tileWidth           = 8;
            int       tileSize            = tileWidth * tileWidth * bpp / 8;
            int       tilesetLength       = tileSet.Length / tileSize;
            int       animatedTilesLength = animatedTiles?.Sum(at => at.GraphicsBuffer.Length / tileSize) ?? 0;
            int       totalTilesetLength  = tilesetLength + animatedTilesLength;

            int tilesX = Math.Min(totalTilesetLength * numPalettes, wrap);
            int tilesY = Mathf.CeilToInt(totalTilesetLength * numPalettes / (float)wrap);

            var tex = TextureHelpers.CreateTexture2D(tilesX * tileWidth, tilesY * tileWidth);

            for (int p = 0; p < numPalettes; p++)
            {
                for (int i = 0; i < tilesetLength; i++)
                {
                    int tileInd = i + p * totalTilesetLength;
                    int tileY   = (tileInd / wrap) * tileWidth;
                    int tileX   = (tileInd % wrap) * tileWidth;

                    tex.FillInTile(
                        imgData: tileSet,
                        imgDataOffset: i * tileSize,
                        pal: pal[p],
                        encoding: is2bpp ? Util.TileEncoding.Planar_2bpp : Util.TileEncoding.Planar_4bpp,
                        tileWidth: tileWidth,
                        flipTextureY: false,
                        tileX: tileX,
                        tileY: tileY,
                        flipTileX: flipX);
                }

                if (animatedTiles != null)
                {
                    int curAnimatedTile = 0;
                    for (int i = 0; i < animatedTiles.Length; i++)
                    {
                        int numTiles = animatedTiles[i].GraphicsBuffer.Length / tileSize;
                        for (int t = 0; t < numTiles; t++)
                        {
                            int tileInd = tilesetLength + curAnimatedTile + p * totalTilesetLength;
                            int tileY   = (tileInd / wrap) * tileWidth;
                            int tileX   = (tileInd % wrap) * tileWidth;

                            tex.FillInTile(
                                imgData: animatedTiles[i].GraphicsBuffer,
                                imgDataOffset: t * tileSize,
                                pal: pal[p],
                                encoding: is2bpp ? Util.TileEncoding.Planar_2bpp : Util.TileEncoding.Planar_4bpp,
                                tileWidth: tileWidth,
                                flipTextureY: false,
                                tileX: tileX,
                                tileY: tileY,
                                flipTileX: flipX);
                            curAnimatedTile++;
                        }
                    }
                }
            }
            if (shadow)
            {
                var colors = tex.GetPixels();
                colors = colors.Select(c => c.a == 0 ? c : Color.black).ToArray();
                tex.SetPixels(colors);
            }

            tex.Apply();
            Unity_AnimatedTile[] unityAnimatedTiles = null;
            if (animatedTiles != null)
            {
                int curAnimatedTile = 0;
                var animTilesDict   = new Dictionary <int, List <int> >();
                foreach (var at in animatedTiles)
                {
                    var tileInd  = (at.VRAMAddress * 2) / 0x20;
                    int numTiles = at.GraphicsBuffer.Length / tileSize;
                    for (int i = 0; i < numTiles; i++)
                    {
                        int key = tileInd + i;
                        if (!animTilesDict.ContainsKey(key))
                        {
                            animTilesDict[key] = new List <int>();
                            animTilesDict[key].Add(key);
                        }
                        animTilesDict[key].Add(tilesetLength + curAnimatedTile);
                        curAnimatedTile++;
                    }
                }
                var unityAnimTilesList = new List <Unity_AnimatedTile>();
                for (int p = 0; p < numPalettes; p++)
                {
                    foreach (var kv in animTilesDict)
                    {
                        Unity_AnimatedTile newAT = new Unity_AnimatedTile()
                        {
                            AnimationSpeed = 2,
                            TileIndices    = kv.Value.Select(t => t + p * totalTilesetLength).ToArray()
                        };
                        //Debug.Log(string.Join(",",newAT.TileIndices));
                        unityAnimTilesList.Add(newAT);
                    }
                }
                unityAnimatedTiles = unityAnimTilesList.ToArray();
            }
            return(new Unity_TileSet(tex, tileWidth)
            {
                SNES_BaseLength = totalTilesetLength,
                AnimatedTiles = unityAnimatedTiles
            });
        }
Beispiel #2
0
 public Instance(Unity_AnimatedTile animatedTile, int tileIndex)
 {
     this.animatedTile = animatedTile;
     this.tileIndex    = tileIndex;
 }