颜色渐变丶渲染效果类---(Unity自学笔记)

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

/**
 * 颜色渐变特效类
 */
public class Gradient : BaseMeshEffect
{
    [Header("水平渲染")]
    [Tooltip("是否启用水平渲染?")]
    public bool horizontal = false;
    public Color32 left = Color.white;
    public Color32 right = Color.white;

    [Space(10)]
    [Header("垂直渲染")]
    [Tooltip("是否启用垂直渲染?")]
    public bool vertical = false;
    public Color32 top = Color.white;
    public Color32 bottom = Color.white;

    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive())
        {
            return;
        }
        if (horizontal && vertical) HorAndVerColor(vh);
        //水平或垂直渲染
        else if (horizontal) HorizontalColor(vh);
        else if (vertical) VerticalColor(vh);
        //throw new System.NotImplementedException();
    }
    //水平且垂直颜色渲染
    private void HorAndVerColor(VertexHelper vh)
    {
        //获取UIVertex
        int count = vh.currentVertCount;
        if (count == 0) return;
        List<UIVertex> vertexs = new List<UIVertex>();
        for (int i = 0; i < count; i++)
        {
            UIVertex vertex = new UIVertex();
            vh.PopulateUIVertex(ref vertex, i);
            vertexs.Add(vertex);
        }
        //获取开头和结尾vh.x/vh.y坐标
        float rightX = vertexs[0].position.x;
        float leftX = vertexs[0].position.x;
        float topY = vertexs[0].position.y;
        float bottomY = vertexs[0].position.y;
        for (int i = 1; i < count; i++)
        {
            float x = vertexs[i].position.x;
            if (x > rightX)
            {
                rightX = x;
            }
            else if (x < leftX)
            {
                leftX = x;
            }
            float y = vertexs[i].position.y;
            if (y > topY)
            {
                topY = y;
            }
            else if (y < bottomY)
            {
                bottomY = y;
            }
        }
        float hor = rightX - leftX;
        float ver = topY - bottomY;
        for (int i = 0; i < count; i++)
        {
            UIVertex vertex = vertexs[i];  //颜色渐变实现
            //水平
            Color32 colorX = Color32.Lerp(left, right, (vertex.position.x - leftX) / hor);
            //垂直
            Color32 colorY = Color32.Lerp(bottom, top, (vertex.position.y - bottomY) / ver);
            vertex.color = ColorPlus(colorX, colorY);
            vh.SetUIVertex(vertex, i);
        }
    }
    //水平方向颜色渐变
    private void HorizontalColor(VertexHelper vh)
    {
        //获取UIVertex
        int count = vh.currentVertCount;
        if (count == 0) return;
        List<UIVertex> vertexs = new List<UIVertex>();
        for (int i = 0; i < count; i++)
        {
            UIVertex vertex = new UIVertex();
            vh.PopulateUIVertex(ref vertex, i);
            vertexs.Add(vertex);
        }
        float rightX = vertexs[0].position.x;
        float leftX = vertexs[0].position.x;
        for (int i = 1; i < count; i++)
        {
            float x = vertexs[i].position.x;
            if (x > rightX)
            {
                rightX = x;
            }
            else if (x < leftX)
            {
                leftX = x;
            }
        }
        float hor = rightX - leftX;
        for (int i = 0; i < count; i++)
        {
            UIVertex vertex = vertexs[i];  //颜色渐变实现
            Color32 color = Color32.Lerp(left, right, (vertex.position.x - leftX) / hor);
            vertex.color = color;
            vh.SetUIVertex(vertex, i);
        }
    }
    //垂直方向颜色渐变
    private void VerticalColor(VertexHelper vh)
    {
        //获取UIVertex
        int count = vh.currentVertCount;
        if (count == 0) return;
        List<UIVertex> vertexs = new List<UIVertex>();
        for (int i = 0; i < count; i++)
        {
            UIVertex vertex = new UIVertex();
            vh.PopulateUIVertex(ref vertex, i);
            vertexs.Add(vertex);
        }
        float topY = vertexs[0].position.y;
        float bottomY = vertexs[0].position.y;
        for (int i = 1; i < count; i++)
        {
            float y = vertexs[i].position.y;
            if (y > topY)
            {
                topY = y;
            }
            else if (y < bottomY)
            {
                bottomY = y;
            }
        }
        float ver = topY - bottomY;
        for (int i = 0; i < count; i++)
        {
            UIVertex vertex = vertexs[i];  //颜色渐变实现
            Color32 color = Color32.Lerp(bottom, top, (vertex.position.y - bottomY) / ver);
            vertex.color = color;
            vh.SetUIVertex(vertex, i);
        }
    }
    //颜色相加
    private Color32 ColorPlus(Color x, Color y)
    {
        return x * y;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值