-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathVFXExpressionMapper.cs
More file actions
154 lines (133 loc) · 4.74 KB
/
Copy pathVFXExpressionMapper.cs
File metadata and controls
154 lines (133 loc) · 4.74 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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using UnityEngine.VFX;
namespace UnityEditor.VFX
{
struct VFXNamedExpression
{
public VFXNamedExpression(VFXExpression exp, string name)
{
this.exp = exp;
this.name = name;
}
public VFXExpression exp;
public string name;
}
class VFXExpressionMapper
{
public struct Data
{
public string fullName { get { return id == -1 ? name : string.Format("{0}_{1}", name, VFXCodeGeneratorHelper.GeneratePrefix((uint)id)); } }
public string name;
public int id;
}
public VFXExpressionMapper()
{
}
public IEnumerable<VFXExpression> expressions => m_ExpressionsData.Keys;
public void AddExpressionsFromSlotContainer(IVFXSlotContainer slotContainer, int blockId)
{
if (slotContainer.activationSlot != null)
AddExpressionsFromSlot(slotContainer.activationSlot, blockId);
foreach (var master in slotContainer.inputSlots)
AddExpressionsFromSlot(master, blockId);
}
public void AddExpressionsFromSlot(VFXSlot masterSlot, int blockId)
{
foreach (var slot in masterSlot.GetExpressionSlots())
{
var exp = slot.GetExpression();
if (!Contains(exp))
AddExpression(exp, slot.fullName, blockId);
}
}
public static VFXExpressionMapper FromBlocks(IEnumerable<VFXBlock> blocks)
{
var mapper = new VFXExpressionMapper();
int cpt = 0;
foreach (var block in blocks)
{
mapper.AddExpression(block.activationExpression, VFXBlock.activationSlotName, cpt);
mapper.AddExpressions(block.parameters, cpt++);
}
return mapper;
}
public static VFXExpressionMapper FromContext(VFXContext context)
{
var mapper = FromBlocks(context.activeFlattenedChildrenWithImplicit);
mapper.AddExpressionsFromSlotContainer(context, -1);
return mapper;
}
public ReadOnlyCollection<Data> GetData(VFXExpression exp)
{
List<Data> data;
if (m_ExpressionsData.TryGetValue(exp, out data))
{
return data.AsReadOnly();
}
return (new List<Data>()).AsReadOnly();
}
public bool Contains(VFXExpression exp)
{
return m_ExpressionsData.ContainsKey(exp);
}
public void AddExpression(VFXExpression exp, Data data)
{
AddExpression(exp, data.name, data.id);
}
public VFXExpression FromNameAndId(string name, int id)
{
foreach (var expression in m_ExpressionsData)
{
if (expression.Value.Any(o => o.name == name && o.id == id))
{
return expression.Key;
}
}
return null;
}
public IEnumerable<VFXNamedExpression> CollectExpression(int id, bool fullname = true)
{
foreach (var expressionData in m_ExpressionsData)
{
foreach (var data in expressionData.Value)
{
if (data.id == id)
{
yield return new VFXNamedExpression(expressionData.Key, fullname ? data.fullName : data.name);
}
}
}
}
public void AddExpression(VFXExpression exp, string name, int id)
{
if (exp == null || name == null)
throw new ArgumentNullException();
if (m_ExpressionsData.SelectMany(o => o.Value).Any(o => o.name == name && o.id == id))
throw new ArgumentException($"{name}_{id} has been added twice: {exp}");
var data = new Data();
data.name = name;
data.id = id;
if (!m_ExpressionsData.ContainsKey(exp))
{
m_ExpressionsData.Add(exp, new List<Data>() { data });
}
else
{
m_ExpressionsData[exp].Add(data);
}
}
public void AddExpression(VFXNamedExpression expression, int id)
{
AddExpression(expression.exp, expression.name, id);
}
public void AddExpressions(IEnumerable<VFXNamedExpression> expressions, int id)
{
foreach (var exp in expressions)
AddExpression(exp, id);
}
private readonly Dictionary<VFXExpression, List<Data>> m_ExpressionsData = new();
}
}