-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathCubeLutAssetImporter.cs
More file actions
218 lines (178 loc) · 6.47 KB
/
Copy pathCubeLutAssetImporter.cs
File metadata and controls
218 lines (178 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using UnityEngine;
namespace UnityEditor.Rendering.PostProcessing
{
sealed class CubeLutAssetImporter : AssetPostprocessor
{
static List<string> s_Excluded = new List<string>()
{
"Linear_to_sRGB_r1",
"Linear_to_Unity_Log_r1",
"sRGB_to_Linear_r1",
"sRGB_to_Unity_Log_r1",
"Unity_Log_to_Linear_r1",
"Unity_Log_to_sRGB_r1"
};
static void OnPostprocessAllAssets(string[] imported, string[] deleted, string[] moved, string[] movedFrom)
{
foreach (string path in imported)
{
string ext = Path.GetExtension(path);
string filename = Path.GetFileNameWithoutExtension(path);
if (string.IsNullOrEmpty(ext) || s_Excluded.Contains(filename))
continue;
ext = ext.ToLowerInvariant();
if (ext.Equals(".cube"))
ImportCubeLut(path);
}
}
// Basic CUBE lut parser
// Specs: http://wwwimages.adobe.com/content/dam/Adobe/en/products/speedgrade/cc/pdfs/cube-lut-specification-1.0.pdf
static void ImportCubeLut(string path)
{
// Remove the 'Assets' part of the path & build absolute path
string fullpath = path.Substring(7);
fullpath = Path.Combine(Application.dataPath, fullpath);
// Read the lut data
string[] lines = File.ReadAllLines(fullpath);
// Start parsing
int i = 0;
int size = -1;
int sizeCube = -1;
var table = new List<Color>();
var domainMin = Color.black;
var domainMax = Color.white;
while (true)
{
if (i >= lines.Length)
{
if (table.Count != sizeCube)
Debug.LogError("Premature end of file");
break;
}
string line = FilterLine(lines[i]);
if (string.IsNullOrEmpty(line))
goto next;
// Header data
if (line.StartsWith("TITLE"))
goto next; // Skip the title tag, we don't need it
if (line.StartsWith("LUT_3D_SIZE"))
{
string sizeStr = line.Substring(11).TrimStart();
if (!int.TryParse(sizeStr, out size))
{
Debug.LogError("Invalid data on line " + i);
break;
}
if (size < 2 || size > 256)
{
Debug.LogError("LUT size out of range");
break;
}
sizeCube = size * size * size;
goto next;
}
if (line.StartsWith("DOMAIN_MIN"))
{
if (!ParseDomain(i, line, ref domainMin)) break;
goto next;
}
if (line.StartsWith("DOMAIN_MAX"))
{
if (!ParseDomain(i, line, ref domainMax)) break;
goto next;
}
// Table
string[] row = line.Split();
if (row.Length != 3)
{
Debug.LogError("Invalid data on line " + i);
break;
}
var color = Color.black;
for (int j = 0; j < 3; j++)
{
float d;
if (!float.TryParse(row[j], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out d))
{
Debug.LogError("Invalid data on line " + i);
break;
}
color[j] = d;
}
table.Add(color);
next:
i++;
}
if (sizeCube != table.Count)
{
Debug.LogError("Wrong table size - Expected " + sizeCube + " elements, got " + table.Count);
return;
}
// Check if the Texture3D already exists, update it in this case (better workflow for
// the user)
string assetPath = Path.ChangeExtension(path, ".asset");
var tex = AssetDatabase.LoadAssetAtPath<Texture3D>(assetPath);
if (tex != null)
{
tex.SetPixels(table.ToArray(), 0);
tex.Apply();
}
else
{
// Generate a new Texture3D
tex = new Texture3D(size, size, size, TextureFormat.RGBAHalf, false)
{
anisoLevel = 0,
filterMode = FilterMode.Bilinear,
wrapMode = TextureWrapMode.Clamp,
};
tex.SetPixels(table.ToArray(), 0);
tex.Apply();
// Save to disk
AssetDatabase.CreateAsset(tex, assetPath);
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
static string FilterLine(string line)
{
var filtered = new StringBuilder();
line = line.TrimStart().TrimEnd();
int len = line.Length;
int i = 0;
while (i < len)
{
char c = line[i];
if (c == '#') // Filters comment out
break;
filtered.Append(c);
i++;
}
return filtered.ToString();
}
static bool ParseDomain(int i, string line, ref Color domain)
{
string[] domainStrs = line.Substring(10).TrimStart().Split();
if (domainStrs.Length != 3)
{
Debug.LogError("Invalid data on line " + i);
return false;
}
for (int j = 0; j < 3; j++)
{
float d;
if (!float.TryParse(domainStrs[j], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out d))
{
Debug.LogError("Invalid data on line " + i);
return false;
}
domain[j] = d;
}
return true;
}
}
}