CubeLutAssetFactory.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using UnityEngine;
  5. using UnityEngine.Rendering.PostProcessing;
  6. namespace UnityEditor.Rendering.PostProcessing
  7. {
  8. // CUBE lut specs:
  9. // http://wwwimages.adobe.com/content/dam/Adobe/en/products/speedgrade/cc/pdfs/cube-lut-specification-1.0.pdf
  10. static class CubeLutAssetFactory
  11. {
  12. const int kVersion = 1;
  13. const int kSize = 33;
  14. #if POSTFX_DEBUG_MENUS
  15. [MenuItem("Tools/Post-processing/Create Utility Luts")]
  16. #endif
  17. static void CreateLuts()
  18. {
  19. Dump("Linear to Unity Log r" + kVersion, ColorUtilities.LinearToLogC);
  20. Dump("Unity Log to Linear r" + kVersion, ColorUtilities.LogCToLinear);
  21. Dump("sRGB to Unity Log r" + kVersion, x => ColorUtilities.LinearToLogC(Mathf.GammaToLinearSpace(x)));
  22. Dump("Unity Log to sRGB r" + kVersion, x => Mathf.LinearToGammaSpace(ColorUtilities.LogCToLinear(x)));
  23. Dump("Linear to sRGB r" + kVersion, Mathf.LinearToGammaSpace);
  24. Dump("sRGB to Linear r" + kVersion, Mathf.GammaToLinearSpace);
  25. AssetDatabase.Refresh();
  26. }
  27. static void Dump(string title, Func<float, float> eval)
  28. {
  29. var sb = new StringBuilder();
  30. sb.AppendFormat("TITLE \"{0}\"\n", title);
  31. sb.AppendFormat("LUT_3D_SIZE {0}\n", kSize);
  32. sb.AppendFormat("DOMAIN_MIN {0} {0} {0}\n", 0f);
  33. sb.AppendFormat("DOMAIN_MAX {0} {0} {0}\n", 1f);
  34. const float kSizeMinusOne = (float)kSize - 1f;
  35. for (int x = 0; x < kSize; x++)
  36. for (int y = 0; y < kSize; y++)
  37. for (int z = 0; z < kSize; z++)
  38. {
  39. float ox = eval((float)x / kSizeMinusOne);
  40. float oy = eval((float)y / kSizeMinusOne);
  41. float oz = eval((float)z / kSizeMinusOne);
  42. // Resolve & Photoshop use BGR as default, let's make it easier for users
  43. sb.AppendFormat("{0} {1} {2}\n", oz, oy, ox);
  44. }
  45. var content = sb.ToString();
  46. var path = Path.Combine(Application.dataPath, string.Format("{0}.cube", title));
  47. File.WriteAllText(path, content);
  48. }
  49. }
  50. }