Example #1
0
 private void hqx4ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     DstImg   = new Bitmap(OriImg);
     DstImg   = HqxSharp.Scale4(DstImg);
     ImgScale = 4;
     refreshImages();
 }
Example #2
0
    public IEnumerator FinalizeTextures(System.Diagnostics.Stopwatch stopWatch)
    {
        if (coordList.Count == 0) // There's nothing that uses this.
        {
            yield break;
        }
        int scaleFactor = 1;


        tileArray   = new Texture2DArray(Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor), coordList.Count, TextureFormat.ARGB32, true);
        normalArray = new Texture2DArray(Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor), coordList.Count, TextureFormat.ARGB32, true, true);

        for (int i = 0; i < coordList.Count; i++)
        {
            var coord        = coordList[i];
            var tileSource   = originalPage.GetPixels(coord.x * tileWidth, (pageHeight - coord.y - 1) * tileHeight, tileWidth, tileHeight);
            var tileSource32 = new Color32[tileSource.Length];
            for (int j = 0; j < tileSource.Length; j++)
            {
                tileSource32[j] = tileSource[j];
            }
            var tileDest32 = new Color32[tileWidth * scaleFactor * tileHeight * scaleFactor];
            switch (scaleFactor)
            {
            case 4:
                HqxSharp.Scale4(tileSource32, tileDest32, tileWidth, tileHeight);
                break;

            case 3:
                HqxSharp.Scale3(tileSource32, tileDest32, tileWidth, tileHeight);
                break;

            case 2:
                HqxSharp.Scale2(tileSource32, tileDest32, tileWidth, tileHeight);
                break;

            default:
                tileDest32 = tileSource32;
                break;
            }
            Texture2D texture = new Texture2D(tileWidth * scaleFactor, tileHeight * scaleFactor, TextureFormat.ARGB32, false);
            texture.SetPixels32(tileDest32);
            TextureScale.Bilinear(texture, Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor));
            tileArray.SetPixels(texture.GetPixels(), i);
            normalArray.SetPixels(TextureTools.Bevel(texture.GetPixels(), texture.width, texture.height), i);
            if (stopWatch.ElapsedMilliseconds > ContentLoader.LoadFrameTimeout)
            {
                yield return(null);

                stopWatch.Reset();
                stopWatch.Start();
            }
        }
        tileArray.Apply();
        normalArray.Apply();
    }
Example #3
0
    public static Texture2D LoadTexture(XAttribute textureAtt, XElement elemtype, Color defaultColor, bool linear = false)
    {
        if (textureAtt == null)
        {
            return(CreateFlatTexture(defaultColor));
        }

        string texturePath = Path.Combine(Path.GetDirectoryName(new Uri(elemtype.BaseUri).LocalPath), textureAtt.Value);

        texturePath = Path.GetFullPath(texturePath);
        if (!File.Exists(texturePath))
        {
            Debug.LogError("File not found: " + texturePath);
            return(CreateFlatTexture(defaultColor));
        }

        byte[]    patternData = File.ReadAllBytes(texturePath);
        Texture2D texture     = new Texture2D(2, 2, TextureFormat.ARGB32, false, linear);

        texture.LoadImage(patternData);
        GameSettings.ClampToMaxSize(texture);
        texture.name = texturePath;
        if (texture.width * 4 <= GameSettings.Instance.rendering.maxTextureSize && texture.height * 4 <= GameSettings.Instance.rendering.maxTextureSize)
        {
            HqxSharp.Scale4(texture);
        }
        else if (texture.width * 3 <= GameSettings.Instance.rendering.maxTextureSize && texture.height * 3 <= GameSettings.Instance.rendering.maxTextureSize)
        {
            HqxSharp.Scale3(texture);
        }
        else if (texture.width * 2 <= GameSettings.Instance.rendering.maxTextureSize && texture.height * 2 <= GameSettings.Instance.rendering.maxTextureSize)
        {
            HqxSharp.Scale2(texture);
        }
        return(texture);
    }
Example #4
0
    private void OnGUI()
    {
        scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
        GUILayout.Label("Texure array maker", EditorStyles.boldLabel);
        baseTexture = (Texture2D)EditorGUILayout.ObjectField("Base texture image", baseTexture, typeof(Texture2D), false);
        tiles_x     = EditorGUILayout.IntField("Horizontal Tiles", tiles_x);
        tiles_y     = EditorGUILayout.IntField("Vertical Tiles", tiles_y);
        if (tiles_x <= 0)
        {
            tiles_x = 1;
        }
        if (tiles_y <= 0)
        {
            tiles_y = 1;
        }

        mipmaps   = EditorGUILayout.Toggle("Enable Mipmaps", mipmaps);
        skipEmpty = EditorGUILayout.Toggle("Skip Empty", skipEmpty);

        scaleMode = (ScaleMode)EditorGUILayout.EnumPopup("Scale mode", scaleMode);
        int scale = 1;

        switch (scaleMode)
        {
        case ScaleMode.None:
            scale = 1;
            break;

        case ScaleMode.HQ2x:
            scale = 2;
            break;

        case ScaleMode.HQ3x:
            scale = 3;
            break;

        case ScaleMode.HQ4x:
            scale = 4;
            break;

        default:
            break;
        }
        if (baseTexture != null)
        {
            int sourceWidth  = baseTexture.width / tiles_x;
            int sourceHeight = baseTexture.height / tiles_y;
            GUILayout.Label("Source Width: " + sourceWidth);
            GUILayout.Label("Source Height: " + sourceHeight);

            int scaledWidth  = sourceWidth * scale;
            int scaledHeight = sourceHeight * scale;
            GUILayout.Label("Scaled Width: " + scaledWidth);
            GUILayout.Label("Scaled Height: " + scaledHeight);

            int potWidth  = Mathf.ClosestPowerOfTwo(scaledWidth);
            int potHeight = Mathf.ClosestPowerOfTwo(scaledHeight);
            GUILayout.Label("Final Width: " + potWidth);
            GUILayout.Label("Final Height: " + potHeight);

            if (GUILayout.Button("Build Array"))
            {
                var tempList = new List <Texture2D>();
                for (int y = tiles_y - 1; y >= 0; y--)
                {
                    for (int x = 0; x < tiles_x; x++)
                    {
                        var pixels = baseTexture.GetPixels(sourceWidth * x, sourceHeight * y, sourceWidth, sourceHeight);
                        if (skipEmpty)
                        {
                            float totalAlpha = 0;
                            foreach (var pixel in pixels)
                            {
                                totalAlpha += pixel.a;
                            }
                            if (totalAlpha < 1) //it's empty, bro.
                            {
                                continue;
                            }
                        }
                        var tempTex = new Texture2D(sourceWidth, sourceHeight, TextureFormat.ARGB32, false);
                        tempTex.SetPixels(pixels);
                        switch (scaleMode)
                        {
                        case ScaleMode.HQ2x:
                            HqxSharp.Scale2(tempTex);
                            break;

                        case ScaleMode.HQ3x:
                            HqxSharp.Scale3(tempTex);
                            break;

                        case ScaleMode.HQ4x:
                            HqxSharp.Scale4(tempTex);
                            break;

                        default:
                            break;
                        }
                        TextureScale.Bilinear(tempTex, potWidth, potHeight);
                        tempList.Add(tempTex);
                    }
                }
                texArray = new Texture2DArray(potWidth, potHeight, tempList.Count, TextureFormat.ARGB32, mipmaps);
                for (int i = 0; i < tempList.Count; i++)
                {
                    texArray.SetPixels(tempList[i].GetPixels(), i);
                }
                texArray.Apply();
                texArray.name = baseTexture.name + "Array";
                tempList.Clear();
                Resources.UnloadUnusedAssets();
            }
        }

        texArray = (Texture2DArray)EditorGUILayout.ObjectField("Texture Array", texArray, typeof(Texture2DArray), true);

        if (texArray != null)
        {
            previewIndex = EditorGUILayout.IntSlider(previewIndex, 0, texArray.depth - 1);
            if (previewTexture == null)
            {
                previewTexture = new Texture2D(texArray.width, texArray.height, TextureFormat.ARGB32, false);
            }
            if (previewTexture.width != texArray.width || previewTexture.height != texArray.height)
            {
                previewTexture.Resize(texArray.width, texArray.height);
            }

            var previewPixels = texArray.GetPixels(previewIndex);

            for (int i = 0; i < previewPixels.Length; i++)
            {
                previewPixels[i] = Color.Lerp(Color.magenta, previewPixels[i], previewPixels[i].a);
            }

            previewTexture.SetPixels(previewPixels);
            previewTexture.Apply();

            EditorGUI.DrawPreviewTexture(EditorGUILayout.GetControlRect(false, previewTexture.height), previewTexture, null, UnityEngine.ScaleMode.ScaleToFit);

            if (GUILayout.Button("Save Texture Array"))
            {
                var path = EditorUtility.SaveFilePanelInProject("Save texture array to asset", texArray.name + ".asset", "asset", "Please select a filename to save the texture atlas to.");
                AssetDatabase.CreateAsset(texArray, path);
                AssetDatabase.Refresh();
            }
            if (GUILayout.Button("Generate Bump"))
            {
                normalArray = new Texture2DArray(texArray.width, texArray.height, texArray.depth, TextureFormat.ARGB32, true, true);
                for (int i = 0; i < normalArray.depth; i++)
                {
                    normalArray.SetPixels(TextureTools.Bevel(texArray.GetPixels(i), texArray.width, texArray.height), i);
                }
                normalArray.Apply();
            }
        }
        if (normalArray != null)
        {
            previewIndex = EditorGUILayout.IntSlider(previewIndex, 0, normalArray.depth - 1);
            if (normalPreviewTexture == null)
            {
                normalPreviewTexture = new Texture2D(normalArray.width, normalArray.height, TextureFormat.ARGB32, false);
            }
            if (normalPreviewTexture.width != normalArray.width || normalPreviewTexture.height != normalArray.height)
            {
                normalPreviewTexture.Resize(normalArray.width, normalArray.height);
            }

            var previewPixels = normalArray.GetPixels(previewIndex);

            for (int i = 0; i < previewPixels.Length; i++)
            {
                Color color = previewPixels[i];
                previewPixels[i] = new Color(color.a, color.g, Mathf.Sqrt(1 - (color.a * 2 - 1) * (color.a * 2 - 1) - (color.g * 2 - 1) * (color.g * 2 - 1)));
            }

            normalPreviewTexture.SetPixels(previewPixels);
            normalPreviewTexture.Apply();

            EditorGUI.DrawPreviewTexture(EditorGUILayout.GetControlRect(false, normalPreviewTexture.height), normalPreviewTexture, null, UnityEngine.ScaleMode.ScaleToFit);

            if (GUILayout.Button("Save Texture Array"))
            {
                var path = EditorUtility.SaveFilePanelInProject("Save texture array to asset", normalArray.name + ".asset", "asset", "Please select a filename to save the texture atlas to.");
                AssetDatabase.CreateAsset(normalArray, path);
                AssetDatabase.Refresh();
            }
        }
        EditorGUILayout.EndScrollView();
    }
Example #5
0
    public void FinalizeTextures()
    {
        if (coordList.Count == 0) // There's nothing that uses this.
        {
            Debug.LogWarningFormat("Tile page \"{0}\" has no sprites used!", pageName);
            return;
        }
        int scaleFactor = 1;

        if (tileWidth * 4 <= GameSettings.Instance.rendering.maxTextureSize && tileHeight * 4 <= GameSettings.Instance.rendering.maxTextureSize)
        {
            scaleFactor = 4;
        }
        else if (tileWidth * 3 <= GameSettings.Instance.rendering.maxTextureSize && tileHeight * 3 <= GameSettings.Instance.rendering.maxTextureSize)
        {
            scaleFactor = 3;
        }
        else if (tileWidth * 2 <= GameSettings.Instance.rendering.maxTextureSize && tileHeight * 2 <= GameSettings.Instance.rendering.maxTextureSize)
        {
            scaleFactor = 2;
        }



        tileArray   = new Texture2DArray(Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor), coordList.Count, TextureFormat.ARGB32, true);
        normalArray = new Texture2DArray(Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor), coordList.Count, TextureFormat.ARGB32, true, true);

        for (int i = 0; i < coordList.Count; i++)
        {
            var coord        = coordList[i];
            var tileSource   = originalPage.GetPixels(coord.x * tileWidth, (pageHeight - coord.y - 1) * tileHeight, tileWidth, tileHeight);
            var tileSource32 = new Color32[tileSource.Length];
            for (int j = 0; j < tileSource.Length; j++)
            {
                tileSource32[j] = tileSource[j];
            }
            var tileDest32 = new Color32[tileWidth * scaleFactor * tileHeight * scaleFactor];
            switch (scaleFactor)
            {
            case 4:
                HqxSharp.Scale4(tileSource32, tileDest32, tileWidth, tileHeight);
                break;

            case 3:
                HqxSharp.Scale3(tileSource32, tileDest32, tileWidth, tileHeight);
                break;

            case 2:
                HqxSharp.Scale2(tileSource32, tileDest32, tileWidth, tileHeight);
                break;

            default:
                tileDest32 = tileSource32;
                break;
            }
            Texture2D texture = new Texture2D(tileWidth * scaleFactor, tileHeight * scaleFactor, TextureFormat.ARGB32, false);
            texture.SetPixels32(tileDest32);
            TextureScale.Bilinear(texture, Mathf.ClosestPowerOfTwo(tileWidth * scaleFactor), Mathf.ClosestPowerOfTwo(tileHeight * scaleFactor));
            tileArray.SetPixels(texture.GetPixels(), i);
            normalArray.SetPixels(TextureTools.Bevel(texture.GetPixels(), texture.width, texture.height), i);
        }
        tileArray.Apply();
        normalArray.Apply();
    }