UGUI—滤镜效果

作者:追风剑情 发布于:2019-11-5 16:07 分类:Unity3d

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 滤镜效果
/// </summary>
public class ImageFilter : MonoBehaviour
{
    public RawImage rawImage;
    public FilterEffect filterEffect;
    public enum FilterEffect
    {
        None,
        Gray,
        Film,
        SepiaTone
    }
    public bool OnStartApply = true;
    private Texture2D rawTexture;

    private void Awake()
    {
        if (rawImage == null)
            rawImage = this.GetComponent<RawImage>();
    }

    void Start()
    {
        if (OnStartApply)
            ApplyFilterEffect(filterEffect);
    }

    // 设置滤镜效果
    public void ApplyFilterEffect(FilterEffect effect)
    {
        if (rawImage == null)
            return;

        // 去掉滤镜效果
        if (effect == FilterEffect.None)
        {
            if (rawTexture != null)
                rawImage.texture = rawTexture;
            return;
        }

        if (rawTexture == null)
            rawTexture = rawImage.texture as Texture2D;

        Color[] colors = rawTexture.GetPixels(0, 0, rawTexture.width, rawTexture.height);
        for (int i = 0; i < colors.Length; i++)
        {
            switch (effect)
            {
                case FilterEffect.Gray:
                    colors[i] = GrayEffect(colors[i]);
                    break;
                case FilterEffect.Film:
                    colors[i] = FilmEffect(colors[i]);
                    break;
                case FilterEffect.SepiaTone:
                    colors[i] = SepiaToneEffect(colors[i]);
                    break;
            }
        }

        Texture2D clone_tex = new Texture2D(rawTexture.width, rawTexture.height, TextureFormat.RGBA32, false);
        clone_tex.SetPixels(colors);
        clone_tex.Apply();

        rawImage.texture = clone_tex;
    }

    // 底片效果
    private Color FilmEffect(Color c)
    {
        c.r = 1 - c.r;
        c.g = 1 - c.g;
        c.b = 1 - c.b;
        return c;
    }

    // 老照片(一种发黄的颜色风格)
    private Color SepiaToneEffect(Color c)
    {
        //重新计算RGB值
        float fr = (c.r * 0.393f) + (c.g * 0.769f) + (c.b * 0.189f);
        float fg = (c.r * 0.349f) + (c.g * 0.686f) + (c.b * 0.168f);
        float fb = (c.r * 0.272f) + (c.g * 0.534f) + (c.b * 0.131f);
        float blend_r = ColorBlend(Noise(), fr, c.r);
        float blend_g = ColorBlend(Noise(), fg, c.g);
        float blend_b = ColorBlend(Noise(), fb, c.b);
        c.r = blend_r;
        c.g = blend_g;
        c.b = blend_b;
        return c;
    }

    // 随机权重
    private float Noise()
    {
        return Random.Range(0f, 1.0f) * 0.5f + 0.5f;
    }

    // 混合颜色
    private float ColorBlend(float scale, float dest, float src)
    {
        return (scale * dest + (1.0f - scale) * src);
    }

    // 灰度图
    private Color GrayEffect(Color c)
    {
        c.r *= 0.299f;
        c.g *= 0.587f;
        c.b *= 0.114f;

        float gray = Mathf.Clamp01(c.r + c.g + c.b);
        c.r = c.g = c.b = gray;
        return c;
    }
}

运行测试

11111.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号