-
Notifications
You must be signed in to change notification settings - Fork 0
/
gltf.go
178 lines (148 loc) · 4.3 KB
/
gltf.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
package loader
import (
"fmt"
"path/filepath"
"time"
"github.com/kasworld/h4o/_examples/app"
"github.com/kasworld/h4o/_examples/usrevent"
"github.com/kasworld/h4o/_examples/util"
"github.com/kasworld/h4o/animation"
"github.com/kasworld/h4o/eventtype"
"github.com/kasworld/h4o/gui"
"github.com/kasworld/h4o/light"
"github.com/kasworld/h4o/loader/gltf"
"github.com/kasworld/h4o/math32"
"github.com/kasworld/h4o/node"
"github.com/kasworld/h4o/util/helper"
)
func init() {
app.DemoMap["loader.gltf"] = &GltfLoader{}
}
type GltfLoader struct {
prevLoaded node.NodeI
selFile *util.FileSelectButton
anims []*animation.Animation
}
// Start is called once at the start of the demo.
func (t *GltfLoader) Start(a *app.App) {
// Creates file selection button
t.selFile = util.NewFileSelectButton(a.DirData()+"/gltf", "Select File", 400, 300)
t.selFile.SetPosition(10, 10)
t.selFile.FS.SetFileFilters("*.gltf", "*.glb")
a.DemoPanel().Add(t.selFile)
t.selFile.Subscribe(usrevent.OnSelect, func(evname eventtype.EventType, ev interface{}) {
fpath := ev.(string)
err := t.loadScene(a, fpath)
if err == nil {
t.selFile.Label.SetText("File: " + filepath.Base(fpath))
t.selFile.SetError("")
} else {
t.selFile.Label.SetText("Select File")
t.selFile.SetError(err.Error())
}
})
// Adds white directional front light
l1 := light.NewDirectional(math32.NewColor("white"), 1.0)
l1.SetPosition(0, 0, 10)
a.Scene().Add(l1)
// Adds white directional top light
l2 := light.NewDirectional(math32.NewColor("white"), 1.0)
l2.SetPosition(0, 10, 0)
a.Scene().Add(l2)
// Adds white directional right light
l3 := light.NewDirectional(math32.NewColor("white"), 1.0)
l3.SetPosition(10, 0, 0)
a.Scene().Add(l3)
// Create axes helper
axes := helper.NewAxes(2)
a.Scene().Add(axes)
// Label for error message
errLabel := gui.NewLabel("")
errLabel.SetFontSize(18)
a.DemoPanel().Add(errLabel)
//fpath := "gltf/DamagedHelmet/glTF/DamagedHelmet.gltf"
fpath := "gltf/CesiumMan/glTF/CesiumMan.gltf"
t.loadScene(a, filepath.Join(a.DirData(), fpath))
t.selFile.Label.SetText("File: " + filepath.Base(fpath))
}
func (t *GltfLoader) loadScene(a *app.App, fpath string) error {
// TODO move camera or scale scene such that it's nicely framed
// TODO do this for other loaders as well
// Remove previous model from the scene
if t.prevLoaded != nil {
t.anims = t.anims[:]
a.Scene().Remove(t.prevLoaded)
t.prevLoaded.Dispose()
t.prevLoaded = nil
}
// Checks file extension
ext := filepath.Ext(fpath)
var g *gltf.GLTF
var err error
// Parses file
if ext == ".gltf" {
g, err = gltf.ParseJSON(fpath)
} else if ext == ".glb" {
g, err = gltf.ParseBin(fpath)
} else {
return fmt.Errorf("unrecognized file extension:%s", ext)
}
if err != nil {
return err
}
//spew.Config.Indent = " "
//spew.Dump(g.Nodes)
//spew.Dump(g.Meshes)
//spew.Dump(g.Accessors)
defaultSceneIdx := 0
if g.Scene != nil {
defaultSceneIdx = *g.Scene
}
// Create default scene
n, err := g.LoadScene(defaultSceneIdx)
if err != nil {
return err
}
//c1 := n.GetNode().Children()[0]
//c2 := c1.GetNode().Children()[0]
//gr, ok := c2.(graphic.IGraphic)
//if ok {
// uvVbo := gr.GetGraphic().GetGeometry().VBO(gls.VertexTexcoord)
// fmt.Println(uvVbo.Attributes())
// fmt.Println(uvVbo.Buffer())
//}
// Scale scene to fit screen
//bbox := n.BoundingBox()
//var scale math32.Vector3
//bbox.Size(&scale)
//largest := math32.Abs(scale.X)
//if math32.Abs(scale.Y) > largest {
// largest = math32.Abs(scale.Y)
// if math32.Abs(scale.Z) > scale.Y{
// largest = math32.Abs(scale.Z)
// }
//}
//largest = 1/largest
//n.GetNode().SetScale(largest, largest, largest)
// Create animations
for i := range g.Animations {
anim, _ := g.LoadAnimation(i)
anim.SetLoop(true)
t.anims = append(t.anims, anim)
}
// Add normals helper
//box := n.GetNode().Children()[0].GetNode().Children()[0]
//normals := graphic.NewNormalsHelper(box.(graphic.IGraphic), 0.1, &math32.Color{0, 0, 1}, 1)
//a.Scene().Add(normals)
a.Scene().Add(n)
t.prevLoaded = n
return nil
}
// Update is called every frame.
func (t *GltfLoader) Update(a *app.App, deltaTime time.Duration) {
for _, anim := range t.anims {
anim.Update(float32(deltaTime.Seconds()))
}
}
// Cleanup is called once at the end of the demo.
func (t *GltfLoader) Cleanup(a *app.App) {}