今天才知道Unity自己就有個(gè)很方便的Gradient Editor, 于是我們不用頻繁切換Photoshop就能很方便快捷的制作一張自定義一維Color LUT。
Inspector界面
自帶梯度編輯器
生成的紋理
原理也很簡(jiǎn)單,使用
Gradient.Evaluate
直接對(duì)Gradient Class的公共實(shí)例采樣就行(Wrap Mode要設(shè)置成Clamp),然后用OnInspectorGUI
增加一個(gè)按鈕用于調(diào)用生成文件的方法。
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);
}
}
}
這里是直接生成為一個(gè)PNG文件用于材質(zhì),當(dāng)然也可以在腳本中直接SetTexture完成。另外我們還能夠在Update里實(shí)時(shí)地去生成我們想要的梯度,給制作不同的效果帶來(lái)一種新的思路。