Example #1
0
        /// <summary>
        /// Draws the gui related
        /// </summary>
        /// <returns>True if there's a need to call the Repaint() method, false otherwise</returns>
        public bool DrawGUI()
        {
            repaint = false;
            Rect windowRect = GUILayoutUtility.GetRect(100, 1000, 60, 60);

            gradientPreviewRect  = new Rect(windowRect.x + 10, windowRect.y + 10, windowRect.width - 20, 25);
            keySelectionAreaRect = new Rect(gradientPreviewRect.x - 10, gradientPreviewRect.yMax, gradientPreviewRect.width + 20, 25);
            GUI.DrawTexture(gradientPreviewRect, gradient.GetTexture());
            if (keyRects == null || keyRects.Length != gradient.keys.Count)
            {
                keyRects = new Rect[gradient.keys.Count];
            }
            Rect selectedRect = Rect.zero;

            for (int i = 0; i < gradient.keys.Count; i++)
            {
                Rect keyRect = new Rect(gradientPreviewRect.x + gradientPreviewRect.width * gradient.keys[i].Time - 10, gradientPreviewRect.yMax, 20, 25);
                keyRects[i] = keyRect;
                if (i == selectedKeyIndex)
                {
                    selectedRect = keyRect;
                }
                else
                {
                    GUI.DrawTexture(keyRect, TSConstants.UpColor);
                    GUI.DrawTexture(keyRect, TSConstants.UpColorInternal, ScaleMode.ScaleToFit, true, 0, gradient.keys[i].Color, 0, 0);
                }
            }
            if (selectedRect != Rect.zero)
            {
                GUI.DrawTexture(selectedRect, TSConstants.UpColorSelected);
                GUI.DrawTexture(selectedRect, TSConstants.UpColorInternal, ScaleMode.ScaleToFit, true, 0, gradient.keys[selectedKeyIndex].Color, 0, 0);
            }

            Color col = EditorGUILayout.ColorField("Color", gradient.keys[selectedKeyIndex].Color);

            if (!col.Equals(gradient.keys[selectedKeyIndex].Color))
            {
                gradient.UpdateKeyColor(selectedKeyIndex, col);
            }

            float time = EditorGUILayout.FloatField("Location", gradient.keys[selectedKeyIndex].Time);

            if (time != gradient.keys[selectedKeyIndex].Time)
            {
                gradient.UpdateKeyTime(selectedKeyIndex, time);
            }

            rampWidth = (RampWidth)EditorGUILayout.EnumPopup("Ramp size", rampWidth);
            if ((int)rampWidth != gradient.GetTexture().width)
            {
                gradient.UpdateTextureWidth((int)rampWidth);
            }

            blendMode = (GradientTexture.BlendMode)EditorGUILayout.EnumPopup("Blend mode", blendMode);
            if (blendMode != gradient.blendMode)
            {
                gradient.blendMode = blendMode;
                gradient.UpdateTexture();
            }

            HandleEvents();
            EditorGUILayout.Space();
            return(repaint);
        }
Example #2
0
        public void TranslateTextureToGradient(Texture2D texture)
        {
#if UNITY_2018_1_OR_NEWER
            if (!texture.isReadable)
            {
                texture = TSFunctions.SetTextureImporterFormat(texture, true);
            }
#else
            texture = TSFunctions.SetTextureImporterFormat(texture, true);
#endif
            gradient  = new GradientTexture((int)rampWidth);
            blendMode = GradientTexture.BlendMode.Linear;
            int     sampleWidth = Mathf.Max(texture.width, texture.height);
            Color[] colors      = new Color[sampleWidth];

            for (int i = 0; i < sampleWidth; i++)
            {
                colors[i] = texture.GetPixel(texture.width * i / sampleWidth, texture.height * i / sampleWidth);
                //Debug.Log(colors[i]);
            }
            Color[] delta         = GetDelta(colors);
            int     deltaVariance = 0;
            for (int i = 0; i < delta.Length; i++)
            {
                if (delta[i].r != 0 || delta[i].g != 0 || delta[i].b != 0)
                {
                    deltaVariance++;
                }
                //Debug.Log("color " + colors[i]);
                //Debug.Log("delta " + delta[i]);
            }
            //Debug.Log(deltaVariance);
            //Debug.Log(Mathf.Max(texture.width, texture.height) * 0.1);
            if (deltaVariance < Mathf.Max(texture.width, texture.height) * 0.1)
            {
                blendMode          = GradientTexture.BlendMode.Fixed;
                gradient.blendMode = blendMode;
            }
            delta[0] = delta[1];

            Color[] deltaSquared = GetDelta(delta);
            List <GradientTexture.ColorKey> KeyChanged = new List <GradientTexture.ColorKey>();
            List <Color> deltaChanged = new List <Color>();
            KeyChanged.Add(new GradientTexture.ColorKey(colors[0], 0));
            deltaChanged.Add(deltaSquared[0]);
            for (int i = 1; i < sampleWidth - 1; i++)
            {
                //Debug.Log("color " + colors[i]);
                //Debug.Log("delta " + delta[i]);
                //Debug.Log("deltaSquared " + deltaSquared[i]);
                if (Mathf.Abs(deltaSquared[i].r) > 0.002 || Mathf.Abs(deltaSquared[i].g) > 0.002 || Mathf.Abs(deltaSquared[i].b) > 0.002)
                {
                    if (blendMode == GradientTexture.BlendMode.Fixed)
                    {
                        KeyChanged.Add(new GradientTexture.ColorKey(colors[i - 1], i / ((float)sampleWidth - 1)));
                        deltaChanged.Add(deltaSquared[i]);
                    }
                    else
                    {
                        KeyChanged.Add(new GradientTexture.ColorKey(colors[i], i / ((float)sampleWidth - 1)));
                        deltaChanged.Add(deltaSquared[i]);
                        //Debug.Log(colors[i] + "-" + (i / (float)sampleWidth));
                    }
                }
            }
            KeyChanged.Add(new GradientTexture.ColorKey(colors[(int)sampleWidth - 1], 1));
            deltaChanged.Add(deltaSquared[(int)sampleWidth - 1]);
            List <GradientTexture.ColorKey> finalKeys = new List <GradientTexture.ColorKey>();
            finalKeys.Add(KeyChanged[0]);
            Color lastDelta = new Color(0, 0, 0, 0);
            if (blendMode == GradientTexture.BlendMode.Fixed)
            {
                for (int i = 1; i < KeyChanged.Count; i++)
                {
                    //float deltaVarianceSum = Mathf.Abs(getDeltaVariance(deltaChanged[i]) - getDeltaVariance(lastDelta));
                    if ((KeyChanged[i].Time - finalKeys[finalKeys.Count - 1].Time) < 0.05)
                    {
                        /*if (getDeltaVariance(deltaChanged[i]) > getDeltaVariance(lastDelta))
                         * {
                         *  finalKeys.RemoveAt(finalKeys.Count - 1);
                         *  finalKeys.Add(KeyChanged[i]);
                         *  lastDelta = deltaChanged[i];
                         * }*/
                    }
                    else
                    {
                        finalKeys.Add(KeyChanged[i]);
                        lastDelta = deltaChanged[i];
                    }
                }
            }
            else
            {
                for (int i = 1; i < KeyChanged.Count; i++)
                {
                    float deltaVarianceSum = Mathf.Abs(getDeltaVariance(deltaChanged[i]) - getDeltaVariance(lastDelta));
                    if ((KeyChanged[i].Time - finalKeys[finalKeys.Count - 1].Time) + deltaVarianceSum > 0.1 && deltaVarianceSum > 0.002)
                    {
                        finalKeys.Add(KeyChanged[i]);
                        lastDelta = deltaChanged[i];
                    }
                }
                if (finalKeys[finalKeys.Count - 1].Time != 1)
                {
                    finalKeys.Add(KeyChanged[KeyChanged.Count - 1]);
                }
            }

            gradient.keys = finalKeys;
            gradient.UpdateTexture();
        }