-
Notifications
You must be signed in to change notification settings - Fork 1
/
animation.go
409 lines (346 loc) · 10.2 KB
/
animation.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
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
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package collada
import (
"fmt"
"github.com/lquesada/cavernal/lib/g3n/engine/core"
"github.com/lquesada/cavernal/lib/g3n/engine/math32"
"strings"
)
// AnimationTarget contains all animation channels for an specific target node
type AnimationTarget struct {
target core.INode
matrix math32.Matrix4 // original node transformation matrix
start float32 // initial input offset value
last float32 // last input value
minInput float32 // minimum input value for all channels
maxInput float32 // maximum input value for all channels
loop bool // animation loop flag
rot math32.Vector3 // rotation in XYZ Euler angles
channels []*ChannelInstance
}
// A ChannelInstance associates an animation parameter channel to an interpolation sampler
type ChannelInstance struct {
sampler *SamplerInstance
action ActionFunc
}
// SamplerInstance specifies the input key frames, output values for these key frames
// and interpolation information. It can be shared by more than one animation
type SamplerInstance struct {
Input []float32 // Input keys (usually time)
Output []float32 // Outputs values for the keys
Interp []string // Names of interpolation functions for each key frame
InTangent []float32 // Origin tangents for Bezier interpolation
OutTangent []float32 // End tangents for Bezier interpolation
}
// ActionFunc is the type for all functions that execute an specific parameter animation
type ActionFunc func(at *AnimationTarget, v float32)
// Reset resets the animation from the beginning
func (at *AnimationTarget) Reset() {
at.last = at.start
at.target.GetNode().SetMatrix(&at.matrix)
}
// SetLoop sets the state of the animation loop flag
func (at *AnimationTarget) SetLoop(loop bool) {
at.loop = loop
}
// SetStart sets the initial offset value
func (at *AnimationTarget) SetStart(v float32) {
at.start = v
}
// Update interpolates the specified input value for each animation target channel
// and executes its corresponding action function. Returns true if the input value
// is inside the key frames ranges or false otherwise.
func (at *AnimationTarget) Update(delta float32) bool {
// Checks if input is less than minimum
at.last = at.last + delta
if at.last < at.minInput {
return false
}
// Checks if input is greater than maximum
if at.last > at.maxInput {
if at.loop {
at.Reset()
} else {
return false
}
}
for i := 0; i < len(at.channels); i++ {
ch := at.channels[i]
// Get interpolated value
v, ok := ch.sampler.Interpolate(at.last)
if !ok {
return false
}
// Call action func
ch.action(at, v)
// Sets final rotation
at.target.GetNode().SetRotation(at.rot.X, at.rot.Y, at.rot.Z)
}
return true
}
// NewAnimationTargets creates and returns a map of all animation targets
// contained in the decoded Collada document and for the previously decoded scene.
// The map is indexed by the node loaderID.
func (d *Decoder) NewAnimationTargets(scene core.INode) (map[string]*AnimationTarget, error) {
if d.dom.LibraryAnimations == nil {
return nil, fmt.Errorf("No animations found")
}
// Maps target node to its animation target instance
targetsMap := make(map[string]*AnimationTarget)
// For each Collada animation element
for _, ca := range d.dom.LibraryAnimations.Animation {
// For each Collada channel for this animation
for _, cc := range ca.Channel {
// Separates the channel target in target id and target action
parts := strings.Split(cc.Target, "/")
if len(parts) < 2 {
return nil, fmt.Errorf("Channel target invalid")
}
targetID := parts[0]
targetAction := parts[1]
// Get the target node object referenced by the target id from the specified scene.
target := scene.GetNode().FindLoaderID(targetID)
if target == nil {
return nil, fmt.Errorf("Target node id:%s not found", targetID)
}
// Get reference to the AnimationTarget for this target in the local map
// If not found creates the animation target and inserts in the map
at := targetsMap[targetID]
if at == nil {
at = new(AnimationTarget)
at.target = target
at.matrix = target.GetNode().Matrix()
targetsMap[targetID] = at
}
// Creates the sampler instance specified from the channel source
si, err := NewSamplerInstance(ca, cc.Source)
if err != nil {
return nil, err
}
// Sets the action function from the target action
var af ActionFunc
switch targetAction {
case "location.X":
af = actionPositionX
case "location.Y":
af = actionPositionY
case "location.Z":
af = actionPositionZ
case "rotationX.ANGLE":
af = actionRotationX
case "rotationY.ANGLE":
af = actionRotationY
case "rotationZ.ANGLE":
af = actionRotationZ
case "scale.X":
af = actionScaleX
case "scale.Y":
af = actionScaleY
case "scale.Z":
af = actionScaleZ
default:
return nil, fmt.Errorf("Unsupported channel target action:%s", targetAction)
}
// Creates the channel instance for this sampler and target action and adds it
// to the current AnimationTarget
ci := &ChannelInstance{si, af}
at.channels = append(at.channels, ci)
}
}
// Set minimum and maximum input values for each animation target
for _, at := range targetsMap {
at.minInput = math32.Infinity
at.maxInput = -math32.Infinity
for _, ch := range at.channels {
// First key frame input
inp := ch.sampler.Input[0]
if inp < at.minInput {
at.minInput = inp
}
// Last key frame input
inp = ch.sampler.Input[len(ch.sampler.Input)-1]
if inp > at.maxInput {
at.maxInput = inp
}
}
}
return targetsMap, nil
}
func actionPositionX(at *AnimationTarget, v float32) {
at.target.GetNode().SetPositionX(v)
}
func actionPositionY(at *AnimationTarget, v float32) {
at.target.GetNode().SetPositionY(v)
}
func actionPositionZ(at *AnimationTarget, v float32) {
at.target.GetNode().SetPositionZ(v)
}
func actionRotationX(at *AnimationTarget, v float32) {
at.rot.X = math32.DegToRad(v)
}
func actionRotationY(at *AnimationTarget, v float32) {
at.rot.Y = math32.DegToRad(v)
}
func actionRotationZ(at *AnimationTarget, v float32) {
at.rot.Z = math32.DegToRad(v)
}
func actionScaleX(at *AnimationTarget, v float32) {
at.target.GetNode().SetScaleX(v)
}
func actionScaleY(at *AnimationTarget, v float32) {
at.target.GetNode().SetScaleY(v)
}
func actionScaleZ(at *AnimationTarget, v float32) {
at.target.GetNode().SetScaleZ(v)
}
// NewSamplerInstance creates and returns a pointer to a new SamplerInstance built
// with data from the specified Collada animation and URI
func NewSamplerInstance(ca *Animation, uri string) (*SamplerInstance, error) {
id := strings.TrimPrefix(uri, "#")
var cs *Sampler
for _, current := range ca.Sampler {
if current.Id == id {
cs = current
break
}
}
if cs == nil {
return nil, fmt.Errorf("Sampler:%s not found", id)
}
// Get sampler inputs
si := new(SamplerInstance)
for _, inp := range cs.Input {
if inp.Semantic == "INPUT" {
data, err := findSourceFloatArray(ca, inp.Source)
if err != nil {
return nil, err
}
si.Input = data
continue
}
if inp.Semantic == "OUTPUT" {
data, err := findSourceFloatArray(ca, inp.Source)
if err != nil {
return nil, err
}
si.Output = data
continue
}
if inp.Semantic == "INTERPOLATION" {
data, err := findSourceNameArray(ca, inp.Source)
if err != nil {
return nil, err
}
si.Interp = data
continue
}
if inp.Semantic == "IN_TANGENT" {
data, err := findSourceFloatArray(ca, inp.Source)
if err != nil {
return nil, err
}
si.InTangent = data
continue
}
if inp.Semantic == "OUT_TANGENT" {
data, err := findSourceFloatArray(ca, inp.Source)
if err != nil {
return nil, err
}
si.OutTangent = data
continue
}
}
return si, nil
}
// Interpolate returns the interpolated output and its validity
// for this sampler for the specified input.
func (si *SamplerInstance) Interpolate(inp float32) (float32, bool) {
// Test limits
if len(si.Input) < 2 {
return 0, false
}
if inp < si.Input[0] {
return 0, false
}
if inp > si.Input[len(si.Input)-1] {
return 0, false
}
// Find key frame interval
var idx int
for idx = 0; idx < len(si.Input)-1; idx++ {
if inp >= si.Input[idx] && inp < si.Input[idx+1] {
break
}
}
// Checks if interval was found
if idx >= len(si.Input)-1 {
return 0, false
}
switch si.Interp[idx] {
case "STEP":
return si.linearInterp(inp, idx), true
case "LINEAR":
return si.linearInterp(inp, idx), true
case "BEZIER":
return si.bezierInterp(inp, idx), true
case "HERMITE":
return si.linearInterp(inp, idx), true
case "CARDINAL":
return si.linearInterp(inp, idx), true
case "BSPLINE":
return si.linearInterp(inp, idx), true
}
return 0, false
}
func (si *SamplerInstance) linearInterp(inp float32, idx int) float32 {
k1 := si.Input[idx]
k2 := si.Input[idx+1]
v1 := si.Output[idx]
v2 := si.Output[idx+1]
return v1 + (v2-v1)*(inp-k1)/(k2-k1)
}
func (si *SamplerInstance) bezierInterp(inp float32, idx int) float32 {
p0 := si.Output[idx]
p1 := si.Output[idx+1]
c0 := si.OutTangent[2*idx+1]
c1 := si.InTangent[2*(idx+1)+1]
k1 := si.Input[idx]
k2 := si.Input[idx+1]
s := (inp - k1) / (k2 - k1)
out := p0*math32.Pow(1-s, 3) + 3*c0*s*math32.Pow(1-s, 2) + 3*c1*s*s*(1-s) + p1*math32.Pow(s, 3)
return out
}
func findSourceNameArray(ca *Animation, uri string) ([]string, error) {
src := findSource(ca, uri)
if src == nil {
return nil, fmt.Errorf("Source:%s not found", uri)
}
na, ok := src.ArrayElement.(*NameArray)
if !ok {
return nil, fmt.Errorf("Source:%s is not NameArray", uri)
}
return na.Data, nil
}
func findSourceFloatArray(ca *Animation, uri string) ([]float32, error) {
src := findSource(ca, uri)
if src == nil {
return nil, fmt.Errorf("Source:%s not found", uri)
}
fa, ok := src.ArrayElement.(*FloatArray)
if !ok {
return nil, fmt.Errorf("Source:%s is not FloatArray", uri)
}
return fa.Data, nil
}
func findSource(ca *Animation, uri string) *Source {
id := strings.TrimPrefix(uri, "#")
for _, src := range ca.Source {
if src.Id == id {
return src
}
}
return nil
}