private Color GetColorAtI(ref GradientData data, int i, int d)
        {
            int   y   = (int)(((float)i) / d * data.texture.height);
            int   x   = (int)(((float)i) / d * data.texture.width);
            Color col = data.texture.GetPixel(x, y);

            return(col);
        }
 private void GradientToTexture(ref GradientData data)
 {
     for (int x = 0; x < data.texture.width; x++)
     {
         Color col = data.gradientObj.gradient.Evaluate((float)x / data.texture.width);
         for (int y = 0; y < data.texture.height; y++)
         {
             data.texture.SetPixel(x, y, col);
         }
     }
     data.texture.Apply();
 }
Ejemplo n.º 3
0
        public static void Open(GradientData data, MaterialProperty prop, TextureData predefinedTextureSettings, bool force_texture_options = false, bool show_texture_options = true)
        {
            texture_settings_data = LoadTextureSettings(prop, predefinedTextureSettings, force_texture_options);
            data.gradient         = TextureHelper.GetGradient(prop.textureValue);
            GradientEditor window = (GradientEditor)EditorWindow.GetWindow(typeof(GradientEditor));

            window.privious_preview_texture = prop.textureValue;
            window.prop = prop;
            window.data = data;
            window.show_texture_options = show_texture_options;
            window.minSize = new Vector2(350, 350);
            window.Show();
        }
        public static void Open(GradientData data, MaterialProperty prop)
        {
            data.gradient         = Helper.GetGradient(prop.textureValue);
            texture_settings_data = null;
            GradientEditor window = (GradientEditor)EditorWindow.GetWindow(typeof(GradientEditor));

            window.privious_preview_texture = prop.textureValue;
            window.prop = prop;
            window.data = data;
            window.SetGradient(data.gradient);
            window.gradient_has_been_edited = false;
            window.Show();
        }
        public static void Open(GradientData data, MaterialProperty prop, bool show_texture_options = true)
        {
            data.gradient         = TextureHelper.GetGradient(prop.textureValue);
            texture_settings_data = null;
            GradientEditor window = (GradientEditor)EditorWindow.GetWindow(typeof(GradientEditor));

            window.privious_preview_texture = prop.textureValue;
            window.prop = prop;
            window.data = data;
            window.SetGradient(data.gradient);
            window.gradient_has_been_edited = false;
            window.show_texture_options     = show_texture_options;
            window.minSize = new Vector2(350, 350);
            window.Show();
        }
        private string GradientToString(ref GradientData data)
        {
            string ret = "";

            foreach (GradientColorKey key in data.gradientObj.gradient.colorKeys)
            {
                ret += "c," + key.color.r + "," + key.color.g + "," + key.color.b + "," + key.time;
            }
            foreach (GradientAlphaKey key in data.gradientObj.gradient.alphaKeys)
            {
                ret += "a," + key.alpha + "," + key.time;
            }
            ret += "m" + ((int)data.gradientObj.gradient.mode);
            return(ret);
        }
        private void TextureUpdated(ref GradientData data)
        {
            data.texture = SetTextureImporterFormat(data.texture, true);
            string gradientInfo = Helper.LoadValueFromFile(data.texture.name, GRADIENT_INFO_FILE_PATH);

            if (gradientInfo != null)
            {
                Debug.Log("Gradient Data: " + gradientInfo);
                Debug.Log("Load Gradient from save file.");
                StringToGradient(ref data, gradientInfo);
                data.serializedGradient = new SerializedObject(data.gradientObj);
                data.colorGradient      = data.serializedGradient.FindProperty("gradient");
            }
            else
            {
                TextureToGradient(ref data);
            }
        }
        private void StringToGradient(ref GradientData data, string s)
        {
            List <GradientColorKey> colorKeys    = new List <GradientColorKey>();
            List <GradientAlphaKey> alphaKeys    = new List <GradientAlphaKey>();
            MatchCollection         colorMatches = Regex.Matches(s, @"c,\d+(\.\d+)?,\d+(\.\d+)?,\d+(\.\d+)?,\d+(\.?\d+)?");
            MatchCollection         alphaMatches = Regex.Matches(s, @"a,\d+(\.\d+)?,\d+(\.\d+)?");
            Match blendMatch = Regex.Match(s, @"m\d+");

            foreach (Match m in colorMatches)
            {
                string[] graddata = Regex.Split(m.Value, @",");
                colorKeys.Add(new GradientColorKey(new Color(float.Parse(graddata[1]), float.Parse(graddata[2]), float.Parse(graddata[3]), 1), float.Parse(graddata[4])));
            }
            foreach (Match m in alphaMatches)
            {
                string[] graddata = Regex.Split(m.Value, @",");
                alphaKeys.Add(new GradientAlphaKey(float.Parse(graddata[1]), float.Parse(graddata[2])));
            }
            data.gradientObj.gradient.SetKeys(colorKeys.ToArray(), alphaKeys.ToArray());
            data.gradientObj.gradient.mode = (GradientMode)(int.Parse(blendMatch.Value.Replace("m", "")));
        }
Ejemplo n.º 9
0
 public void Init(MaterialProperty prop)
 {
     data = new GradientData();
     data.preview_texture = prop.textureValue;
     is_init = true;
 }
        private void TextureToGradient(ref GradientData data)
        {
            Debug.Log("Texture converted to gradient.");

            int d = (int)Mathf.Sqrt(Mathf.Pow(data.texture.width, 2) + Mathf.Pow(data.texture.height, 2));
            List <GradientColorKey> colorKeys = new List <GradientColorKey>();
            List <GradientAlphaKey> alphaKeys = new List <GradientAlphaKey>();

            colorKeys.Add(new GradientColorKey(data.texture.GetPixel(data.texture.width - 1, data.texture.height - 1), 1));
            alphaKeys.Add(new GradientAlphaKey(data.texture.GetPixel(data.texture.width - 1, data.texture.height - 1).a, 1));
            colorKeys.Add(new GradientColorKey(data.texture.GetPixel(0, 0), 0));
            alphaKeys.Add(new GradientAlphaKey(data.texture.GetPixel(0, 0).a, 0));
            int colKeys        = 0;
            int alphaKeysCount = 0;

            bool isFlat    = false;
            bool isNotFlat = false;

            float[][] prevSteps = new float[][] { GetSteps(GetColorAtI(ref data, 0, d), GetColorAtI(ref data, 1, d)), GetSteps(GetColorAtI(ref data, 0, d), GetColorAtI(ref data, 1, d)) };

            bool wasFlat         = false;
            int  maxBetweenFlats = 3;
            int  minFlat         = 3;
            int  flats           = 0;
            int  prevFlats       = 0;
            int  nonFlats        = 0;

            float[][] steps     = new float[d][];
            float[]   alphaStep = new float[d];
            Color     prevColor = GetColorAtI(ref data, 0, d);

            for (int i = 0; i < d; i++)
            {
                Color col = GetColorAtI(ref data, i, d);
                steps[i]     = GetSteps(prevColor, col);
                alphaStep[i] = Mathf.Abs(prevColor.a - col.a);
                prevColor    = col;
            }
            for (int r = 0; r < 1; r++)
            {
                for (int i = 1; i < d - 1; i++)
                {
                    //Debug.Log(i+": "+steps[i][0] + "," + steps[i][1] + ","+steps[i][0]);
                    bool returnToOldVal = false;
                    if (!SameSteps(steps[i], steps[i + 1]) && SimilarSteps(steps[i], steps[i + 1], 0.1f))
                    {
                        int n = i;
                        while (++n < d && SimilarSteps(steps[i - 1], steps[n], 0.1f))
                        {
                            if (SameSteps(steps[i - 1], steps[n]))
                            {
                                returnToOldVal = true;
                            }
                        }
                    }
                    if (returnToOldVal)
                    {
                        steps[i] = steps[i - 1];
                    }

                    returnToOldVal = false;
                    //Debug.Log(i + ": " + steps[i][0] + "," + steps[i][1] + "," + steps[i][0]);
                }
            }


            Color lastStableColor = GetColorAtI(ref data, 0, d);
            float lastStableTime  = 0;
            bool  added           = false;

            for (int i = 1; i < d; i++)
            {
                Color   col         = GetColorAtI(ref data, i, d);
                float[] newColSteps = steps[i];
                float   time        = (float)(i) / d;

                float[] diff = new float[] { prevSteps[0][0] - newColSteps[0], prevSteps[0][1] - newColSteps[1], prevSteps[0][2] - newColSteps[2] };

                if (diff[0] == 0 && diff[1] == 0 && diff[2] == 0)
                {
                    lastStableColor = col;
                    lastStableTime  = time;
                    added           = false;
                }
                else
                {
                    if (added == false && colKeys++ < 6)
                    {
                        colorKeys.Add(new GradientColorKey(lastStableColor, lastStableTime));
                    }
                    added = true;
                }

                float alphaDiff = Mathf.Abs(alphaStep[i - 1] - alphaStep[i]);
                if (alphaDiff > 0.05 && ++alphaKeysCount < 6)
                {
                    alphaKeys.Add(new GradientAlphaKey(col.a, time));
                }

                prevSteps[1] = prevSteps[0];
                prevSteps[0] = newColSteps;

                bool thisOneFlat = newColSteps[0] == 0 && newColSteps[1] == 0 && newColSteps[2] == 0;
                if (thisOneFlat)
                {
                    flats++;
                }
                else if (!wasFlat && !thisOneFlat)
                {
                    nonFlats++;
                }
                else if (wasFlat && !thisOneFlat)
                {
                    prevFlats = flats; flats = 0; nonFlats = 1;
                }
                if (flats >= minFlat && prevFlats >= minFlat && nonFlats <= maxBetweenFlats)
                {
                    isFlat = true;
                }
                if (nonFlats > maxBetweenFlats)
                {
                    isNotFlat = true;
                }
                wasFlat = thisOneFlat;
            }
            data.gradientObj.gradient.SetKeys(colorKeys.ToArray(), alphaKeys.ToArray());
            if (isFlat && !isNotFlat)
            {
                data.gradientObj.gradient.mode = GradientMode.Fixed;
            }
            data.serializedGradient = new SerializedObject(data.gradientObj);
            data.colorGradient      = data.serializedGradient.FindProperty("gradient");
            ThryEditor.repaint();
        }
        private string GradientFileName(ref GradientData data, string material_name)
        {
            string hash = "" + GradientToString(ref data).GetHashCode();

            return(GradientFileName(hash, material_name));
        }
        public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
        {
            GradientData data;

            if (ThryEditor.currentlyDrawing.property_data != null)
            {
                data = (GradientData)ThryEditor.currentlyDrawing.property_data;
            }
            else
            {
                data = new GradientData(); data.saved = true; ThryEditor.currentlyDrawing.property_data = data;
            }

            if (data.gradientObj == null)
            {
                data.gradientObj = GradientObject.CreateInstance <GradientObject>();
                if (prop.textureValue != null)
                {
                    data.texture = (Texture2D)prop.textureValue;
                    TextureUpdated(ref data);
                }
                else
                {
                    data.texture            = new Texture2D(256, 1);
                    data.serializedGradient = new SerializedObject(data.gradientObj);
                    data.colorGradient      = data.serializedGradient.FindProperty("gradient");
                }
                data.saved = true;
            }
            EditorGUI.BeginChangeCheck();
            editor.TexturePropertyMiniThumbnail(position, prop, "", "");
            if (EditorGUI.EndChangeCheck())
            {
                data.texture = (Texture2D)prop.textureValue;
                TextureUpdated(ref data);
            }
            EditorGUI.BeginChangeCheck();
            EditorGUI.PropertyField(position, data.colorGradient, new GUIContent("       " + label.text, label.tooltip));
            string windowName = "";

            if (EditorWindow.focusedWindow != null)
            {
                windowName = EditorWindow.focusedWindow.titleContent.text;
            }
            bool isGradientEditor = windowName == "Gradient Editor";

            if (isGradientEditor)
            {
                data.gradientWindow = EditorWindow.focusedWindow;
            }
            bool changed = EditorGUI.EndChangeCheck();

            if (changed)
            {
                if (data.texture == prop.textureValue)
                {
                    data.texture = new Texture2D(256, 1);
                }
                data.serializedGradient.ApplyModifiedProperties();
                GradientToTexture(ref data);
                prop.textureValue = data.texture;
                data.saved        = false;
            }

            if (data.gradientWindow == null && !data.saved)
            {
                byte[] encoding = data.texture.EncodeToPNG();

                string gradient_data = GradientToString(ref data);
                string gradient_name = Config.Get().gradient_name;
                gradient_name = gradient_name.Replace("<material>", editor.target.name);
                gradient_name = gradient_name.Replace("<hash>", "" + gradient_data.GetHashCode());
                gradient_name = gradient_name.Replace("<prop>", prop.name);

                string path = "Assets/Textures/Gradients/" + gradient_name;
                Debug.Log("Gradient saved at \"" + path + "\".");
                Helper.writeBytesToFile(encoding, path);

                Helper.SaveValueToFile(gradient_name, gradient_data, GRADIENT_INFO_FILE_PATH);

                AssetDatabase.ImportAsset(path);
                Texture tex = (Texture)EditorGUIUtility.Load(path);
                tex.wrapMode = TextureWrapMode.Clamp;
                SetTextureImporterFormat((Texture2D)tex, true);
                prop.textureValue = tex;
                data.saved        = true;
            }
        }