Ejemplo n.º 1
0
        public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
        {
            CurveData data = (CurveData)ThryEditor.currentlyDrawing.currentProperty.property_data;

            if (data == null)
            {
                data       = new CurveData();
                data.curve = new AnimationCurve();
                if (ThryEditor.currentlyDrawing.currentProperty.options.image == null)
                {
                    data.imageData = new ImageData();
                }
                else
                {
                    data.imageData = ThryEditor.currentlyDrawing.currentProperty.options.image;
                }
            }

            editor.TexturePropertyMiniThumbnail(position, prop, "", "");

            EditorGUI.BeginChangeCheck();
            data.curve = EditorGUI.CurveField(position, new GUIContent("       " + label.text, label.tooltip), data.curve);
            if (EditorGUI.EndChangeCheck())
            {
                data.texture      = Converter.CurveToTexture(data.curve, data.imageData.width, data.imageData.height, data.imageData.channel);
                prop.textureValue = data.texture;
                data.saved        = false;
            }

            string windowName = "";

            if (EditorWindow.focusedWindow != null)
            {
                windowName = EditorWindow.focusedWindow.titleContent.text;
            }
            bool isCurveEditor = windowName == "Curve";

            if (isCurveEditor)
            {
                data.window = EditorWindow.focusedWindow;
            }
            if (data.window == null && !data.saved)
            {
                Debug.Log(prop.textureValue.ToString());
                Texture saved_texture = Helper.SaveTextureAsPNG(data.texture, PATH.TEXTURES_DIR + "curves/" + data.curve.GetHashCode() + ".png", null);
                prop.textureValue = saved_texture;
                data.saved        = true;
            }

            ThryEditor.currentlyDrawing.currentProperty.property_data = data;
        }
 public void OnDestroy()
 {
     if (gradient_has_been_edited)
     {
         if (data.preview_texture.GetType() == typeof(Texture2D))
         {
             string  file_name = GradientFileName(data.gradient, prop.targets[0].name);;
             Texture saved     = Helper.SaveTextureAsPNG((Texture2D)data.preview_texture, "Assets/Textures/Gradients/" + file_name, textureSettings);
             file_name = Regex.Replace(file_name, @"\.((png)|(jpg))$", "");
             Helper.SaveValueToFile(file_name, Parser.ObjectToString(data.gradient), ".thry_gradients");
             prop.textureValue = saved;
         }
     }
     else
     {
         UpdatePreviewTexture(privious_preview_texture);
     }
 }
Ejemplo n.º 3
0
        public static Texture TextToTexture(string text, string alphabetTextureName)
        {
            string[] guids = AssetDatabase.FindAssets(alphabetTextureName + " t:texture");
            if (guids.Length > 0)
            {
                string path = null;
                foreach (string g in guids)
                {
                    string p = AssetDatabase.GUIDToAssetPath(g);
                    if (p.Contains("/" + alphabetTextureName + "."))
                    {
                        path = p;
                    }
                }
                if (path == null)
                {
                    Debug.LogWarning("Alphabet texture could not be found.");
                    return(null);
                }
                TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(path);
                importer.isReadable = true;
                importer.SaveAndReimport();

                Texture2D alphabet_texture = AssetDatabase.LoadAssetAtPath <Texture2D>(path);

                Color background = alphabet_texture.GetPixel(0, 0);

                //load letter data from alphabet
                int     padding              = 0;
                int[][] letterSpaces         = new int[62][];
                int     currentLetterIndex   = -1;
                bool    wasLetter            = false;
                int     pixelLetterFreshhold = 3;
                for (int x = 0; x < alphabet_texture.width; x++)
                {
                    int pixelIsLetter = 0;
                    for (int y = 0; y < alphabet_texture.height; y++)
                    {
                        if (Helper.ColorDifference(background, alphabet_texture.GetPixel(x, y)) > 0.01)
                        {
                            pixelIsLetter += 1;
                        }
                    }
                    bool isLetter = pixelIsLetter > pixelLetterFreshhold;
                    if (isLetter && !wasLetter)
                    {
                        currentLetterIndex += 1;
                        if (currentLetterIndex >= letterSpaces.Length)
                        {
                            break;
                        }
                        letterSpaces[currentLetterIndex]    = new int[2];
                        letterSpaces[currentLetterIndex][0] = x;
                    }
                    else if (isLetter && wasLetter)
                    {
                        letterSpaces[currentLetterIndex][1] += 1;
                    }
                    else if (!isLetter)
                    {
                        padding += 1;
                    }
                    wasLetter = isLetter;
                }
                int spaceWidth = 0;
                foreach (int[] s in letterSpaces)
                {
                    if (s != null)
                    {
                        spaceWidth += s[1];
                    }
                }
                spaceWidth /= 62;
                padding    /= 61;

                int a     = 97;
                int A     = 65;
                int zero  = 48;
                int space = 32;

                int   totalWidth     = 0;
                int[] letterIndecies = new int[text.Length];

                // text to alphabet texture letter indecies
                int i = 0;
                foreach (char c in text)
                {
                    if ((int)c == 10)
                    {
                        continue;
                    }
                    int letterIndex = 0;
                    if ((int)c - a >= 0)
                    {
                        letterIndex = ((int)c - a) * 2 + 1;
                    }
                    else if ((int)c - A >= 0)
                    {
                        letterIndex = ((int)c - A) * 2;
                    }
                    else if ((int)c - zero >= 0)
                    {
                        letterIndex = 52 + (int)c - zero;
                    }
                    else if ((int)c - space >= 0)
                    {
                        letterIndex = -1;
                    }
                    //Debug.Log("Char: " + c +","+ (int)c+","+letterIndex);
                    letterIndecies[i] = letterIndex;
                    if (letterIndex == -1)
                    {
                        totalWidth += spaceWidth;
                    }
                    else
                    {
                        totalWidth += letterSpaces[letterIndex][1] + padding;
                    }
                    i++;
                }

                Texture2D text_texture = new Texture2D(totalWidth, alphabet_texture.height);

                Debug.Log("Padding: " + padding);
                Debug.Log("Total width: " + totalWidth);
                // indecies to texture
                int letterX = 0;
                for (i = 0; i < letterIndecies.Length; i++)
                {
                    //Debug.Log(i + "," + letterIndecies[i]);
                    if (letterIndecies[i] == -1)
                    {
                        for (int x = 0; x < spaceWidth; x++)
                        {
                            for (int y = 0; y < text_texture.height; y++)
                            {
                                text_texture.SetPixel(letterX + x, y, background);
                            }
                        }
                        letterX += spaceWidth;
                    }
                    else
                    {
                        for (int x = 0; x < letterSpaces[letterIndecies[i]][1] + padding; x++)
                        {
                            for (int y = 0; y < text_texture.height; y++)
                            {
                                text_texture.SetPixel(letterX + x, y, alphabet_texture.GetPixel(letterSpaces[letterIndecies[i]][0] + x - padding / 2, y));
                            }
                        }
                        letterX += letterSpaces[letterIndecies[i]][1] + padding;
                    }
                }

                string file_name = "text_" + Regex.Replace(text, @"\s", "_");
                string save_path = "Assets/Textures/Text/" + file_name + ".png";
                return(Helper.SaveTextureAsPNG(text_texture, save_path, null));
            }
            else
            {
                Debug.LogWarning("Alphabet texture could not be found.");
            }
            return(null);
        }