-
Notifications
You must be signed in to change notification settings - Fork 0
/
morphtargets.go
109 lines (92 loc) · 2.92 KB
/
morphtargets.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
package other
import (
"log"
"time"
"github.com/kasworld/h4o/_examples/app"
"github.com/kasworld/h4o/eventtype"
"github.com/kasworld/h4o/geometry"
"github.com/kasworld/h4o/graphic"
"github.com/kasworld/h4o/material"
"github.com/kasworld/h4o/math32"
"github.com/kasworld/h4o/gls"
"github.com/kasworld/h4o/light"
"github.com/kasworld/h4o/texture"
)
func init() {
app.DemoMap["other.morphtargets"] = &MorphTargets{}
}
type MorphTargets struct {
morphMesh *graphic.Mesh
count float32
weights []float32
}
// Start is called once at the start of the demo.
func (t *MorphTargets) Start(a *app.App) {
// Adds white directional front light
dir1 := light.NewDirectional(&math32.Color{1, 1, 1}, 1.0)
dir1.SetPosition(0, 5, 10)
a.Scene().Add(dir1)
// Create base geometry
geom := geometry.NewSphere(1, 32, 16)
morphGeom := geometry.NewMorphGeometry(geom.GetGeometry())
var target1Vertices, target2Vertices math32.ArrayF32
vertIdx := 0
geom.ReadVertices(func(vertex math32.Vector3) bool {
var vertex1, vertex2 math32.Vector3
vertex1.Copy(&vertex)
vertex2.Copy(&vertex)
vertex1.Y += math32.Sin(vertex1.X)
vertex1.Z += math32.Cos(vertex1.Y)
//vertex1.X *= 2
vertex2.X -= math32.Sin(vertex2.Y)
vertex2.Z -= math32.Cos(vertex2.X)
//vertex2.Y *= 2
target1Vertices.AppendVector3(&vertex1)
target2Vertices.AppendVector3(&vertex2)
vertIdx++
return false
})
target1 := geometry.NewGeometry()
target1.AddVBO(gls.NewVBO(target1Vertices).AddAttrib(gls.VertexPosition))
target2 := geometry.NewGeometry()
target2.AddVBO(gls.NewVBO(target2Vertices).AddAttrib(gls.VertexPosition))
morphGeom.AddMorphTargets(target1, target2)
// Create texture
texfile := a.DirData() + "/images/checkerboard.jpg"
tex1, err := texture.NewTexture2DFromImage(texfile)
if err != nil {
log.Fatal("Error loading texture: %s", err)
}
tex1.SetWrapS(gls.REPEAT)
tex1.SetWrapT(gls.REPEAT)
tex1.SetRepeat(2, 2)
mat := material.NewStandard(&math32.Color{0, 1, 0})
mat.AddTexture(tex1)
t.morphMesh = graphic.NewMesh(morphGeom, mat)
a.Scene().Add(t.morphMesh)
weights := []float32{0.5, 0.5}
morphGeom.SetWeights(weights)
// Add controls
if a.ControlFolder() == nil {
return
}
g1 := a.ControlFolder().AddGroup("Morph target weights")
g1s1 := g1.AddSlider("Weight 0:", 1, 0.5)
g1s1.Subscribe(eventtype.OnChange, func(evname eventtype.EventType, ev interface{}) {
weights[0] = g1s1.Value()
morphGeom.SetWeights(weights)
})
g1s2 := g1.AddSlider("Weight 1:", 1, 0.5)
g1s2.Subscribe(eventtype.OnChange, func(evname eventtype.EventType, ev interface{}) {
weights[1] = g1s2.Value()
morphGeom.SetWeights(weights)
})
}
// Update is called every frame.
func (t *MorphTargets) Update(a *app.App, deltaTime time.Duration) {
// Rotate at 1 rotation each 10 seconds
delta := float32(deltaTime.Seconds()) * 2 * math32.Pi / 10
t.morphMesh.RotateY(delta)
}
// Cleanup is called once at the end of the demo.
func (t *MorphTargets) Cleanup(a *app.App) {}