コード例 #1
0
        /// <summary>
        /// Sets the min and max values available in the ramp
        /// </summary>
        /// <param name="properties">MaterialProperty array</param>
        public void GenerateRampMinMax(MaterialProperty[] properties)
        {
            foreach (Material m in FindProperty("_MainRampMin", properties).targets)
            {
                Texture2D ramp = (Texture2D)m.GetTexture("_Ramp");
                Color     min  = new Color(100, 100, 100, 0);
                Color     max  = new Color(0, 0, 0, 1);
                if (ramp != null)
                {
#if UNITY_2018_1_OR_NEWER
                    if (!ramp.isReadable)
                    {
                        ramp = TSFunctions.SetTextureImporterFormat(ramp, true);
                    }
                    foreach (Color c in ramp.GetPixels())
                    {
                        if (min.r > c.r)
                        {
                            min.r = c.r;
                        }
                        if (min.g > c.g)
                        {
                            min.g = c.g;
                        }
                        if (min.b > c.b)
                        {
                            min.b = c.b;
                        }
                        if (max.r < c.r)
                        {
                            max.r = c.r;
                        }
                        if (max.g < c.g)
                        {
                            max.g = c.g;
                        }
                        if (max.b < c.b)
                        {
                            max.b = c.b;
                        }
                    }
#else
                    try
                    {
                        foreach (Color c in ramp.GetPixels())
                        {
                            if (min.r > c.r)
                            {
                                min.r = c.r;
                            }
                            if (min.g > c.g)
                            {
                                min.g = c.g;
                            }
                            if (min.b > c.b)
                            {
                                min.b = c.b;
                            }
                            if (max.r < c.r)
                            {
                                max.r = c.r;
                            }
                            if (max.g < c.g)
                            {
                                max.g = c.g;
                            }
                            if (max.b < c.b)
                            {
                                max.b = c.b;
                            }
                        }
                    }
                    catch (UnityException)
                    {
                        ramp = TSFunctions.SetTextureImporterFormat(ramp, true);
                        foreach (Color c in ramp.GetPixels())
                        {
                            if (min.r > c.r)
                            {
                                min.r = c.r;
                            }
                            if (min.g > c.g)
                            {
                                min.g = c.g;
                            }
                            if (min.b > c.b)
                            {
                                min.b = c.b;
                            }
                            if (max.r < c.r)
                            {
                                max.r = c.r;
                            }
                            if (max.g < c.g)
                            {
                                max.g = c.g;
                            }
                            if (max.b < c.b)
                            {
                                max.b = c.b;
                            }
                        }
                    }
#endif
                }
                else
                {
                    min = new Color(0.9f, 0.9f, 0.9f, 1);
                    max = Color.white;
                }
                float intensity = m.GetFloat("_ShadowIntensity");
                if (intensity == 0)
                {
                    intensity = 0.001f;
                }
                Color remapMin = new Color(1 - intensity, 1 - intensity, 1 - intensity, 1);

                min *= m.GetColor("_RampColor").linear;
                max *= m.GetColor("_RampColor").linear;

                min = remap(min, Color.black, Color.white, remapMin, Color.white);
                max = remap(max, Color.black, Color.white, remapMin, Color.white);

                m.SetColor("_MainRampMin", min);
                m.SetColor("_MainRampMax", max);
            }
        }
コード例 #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();
        }