forked from dskinner/material
/
anim.go
57 lines (50 loc) · 1.16 KB
/
anim.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
package material
import (
"time"
"dasa.cc/snd"
"golang.org/x/mobile/exp/f32"
)
var (
ExpSig, LinSig snd.Discrete
)
func init() {
ExpSig = snd.ExpDecay()
ExpSig.UnitInverse()
LinSig = snd.LinearDecay()
LinSig.UnitInverse()
}
type Interpolator struct {
Sig snd.Discrete
Dur time.Duration
Loop bool
}
func Animate(mat *f32.Mat4, interp Interpolator, fn func(m *f32.Mat4, dt float32)) (quit chan struct{}) {
m := *mat // copy; translate is always relative to resting position
quit = make(chan struct{}, 1)
go func() {
ticker := time.NewTicker(16 * time.Millisecond)
start := time.Now()
for {
select {
case <-quit:
ticker.Stop()
return
case now := <-ticker.C:
since := now.Sub(start)
t := float64(since%interp.Dur) / float64(interp.Dur)
if !interp.Loop && since >= interp.Dur {
quit <- struct{}{}
t = 1
}
dt := float32(interp.Sig.SampleUnit(t))
fn(&m, dt)
}
}
}()
return quit
}
func AnimateRotate(angle f32.Radian, axis f32.Vec3, mat *f32.Mat4, interp Interpolator) (quit chan struct{}) {
return Animate(mat, interp, func(m *f32.Mat4, dt float32) {
mat.Rotate(m, f32.Radian(dt*float32(angle)), &axis)
})
}