예제 #1
0
        private static Texture2D RescalePowerOf2(Texture2D tex)
        {
            var imageSize = new Vector2(Mathf.Max(Mathf.NextPowerOfTwo(tex.width), 1), Mathf.Max(Mathf.NextPowerOfTwo(tex.height), 1));

            TextureScaler.scale(tex, (int)imageSize.x, (int)imageSize.y);
            return(tex);
        }
예제 #2
0
        public static Texture2D RenderSpriteLine(UIDynamicFont font, UITextureAtlas atlas, string spriteName, Color bgColor, string text, float textScale = 1)
        {
            UITextureAtlas.SpriteInfo spriteInfo = atlas[spriteName];
            if (spriteInfo == null)
            {
                CODebugBase <InternalLogChannel> .Warn(InternalLogChannel.UI, "Missing sprite " + spriteName + " in " + atlas.name);

                return(null);
            }
            else
            {
                textScale *= 2;

                Texture2D texture        = atlas.texture;
                float     calcHeight     = font.size * textScale * 2;
                float     calcProportion = spriteInfo.region.width * texture.width / (spriteInfo.region.height * texture.height);
                float     calcWidth      = Mathf.CeilToInt(calcHeight * calcProportion);

                int height = Mathf.CeilToInt(calcHeight);
                int width  = Mathf.CeilToInt(calcWidth);

                float textureScale = height / (spriteInfo.region.height * texture.height);

                LogUtils.DoLog($"height = {height} - width = {width} -  renderer.pixelRatio = 1 - textureScale = {height} / {(spriteInfo.region.height * texture.height)}");

                var   size        = new Vector3(width, height);
                float borderWidth = textScale * 3;

                Vector2 textDimensions = MeasureTextWidth(font, text, textScale, out Vector2 yBounds);
                float   borderBottom   = Mathf.Max(0, (spriteInfo.border.bottom * textScale * 2) + Mathf.Min(0, yBounds.x));
                var     textAreaSize   = new Vector4((spriteInfo.border.left * textScale * 2) + borderWidth, borderBottom + borderWidth, width - (spriteInfo.border.horizontal * textScale * 2) - borderWidth, height - (spriteInfo.border.top * textScale * 2) - borderBottom - borderWidth);

                float multipler = Mathf.Min(Mathf.Min(3.5f, textAreaSize.z / textDimensions.x), Mathf.Min(3.5f, textAreaSize.w / textDimensions.y));
                if (multipler > 1)
                {
                    textScale     *= 1 + ((multipler - 1) / 2.1f);
                    multipler      = 1;
                    textDimensions = MeasureTextWidth(font, text, textScale, out yBounds);
                }

                var imageSize = new Vector2(Mathf.NextPowerOfTwo((int)Mathf.Max(textDimensions.x * multipler, width)), Mathf.NextPowerOfTwo((int)Mathf.Max(textDimensions.y, height)));


                var tex = new Texture2D((int)imageSize.x, (int)imageSize.y, TextureFormat.ARGB32, false);
                tex.SetPixels(new Color[(int)(imageSize.x * imageSize.y)]);


                var texText = new Texture2D((int)textDimensions.x, (int)textDimensions.y, TextureFormat.ARGB32, false);
                texText.SetPixels(new Color[(int)(textDimensions.x * textDimensions.y)]);

                Color contrastColor = KlyteMonoUtils.ContrastColor(bgColor);

                Vector2 position = RenderSprite(atlas, spriteName, contrastColor, tex, textureScale);
                RenderSprite(atlas, spriteName, bgColor, tex, null, tex.height - (int)(borderWidth * 2), null, new Vector2((textScale / 2) - 0.5f, (textScale / 2) - 0.5f), (a, b) =>
                {
                    if (b.a == 1)
                    {
                        return(b);
                    }

                    if (b.a == 0)
                    {
                        return(a);
                    }

                    float totalAlpha = a.a + b.a;
                    return((a * (1 - b.a)) + (b * b.a));
                });
                Vector2 posText = position + VectorUtils.XY(textAreaSize) + new Vector2((textAreaSize.z / 2) - (textDimensions.x * multipler / 2) + 1, (textAreaSize.w / 2) - (textDimensions.y / 2) - (yBounds.x / 2));

                RenderText(font, text, new Vector3(0, -yBounds.x), textScale, contrastColor, bgColor, texText);

                if (multipler < 1)
                {
                    TextureScaler.scale(texText, (int)(texText.width * multipler), texText.height);
                }
                MergeTextures(tex, texText.GetPixels(), (int)posText.x, (int)posText.y, texText.width, texText.height, false);
                UnityEngine.Object.Destroy(texText);
                tex.Apply();

                return(tex);
            }
        }