Beispiel #1
0
    public Texture2D GetFrameTexture()
    {
        if (frameTexture == null)
        {
            frameTexture = PIATexture.CreateBlank(PIASession.Instance.ImageData.Width, PIASession.Instance.ImageData.Height);
        }
        PIATexture.Wipe(frameTexture);
        frameTexture.filterMode = FilterMode.Point;

        // ordering up textures so the last layer is always drawn on top of the first layer
        textures = textures.OrderBy(x => x.LayerIndex).ToList();

        //building up the texture without any filters
        foreach (var item in textures)
        {
            for (int x = 0; x < PIASession.Instance.ImageData.Width; x++)
            {
                for (int y = 0; y < PIASession.Instance.ImageData.Height; y++)
                {
                    Color pixelColor = item.Texture.GetPixel(x, y);
                    if (pixelColor.a > 0)
                    {
                        frameTexture.SetPixel(x, y, pixelColor);
                    }
                }
            }
        }
        frameTexture.Apply();

        return(frameTexture);
    }
Beispiel #2
0
    public void Init(PIAImageData _imageData)
    {
        textures                = new List <PIATexture>();
        frameTexture            = PIATexture.CreateBlank(_imageData.Width, _imageData.Height);
        frameTextureWithFilters = PIATexture.CreateBlank(_imageData.Width, _imageData.Height);

        // we make sure every time a new frame is created it contains the same amount of sub-textures
        // as the layers in the image
        for (int i = 0; i < _imageData.Layers.Count; i++)
        {
            AddTexture(_imageData, i);
        }
    }
Beispiel #3
0
        public static Texture2D GenerateSpriteSheet(int framesCount, int frameWidth, int frameHeight, PIAFrame[] frames)
        {
            Texture2D spriteSheet;
            int       rows = (int)Mathf.Sqrt(framesCount);

            int spriteSheetWidth = (framesCount * frameWidth) / rows;

            spriteSheetWidth += spriteSheetWidth % frameWidth;

            int spriteSheetHeight = frameHeight * rows;

            spriteSheetHeight += spriteSheetHeight % frameHeight;

            spriteSheet = PIATexture.CreateBlank(spriteSheetWidth, spriteSheetHeight);
            int offsetX = 0;
            int offsetY = spriteSheetHeight - frameHeight;


            for (int i = 0; i < framesCount; i++)
            {
                if (i != 0 && (frameWidth * i) % spriteSheetWidth == 0)
                {
                    offsetY -= frameHeight;
                    offsetX  = 0;
                }

                for (int x = 0; x < frameWidth; x++)
                {
                    for (int y = 0; y < frameHeight; y++)
                    {
                        Color framePixelColor = frames[i].GetFrameTexture().GetPixel(x, y);
                        spriteSheet.SetPixel(x + offsetX, y + offsetY, framePixelColor);
                        spriteSheet.Apply();
                    }
                }
                offsetX += frameWidth;
            }


            return(spriteSheet);
        }
Beispiel #4
0
    public Texture2D GetFrameTextureWithLayerFilters()
    {
        // in case we load from a saved asset
        if (frameTextureWithFilters == null)
        {
            frameTextureWithFilters = PIATexture.CreateBlank(PIASession.Instance.ImageData.Width, PIASession.Instance.ImageData.Height);
        }

        // clean up
        PIATexture.Wipe(frameTextureWithFilters);
        PIAImageData imageData = PIASession.Instance.ImageData;

        frameTextureWithFilters.filterMode = FilterMode.Point;

        // caching hidden layers indexes
        List <int> hiddenLayersIndex = new List <int>();

        foreach (var item in imageData.Layers)
        {
            if (item.Hidden == true)
            {
                hiddenLayersIndex.Add(item.Index);
            }
        }

        // ordering up textures so the last layer is always drawn on top of the first layer
        textures = textures.OrderBy(x => x.LayerIndex).ToList();

        int currentLayerTextureIndex = 0;

        // building the filtered texture from each sub-texture (layer) in the frame
        for (int i = 0; i < textures.Count; i++)
        {
            var item = textures[i];
            // skipping hidden layers
            if (hiddenLayersIndex.Contains(item.LayerIndex))
            {
                continue;
            }

            // if the sub-texture index is NOT the current selected index it gets painted transparent (alpha/2)
            if (imageData.CurrentLayer != item.LayerIndex)
            {
                for (int x = 0; x < PIASession.Instance.ImageData.Width; x++)
                {
                    for (int y = 0; y < PIASession.Instance.ImageData.Height; y++)
                    {
                        Color pixelColor = item.Texture.GetPixel(x, y);
                        pixelColor.a = pixelColor.a / 2;
                        if (pixelColor.a > 0)
                        {
                            frameTextureWithFilters.SetPixel(x, y, pixelColor);
                        }
                    }
                }
            }
            else
            {
                currentLayerTextureIndex = i;
            }
        }

        // drawing current layer texture
        for (int x = 0; x < PIASession.Instance.ImageData.Width; x++)
        {
            for (int y = 0; y < PIASession.Instance.ImageData.Height; y++)
            {
                Color pixelColor = textures[currentLayerTextureIndex].Texture.GetPixel(x, y);
                if (pixelColor.a > 0)
                {
                    frameTextureWithFilters.SetPixel(x, y, pixelColor);
                }
            }
        }

        frameTextureWithFilters.Apply();



        return(frameTextureWithFilters);
    }