/
fbx_node.go
304 lines (270 loc) · 6 KB
/
fbx_node.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
package fbx
import (
"fmt"
"io"
"reflect"
"strings"
"github.com/binzume/modelconv/geom"
)
type Node struct {
Name string
Attributes AttributeList
Children []*Node
}
func NewNode(name string, values ...interface{}) *Node {
var attrs []*Attribute
for _, v := range values {
attrs = append(attrs, &Attribute{Value: v})
}
return &Node{Name: name, Attributes: attrs}
}
func (n *Node) AddChild(node *Node) {
n.Children = append(n.Children, node)
}
func (n *Node) RemoveChild(node *Node) bool {
for i, c := range n.Children {
if c == node {
n.Children = append(n.Children[:i], n.Children[i+1:]...)
return true
}
}
return false
}
func (n *Node) FindChild(name string) *Node {
if n == nil {
return nil
}
for _, c := range n.Children {
if c.Name == name {
return c
}
}
return nil
}
func (n *Node) GetChildren() []*Node {
if n == nil {
return nil
}
return n.Children
}
func (n *Node) Attr(i int) *Attribute {
if n == nil {
return nil
}
return n.Attributes.Get(i)
}
func (n *Node) GetInt(defvalue int) int {
return n.Attr(0).ToInt(defvalue)
}
func (n *Node) GetInt64(defvalue int64) int64 {
return n.Attr(0).ToInt64(defvalue)
}
func (n *Node) GetFloat32(defvalue float32) float32 {
return n.Attr(0).ToFloat32(defvalue)
}
func (n *Node) GetString() string {
return n.Attr(0).ToString()
}
func (n *Node) GetInt32Array() []int32 {
array := n.Attr(0).ToInt32Array()
if array == nil && n.Attr(0) != nil {
for _, attr := range n.Attributes {
array = append(array, int32(attr.ToInt(0)))
}
}
return array
}
func (n *Node) GetFloat32Array() []float32 {
array := n.Attr(0).ToFloat32Array()
if array == nil && n.Attr(0) != nil {
for _, attr := range n.Attributes {
array = append(array, attr.ToFloat32(0))
}
}
return array
}
func (n *Node) GetVec2Array() []*geom.Vector2 {
farray := n.GetFloat32Array()
var vv []*geom.Vector2
for i := 0; i < len(farray)/2; i++ {
vv = append(vv, &geom.Vector2{X: farray[i*2], Y: farray[i*2+1]})
}
return vv
}
func (n *Node) GetVec3Array() []*geom.Vector3 {
farray := n.GetFloat32Array()
var vv []*geom.Vector3
for i := 0; i < len(farray)/3; i++ {
vv = append(vv, &geom.Vector3{X: farray[i*3], Y: farray[i*3+1], Z: farray[i*3+2]})
}
return vv
}
type Attribute struct {
Value interface{}
}
type AttributeList []*Attribute
func (p AttributeList) Get(i int) *Attribute {
if i >= len(p) {
return nil
}
return p[i]
}
func (p AttributeList) ToInt(defvalue int) int {
return p.Get(0).ToInt(defvalue)
}
func (p AttributeList) ToFloat32(defvalue float32) float32 {
return p.Get(0).ToFloat32(defvalue)
}
func (p AttributeList) ToString() string {
return p.Get(0).ToString()
}
func (p AttributeList) ToVector3(x, y, z float32) *geom.Vector3 {
return &geom.Vector3{X: p.Get(0).ToFloat32(x), Y: p.Get(1).ToFloat32(y), Z: p.Get(2).ToFloat32(z)}
}
func (p Attribute) Len() uint {
rv := reflect.ValueOf(p.Value)
if rv.Kind() == reflect.Slice {
return uint(rv.Len())
}
return 0
}
func (p *Attribute) ToInt(defvalue int) int {
return int(p.ToInt64(int64(defvalue)))
}
func (p *Attribute) ToInt64(defvalue int64) int64 {
if p == nil {
return defvalue
}
if v, ok := p.Value.(int8); ok {
return int64(v)
} else if v, ok := p.Value.(int16); ok {
return int64(v)
} else if v, ok := p.Value.(int32); ok {
return int64(v)
} else if v, ok := p.Value.(int64); ok {
return int64(v)
}
return defvalue
}
func (p *Attribute) ToFloat32(defvalue float32) float32 {
return float32(p.ToFloat64(float64(defvalue)))
}
func (p *Attribute) ToFloat64(defvalue float64) float64 {
if p == nil {
return defvalue
}
if v, ok := p.Value.(float32); ok {
return float64(v)
} else if v, ok := p.Value.(float64); ok {
return float64(v)
} else if v, ok := p.Value.(int8); ok {
return float64(v)
} else if v, ok := p.Value.(int16); ok {
return float64(v)
} else if v, ok := p.Value.(int32); ok {
return float64(v)
} else if v, ok := p.Value.(int64); ok {
return float64(v)
}
return defvalue
}
func (p *Attribute) ToString() string {
if p == nil {
return ""
}
if v, ok := p.Value.(string); ok {
return string(v)
} else if v, ok := p.Value.([]byte); ok {
return string(v)
}
return ""
}
func (p *Attribute) ToInt32Array() []int32 {
if p == nil {
return nil
}
var r []int32
switch v := p.Value.(type) {
case []int32:
r = v
case []int8:
for _, v := range v {
r = append(r, int32(v))
}
case []int16:
for _, v := range v {
r = append(r, int32(v))
}
case []int64:
for _, v := range v {
r = append(r, int32(v))
}
}
return r
}
func (p *Attribute) ToFloat32Array() []float32 {
if p == nil {
return nil
}
var r []float32
switch v := p.Value.(type) {
case []float32:
r = v
case []float64:
for _, v := range v {
r = append(r, float32(v))
}
case []int32:
for _, v := range v {
r = append(r, float32(v))
}
case []int64:
for _, v := range v {
r = append(r, float32(v))
}
}
return r
}
func (p *Attribute) String() string {
switch v := p.Value.(type) {
case string:
segments := strings.SplitN(v, "\x00\x01", 2)
if len(segments) == 2 {
segments[0], segments[1] = segments[1], segments[0]
}
return fmt.Sprintf("%q", strings.Join(segments, "::"))
case []byte:
return fmt.Sprintf("\"%v\"", v)
default:
return fmt.Sprint(v)
}
}
// Dump node as fbx ASCII format.
func (n *Node) Dump(w io.Writer, d int, full bool) {
fmt.Fprint(w, strings.Repeat(" ", d), n.Name, ":")
var arrayReplacer = strings.NewReplacer("[", "{\n a:", "]", "\n"+strings.Repeat(" ", d)+"}", " ", ", ")
for i, p := range n.Attributes {
if !full && p.Len() > 16 {
fmt.Fprintf(w, " *%d { SKIPPED }", p.Len())
continue
}
s := p.String()
if p.Len() > 0 {
s = fmt.Sprint("*", p.Len(), " ", arrayReplacer.Replace(s))
}
if i == 0 {
fmt.Fprint(w, " ", s)
} else {
fmt.Fprint(w, ", ", s)
}
}
if len(n.Children) > 0 || len(n.Attributes) == 0 {
fmt.Fprintln(w, " {")
for _, c := range n.Children {
c.Dump(w, d+1, full)
}
fmt.Fprintln(w, strings.Repeat(" ", d)+"}")
} else {
fmt.Fprintln(w, "")
}
}