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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class FoldEditorWindow : EditorWindow
{
[SerializeField] Material targetMaterial;
[SerializeField] Vector2 scrollPos;
[SerializeReference] List<DeformOperation> operations = new();
[SerializeField] List<int> expandedOps = new();
[SerializeField] bool liveUpdate = true;
[MenuItem("Tools/yum_food/Fold")]
static void ShowWindow()
{
var window = GetWindow<FoldEditorWindow>("Fold");
window.minSize = new Vector2(400, 300);
}
void OnGUI()
{
EditorGUILayout.Space(5);
DrawHeader();
EditorGUILayout.Space(10);
if (targetMaterial == null)
{
EditorGUILayout.HelpBox("Select a material to build deformation pipelines", MessageType.Info);
return;
}
DrawToolbar();
EditorGUILayout.Space(5);
EditorGUI.BeginChangeCheck();
scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
DrawOperationsList();
EditorGUILayout.EndScrollView();
if (EditorGUI.EndChangeCheck() && liveUpdate && targetMaterial != null)
ApplyToMaterial();
EditorGUILayout.Space(5);
DrawFooter();
}
void DrawHeader()
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Target Material", EditorStyles.boldLabel, GUILayout.Width(100));
var newMat = EditorGUILayout.ObjectField(targetMaterial, typeof(Material), false) as Material;
if (newMat != targetMaterial)
{
targetMaterial = newMat;
if (targetMaterial != null)
LoadFromMaterial();
}
EditorGUILayout.EndHorizontal();
}
void DrawToolbar()
{
EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
bool pipelineFull = operations.Count >= 16;
using (new EditorGUI.DisabledScope(pipelineFull))
{
var content = pipelineFull
? new GUIContent("Add Operation", "Pipeline full (16/16)")
: new GUIContent("Add Operation");
if (GUILayout.Button(content, EditorStyles.toolbarDropDown, GUILayout.Width(120)))
ShowAddOperationMenu();
}
GUILayout.FlexibleSpace();
liveUpdate = GUILayout.Toggle(liveUpdate, "Live", EditorStyles.toolbarButton, GUILayout.Width(40));
EditorGUILayout.EndHorizontal();
}
void DrawOperationsList()
{
if (operations.Count == 0)
{
EditorGUILayout.HelpBox("No operations. Click 'Add Operation' to begin.", MessageType.Info);
return;
}
for (int i = 0; i < operations.Count; i++)
{
DrawOperation(i);
EditorGUILayout.Space(3);
}
}
void DrawOperation(int index)
{
var op = operations[index];
bool isExpanded = expandedOps.Contains(index);
var bgColor = isExpanded ? new Color(0.3f, 0.5f, 0.8f, 0.3f) : new Color(0.2f, 0.2f, 0.2f, 0.3f);
EditorGUILayout.BeginVertical(GUI.skin.box);
GUI.backgroundColor = bgColor;
// Header with drag handle and delete button
EditorGUILayout.BeginHorizontal();
// Drag handle
GUILayout.Label($"#{index}", GUILayout.Width(30));
// Operation name (clickable to expand/collapse)
if (GUILayout.Button(op.GetDisplayName(), EditorStyles.boldLabel))
{
if (isExpanded) expandedOps.Remove(index);
else expandedOps.Add(index);
}
GUILayout.FlexibleSpace();
// Move up/down
GUI.enabled = index > 0;
if (GUILayout.Button("▲", GUILayout.Width(25)))
{
operations.RemoveAt(index);
operations.Insert(index - 1, op);
if (expandedOps.Contains(index))
{
expandedOps.Remove(index);
expandedOps.Add(index - 1);
}
}
GUI.enabled = index < operations.Count - 1;
if (GUILayout.Button("▼", GUILayout.Width(25)))
{
operations.RemoveAt(index);
operations.Insert(index + 1, op);
if (expandedOps.Contains(index))
{
expandedOps.Remove(index);
expandedOps.Add(index + 1);
}
}
GUI.enabled = true;
// Delete button
if (GUILayout.Button("×", GUILayout.Width(25)))
{
operations.RemoveAt(index);
expandedOps.Remove(index);
// Adjust indices in expandedOps
for (int i = 0; i < expandedOps.Count; i++)
{
if (expandedOps[i] > index) expandedOps[i]--;
}
}
EditorGUILayout.EndHorizontal();
// Draw parameters when expanded
if (isExpanded)
{
EditorGUILayout.Space(5);
EditorGUI.indentLevel++;
op.DrawParameters();
EditorGUI.indentLevel--;
}
GUI.backgroundColor = Color.white;
EditorGUILayout.EndVertical();
}
void DrawFooter()
{
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Apply to Material", GUILayout.Height(30)))
ApplyToMaterial();
if (GUILayout.Button("Clear All", GUILayout.Height(30), GUILayout.Width(100)))
{
operations.Clear();
expandedOps.Clear();
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space(5);
EditorGUILayout.LabelField($"Operations: {operations.Count}/16", EditorStyles.miniLabel);
}
void ShowAddOperationMenu()
{
var menu = new GenericMenu();
menu.AddItem(new GUIContent("Tube to Plane"), false, () => AddOperation(new TubeToPlaneOp()));
menu.AddItem(new GUIContent("Plane to Tube"), false, () => AddOperation(new PlaneToTubeOp()));
menu.AddItem(new GUIContent("Plane to Hemi-Octahedron"), false, () => AddOperation(new PlaneToHemiOctahedronOp()));
menu.AddItem(new GUIContent("Hemi-Octahedron to Plane"), false, () => AddOperation(new HemiOctahedronToPlaneOp()));
menu.AddSeparator("");
menu.AddItem(new GUIContent("Point Align"), false, () => AddOperation(new PointAlignOp()));
menu.AddItem(new GUIContent("Axis Align"), false, () => AddOperation(new AxisAlignOp()));
menu.AddSeparator("");
menu.AddItem(new GUIContent("Scale"), false, () => AddOperation(new ScaleOp()));
menu.AddItem(new GUIContent("Norm Conversion"), false, () => AddOperation(new NormConversionOp()));
menu.AddItem(new GUIContent("Seal"), false, () => AddOperation(new SealOp()));
menu.AddSeparator("");
menu.AddItem(new GUIContent("Sine Waves"), false, () => AddOperation(new SineWavesOp()));
menu.AddItem(new GUIContent("FBM"), false, () => AddOperation(new FBMOp()));
menu.ShowAsContext();
}
void AddOperation(DeformOperation op)
{
if (operations.Count >= 16)
return;
operations.Add(op);
expandedOps.Add(operations.Count - 1);
}
void ApplyToMaterial()
{
var builder = FoldPipelineBuilder.Create().For(targetMaterial);
foreach (var op in operations)
op.ApplyTo(builder);
builder.Apply();
}
void LoadFromMaterial()
{
operations.Clear();
expandedOps.Clear();
for (int i = 0; i < 16; i++)
{
var prefix = $"_Vertex_Deformation_Slot_{i}_";
if (targetMaterial.GetFloat(prefix + "Enabled") < 0.5f)
break;
int opcode = targetMaterial.GetInteger(prefix + "Opcode");
if (opcode == FoldPipelineBuilder.Opcodes.None)
break;
var slot = new FoldSlot
{
opcode = opcode,
float0 = targetMaterial.GetFloat(prefix + "Float_0"),
float1 = targetMaterial.GetFloat(prefix + "Float_1"),
float2 = targetMaterial.GetFloat(prefix + "Float_2"),
float3 = targetMaterial.GetFloat(prefix + "Float_3"),
vec0 = targetMaterial.GetVector(prefix + "Vector_0"),
vec1 = targetMaterial.GetVector(prefix + "Vector_1"),
vec2 = targetMaterial.GetVector(prefix + "Vector_2"),
vec3 = targetMaterial.GetVector(prefix + "Vector_3"),
};
DeformOperation op = opcode switch
{
FoldPipelineBuilder.Opcodes.TubeToPlane => new TubeToPlaneOp(slot),
FoldPipelineBuilder.Opcodes.PlaneToTube => new PlaneToTubeOp(slot),
FoldPipelineBuilder.Opcodes.PlaneToHemiOctahedron => new PlaneToHemiOctahedronOp(slot),
FoldPipelineBuilder.Opcodes.HemiOctahedronToPlane => new HemiOctahedronToPlaneOp(slot),
FoldPipelineBuilder.Opcodes.Scale => new ScaleOp(slot),
FoldPipelineBuilder.Opcodes.PointAlign => new PointAlignOp(slot),
FoldPipelineBuilder.Opcodes.AxisAlign => new AxisAlignOp(slot),
FoldPipelineBuilder.Opcodes.NormConversion => new NormConversionOp(slot),
FoldPipelineBuilder.Opcodes.Seal => new SealOp(slot),
FoldPipelineBuilder.Opcodes.SineWaves => new SineWavesOp(),
FoldPipelineBuilder.Opcodes.FBM => new FBMOp(),
_ => null
};
if (op == null)
break;
operations.Add(op);
}
}
}
// Base class for deformation operations
[System.Serializable]
public abstract class DeformOperation
{
public abstract string GetDisplayName();
public abstract void DrawParameters();
public abstract void ApplyTo(FoldPipelineBuilder builder);
}
[System.Serializable]
public class TubeToPlaneOp : DeformOperation
{
public Vector3 p = Vector3.zero;
public Vector3 r = Vector3.right;
public Vector3 s = Vector3.forward;
public float t = 1f;
public TubeToPlaneOp() { }
public TubeToPlaneOp(FoldSlot slot) { p = slot.vec0; r = slot.vec1; s = slot.vec2; t = slot.float0; }
public override string GetDisplayName() => "Tube to Plane";
public override void DrawParameters()
{
p = EditorGUILayout.Vector3Field("Origin (p)", p);
r = EditorGUILayout.Vector3Field("R Axis", r);
s = EditorGUILayout.Vector3Field("S Axis", s);
t = EditorGUILayout.Slider("Interpolation (t)", t, 0f, 1f);
}
public override void ApplyTo(FoldPipelineBuilder builder) =>
builder.TubeToPlane(p, r, s, t);
}
[System.Serializable]
public class PlaneToTubeOp : DeformOperation
{
public Vector3 p = Vector3.zero;
public Vector3 r = Vector3.right;
public Vector3 s = Vector3.forward;
public float t = 1f;
public PlaneToTubeOp() { }
public PlaneToTubeOp(FoldSlot slot) { p = slot.vec0; r = slot.vec1; s = slot.vec2; t = slot.float0; }
public override string GetDisplayName() => "Plane to Tube";
public override void DrawParameters()
{
p = EditorGUILayout.Vector3Field("Origin (p)", p);
r = EditorGUILayout.Vector3Field("R Axis", r);
s = EditorGUILayout.Vector3Field("S Axis", s);
t = EditorGUILayout.Slider("Interpolation (t)", t, 0f, 1f);
}
public override void ApplyTo(FoldPipelineBuilder builder) =>
builder.PlaneToTube(p, r, s, t);
}
[System.Serializable]
public class PlaneToHemiOctahedronOp : DeformOperation
{
public Vector3 p = Vector3.zero;
public Vector3 r = Vector3.right;
public Vector3 s = Vector3.forward;
public float t = 1f;
public PlaneToHemiOctahedronOp() { }
public PlaneToHemiOctahedronOp(FoldSlot slot) { p = slot.vec0; r = slot.vec1; s = slot.vec2; t = slot.float0; }
public override string GetDisplayName() => "Plane to Hemi-Octahedron";
public override void DrawParameters()
{
p = EditorGUILayout.Vector3Field("Origin (p)", p);
r = EditorGUILayout.Vector3Field("R Axis", r);
s = EditorGUILayout.Vector3Field("S Axis", s);
t = EditorGUILayout.Slider("Interpolation (t)", t, 0f, 1f);
}
public override void ApplyTo(FoldPipelineBuilder builder) =>
builder.PlaneToHemiOctahedron(p, r, s, t);
}
[System.Serializable]
public class ScaleOp : DeformOperation
{
public Vector3 k = Vector3.one;
public float t = 1f;
public ScaleOp() { }
public ScaleOp(FoldSlot slot) { k = slot.vec0; t = slot.float0; }
public override string GetDisplayName() => "Scale";
public override void DrawParameters()
{
k = EditorGUILayout.Vector3Field("Scale", k);
t = EditorGUILayout.Slider("Interpolation (t)", t, 0f, 1f);
}
public override void ApplyTo(FoldPipelineBuilder builder) =>
builder.Scale(k, t);
}
[System.Serializable]
public class HemiOctahedronToPlaneOp : DeformOperation
{
public Vector3 p = Vector3.zero;
public Vector3 r = Vector3.right;
public Vector3 s = Vector3.forward;
public float t = 1f;
public HemiOctahedronToPlaneOp() { }
public HemiOctahedronToPlaneOp(FoldSlot slot) { p = slot.vec0; r = slot.vec1; s = slot.vec2; t = slot.float0; }
public override string GetDisplayName() => "Hemi-Octahedron to Plane";
public override void DrawParameters()
{
p = EditorGUILayout.Vector3Field("Origin (p)", p);
r = EditorGUILayout.Vector3Field("R Axis", r);
s = EditorGUILayout.Vector3Field("S Axis", s);
t = EditorGUILayout.Slider("Interpolation (t)", t, 0f, 1f);
}
public override void ApplyTo(FoldPipelineBuilder builder) =>
builder.HemiOctahedronToPlane(p, r, s, t);
}
[System.Serializable]
public class PointAlignOp : DeformOperation
{
public Vector3 po = Vector3.zero;
public Vector3 pp = Vector3.up;
public Vector3 r = Vector3.right;
public float t = 1f;
public PointAlignOp() { }
public PointAlignOp(FoldSlot slot) { po = slot.vec0; pp = slot.vec1; r = slot.vec2; t = slot.float0; }
public override string GetDisplayName() => "Point Align";
public override void DrawParameters()
{
po = EditorGUILayout.Vector3Field("Origin Point (po)", po);
pp = EditorGUILayout.Vector3Field("Target Point (pp)", pp);
r = EditorGUILayout.Vector3Field("Rotation Axis (r)", r);
t = EditorGUILayout.Slider("Interpolation (t)", t, 0f, 1f);
}
public override void ApplyTo(FoldPipelineBuilder builder) =>
builder.PointAlign(po, pp, r, t);
}
[System.Serializable]
public class AxisAlignOp : DeformOperation
{
public Vector3 po = Vector3.zero;
public Vector3 pp = Vector3.up;
public Vector3 r = Vector3.right;
public float t = 1f;
public AxisAlignOp() { }
public AxisAlignOp(FoldSlot slot) { po = slot.vec0; pp = slot.vec1; r = slot.vec2; t = slot.float0; }
public override string GetDisplayName() => "Axis Align";
public override void DrawParameters()
{
po = EditorGUILayout.Vector3Field("Origin Point (po)", po);
pp = EditorGUILayout.Vector3Field("Target Point (pp)", pp);
r = EditorGUILayout.Vector3Field("Rotation Axis (r)", r);
t = EditorGUILayout.Slider("Interpolation (t)", t, 0f, 1f);
}
public override void ApplyTo(FoldPipelineBuilder builder) =>
builder.AxisAlign(po, pp, r, t);
}
[System.Serializable]
public class NormConversionOp : DeformOperation
{
public float inputK = 2f;
public float outputK = 1f;
public float t = 1f;
public NormConversionOp() { }
public NormConversionOp(FoldSlot slot) { inputK = slot.float0; outputK = slot.float1; t = slot.float2; }
public override string GetDisplayName() => $"Norm Conversion (L{FormatNorm(inputK)}→L{FormatNorm(outputK)})";
string FormatNorm(float k)
{
if (float.IsPositiveInfinity(k)) return "∞";
return k.ToString("F1");
}
public override void DrawParameters()
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Input Norm (k)", GUILayout.Width(120));
inputK = EditorGUILayout.FloatField(inputK);
if (GUILayout.Button("L∞", GUILayout.Width(30))) inputK = float.PositiveInfinity;
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Output Norm (k)", GUILayout.Width(120));
outputK = EditorGUILayout.FloatField(outputK);
if (GUILayout.Button("L∞", GUILayout.Width(30))) outputK = float.PositiveInfinity;
EditorGUILayout.EndHorizontal();
t = EditorGUILayout.Slider("Interpolation (t)", t, 0f, 1f);
EditorGUILayout.HelpBox(
"Common: L1=diamond, L2=sphere, L∞=cube",
MessageType.Info);
}
public override void ApplyTo(FoldPipelineBuilder builder) =>
builder.NormConversion(inputK, outputK, t);
}
[System.Serializable]
public class SealOp : DeformOperation
{
public float A = 0.1f;
public float k = 2f;
public float st = 0.8f;
public float t = 1f;
public SealOp() { }
public SealOp(FoldSlot slot) { A = slot.float0; k = slot.float1; st = slot.float2; t = slot.float3; }
public override string GetDisplayName() => "Seal";
public override void DrawParameters()
{
A = EditorGUILayout.FloatField("Amplitude (A)", A);
k = EditorGUILayout.FloatField("Smoothness (k)", k);
st = EditorGUILayout.FloatField("Scale (st)", st);
t = EditorGUILayout.Slider("Interpolation (t)", t, 0f, 1f);
}
public override void ApplyTo(FoldPipelineBuilder builder) =>
builder.Seal(A, k, st, t);
}
[System.Serializable]
public class SineWavesOp : DeformOperation
{
public override string GetDisplayName() => "Sine Waves";
public override void DrawParameters()
{
EditorGUILayout.HelpBox("Sine waves use shader-side parameters", MessageType.Info);
}
public override void ApplyTo(FoldPipelineBuilder builder) =>
builder.SineWaves();
}
[System.Serializable]
public class FBMOp : DeformOperation
{
public override string GetDisplayName() => "FBM (Fractal Brownian Motion)";
public override void DrawParameters()
{
EditorGUILayout.HelpBox("FBM uses shader-side parameters", MessageType.Info);
}
public override void ApplyTo(FoldPipelineBuilder builder) =>
builder.FBM();
}
|