Exemplo n.º 1
0
        private static Texture2D BuildAtlasWithEncodedSettings(GradientSettings[] settings, Texture2D atlas)
        {
            var oldActive = RenderTexture.active;

            int width  = atlas.width + 3;
            int height = Math.Max(settings.Length, atlas.height);

            var desc = new RenderTextureDescriptor(width, height, RenderTextureFormat.ARGB32, 0)
            {
                sRGB = QualitySettings.activeColorSpace == ColorSpace.Linear
            };
            var rt = RenderTexture.GetTemporary(desc);

            GL.Clear(false, true, Color.black, 1.0f);
            Graphics.Blit(atlas, rt, Vector2.one, new Vector2(-3.0f / width, 0.0f));
            RenderTexture.active = rt;

            Texture2D copy = new Texture2D(width, height, TextureFormat.RGBA32, false);

            copy.hideFlags = HideFlags.HideAndDontSave;
            copy.ReadPixels(new Rect(0, 0, width, height), 0, 0);

            // This encoding procedure is duplicated a few times, do something about it
            var rawSettingsTex = new VectorUtils.RawTexture()
            {
                Width = 3, Height = settings.Length,
                Rgba  = new Color32[3 * settings.Length]
            };

            for (int i = 0; i < settings.Length; ++i)
            {
                var g = settings[i];

                // There are 3 consecutive pixels to store the settings
                int destX = 0;
                int destY = i;

                if (g.gradientType == GradientType.Radial)
                {
                    var focus = g.radialFocus;
                    focus  += Vector2.one;
                    focus  /= 2.0f;
                    focus.y = 1.0f - focus.y;

                    VectorUtils.WriteRawFloat4Packed(rawSettingsTex, ((float)g.gradientType) / 255, ((float)g.addressMode) / 255, focus.x, focus.y, destX++, destY);
                }
                else
                {
                    VectorUtils.WriteRawFloat4Packed(rawSettingsTex, 0.0f, ((float)g.addressMode) / 255, 0.0f, 0.0f, destX++, destY);
                }

                var pos  = g.location.position;
                var size = g.location.size;
                size.x -= 1;
                size.y -= 1;
                VectorUtils.WriteRawInt2Packed(rawSettingsTex, (int)pos.x + 3, (int)pos.y, destX++, destY);
                VectorUtils.WriteRawInt2Packed(rawSettingsTex, (int)size.x, (int)size.y, destX++, destY);
            }

            copy.SetPixels32(0, 0, 3, settings.Length, rawSettingsTex.Rgba, 0);
            copy.Apply();

            RenderTexture.active = oldActive;
            RenderTexture.ReleaseTemporary(rt);

            return(copy);
        }