-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathVisualEffectControlPlayableBehaviour.cs
More file actions
245 lines (221 loc) · 8.75 KB
/
Copy pathVisualEffectControlPlayableBehaviour.cs
File metadata and controls
245 lines (221 loc) · 8.75 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
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
#if VFX_HAS_TIMELINE
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.VFX.Utility;
namespace UnityEngine.VFX
{
[Serializable]
struct EventAttributes
{
[SerializeReference]
public EventAttribute[] content;
}
[Serializable]
abstract class EventAttribute
{
public ExposedProperty id;
public abstract bool ApplyToVFX(VFXEventAttribute eventAttribute);
}
[Serializable]
abstract class EventAttributeValue<T> : EventAttribute
{
private readonly Func<VFXEventAttribute, int, bool> m_HasFunc;
private readonly Action<VFXEventAttribute, int, T> m_ApplyFunc;
protected EventAttributeValue(Func<VFXEventAttribute, int, bool> hasFunc, Action<VFXEventAttribute, int, T> applyFunc)
{
m_HasFunc = hasFunc;
m_ApplyFunc = applyFunc;
}
public sealed override bool ApplyToVFX(VFXEventAttribute eventAttribute)
{
if (!m_HasFunc(eventAttribute, id))
return false;
m_ApplyFunc(eventAttribute, id, value);;
return true;
}
public T value;
}
[Serializable]
class EventAttributeFloat : EventAttributeValue<float>
{
public EventAttributeFloat() : base((e, id) => e.HasFloat(id), (e, id, value) => e.SetFloat(id, value)) { }
}
[Serializable]
class EventAttributeVector2 : EventAttributeValue<Vector2>
{
public EventAttributeVector2() : base((e, id) => e.HasVector2(id), (e, id, value) => e.SetVector2(id, value)) { }
}
[Serializable]
class EventAttributeVector3 : EventAttributeValue<Vector3>
{
public EventAttributeVector3() : base((e, id) => e.HasVector3(id), (e, id, value) => e.SetVector3(id, value)) { }
}
[Serializable]
class EventAttributeColor : EventAttributeVector3 {}
[Serializable]
class EventAttributeVector4 : EventAttributeValue<Vector4>
{
public EventAttributeVector4() : base((e, id) => e.HasVector4(id), (e, id, value) => e.SetVector4(id, value)) { }
}
[Serializable]
class EventAttributeInt : EventAttributeValue<int>
{
public EventAttributeInt() : base((e, id) => e.HasInt(id), (e, id, value) => e.SetInt(id, value)) { }
}
[Serializable]
class EventAttributeUInt : EventAttributeValue<uint>
{
public EventAttributeUInt() : base((e, id) => e.HasUint(id), (e, id, value) => e.SetUint(id, value)) { }
}
[Serializable]
class EventAttributeBool : EventAttributeValue<bool>
{
public EventAttributeBool() : base((e, id) => e.HasBool(id), (e, id, value) => e.SetBool(id, value)) { }
}
static class VFXTimeSpaceHelper
{
public static IEnumerable<VisualEffectPlayableSerializedEvent> GetEventNormalizedSpace(PlayableTimeSpace space, VisualEffectControlPlayableBehaviour source)
{
return GetEventNormalizedSpace(space, source.events, source.clipStart, source.clipEnd);
}
private static IEnumerable<VisualEffectPlayableSerializedEvent> CollectClipEvents(VisualEffectControlClip source)
{
if (source.clipEvents != null)
{
foreach (var clip in source.clipEvents)
{
var eventEnter = (VisualEffectPlayableSerializedEvent)clip.enter;
var eventExit = (VisualEffectPlayableSerializedEvent)clip.exit;
eventEnter.editorColor = eventExit.editorColor = clip.editorColor;
yield return eventEnter;
yield return eventExit;
}
}
}
public static IEnumerable<VisualEffectPlayableSerializedEvent> GetEventNormalizedSpace(PlayableTimeSpace space, VisualEffectControlClip source, bool clipEvents)
{
IEnumerable<VisualEffectPlayableSerializedEvent> sourceEvents;
if (clipEvents)
sourceEvents = CollectClipEvents(source);
else
sourceEvents = source.singleEvents;
return GetEventNormalizedSpace(space, sourceEvents, source.clipStart, source.clipEnd);
}
private static IEnumerable<VisualEffectPlayableSerializedEvent> GetEventNormalizedSpace(PlayableTimeSpace space, IEnumerable<VisualEffectPlayableSerializedEvent> events, double clipStart, double clipEnd)
{
foreach (var itEvent in events)
{
var copy = itEvent;
copy.timeSpace = space;
copy.time = GetTimeInSpace(itEvent.timeSpace, itEvent.time, space, clipStart, clipEnd);
yield return copy;
}
}
public static double GetTimeInSpace(PlayableTimeSpace srcSpace, double srcTime, PlayableTimeSpace dstSpace, double clipStart, double clipEnd)
{
if (srcSpace == dstSpace)
return srcTime;
if (dstSpace == PlayableTimeSpace.AfterClipStart)
{
switch (srcSpace)
{
case PlayableTimeSpace.BeforeClipEnd:
return clipEnd - srcTime - clipStart;
case PlayableTimeSpace.Percentage:
return (clipEnd - clipStart) * (srcTime / 100.0);
case PlayableTimeSpace.Absolute:
return srcTime - clipStart;
}
}
else if (dstSpace == PlayableTimeSpace.BeforeClipEnd)
{
switch (srcSpace)
{
case PlayableTimeSpace.AfterClipStart:
return clipEnd - srcTime - clipStart;
case PlayableTimeSpace.Percentage:
return clipEnd - clipStart - (clipEnd - clipStart) * (srcTime / 100.0);
case PlayableTimeSpace.Absolute:
return clipEnd - srcTime;
}
}
else if (dstSpace == PlayableTimeSpace.Percentage)
{
switch (srcSpace)
{
case PlayableTimeSpace.AfterClipStart:
return 100.0 * (srcTime) / (clipEnd - clipStart);
case PlayableTimeSpace.BeforeClipEnd:
return 100.0 * (clipEnd - srcTime - clipStart) / (clipEnd - clipStart);
case PlayableTimeSpace.Absolute:
return 100.0 * (srcTime - clipStart) / (clipEnd - clipStart);
}
}
else if (dstSpace == PlayableTimeSpace.Absolute)
{
switch (srcSpace)
{
case PlayableTimeSpace.AfterClipStart:
return clipStart + srcTime;
case PlayableTimeSpace.BeforeClipEnd:
return clipEnd - srcTime;
case PlayableTimeSpace.Percentage:
return clipStart + (clipEnd - clipStart) * (srcTime / 100.0);
}
}
//Other conversion
throw new NotImplementedException(srcSpace + " to " + dstSpace);
}
}
enum PlayableTimeSpace
{
AfterClipStart,
BeforeClipEnd,
Percentage,
Absolute
}
[Serializable]
struct VisualEffectPlayableSerializedEvent
{
public Color editorColor;
public double time;
public PlayableTimeSpace timeSpace;
public ExposedProperty name;
public EventAttributes eventAttributes;
}
[Serializable]
struct VisualEffectPlayableSerializedEventNoColor
{
public double time;
public PlayableTimeSpace timeSpace;
public ExposedProperty name;
public EventAttributes eventAttributes;
public static implicit operator VisualEffectPlayableSerializedEvent(VisualEffectPlayableSerializedEventNoColor evt)
{
return new VisualEffectPlayableSerializedEvent()
{
time = evt.time,
timeSpace = evt.timeSpace,
name = evt.name,
eventAttributes = evt.eventAttributes
};
}
}
class VisualEffectControlPlayableBehaviour : PlayableBehaviour
{
public double clipStart { get; set; }
public double clipEnd { get; set; }
public bool scrubbing { get; set; }
public bool reinitEnter { get; set; }
public bool reinitExit { get; set; }
public uint startSeed { get; set; }
public VisualEffectPlayableSerializedEvent[] events { get; set; }
public uint clipEventsCount { get; set; }
public uint prewarmStepCount { get; set; }
public float prewarmDeltaTime { get; set; }
public ExposedProperty prewarmEvent { get; set; }
}
}
#endif