Skip to content

Instantly share code, notes, and snippets.

@wonkee-kim
Last active December 9, 2019 01:30
Show Gist options
  • Save wonkee-kim/58c75a3226e1e84e8b1af2167accfe1f to your computer and use it in GitHub Desktop.
Save wonkee-kim/58c75a3226e1e84e8b1af2167accfe1f to your computer and use it in GitHub Desktop.
LUT default image generator on Unity editor.
// https://gist.github.com/WonkyKIM/58c75a3226e1e84e8b1af2167accfe1f
#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEngine;
public class LookupTextureGenerator : MonoBehaviour {
private enum LUTSizeList {
LDR16,
LDR32
}
[SerializeField] private static string path = "Textures";
[SerializeField] private static string filename = "LUT_default";
[MenuItem("Tools/Generate LUT/LDR16")]
public static void Create16() {
CreateLookUpTexture(LUTSizeList.LDR16);
}
[MenuItem("Tools/Generate LUT/LDR32")]
public static void Create32() {
CreateLookUpTexture(LUTSizeList.LDR32);
}
private static void CreateLookUpTexture(LUTSizeList _size) {
int resolution = GetResolutionFromList(_size);
Texture2D texture = new Texture2D(resolution * resolution, resolution, TextureFormat.RGB24, false);
// Set pixel values
float step = 1.0f / (resolution - 1);
for(int z = 0; z<resolution; z++) {
for(int y = 0; y < resolution; y++) {
for(int x = 0; x < resolution; x++) {
int ix = x + z*resolution;
int iy = y;
Color color = new Color(x * step, y * step, z * step);
texture.SetPixel(ix, iy, color);
}
}
}
texture.Apply();
// Encode texture into PNG
// https://docs.unity3d.com/ScriptReference/ImageConversion.EncodeToPNG.html
byte[] bytes = texture.EncodeToPNG();
//Object.Destroy(texture);
string filePath = $"{Application.dataPath }/{path}";
if(!Directory.Exists(filePath)) {
Directory.CreateDirectory(filePath);
}
string file = $"{filePath}/{filename}_{_size.ToString()}.png";
File.WriteAllBytes(file, bytes);
Debug.Log($"LUT File has been generated into <color=green>'{file}'.</color>");
}
private static int GetResolutionFromList(LUTSizeList _size) {
switch(_size) {
case LUTSizeList.LDR16:
return 16;
case LUTSizeList.LDR32:
return 32;
default:
return 16;
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment