Unity 字体二(三)颜色渐变

字体颜色渐变

我们先看看二颜色渐变效果:


二颜色渐变效果

我们看看三颜色渐变效果:


三颜色渐变效果

左面Scene场景是网格状态。

通过Scene场景我们可以看见,我们需要向中间插入两个点,做个顶点过渡。

顶点部署应该是这样的:

     *  TL--------TR
     *  |          |
     *  |          |
     *  CL--------CR
     *  |          |
     *  |          |
     *  BL--------BR

首先我们要知道,显示文本有个文本区域,而这个区域的坐标是基于局部坐标,所以我们要先转出到屏幕坐标,然后再换算。

准备的变量和枚举:

public enum GradientType
{
    TowColor = 0,
    ThreeColor,
}
    [SerializeField] private GradientType m_GradientType = GradientType.TowColor;
    [SerializeField] private Color32 m_TopColor = Color.white;
    [SerializeField] private Color32 m_MiddleColor = Color.white;
    [SerializeField] private Color32 m_BottomColor = Color.black;
    [Range(0f, 1f)] [SerializeField] private float m_ColorOffset = 0.5f;
    [SerializeField] private Camera m_UICamera;
    private List<UIVertex> iVertices = new List<UIVertex>();

核心换算代码:

vh.GetUIVertexStream(iVertices);
        vh.Clear();


        for (int i = 0; i < iVertices.Count; i += 6)
        {

            UIVertex TL = iVertices[i + 0];
            UIVertex TR = iVertices[i + 1];
            UIVertex CR = iVertices[i + 2];
            UIVertex CL = iVertices[i + 4];
            UIVertex BL = iVertices[i + 4];
            UIVertex BR = iVertices[i + 2];

            Vector2 uvTL = iVertices[i + 0].uv0;
            Vector2 uvTR = iVertices[i + 1].uv0;
            Vector2 uvBR = iVertices[i + 2].uv0;
            Vector2 uvBL = iVertices[i + 4].uv0;

            Vector3 TR_World_Pos = this.transform.TransformPoint(TR.position);
            Vector3 BR_World_Pos = this.transform.TransformPoint(BR.position);
            Vector3 TR_Screen_Pos = TR_World_Pos;
            Vector3 BR_Screen_Pos = BR_World_Pos;
            
            if (this.m_UICamera != null)
            {
                TR_Screen_Pos = this.m_UICamera.WorldToScreenPoint(TR_World_Pos);
                BR_Screen_Pos = this.m_UICamera.WorldToScreenPoint(BR_World_Pos);
            }

            float yHeight = (TR_Screen_Pos.y - BR_Screen_Pos.y) * this.m_ColorOffset;
            Vector2 C_Screen_Pos = new Vector2(TR_Screen_Pos.x, TR_Screen_Pos.y - yHeight);

            if (this.m_UICamera != null)
            {
                C_Screen_Pos = this.m_UICamera.ScreenToWorldPoint(C_Screen_Pos);
            }
            CR.position = this.transform.InverseTransformPoint(C_Screen_Pos);
            CL.position.y = CR.position.y;

            CR.uv0.y = CR.uv0.y * this.m_ColorOffset;
            CL.uv0.y = CL.uv0.y * this.m_ColorOffset;

            TL.color = this.m_TopColor;
            TR.color = this.m_TopColor;
            CR.color = this.m_MiddleColor;
            CL.color = this.m_MiddleColor;
            BL.color = this.m_BottomColor;
            BR.color = this.m_BottomColor;

            vh.AddVert(TL);
            vh.AddVert(TR);
            vh.AddVert(CR);
            vh.AddVert(CL);
            vh.AddVert(BL);
            vh.AddVert(BR);
        }

上面的代码意思就是先清理原始数据,然后把坐标转到屏幕坐标系上,需要经过两步 局部坐标 => 世界坐标 => 屏幕坐标,然后给y做某个点上距离缩放:

屏幕坐标计算

float yHeight = (TR_Screen_Pos.y - BR_Screen_Pos.y) * this.m_ColorOffset;在这个距离做个缩放,TR_Screen_Pos.y - yHeight 计算出来差值,就是我们需要的中点高度。(CR\CL的高度是一样的,计算一次即可,X值保留默认。)

接下来添加三角面:


        for (int i = 0; i < vh.currentVertCount; i += 6)
        {
            if (this.m_GradientType == GradientType.ThreeColor)
            {
                vh.AddTriangle(i + 0, i + 1, i + 2);
                vh.AddTriangle(i + 2, i + 3, i + 0);
                vh.AddTriangle(i + 3, i + 2, i + 5);
                vh.AddTriangle(i + 5, i + 4, i + 3);
            }
            else
            {
                vh.AddTriangle(i + 0, i + 1, i + 5);
                vh.AddTriangle(i + 0, i + 5, i + 4);
            }
        }

大致就是这些。

补充,上面版本有些bug,核心算法做个优化:

/*
         *  TL--------TR
         *  |          |
         *  |          |
         *  CL--------CR
         *  |          |
         *  |          |
         *  BL--------BR
         * **/


        for (int i = 0; i < count; i++)
        {
            UIVertex vertex = UIVertex.simpleVert;
            vh.PopulateUIVertex(ref vertex, i);
            this.iVertices.Add(vertex);
        }
        vh.Clear();

        for (int i = 0; i < this.iVertices.Count; i += 4)
        {

            UIVertex TL = this.GeneralUIVertex(this.iVertices[i + 0]);
            UIVertex TR = this.GeneralUIVertex(this.iVertices[i + 1]);
            UIVertex BR = this.GeneralUIVertex(this.iVertices[i + 2]);
            UIVertex BL = this.GeneralUIVertex(this.iVertices[i + 3]);

            TL.color = this.m_TopColor;
            TR.color = this.m_TopColor;
            BL.color = this.m_BottomColor;
            BR.color = this.m_BottomColor;


            vh.AddVert(TL);
            vh.AddVert(TR);

            if (this.m_GradientType == GradientType.ThreeColor)
            {
                UIVertex CR = this.GeneralUIVertex(this.iVertices[i + 2]);
                UIVertex CL = this.GeneralUIVertex(this.iVertices[i + 3]);

                CR.position = (TR.position + BR.position) * this.m_ColorOffset;
                CL.position = (TL.position + BL.position) * this.m_ColorOffset;
                CR.uv0 = (TR.uv0 + BR.uv0) * this.m_ColorOffset;
                CL.uv0 = (TL.uv0 + BL.uv0) * this.m_ColorOffset;
                CR.color = this.m_MiddleColor;
                CL.color = this.m_MiddleColor;

                vh.AddVert(CR);
                vh.AddVert(CL);
            }

            vh.AddVert(BR);
            vh.AddVert(BL);
        }

        int step = 4;
        if (this.m_GradientType == GradientType.ThreeColor)
        {
            step = 6;
        }

        for (int i = 0; i < vh.currentVertCount; i += step)
        {
            if (this.m_GradientType == GradientType.ThreeColor)
            {
                vh.AddTriangle(i + 0, i + 1, i + 2);
                vh.AddTriangle(i + 2, i + 3, i + 0);
                vh.AddTriangle(i + 3, i + 2, i + 4);
                vh.AddTriangle(i + 4, i + 5, i + 3);
            }
            else
            {
                vh.AddTriangle(i + 0, i + 1, i + 2);
                vh.AddTriangle(i + 2, i + 3, i + 0);
            }
        }
    private UIVertex GeneralUIVertex(UIVertex vertex)
    {
        UIVertex result     = UIVertex.simpleVert;
        result.normal       = new Vector3(vertex.normal.x, vertex.normal.y, vertex.normal.z);
        result.position     = new Vector3(vertex.position.x, vertex.position.y, vertex.position.z);
        result.tangent      = new Vector4(vertex.tangent.x, vertex.tangent.y, vertex.tangent.z, vertex.tangent.w);
        result.uv0          = new Vector2(vertex.uv0.x, vertex.uv0.y);
        result.uv1          = new Vector2(vertex.uv1.x, vertex.uv1.y);
        return result;
    }

最终代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System;

public enum GradientType
{
    TowColor = 0,
    ThreeColor,
}

/// <summary>
/// 渐变字体
/// </summary>
public class Gradient : BaseMeshEffect
{
    [SerializeField] private GradientType m_GradientType = GradientType.TowColor;
    [SerializeField] private Color32 m_TopColor = Color.white;
    [SerializeField] private Color32 m_MiddleColor = Color.white;
    [SerializeField] private Color32 m_BottomColor = Color.black;
    [Range(0f, 1f)] [SerializeField] private float m_ColorOffset = 0.5f;
    [SerializeField] private Camera m_UICamera;
    private List<UIVertex> iVertices = new List<UIVertex>();

    public override void ModifyMesh(VertexHelper vh)
    {
        this.iVertices.Clear();

        if (!IsActive())
        {
            return;
        }

        var count = vh.currentVertCount;
        if (count == 0)
            return;

        /*
         *  TL--------TR
         *  |          |
         *  |          |
         *  CL--------CR
         *  |          |
         *  |          |
         *  BL--------BR
         * **/


        for (int i = 0; i < count; i++)
        {
            UIVertex vertex = UIVertex.simpleVert;
            vh.PopulateUIVertex(ref vertex, i);
            this.iVertices.Add(vertex);
        }
        vh.Clear();

        for (int i = 0; i < this.iVertices.Count; i += 4)
        {

            UIVertex TL = this.GeneralUIVertex(this.iVertices[i + 0]);
            UIVertex TR = this.GeneralUIVertex(this.iVertices[i + 1]);
            UIVertex BR = this.GeneralUIVertex(this.iVertices[i + 2]);
            UIVertex BL = this.GeneralUIVertex(this.iVertices[i + 3]);

            TL.color = this.m_TopColor;
            TR.color = this.m_TopColor;
            BL.color = this.m_BottomColor;
            BR.color = this.m_BottomColor;


            vh.AddVert(TL);
            vh.AddVert(TR);

            if (this.m_GradientType == GradientType.ThreeColor)
            {
                UIVertex CR = this.GeneralUIVertex(this.iVertices[i + 2]);
                UIVertex CL = this.GeneralUIVertex(this.iVertices[i + 3]);

                CR.position = (TR.position + BR.position) * this.m_ColorOffset;
                CL.position = (TL.position + BL.position) * this.m_ColorOffset;
                CR.uv0 = (TR.uv0 + BR.uv0) * this.m_ColorOffset;
                CL.uv0 = (TL.uv0 + BL.uv0) * this.m_ColorOffset;
                CR.color = this.m_MiddleColor;
                CL.color = this.m_MiddleColor;

                vh.AddVert(CR);
                vh.AddVert(CL);
            }

            vh.AddVert(BR);
            vh.AddVert(BL);
        }

        int step = 4;
        if (this.m_GradientType == GradientType.ThreeColor)
        {
            step = 6;
        }

        for (int i = 0; i < vh.currentVertCount; i += step)
        {
            if (this.m_GradientType == GradientType.ThreeColor)
            {
                vh.AddTriangle(i + 0, i + 1, i + 2);
                vh.AddTriangle(i + 2, i + 3, i + 0);
                vh.AddTriangle(i + 3, i + 2, i + 4);
                vh.AddTriangle(i + 4, i + 5, i + 3);
            }
            else
            {
                vh.AddTriangle(i + 0, i + 1, i + 2);
                vh.AddTriangle(i + 2, i + 3, i + 0);
            }
        }

    }

    private UIVertex GeneralUIVertex(UIVertex vertex)
    {
        UIVertex result     = UIVertex.simpleVert;
        result.normal       = new Vector3(vertex.normal.x, vertex.normal.y, vertex.normal.z);
        result.position     = new Vector3(vertex.position.x, vertex.position.y, vertex.position.z);
        result.tangent      = new Vector4(vertex.tangent.x, vertex.tangent.y, vertex.tangent.z, vertex.tangent.w);
        result.uv0          = new Vector2(vertex.uv0.x, vertex.uv0.y);
        result.uv1          = new Vector2(vertex.uv1.x, vertex.uv1.y);
        return result;
    }
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,589评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,615评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,933评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,976评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,999评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,775评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,474评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,359评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,854评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,007评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,146评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,826评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,484评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,029评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,153评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,420评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,107评论 2 356