-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeline.go
294 lines (245 loc) · 6.32 KB
/
timeline.go
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
package spine
type CurveTimeline struct {
Time []float32
Curve []Curve
}
func (t *CurveTimeline) Duration() float32 {
if len(t.Time) == 0 {
return 0
}
return t.Time[len(t.Time)-1]
}
func (t *CurveTimeline) Find(time float32) (int, int, float32) {
if len(t.Time) == 0 || time < t.Time[0] {
return -1, -1, 0
}
nextKey := 0
for nextKey < len(t.Time) && time >= t.Time[nextKey] {
nextKey++
}
currentKey := nextKey - 1
if currentKey < 0 {
currentKey = 0
}
if nextKey >= len(t.Time) {
nextKey = len(t.Time) - 1
}
current := time - t.Time[currentKey]
delta := t.Time[nextKey] - t.Time[currentKey]
mix := float32(0.0)
if delta != 0 {
mix = current / delta
}
if len(t.Curve) > 0 {
mix = t.Curve[currentKey].Evaluate(mix)
}
return currentKey, nextKey, clamp01(mix)
}
type RotateTimeline struct {
Bone int
CurveTimeline
Angle []float32
}
func (t *RotateTimeline) Apply(skeleton *Skeleton, time float32, alpha float32) {
p0, p1, pmix := t.Find(time)
bone := skeleton.Bones[t.Bone]
if p0 < 0 {
bone.Local.Rotate = lerpAngle(bone.Local.Rotate, 0, alpha)
return
}
target := lerpAngle(t.Angle[p0], t.Angle[p1], pmix)
bone.Local.Rotate = lerpAngle(bone.Local.Rotate, target, alpha)
}
type TranslateTimeline struct {
Bone int
CurveTimeline
Translate []Vector
}
func (t *TranslateTimeline) Apply(skeleton *Skeleton, time float32, alpha float32) {
p0, p1, pmix := t.Find(time)
bone := skeleton.Bones[t.Bone]
if p0 < 0 {
bone.Local.Translate = lerpVector(bone.Local.Translate, V0, alpha)
return
}
target := lerpVector(t.Translate[p0], t.Translate[p1], pmix)
bone.Local.Translate = lerpVector(bone.Local.Translate, target, alpha)
}
type ScaleTimeline struct {
Bone int
CurveTimeline
Scale []Vector
}
func (t *ScaleTimeline) Apply(skeleton *Skeleton, time float32, alpha float32) {
p0, p1, pmix := t.Find(time)
bone := skeleton.Bones[t.Bone]
if p0 < 0 {
bone.Local.Scale = lerpVector(bone.Local.Scale, V1, alpha)
return
}
target := lerpVector(t.Scale[p0], t.Scale[p1], pmix)
bone.Local.Scale = lerpVector(bone.Local.Scale, target, alpha)
}
type ShearTimeline struct {
Bone int
CurveTimeline
Shear []Vector
}
func (t *ShearTimeline) Apply(skeleton *Skeleton, time float32, alpha float32) {
p0, p1, pmix := t.Find(time)
bone := skeleton.Bones[t.Bone]
if p0 < 0 {
bone.Local.Shear = lerpAngleVector(bone.Local.Shear, V0, alpha)
return
}
target := lerpAngleVector(t.Shear[p0], t.Shear[p1], pmix)
bone.Local.Shear = lerpAngleVector(bone.Local.Shear, target, alpha)
}
type ColorTimeline struct {
Slot int
CurveTimeline
Color []Color
}
func (t *ColorTimeline) Apply(skeleton *Skeleton, time float32, alpha float32) {
p0, p1, pmix := t.Find(time)
slot := skeleton.Slots[t.Slot]
if p0 < 0 {
slot.Color = lerpColor(slot.Color, slot.Data.Color, alpha)
return
}
target := lerpColor(t.Color[p0], t.Color[p1], pmix)
slot.Color = lerpColor(slot.Color, target, alpha)
}
type TwoColorTimeline struct {
Slot int
CurveTimeline
Color [][2]Color
}
func (t *TwoColorTimeline) Apply(skeleton *Skeleton, time float32, alpha float32) {
p0, p1, pmix := t.Find(time)
slot := skeleton.Slots[t.Slot]
if p0 < 0 {
slot.Color = lerpColor(slot.Color, slot.Data.Color, alpha)
slot.Dark = lerpColor(slot.Dark, slot.Data.Dark, alpha)
return
}
c0, c1 := t.Color[p0], t.Color[p1]
targetLight := lerpColor(c0[0], c1[0], pmix)
slot.Color = lerpColor(slot.Color, targetLight, alpha)
targetDark := lerpColor(c0[1], c1[1], pmix)
slot.Dark = lerpColor(slot.Dark, targetDark, alpha)
}
type AttachmentTimeline struct {
Slot int
CurveTimeline
Attachment []string
}
func (t *AttachmentTimeline) Apply(skeleton *Skeleton, time float32, alpha float32) {
if alpha < 0.5 {
return
}
p0, _, _ := t.Find(time)
slot := skeleton.Slots[t.Slot]
if p0 < 0 {
slot.Attachment = skeleton.Attachment(t.Slot, slot.Data.Attachment)
slot.Deform = nil
return
}
slot.Attachment = skeleton.Attachment(t.Slot, t.Attachment[p0])
slot.Deform = nil
}
type DeformTimeline struct {
CurveTimeline
Skin *Skin
Slot int
Mesh string
Deform [][]Vector
}
func (t *DeformTimeline) Apply(skeleton *Skeleton, time float32, alpha float32) {
// TODO: figure out proper matchup between attachment and skin
if t.Skin != skeleton.Skin {
return
}
slot := skeleton.Slots[t.Slot]
mesh, ok := slot.Attachment.(*MeshAttachment)
if !ok || t.Mesh != mesh.Name {
return
}
p0, p1, pmix := t.Find(time)
if p0 < 0 {
slot.Deform = make([]Vector, len(t.Deform[0]))
return
}
// TODO: this may happen during attachment change
// this should be reset else where, otherwise we have leftovers
// from previous attachment deform
if len(slot.Deform) == 0 {
slot.Deform = make([]Vector, len(t.Deform[p0]))
}
if len(slot.Deform) != len(t.Deform[p0]) {
panic("invalid state")
}
v0, v1 := t.Deform[p0], t.Deform[p1]
for i := range slot.Deform {
target := lerpVector(v0[i], v1[i], pmix)
slot.Deform[i] = lerpVector(slot.Deform[i], target, alpha)
}
}
type EventTimeline struct {
CurveTimeline
Event []string
}
func (t *EventTimeline) Apply(skeleton *Skeleton, time float32, alpha float32) {
//TODO:
/*
if alpha < 0.5 {
return
}
p0, _, _ := t.Find(time)
var attachment Attachment
attachmentName := t.Attachment[p0]
if attachmentName != "" {
attachment = skeleton.Attachment(t.Slot, attachmentName)
}
slot := skeleton.Slots[t.Slot]
slot.Attachment = attachment
*/
}
type OrderTimeline struct {
CurveTimeline
Order [][]int
}
func (t *OrderTimeline) Apply(skeleton *Skeleton, time float32, alpha float32) {
if alpha < 0.5 {
return
}
p0, _, _ := t.Find(time)
if p0 < 0 {
copy(skeleton.Order, skeleton.Slots)
return
}
order := t.Order[p0]
if len(order) == 0 {
for i, slot := range t.Order[p0] {
skeleton.Order[i] = skeleton.Slots[slot]
}
} else {
copy(skeleton.Order, skeleton.Slots)
}
}
type TransformConstraintTimeline struct {
Constraint int
CurveTimeline
Transform []TransformMix
}
func (t *TransformConstraintTimeline) Apply(skeleton *Skeleton, time float32, alpha float32) {
p0, p1, pmix := t.Find(time)
constraint := skeleton.TransfromConstraints[t.Constraint]
if p0 < 0 {
constraint.Mix = LerpTransformMix(constraint.Mix, constraint.Data.Mix, alpha)
return
}
a, b := t.Transform[p0], t.Transform[p1]
target := LerpTransformMix(a, b, pmix)
constraint.Mix = LerpTransformMix(constraint.Mix, target, alpha)
}