自定义色彩梯度LUT生成

今天才知道Unity自己就有个很方便的Gradient Editor, 于是我们不用频繁切换Photoshop就能很方便快捷的制作一张自定义一维Color LUT。

Inspector界面

自带梯度编辑器

生成的纹理

原理也很简单,使用Gradient.Evaluate直接对Gradient Class的公共实例采样就行(Wrap Mode要设置成Clamp),然后用OnInspectorGUI增加一个按钮用于调用生成文件的方法。

using UnityEngine;
using System.Collections;
using System.IO;

public class GradientTexture : MonoBehaviour 
{
    public Gradient gradient = new Gradient();
    public int resolution = 256;
    public string fileName;

    private Texture2D texture;

    public Texture2D Generate(bool makeNoLongerReadable = false)
    {
        Texture2D tex = new Texture2D(resolution, 1, TextureFormat.ARGB32, false, true);
        tex.filterMode = FilterMode.Bilinear;
        tex.wrapMode = TextureWrapMode.Clamp;
        tex.anisoLevel = 1;


        Color[] colors = new Color[resolution];
        float div = (float)resolution;
        for (int i = 0; i < resolution; ++i)
        {
            float t = (float)i/div;
            colors[i] = gradient.Evaluate(t);
        }
        tex.SetPixels(colors);
        tex.Apply(false, makeNoLongerReadable);

        return tex;
    }

    public void GenerateFile()
    {
        byte[] bytes = texture.EncodeToPNG();
        File.WriteAllBytes(Application.dataPath + "/Textures/" + fileName + ".png", bytes);
    }

    public void Refresh()
    {
        if (texture != null)
        {
            DestroyImmediate(texture);
        }
        texture = Generate();

    }
        
    void OnDestroy()
    {
        if (texture != null)
        {
            DestroyImmediate(texture);
        }
    }
}

这里是直接生成为一个PNG文件用于材质,当然也可以在脚本中直接SetTexture完成。另外我们还能够在Update里实时地去生成我们想要的梯度,给制作不同的效果带来一种新的思路。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、Unity简介 1. Unity界面 Shift + Space : 放大界面 Scene界面按钮渲染模式2D...
    MYves阅读 8,436评论 0 22
  • This article is a record of my journey to learn Game Deve...
    蔡子聪阅读 3,875评论 0 9
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,241评论 4 61
  • 初次认识静,她的人,像她的名字一样安静,人群中你都没办法找到她,瘦小而安静,但她却又像一抹净莲一样,洁净,清丽,望...
    墨香雨澜阅读 648评论 4 3
  • 钱穆说:“古往今来有大成就者,诀窍无他,都是能人肯下笨劲。” 心在一艺,其艺必工,一心在一职,其职必举。成长不是一...
    谷马砺兵阅读 267评论 0 1