Пример #1
0
 /// <summary>
 /// Constructor
 /// </summary>
 public GradientEditor()
 {
     gradient  = new GradientTexture(1024);
     rampWidth = RampWidth.L_1024;
     blendMode = GradientTexture.BlendMode.Linear;
 }
Пример #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();
        }