-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
size.go
129 lines (125 loc) · 4.72 KB
/
size.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
package binary
import (
"image/color"
"reflect"
"github.com/qmuntal/gltf"
)
// SizeOfElement returns the size, in bytes, of an element.
// The element size may not be (component size) * (number of components),
// as some of the elements are tightly packed in order to ensure
// that they are aligned to 4-byte boundaries.
func SizeOfElement(c gltf.ComponentType, t gltf.AccessorType) uint32 {
// special cases
switch {
case (t == gltf.AccessorVec3 || t == gltf.AccessorVec2) && (c == gltf.ComponentByte || c == gltf.ComponentUbyte):
return 4
case t == gltf.AccessorVec3 && (c == gltf.ComponentShort || c == gltf.ComponentUshort):
return 8
case t == gltf.AccessorMat2 && (c == gltf.ComponentByte || c == gltf.ComponentUbyte):
return 8
case t == gltf.AccessorMat3 && (c == gltf.ComponentByte || c == gltf.ComponentUbyte):
return 12
case t == gltf.AccessorMat3 && (c == gltf.ComponentShort || c == gltf.ComponentUshort):
return 24
}
return c.ByteSize() * t.Components()
}
// Type returns the associated glTF type data.
// If data is an slice, it also returns the length of the slice.
// If data does not have an associated glTF type length will be 0.
func Type(data interface{}) (c gltf.ComponentType, t gltf.AccessorType, length uint32) {
v := reflect.ValueOf(data)
switch v.Kind() {
case reflect.Slice:
length = uint32(v.Len())
}
switch data.(type) {
case []int8, int8:
c, t = gltf.ComponentByte, gltf.AccessorScalar
case [][2]int8, [2]int8:
c, t = gltf.ComponentByte, gltf.AccessorVec2
case [][3]int8, [3]int8:
c, t = gltf.ComponentByte, gltf.AccessorVec3
case [][4]int8, [4]int8:
c, t = gltf.ComponentByte, gltf.AccessorVec4
case [][2][2]int8, [2][2]int8:
c, t = gltf.ComponentByte, gltf.AccessorMat2
case [][3][3]int8, [3][3]int8:
c, t = gltf.ComponentByte, gltf.AccessorMat3
case [][4][4]int8, [4][4]int8:
c, t = gltf.ComponentByte, gltf.AccessorMat4
case []uint8, uint8:
c, t = gltf.ComponentUbyte, gltf.AccessorScalar
case [][2]uint8, [2]uint8:
c, t = gltf.ComponentUbyte, gltf.AccessorVec2
case [][3]uint8, [3]uint8:
c, t = gltf.ComponentUbyte, gltf.AccessorVec3
case []color.RGBA, color.RGBA, [][4]uint8, [4]uint8:
c, t = gltf.ComponentUbyte, gltf.AccessorVec4
case [][2][2]uint8, [2][2]uint8:
c, t = gltf.ComponentUbyte, gltf.AccessorMat2
case [][3][3]uint8, [3][3]uint8:
c, t = gltf.ComponentUbyte, gltf.AccessorMat3
case [][4][4]uint8, [4][4]uint8:
c, t = gltf.ComponentUbyte, gltf.AccessorMat4
case []int16, int16:
c, t = gltf.ComponentShort, gltf.AccessorScalar
case [][2]int16, [2]int16:
c, t = gltf.ComponentShort, gltf.AccessorVec2
case [][3]int16, [3]int16:
c, t = gltf.ComponentShort, gltf.AccessorVec3
case [][4]int16, [4]int16:
c, t = gltf.ComponentShort, gltf.AccessorVec4
case [][2][2]int16, [2][2]int16:
c, t = gltf.ComponentShort, gltf.AccessorMat2
case [][3][3]int16, [3][3]int16:
c, t = gltf.ComponentShort, gltf.AccessorMat3
case [][4][4]int16, [4][4]int16:
c, t = gltf.ComponentShort, gltf.AccessorMat4
case []uint16, uint16:
c, t = gltf.ComponentUshort, gltf.AccessorScalar
case [][2]uint16, [2]uint16:
c, t = gltf.ComponentUshort, gltf.AccessorVec2
case [][3]uint16, [3]uint16:
c, t = gltf.ComponentUshort, gltf.AccessorVec3
case []color.RGBA64, color.RGBA64, [][4]uint16, [4]uint16:
c, t = gltf.ComponentUshort, gltf.AccessorVec4
case [][2][2]uint16, [2][2]uint16:
c, t = gltf.ComponentUshort, gltf.AccessorMat2
case [][3][3]uint16, [3][3]uint16:
c, t = gltf.ComponentUshort, gltf.AccessorMat3
case [][4][4]uint16, [4][4]uint16:
c, t = gltf.ComponentUshort, gltf.AccessorMat4
case []uint32, uint32:
c, t = gltf.ComponentUint, gltf.AccessorScalar
case [][2]uint32, [2]uint32:
c, t = gltf.ComponentUint, gltf.AccessorVec2
case [][3]uint32, [3]uint32:
c, t = gltf.ComponentUint, gltf.AccessorVec3
case [][4]uint32, [4]uint32:
c, t = gltf.ComponentUint, gltf.AccessorVec4
case [][2][2]uint32, [2][2]uint32:
c, t = gltf.ComponentUint, gltf.AccessorMat2
case [][3][3]uint32, [3][3]uint32:
c, t = gltf.ComponentUint, gltf.AccessorMat3
case [][4][4]uint32, [4][4]uint32:
c, t = gltf.ComponentUint, gltf.AccessorMat4
case []float32, float32:
c, t = gltf.ComponentFloat, gltf.AccessorScalar
case [][2]float32, [2]float32:
c, t = gltf.ComponentFloat, gltf.AccessorVec2
case []gltf.RGB, gltf.RGB, [][3]float32, [3]float32:
c, t = gltf.ComponentFloat, gltf.AccessorVec3
case []gltf.RGBA, gltf.RGBA, [][4]float32, [4]float32:
c, t = gltf.ComponentFloat, gltf.AccessorVec4
case [][2][2]float32, [2][2]float32:
c, t = gltf.ComponentFloat, gltf.AccessorMat2
case [][3][3]float32, [3][3]float32:
c, t = gltf.ComponentFloat, gltf.AccessorMat3
case [][4][4]float32, [4][4]float32:
c, t = gltf.ComponentFloat, gltf.AccessorMat4
default:
length = 0
}
return
}