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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
[CustomEditor(typeof(LinearPipeline))]
public class LinearPipelineEditor : Editor
{
private const string MATERIAL_SUFFIX = "_Mat";
private const string TEXTURE_SUFFIX = "_Tex";
private SerializedProperty pipelineGeneratedPathProperty;
private SerializedProperty initialStateProperty;
private static Shader shader;
private static int radix = 16;
private static int fftResolution = 256;
private static bool isInverse = false;
private static bool doBothFFTAndInverse = false;
private static int preFFTStages = 0;
private static int postFFTStages = 0;
private void OnEnable()
{
pipelineGeneratedPathProperty = serializedObject.FindProperty("pipelineGeneratedPath");
initialStateProperty = serializedObject.FindProperty("initialState");
// Set default shader if not already set
if (shader == null)
{
shader = Shader.Find("yum_food/fft");
}
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(serializedObject.FindProperty("materials"), true);
EditorGUILayout.PropertyField(serializedObject.FindProperty("renderTextures"), true);
LinearPipeline pipeline = (LinearPipeline)target;
EditorGUILayout.Space();
EditorGUILayout.LabelField("Pipeline Generation", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(pipelineGeneratedPathProperty);
EditorGUILayout.PropertyField(initialStateProperty);
serializedObject.ApplyModifiedProperties();
shader = EditorGUILayout.ObjectField("Shader", shader, typeof(Shader), false) as Shader;
radix = Mathf.Max(2, EditorGUILayout.IntField("Radix", radix));
fftResolution = Mathf.Max(2, EditorGUILayout.IntField("FFT Resolution (N)", fftResolution));
EditorGUI.BeginDisabledGroup(doBothFFTAndInverse);
isInverse = EditorGUILayout.Toggle("Inverse FFT", isInverse);
EditorGUI.EndDisabledGroup();
doBothFFTAndInverse = EditorGUILayout.Toggle("Both FFT and Inverse FFT", doBothFFTAndInverse);
preFFTStages = Mathf.Max(0, EditorGUILayout.IntField("Pre-FFT Stages", preFFTStages));
postFFTStages = Mathf.Max(0, EditorGUILayout.IntField("Post-FFT Stages", postFFTStages));
int fftStages = CalculateFFTStages(fftResolution, radix);
EditorGUILayout.HelpBox($"FFT stages: {fftStages} (including bit reverse)", MessageType.Info);
EditorGUILayout.Space();
GUI.enabled = shader != null;
if (GUILayout.Button("Create Pipeline", GUILayout.Height(30)))
{
CreatePipeline(pipeline);
}
GUI.enabled = true;
if (shader == null)
{
EditorGUILayout.HelpBox("Please specify a shader to create the pipeline.", MessageType.Warning);
}
}
private int CalculateFFTStages(int n, int radix)
{
return Mathf.CeilToInt(Mathf.Log(n) / Mathf.Log(radix)) * 2 + 1;
}
private void CreatePipeline(LinearPipeline pipeline)
{
string pipelineName = pipeline.gameObject.name;
string pipelinePath = Path.Combine(pipeline.pipelineGeneratedPath, pipelineName);
// Ensure directories exist
if (!AssetDatabase.IsValidFolder(pipeline.pipelineGeneratedPath))
{
CreateFolderRecursive(pipeline.pipelineGeneratedPath);
}
if (!AssetDatabase.IsValidFolder(pipelinePath))
{
AssetDatabase.CreateFolder(pipeline.pipelineGeneratedPath, pipelineName);
}
Undo.RegisterFullObjectHierarchyUndo(pipeline.gameObject, "Create Pipeline");
// Clear existing children
ClearChildren(pipeline.transform);
// Generate stage names
List<string> stageNames = GenerateStageNames();
// Create arrays for materials and textures
Material[] materials = new Material[stageNames.Count];
RenderTexture[] textures = new RenderTexture[stageNames.Count];
// Create each stage
for (int i = 0; i < stageNames.Count; i++)
{
CreateStage(pipeline.transform, stageNames[i], pipelinePath, i, ref materials, ref textures);
}
// Assign textures to materials
AssignTexturesToMaterials(materials, textures, pipeline.initialState);
// Update pipeline component
pipeline.materials = materials;
pipeline.renderTextures = textures;
EditorUtility.SetDirty(pipeline);
AssetDatabase.SaveAssets();
Debug.Log($"[LinearPipeline] Created pipeline '{pipelineName}' with {materials.Length} stages");
}
private List<string> GenerateStageNames()
{
List<string> names = new List<string>();
int totalFFTStages = CalculateFFTStages(fftResolution, radix);
// Pre-FFT stages
for (int i = 0; i < preFFTStages; i++)
{
names.Add($"Pre_Stage_{i:D2}");
}
// FFT stages
if (doBothFFTAndInverse || !isInverse)
{
for (int i = 0; i < totalFFTStages; i++)
{
names.Add($"FFT_Stage_{i:D2}");
}
}
// Inverse FFT stages
if (doBothFFTAndInverse || isInverse)
{
for (int i = 0; i < totalFFTStages; i++)
{
names.Add($"IFFT_Stage_{i:D2}");
}
}
// Post-FFT stages
for (int i = 0; i < postFFTStages; i++)
{
names.Add($"Post_Stage_{i:D2}");
}
return names;
}
private void CreateStage(Transform parent, string stageName, string basePath, int index,
ref Material[] materials, ref RenderTexture[] textures)
{
// Create stage GameObject as a quad
GameObject stageGO = GameObject.CreatePrimitive(PrimitiveType.Quad);
stageGO.name = stageName;
stageGO.transform.SetParent(parent);
stageGO.transform.localPosition = new Vector3(index * 1.0f, 0, 0); // Space 1 meter apart on X axis
stageGO.transform.localRotation = Quaternion.identity;
stageGO.transform.localScale = Vector3.one;
DestroyImmediate(stageGO.GetComponent<MeshCollider>());
// Create or update material
string materialPath = $"{basePath}/{stageName}{MATERIAL_SUFFIX}.mat";
Material material = AssetDatabase.LoadAssetAtPath<Material>(materialPath);
if (material == null)
{
material = new Material(shader);
AssetDatabase.CreateAsset(material, materialPath);
}
else
{
material.shader = shader;
EditorUtility.SetDirty(material);
}
// Set shader properties based on stage type
ConfigureMaterialProperties(material, stageName);
// Create or update render texture
string texturePath = $"{basePath}/{stageName}{TEXTURE_SUFFIX}.renderTexture";
RenderTexture texture = AssetDatabase.LoadAssetAtPath<RenderTexture>(texturePath);
if (texture == null)
{
texture = new RenderTexture(fftResolution, fftResolution, 0, RenderTextureFormat.ARGBFloat)
{
filterMode = FilterMode.Point,
wrapMode = TextureWrapMode.Clamp
};
AssetDatabase.CreateAsset(texture, texturePath);
}
else
{
texture.width = fftResolution;
texture.height = fftResolution;
EditorUtility.SetDirty(texture);
}
// Assign material to renderer
MeshRenderer renderer = stageGO.GetComponent<MeshRenderer>();
renderer.sharedMaterial = material;
// Store in arrays
materials[index] = material;
textures[index] = texture;
}
private void ConfigureMaterialProperties(Material material, string stageName)
{
// Set common properties
material.SetInt("_N", fftResolution);
material.SetInt("_Radix", radix);
// Reset all flags
material.SetFloat("_Passthrough", 0f);
material.SetFloat("_LDS", 0f);
material.SetFloat("_Luminance", 0f);
material.SetFloat("_Inverse", 0f);
material.SetFloat("_BitReversal", 0f);
// Configure based on stage type
if (stageName.StartsWith("Pre_Stage_"))
{
// Pre-processing stages - set as passthrough
material.SetFloat("_Passthrough", 1f);
material.SetInt("_Stage", 0);
}
else if (stageName.StartsWith("FFT_Stage_"))
{
// Extract stage number
string stageNumStr = stageName.Replace("FFT_Stage_", "");
if (int.TryParse(stageNumStr, out int stageNum))
{
material.SetInt("_Stage", stageNum);
// Last stage is bit reversal
int totalStages = CalculateFFTStages(fftResolution, radix);
if (stageNum == totalStages - 1)
{
material.SetFloat("_BitReversal", 1f);
}
}
material.SetFloat("_Inverse", 0f);
}
else if (stageName.StartsWith("IFFT_Stage_"))
{
// Extract stage number
string stageNumStr = stageName.Replace("IFFT_Stage_", "");
if (int.TryParse(stageNumStr, out int stageNum))
{
material.SetInt("_Stage", stageNum);
// Last stage is bit reversal
int totalStages = CalculateFFTStages(fftResolution, radix);
if (stageNum == totalStages - 1)
{
material.SetFloat("_BitReversal", 1f);
}
}
material.SetFloat("_Inverse", 1f);
}
else if (stageName.StartsWith("Post_Stage_"))
{
// Post-processing stages - set as passthrough
material.SetFloat("_Passthrough", 1f);
material.SetInt("_Stage", 0);
}
EditorUtility.SetDirty(material);
}
private void AssignTexturesToMaterials(Material[] materials, RenderTexture[] textures, Texture initialState)
{
for (int i = 0; i < materials.Length; i++)
{
Texture inputTexture = (i == 0) ? initialState : textures[i - 1];
if (inputTexture != null)
{
materials[i].SetTexture("_MainTex", inputTexture);
}
EditorUtility.SetDirty(materials[i]);
}
}
private void ClearChildren(Transform parent)
{
while (parent.childCount > 0)
{
DestroyImmediate(parent.GetChild(0).gameObject);
}
}
private void CreateFolderRecursive(string path)
{
string[] folders = path.Split('/');
string currentPath = folders[0];
for (int i = 1; i < folders.Length; i++)
{
string nextPath = Path.Combine(currentPath, folders[i]);
if (!AssetDatabase.IsValidFolder(nextPath))
{
AssetDatabase.CreateFolder(currentPath, folders[i]);
}
currentPath = nextPath;
}
}
}
#endif
|