gltf.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gltf
  5. // GLTF is the root object for a glTF asset
  6. type GLTF struct {
  7. ExtensionsUsed []string
  8. ExtensionsRequired []string
  9. Accessors []Accessor
  10. Animations []Animation
  11. Asset Asset
  12. Buffers []Buffer
  13. BufferViews []BufferView
  14. Cameras []Camera
  15. Images []Image
  16. Materials []Material
  17. Meshes []Mesh
  18. Nodes []Node
  19. Samplers []Sampler
  20. Scene *int
  21. Scenes []Scene
  22. Skins []Skin
  23. Textures []Texture
  24. Extensions map[string]interface{}
  25. Extras interface{}
  26. path string // file path for resources
  27. data []byte // binary file Chunk 1 data
  28. }
  29. // Accessor describes a view into a BufferView
  30. type Accessor struct {
  31. BufferView *int // The index of the buffer view
  32. ByteOffset *int // The offset relative to the start of the BufferView in bytes
  33. ComponentType int // The datatype of components in the attribute
  34. Normalized bool // Specifies whether integer data values should be normalized
  35. Count int // The number of attributes referenced by this accessor
  36. Type string // Specifies if the attribute is a scalar, vector or matrix
  37. Max []float32 // Maximum value of each component in this attribute
  38. Min []float32 // Minimum value of each component in this attribute
  39. Sparse *Sparse // Sparse storage attribute that deviates from their initialization value
  40. Name string // The user-defined name of this object
  41. Extensions map[string]interface{} // Dictionary object with extension specific objects
  42. Extras interface{} // Application-specific data
  43. }
  44. // A Keyframe animation
  45. type Animation struct {
  46. Channels []Channel // An array of Channels
  47. Samplers []Sampler // An array of samplers that combines input and output accessors with an interpolation algorithm to define a keyframe graph
  48. Name string // The user-defined name of this object
  49. Extensions map[string]interface{} // Dictionary object with extension specific objects
  50. Extras interface{} // Application-specific data
  51. }
  52. // Combines input and output accessors with an interpolation algorithm to define a keyframe graph
  53. type AnimationSampler struct {
  54. Input int // The index of the accessor containing keyframe input values
  55. Interpolation string // Interpolation algorithm
  56. Output int // The index of an accessor containing keyframe output values
  57. Extensions map[string]interface{} // Dictionary object with extension specific objects
  58. Extras interface{} // Application-specific data
  59. }
  60. // Metadata about the glTF asset.
  61. type Asset struct {
  62. Copyright string // A copyright message suitable for display to credit the content creator
  63. Generator string // Tool that generated this glTF model. Useful for debugging
  64. Version string // The glTF version that this asset targets
  65. MinVersion string // The minimum glTF version that this asset targets
  66. Extensions map[string]interface{} // Dictionary object with extension specific objects
  67. Extras interface{} // Application-specific data
  68. }
  69. // A Buffer points to binary geometry, animation or skins
  70. type Buffer struct {
  71. Uri string // The URI of the buffer
  72. ByteLength int // The length of the buffer in bytes
  73. Name string // The user-defined name of this object
  74. Extensions map[string]interface{} // Dictionary object with extension specific objects
  75. Extras interface{} // Application-specific data
  76. data []byte // cached buffer data
  77. }
  78. // A view into a buffer generally representing a subset of the buffer.
  79. type BufferView struct {
  80. Buffer int // The index of the buffer
  81. ByteOffset *int // The offset into the buffer in bytes
  82. ByteLength int // The length of the buffer view in bytes
  83. ByteStride *int // The stride in bytes
  84. Target *int // The target that the GPU buffer should be bound to
  85. Name string // The user-defined name of this object
  86. Extensions map[string]interface{} // Dictionary object with extension specific objects
  87. Extras interface{} // Application-specific data
  88. }
  89. // A camera's projection.
  90. type Camera struct {
  91. Orthographic *Orthographic // An orthographic camera containing properties to create an orthographic projection matrix
  92. Perspective *Perspective // A perspective camera containing properties to create a perspective projection matrix
  93. Type string // Specifies if the camera uses a perspective or orthographic projection
  94. Name string // The user-defined name of this object
  95. Extensions map[string]interface{} // Dictionary object with extension specific objects
  96. Extras interface{} // Application-specific data
  97. }
  98. // Targets an animation's sampler at a node's property
  99. type Channel struct {
  100. Sampler int // The index of a sampler in this animation used to compute the value of the target
  101. Target Target // The index of the node and TRS property to target
  102. Extensions map[string]interface{} // Dictionary object with extension specific objects
  103. Extras interface{} // Application-specific data
  104. }
  105. // Image data used to create a texture
  106. type Image struct {
  107. Uri string // The URI of the image
  108. MimeType string // The image's MIME type
  109. BufferView *int // The index of the BufferView the contains the image
  110. Name string // The user-defined name of this object
  111. Extensions map[string]interface{} // Dictionary object with extension specific objects
  112. Extras interface{} // Application-specific data
  113. }
  114. // Indices of those attributes that deviate from their initialization value.
  115. type Indices struct {
  116. BufferView int // The index of the BufferView with sparse indices
  117. ByteOffset int // The offset relative to the start of the BufferView in bytes
  118. ComponentType int // The indices data type
  119. Extensions map[string]interface{} // Dictionary object with extension specific objects
  120. Extras interface{} // Application-specific data
  121. }
  122. // Material describes the material appearance of a primitive
  123. type Material struct {
  124. Name string // The user-defined name of this object
  125. Extensions map[string]interface{} // Dictionary object with extension specific objects
  126. Extras interface{} // Application-specific data
  127. PbrMetallicRoughness *PbrMetallicRoughness
  128. NormalTexture *NormalTextureInfo // The normal map texture
  129. OcclusionTexture *OcclusionTextureInfo // The occlusion map texture
  130. EmissiveTexture *TextureInfo // The emissive map texture
  131. EmissiveFactor [3]float32 // The emissive color of the material
  132. AlphaMode string // The alpha rendering mode of the material
  133. AlphaCutoff float32 // The alpha cutoff value of the material
  134. DoubleSided bool // Specifies whether the material is double sided
  135. }
  136. // Mesh is a set of primitives to be rendered.
  137. type Mesh struct {
  138. Primitives []Primitive // Array of primitives
  139. Weights []float32 // Array of weights to be applied to the Morph Targets
  140. Name string // The user-define name of this object
  141. Extensions map[string]interface{} // Dictionary object with extension specific objects
  142. Extras interface{} // Application-specific data
  143. }
  144. // A Node in the hierarchy
  145. type Node struct {
  146. Camera *int // Index of the camera referenced by this node
  147. Children []int // The indices of this node's children
  148. Skin *int // The index of the skin referenced by this node
  149. Matrix *[16]float32 // Floating point 4x4 transformation matrix in column-major order
  150. Mesh *int // The index of the mesh in this node
  151. Rotation *[4]float32 // The node's unit quaternion rotation in the order x,y,z,w
  152. Scale *[3]float32 // The node's non-uniform scale
  153. Translation *[3]float32 // The node's translation
  154. Weights []float32 // The weight's of the instantiated Morph Target
  155. Name string // User-defined name of this object
  156. Extensions map[string]interface{} // Dictionary object with extension specific objects
  157. Extras interface{} // Application-specific data
  158. }
  159. // Reference to a texture
  160. type NormalTextureInfo struct {
  161. Index int // The index of the texture
  162. TexCoord int // The set index of texture's TEXCOORD attribute used for texture coordinate mapping
  163. Scale float32 // The scalar multiplier applied to each normal vector of the normal texture
  164. Extensions map[string]interface{} // Dictionary object with extension specific objects
  165. Extras interface{} // Application-specific data
  166. }
  167. // Reference to a texture
  168. type OcclusionTextureInfo struct {
  169. Index int // The index of the texture
  170. TexCoord int // The set index of texture's TEXCOORD attribute used for texture coordinate mapping
  171. Strength float32 // A scalar multiplier controlling the amount of occlusion applied
  172. Extensions map[string]interface{} // Dictionary object with extension specific objects
  173. Extras interface{} // Application-specific data
  174. }
  175. // An orthographic camera containing properties to create an orthographic projection matrix
  176. type Orthographic struct {
  177. Xmag float32 // The floating-point horizontal magnification of the view
  178. Ymag float32 // The floating-point vertical magnification of the view
  179. Zfar float32 // The floating-point distance to the far clipping plane. zfar must be greater than znear
  180. Znear float32 // The floating-point distance to the near clipping plane
  181. Extensions map[string]interface{} // Dictionary object with extension specific objects
  182. Extras interface{} // Application-specific data
  183. }
  184. // A set of parameter values that are used to define the metallic-roughness material model
  185. // from Physically-Based Rendering (PBR) methodology.
  186. type PbrMetallicRoughness struct {
  187. BaseColorFactor [4]float32 // The material's base color factor
  188. BaseColorTexture *TextureInfo // The base color texture
  189. MetallicFactor float32 // The metalness of the material
  190. RoughnessFactor float32 // The roughness of the material
  191. MetallicRoughnessTexture *TextureInfo // The metallic-roughness texture
  192. Extensions map[string]interface{} // Dictionary object with extension specific objects
  193. Extras interface{} // Application-specific data
  194. }
  195. // A perspective camera containing properties to create a perspective projection matrix
  196. type Perspective struct {
  197. AspectRatio *float32 // The floating-point aspect ratio of the field of view
  198. Yfov float32 // The floating-point vertical field of view in radians
  199. Zfar *float32 // The floating-point distance to the far clipping plane
  200. Znear float32 // The floating-point distance to the near clipping plane.
  201. Extensions map[string]interface{} // Dictionary object with extension specific objects
  202. Extras interface{} // Application-specific data
  203. }
  204. // Geometry to be rendered with the given material
  205. type Primitive struct {
  206. Attributes map[string]int // A dictionary object, where each key corresponds to mesh attribute semantic and each value is the index of the accessor containing attribute's data
  207. Indices *int // The index of the accessor that contains the indices
  208. Material *int // The index of the material to apply to this primitive when rendering
  209. Mode *int // The type of primitive to render
  210. Targets []map[string]int // An array of Morph Targets
  211. Extensions map[string]interface{} // Dictionary object with extension specific objects
  212. Extras interface{} // Application-specific data
  213. }
  214. // Texture sampler properties for filtering and wrapping modes
  215. type Sampler struct {
  216. MagFilter *int // Magnification filter
  217. MinFilter *int // Minification filter
  218. WrapS *int // s coordinate wrapping mode
  219. WrapT *int // t coordinate wrapping mode
  220. Name string // The user-define name for this object
  221. Extensions map[string]interface{} // Dictionary object with extension specific objects
  222. Extras interface{} // Application-specific data
  223. }
  224. // The root nodes of a scene
  225. type Scene struct {
  226. Nodes []int // The indices of each root node
  227. Name string // The user-define name for this object
  228. Extensions map[string]interface{} // Dictionary object with extension specific objects
  229. Extras interface{} // Application-specific data
  230. }
  231. // Joints and matrices defining a skin.
  232. type Skin struct {
  233. InverseBindMatrices int // The index of the accessor containing the 4x4 inverse-bind matrices
  234. Skeleton int // The index of the node used as a skeleton root
  235. Joints []int // Indices of skeleton nodes, used as joints in this skin
  236. Name string // The user-define name for this object
  237. Extensions map[string]interface{} // Dictionary object with extension specific objects
  238. Extras interface{} // Application-specific data
  239. }
  240. // Sparse storage of attributes that deviate from their initialization value.
  241. type Sparse struct {
  242. Count int // Number of entries stored in the sparse array
  243. //Indices
  244. //Values
  245. Extensions map[string]interface{} // Dictionary object with extension specific objects
  246. Extras interface{} // Application-specific data
  247. }
  248. // The index of the node and TRS property than an animation channel targets
  249. type Target struct {
  250. Node int // The index of the node to target
  251. Path string // The name of the node's TRS property to modify
  252. Extensions map[string]interface{} // Dictionary object with extension specific objects
  253. Extras interface{} // Application-specific data
  254. }
  255. // A texture and its sampler.
  256. type Texture struct {
  257. Sampler int // The index of the sampler used by this texture
  258. Source int // The index of the image used by this texture
  259. Name string // The user-define name for this object
  260. Extensions map[string]interface{} // Dictionary object with extension specific objects
  261. Extras interface{} // Application-specific data
  262. }
  263. // Reference to a texture.
  264. type TextureInfo struct {
  265. Index int // The index of the texture
  266. TexCoord int // The set index of texture's TEXCOORD attribute used for texture coordinate mapping
  267. Extensions map[string]interface{} // Dictionary object with extension specific objects
  268. Extras interface{} // Application-specific data
  269. }
  270. // Array of size accessor.sparse.count times number of components storing
  271. // the displaced accessor attributes pointed by accessor.sparse.indices.
  272. type Values struct {
  273. BufferView int // The index of the bufferView with sparse values
  274. ByteOffset int // he offset relative to the start of the bufferView in bytes
  275. Extensions map[string]interface{} // Dictionary object with extension specific objects
  276. Extras interface{} // Application-specific data
  277. }
  278. const (
  279. POINTS = 0
  280. LINES = 1
  281. LINE_LOOP = 2
  282. LINE_STRIP = 3
  283. TRIANGLES = 4
  284. TRIANGLE_STRIP = 5
  285. TRIANGLE_FAN = 6
  286. ARRAY_BUFFER = 34962
  287. ELEMENT_ARRAY_BUFFER = 34963
  288. NEAREST = 9728
  289. LINEAR = 9729
  290. NEAREST_MIPMAP_NEAREST = 9984
  291. LINEAR_MIPMAP_NEAREST = 9985
  292. NEAREST_MIPMAP_LINEAR = 9986
  293. LINEAR_MIPMAP_LINEAR = 9987
  294. CLAMP_TO_EDGE = 33071
  295. MIRRORED_REPEAT = 33648
  296. REPEAT = 10497
  297. UNSIGNED_BYTE = 5121
  298. UNSIGNED_SHORT = 5123
  299. UNSIGNED_INT = 5125
  300. FLOAT = 5126
  301. )
  302. const (
  303. POSITION = "POSITION"
  304. NORMAL = "NORMAL"
  305. TANGENT = "TANGENT"
  306. TEXCOORD_0 = "TEXCOORD_0"
  307. TEXCOORD_1 = "TEXCOORD_1"
  308. COLOR_0 = "COLOR_0"
  309. JOINTS_0 = "JOINTS_0"
  310. WEIGHTS_0 = "WEIGHTS_0"
  311. SCALAR = "SCALAR"
  312. VEC2 = "VEC2"
  313. VEC3 = "VEC3"
  314. VEC4 = "VEC4"
  315. MAT2 = "MAT2"
  316. MAT3 = "MAT3"
  317. MAT4 = "MAT4"
  318. )
  319. // TypeSizes maps the attribute type to the number of its elements
  320. var TypeSizes = map[string]int{
  321. SCALAR: 1,
  322. VEC2: 2,
  323. VEC3: 3,
  324. VEC4: 4,
  325. MAT2: 4,
  326. MAT3: 9,
  327. MAT4: 16,
  328. }
  329. type GLB struct {
  330. Header GLBHeader
  331. JSON GLBChunk
  332. Data GLBChunk
  333. }
  334. type GLBHeader struct {
  335. Magic uint32
  336. Version uint32
  337. Length uint32
  338. }
  339. type GLBChunk struct {
  340. Length uint32
  341. Type uint32
  342. }
  343. const (
  344. GLBMagic = 0x46546C67
  345. GLBJson = 0x4E4F534A
  346. GLBBin = 0x004E4942
  347. )