/
gltf.go
163 lines (134 loc) · 3.72 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
package loader
import (
"fmt"
"path/filepath"
"github.com/g3n/engine/core"
"github.com/g3n/engine/graphic"
"github.com/g3n/engine/gui"
"github.com/g3n/engine/light"
"github.com/g3n/engine/loader/gltf"
"github.com/g3n/engine/math32"
"github.com/g3n/g3nd/demos"
"github.com/g3n/g3nd/app"
"github.com/g3n/g3nd/util"
"github.com/g3n/engine/animation"
)
func init() {
demos.Map["loader.gltf"] = &GltfLoader{}
}
type GltfLoader struct {
prevLoaded core.INode
selFile *util.FileSelectButton
anims []*animation.Animation
}
func (t *GltfLoader) Initialize(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.GuiPanel().Add(t.selFile)
t.selFile.Subscribe("OnSelect", func(evname string, 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)
// Adds axis helper
axis := graphic.NewAxisHelper(2)
a.Scene().Add(axis)
// Label for error message
errLabel := gui.NewLabel("")
errLabel.SetFontSize(18)
a.Gui().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) Render(a *app.App) {
for _, anim := range t.anims {
anim.Update(a.FrameDeltaSeconds())
}
}
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
}
// 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
}