unity3dText倒影效果

测试.png
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

[AddComponentMenu("UI/Effects/MirrorText")]
public class MirrorText : BaseMeshEffect
{
    //距离,限制范围0-30
    [Range(0, 30)]
    public float distance;

    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive() || vh.currentVertCount == 0)
        {
            return;
        }
        List<UIVertex> vertexs = new List<UIVertex>();
        vh.GetUIVertexStream(vertexs);
        UIVertex vt;
        int count = vertexs.Count;
        float miny = vertexs[0].position.y;
        float maxy = vertexs[0].position.y;
        for (int i = 1; i < count; i++)
        {
            if (vertexs[i].position.y < miny)
            {
                miny = vertexs[i].position.y;
            }
            else if (vertexs[i].position.y > maxy)
            {
                maxy = vertexs[i].position.y;
            }
        }
        float uiElementHeight = maxy - miny;
        float mirrorMinY = -maxy + 2 * miny - distance;
        Color32 top = GetComponent<Text>().color;
        Color32 bottom = new Color(top.r, top.g, top.b, 0);
        for (int i = 0; i < count; i++)
        {
            vt = vertexs[i];
            vertexs.Add(vt);
            Vector3 v = vt.position;
            v.y = -v.y + 2 * miny - distance;
            vt.position = v;

            //透明度效果
            vt.color = Color32.Lerp(bottom, top, (vt.position.y - mirrorMinY) / uiElementHeight);
            vertexs[i + count] = vt;
        }
        vh.Clear();
        vh.AddUIVertexTriangleStream(vertexs);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 提莫不胖阅读 3,677评论 0 0
  • 找到一张合适的水面波纹图片,或者自己制作一张,保存为psd格式,然后把要制作倒影的图层用滤镜-模糊-动态模糊效果处...
    ngjjc阅读 2,738评论 0 0
  • 先来看一下倒影的效果,从效果图中可以看出好像图片的下方是图片本身在水中的倒影。其实真正实现起来很简单,核心...
    ZhengYaWei阅读 3,430评论 0 6
  • 看着形形色色的人,吵闹着,于心中的那份寂静,是时间
    懿范阅读 838评论 0 0
  • “还记得你走过的雨后操场吗?”“还记得那年丁香花开的季节吗?”“还记得你打篮球时固执反穿的校服衣吗?”“还记得我一...
    小雨的雨阅读 3,427评论 0 1