Texture2D PackTexture(Vector2Int resolution, Texture2D red, Texture2D green, Texture2D blue, Texture2D alpha)
        {
            if (!PackerShader)
            {
                Debug.LogWarning(log_prefix + "Packer shader is missing or invalid. Can't pack textures.");
                return(null);
            }

            // Setup Material
            var mat = new Material(PackerShader);

            mat.SetTexture("_Red", red);
            mat.SetTexture("_Green", green);
            mat.SetTexture("_Blue", blue);
            mat.SetTexture("_Alpha", alpha);

            // Create texture and render to it
            var tex = new Texture2D(resolution.x, resolution.y);

            tex.BakeMaterialToTexture(mat);

            // Cleanup
            PoiHelpers.DestroyAppropriate(mat);

            return(tex);
        }
Ejemplo n.º 2
0
        void DoPack()
        {
            if (PackerShadersExist)
            {
                Texture2D red   = packRed;
                Texture2D green = packGreen;
                Texture2D blue  = packBlue;
                Texture2D alpha = packAlpha;

                if (showChannelPicker)
                {
                    red   = packRed.GetChannelAsTexture(redTexChan);
                    green = packRed.GetChannelAsTexture(greenTexChan);
                    blue  = packRed.GetChannelAsTexture(blueTexChan);
                    alpha = packRed.GetChannelAsTexture(alphaTexChan);
                }

                Texture2D packResult = PoiHelpers.PackTextures(PackSize, red, green, blue, alpha);
                if (packResult)
                {
                    string path = $"{savePath}/Packed/{packedName}.png";
                    packResult.SaveTextureAsset(path, true);
                    Debug.Log(LOG_PREFIX + "Finished packing texture at " + path);
                    PoiHelpers.PingAssetAtPath(path);
                }
            }
        }
Ejemplo n.º 3
0
        void DrawTextureSelector(string label, ref Texture2D tex, ref TextureChannel selectedChannel, ref bool invert)
        {
            EditorGUILayout.BeginVertical(EditorStyles.helpBox);
            {
                EditorGUILayout.BeginHorizontal();
                {
                    EditorGUILayout.BeginVertical();
                    {
                        var labelContent = new GUIContent(label);
                        var size         = EditorStyles.boldLabel.CalcSize(labelContent);

                        EditorGUILayout.LabelField(labelContent, EditorStyles.boldLabel, GUILayout.MaxWidth(size.x));

                        GUILayout.Space(15 * EditorGUIUtility.pixelsPerPoint);

                        GUILayout.FlexibleSpace();

                        invert = EditorGUILayout.ToggleLeft(INVERT_LABEL, invert, GUILayout.MaxWidth(InvertToggleWidth));
                    }
                    EditorGUILayout.EndVertical();

                    tex = EditorGUILayout.ObjectField(GUIContent.none, tex, typeof(Texture2D), true, GUILayout.ExpandHeight(true)) as Texture2D;
                }
                EditorGUILayout.EndHorizontal();

                if (showChannelPicker)
                {
                    EditorGUI.BeginDisabledGroup(!tex);
                    selectedChannel = PoiHelpers.DrawChannelSelector(selectedChannel, ChannelLabels);
                    EditorGUI.EndDisabledGroup();
                }
            }
            EditorGUILayout.EndVertical();
        }
        void DrawUnpackUI()
        {
            EditorGUI.BeginChangeCheck();
            {
                unpackSource =
                    EditorGUILayout.ObjectField("Packed Texture", unpackSource, typeof(Texture2D), true) as Texture2D;
            }
            if (EditorGUI.EndChangeCheck() && unpackSizeAutoSelect)
            {
                // Get biggest texture size from selections and make a selection in our sizes list
                var tempSize = PoiHelpers.GetMaxSizeFromTextures(unpackSource);
                if (tempSize != default)
                {
                    UnpackSize = tempSize.ClosestPowerOfTwo(AUTO_SELECT_CEILING);
                }
            }

            PoiHelpers.DrawLine();

            EditorGUI.BeginDisabledGroup(!unpackSource);
            {
                unpackedName = EditorGUILayout.TextField("File name", unpackedName);

                EditorGUILayout.Space();

                UnpackSize = PoiHelpers.DrawResolutionPicker(UnpackSize, ref unpackSizeIsLinked, ref unpackSizeAutoSelect, SizePresets,
                                                             SizePresetNames);

                PoiHelpers.DrawLine();

                if (GUILayout.Button("Unpack"))
                {
                    var    output   = UnpackTextureToChannels(unpackSource, UnpackSize);
                    string pingPath = null;
                    try
                    {
                        AssetDatabase.StartAssetEditing();
                        foreach (var kv in output)
                        {
                            if (string.IsNullOrWhiteSpace(pingPath))
                            {
                                pingPath = $"{savePath}/Unpacked/{unpackedName}_{kv.Key}.png";
                            }
                            kv.Value?.SaveTextureAsset($"{savePath}/Unpacked/{unpackedName}_{kv.Key}.png", true);
                        }
                    }
                    catch {}
                    finally
                    {
                        AssetDatabase.StopAssetEditing();
                    }

                    Debug.Log(log_prefix + "Finished unpacking texture at " + pingPath);
                    PoiHelpers.PingAssetAtPath(pingPath);
                }
            }
            EditorGUI.EndDisabledGroup();

            PoiHelpers.DrawLine();
        }
Ejemplo n.º 5
0
        void DoUnpack(TextureChannel singleChannel = TextureChannel.RGBA)
        {
            if (!PackerShadersExist)
            {
                return;
            }

            var channelTextures = new Dictionary <string, Texture2D>();

            if (singleChannel == TextureChannel.RGBA)
            {
                channelTextures = PoiHelpers.UnpackTextureToChannels(unpackSource, unpackInvert, UnpackSize);
            }
            else
            {
                channelTextures[singleChannel.ToString().ToLower()] = unpackSource.GetChannelAsTexture(singleChannel, unpackInvert, UnpackSize);
            }


            string pingPath = null;

            pingPath = SaveTextures(channelTextures, pingPath);

            Debug.Log(LOG_PREFIX + "Finished unpacking texture at " + pingPath);
            PoiHelpers.PingAssetAtPath(pingPath);
        }
Ejemplo n.º 6
0
        void DrawUnpackUI()
        {
            _scroll = EditorGUILayout.BeginScrollView(_scroll);
            {
                EditorGUI.BeginChangeCheck();
                {
                    DrawTextureSelector(PACKED_TEXTURE_LABEL, ref unpackSource, ref unpackChan, ref unpackInvert);
                }
                if (EditorGUI.EndChangeCheck() && unpackSizeAutoSelect)
                {
                    // Get biggest texture size from selections and make a selection in our sizes list
                    var tempSize = PoiHelpers.GetMaxSizeFromTextures(unpackSource);
                    if (tempSize != default)
                    {
                        UnpackSize = tempSize.ClosestPowerOfTwo(AUTO_SELECT_CEILING);
                    }
                }

                DrawShowChannelPicker(ref showChannelPicker);
            }
            EditorGUILayout.EndScrollView();

            EditorGUI.BeginDisabledGroup(!unpackSource);
            {
                UnpackSize = DrawTextureSizeSettings(UnpackSize, ref unpackedName, ref unpackSizeIsLinked, ref unpackSizeAutoSelect);

                if (GUILayout.Button("Unpack", PoiStyles.BigButton))
                {
                    DoUnpack(unpackChan);
                }
            }
            EditorGUI.EndDisabledGroup();

            EditorGUILayout.Space();
        }
Ejemplo n.º 7
0
 private Vector2Int DrawTextureSizeSettings(Vector2Int size, ref string fileName, ref bool sizeIsLinked, ref bool sizeAutoSelect)
 {
     EditorGUILayout.BeginVertical(EditorStyles.helpBox);
     {
         fileName = EditorGUILayout.TextField("File name", fileName);
         EditorGUILayout.Space();
         size = PoiHelpers.DrawResolutionPicker(size, ref sizeIsLinked, ref sizeAutoSelect, SizePresets, SizePresetNames);
     }
     EditorGUILayout.EndVertical();
     return(size);
 }
Ejemplo n.º 8
0
        void DrawTextureSelector(string label, ref Texture2D tex, ref TextureChannel selectedChannel)
        {
            EditorGUILayout.BeginVertical(EditorStyles.helpBox);
            {
                tex = EditorGUILayout.ObjectField(label, tex, typeof(Texture2D), true, GUILayout.ExpandHeight(true)) as Texture2D;

                if (showChannelPicker)
                {
                    EditorGUI.BeginDisabledGroup(!tex);
                    selectedChannel = PoiHelpers.DrawChannelSelector(selectedChannel);
                    EditorGUI.EndDisabledGroup();
                }
            }
            EditorGUILayout.EndHorizontal();
        }
        void OnGUI()
        {
            PoiHelpers.DrawLine();
            selectedTab = GUILayout.Toolbar(selectedTab, TabNames);
            PoiHelpers.DrawLine();

            if (selectedTab == (int)Tab.Pack)
            {
                DrawPackUI();
            }
            else
            {
                DrawUnpackUI();
            }
        }
        void DrawPackUI()
        {
            EditorGUI.BeginChangeCheck();
            {
                packRed   = EditorGUILayout.ObjectField("Red", packRed, typeof(Texture2D), true) as Texture2D;
                packGreen = EditorGUILayout.ObjectField("Green", packGreen, typeof(Texture2D), true) as Texture2D;
                packBlue  = EditorGUILayout.ObjectField("Blue", packBlue, typeof(Texture2D), true) as Texture2D;
                packAlpha = EditorGUILayout.ObjectField("Alpha", packAlpha, typeof(Texture2D), true) as Texture2D;
            }
            if (EditorGUI.EndChangeCheck() && packSizeAutoSelect)
            {
                // Get biggest texture size from selections and make a selection in our sizes list
                var tempSize = PoiHelpers.GetMaxSizeFromTextures(packRed, packGreen, packBlue, packAlpha);
                if (tempSize != default)
                {
                    PackSize = tempSize.ClosestPowerOfTwo(AUTO_SELECT_CEILING);
                }
            }

            PoiHelpers.DrawLine();

            bool disabled = new bool[] { packRed, packGreen, packBlue, packAlpha }.Count(b => b) < 2;

            EditorGUI.BeginDisabledGroup(disabled);
            {
                packedName = EditorGUILayout.TextField("File name", packedName);

                EditorGUILayout.Space();

                PackSize = PoiHelpers.DrawResolutionPicker(PackSize, ref packSizeIsLinked, ref packSizeAutoSelect, SizePresets, SizePresetNames);

                EditorGUILayout.Space();
                PoiHelpers.DrawLine();

                if (GUILayout.Button("Pack"))
                {
                    var packResult = PackTexture(PackSize, packRed, packGreen, packBlue, packAlpha);
                    if (packResult)
                    {
                        string path = $"{savePath}/Packed/{packedName}.png";
                        packResult.SaveTextureAsset(path, true);
                        Debug.Log(log_prefix + "Finished packing texture at " + path);
                        PoiHelpers.PingAssetAtPath(path);
                    }
                }
            }
            PoiHelpers.DrawLine();
        }
Ejemplo n.º 11
0
        void OnGUI()
        {
            EditorGUILayout.LabelField("Poi Texture Packer", PoiStyles.TitleLabel);
            EditorGUILayout.LabelField(SubTitle);

            PoiHelpers.DrawLine();

            selectedTab = GUILayout.Toolbar(selectedTab, TabNames);

            if (selectedTab == (int)Tab.Pack)
            {
                DrawPackUI();
            }
            else
            {
                DrawUnpackUI();
            }
        }
Ejemplo n.º 12
0
        void DrawPackUI()
        {
            _scroll = EditorGUILayout.BeginScrollView(_scroll);
            {
                EditorGUI.BeginChangeCheck();
                {
                    DrawTextureSelector(RED_TEXTURE_LABEL, ref packRed, ref redTexChan, ref redInvert);
                    DrawTextureSelector(GREEN_TEXTURE_LABEL, ref packGreen, ref greenTexChan, ref greenInvert);
                    DrawTextureSelector(BLUE_TEXTURE_LABEL, ref packBlue, ref blueTexChan, ref blueInvert);
                    DrawTextureSelector(ALPHA_TEXTURE_LABEL, ref packAlpha, ref alphaTexChan, ref alphaInvert);
                }
                if (EditorGUI.EndChangeCheck() && packSizeAutoSelect)
                {
                    // Get biggest texture size from selections and make a selection in our sizes list
                    var tempSize = PoiHelpers.GetMaxSizeFromTextures(packRed, packGreen, packBlue, packAlpha);
                    if (tempSize != default)
                    {
                        PackSize = tempSize.ClosestPowerOfTwo(AUTO_SELECT_CEILING);
                    }
                }
            }
            EditorGUILayout.EndScrollView();

            DrawShowChannelPicker(ref showChannelPicker);

            bool disabled = new bool[] { packRed, packGreen, packBlue, packAlpha }.Count(b => b) < 2;

            EditorGUI.BeginDisabledGroup(disabled);
            {
                PackSize = DrawTextureSizeSettings(PackSize, ref packedName, ref packSizeIsLinked, ref packSizeAutoSelect);

                if (GUILayout.Button("Pack", PoiStyles.BigButton))
                {
                    DoPack();
                }

                EditorGUILayout.Space();
            }
            EditorGUI.EndDisabledGroup();
        }
Ejemplo n.º 13
0
        void DrawPackUI()
        {
            EditorGUI.BeginChangeCheck();
            {
                DrawTextureSelector("Red", ref packRed, ref redTexChan);
                DrawTextureSelector("Green", ref packGreen, ref greenTexChan);
                DrawTextureSelector("Blue", ref packBlue, ref blueTexChan);
                DrawTextureSelector("Alpha", ref packAlpha, ref alphaTexChan);
            }
            if (EditorGUI.EndChangeCheck() && packSizeAutoSelect)
            {
                // Get biggest texture size from selections and make a selection in our sizes list
                var tempSize = PoiHelpers.GetMaxSizeFromTextures(packRed, packGreen, packBlue, packAlpha);
                if (tempSize != default)
                {
                    PackSize = tempSize.ClosestPowerOfTwo(AUTO_SELECT_CEILING);
                }
            }

            EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
            showChannelPicker = EditorGUILayout.ToggleLeft("Pick source channel", showChannelPicker);
            EditorGUILayout.EndHorizontal();

            bool disabled = new bool[] { packRed, packGreen, packBlue, packAlpha }.Count(b => b) < 2;

            EditorGUI.BeginDisabledGroup(disabled);
            {
                PackSize = DrawTextureSizeSettings(PackSize, ref packedName, ref packSizeIsLinked, ref packSizeAutoSelect);

                if (GUILayout.Button("Pack", PoiStyles.BigButton))
                {
                    DoPack();
                }

                EditorGUILayout.Space();
            }
            EditorGUI.EndDisabledGroup();
        }
Ejemplo n.º 14
0
        void DrawUnpackUI()
        {
            EditorGUI.BeginChangeCheck();
            {
                DrawTextureSelector("Packed Texture", ref unpackSource);
            }
            if (EditorGUI.EndChangeCheck() && unpackSizeAutoSelect)
            {
                // Get biggest texture size from selections and make a selection in our sizes list
                var tempSize = PoiHelpers.GetMaxSizeFromTextures(unpackSource);
                if (tempSize != default)
                {
                    UnpackSize = tempSize.ClosestPowerOfTwo(AUTO_SELECT_CEILING);
                }
            }

            EditorGUI.BeginDisabledGroup(!unpackSource);
            {
                UnpackSize = DrawTextureSizeSettings(UnpackSize, ref unpackedName, ref unpackSizeIsLinked, ref unpackSizeAutoSelect);

                if (GUILayout.Button("Unpack", PoiStyles.BigButton))
                {
                    if (PackerShadersExist)
                    {
                        var    channelTextures = PoiHelpers.UnpackTextureToChannels(unpackSource, UnpackSize);
                        string pingPath        = null;
                        pingPath = SaveTextures(channelTextures, pingPath);

                        Debug.Log(LOG_PREFIX + "Finished unpacking texture at " + pingPath);
                        PoiHelpers.PingAssetAtPath(pingPath);
                    }
                }
            }
            EditorGUI.EndDisabledGroup();

            EditorGUILayout.Space();
        }