Documentation ¶
Overview ¶
Package binary implements simple translation between numbers and byte sequences.
Numbers are translated by reading and writing fixed-size values. A fixed-size value is either a fixed-size arithmetic type (bool, int8, uint8, int16, float32, complex64, ...) or an array or struct containing only fixed-size values.
Even though the defined structs don't implement a common interface, they all follow the same `generic` interface, and the only difference is the type of the value:
type Component interface { Scalar([]byte) interface{} Vec2([]byte) [2]interface{} Vec3([]byte) [2]interface{} Vec4([]byte) [2]interface{} Mat2([]byte) [2][2]interface{} Mat3([]byte) [3][3]interface{} Mat4([]byte) [4][4]interface{} PutScalar([]byte, interface{}) PutVec2([]byte, [2]interface{}) PutVec3([]byte, [3]interface{}) PutVec4([]byte, [4]interface{}) PutMat2([]byte, [2][2]interface{}) PutMat3([]byte, [3][3]interface{}) PutMat4([]byte, [4][4]interface{}) }
This package favors simplicity and compliance over efficiency, but it is still an order of magnitude more performant than using the built-int `binary` package, as it implements fast paths for basic glTF types and slices.
Index ¶
- Variables
- func MakeSlice(c gltf.ComponentType, t gltf.AccessorType, count uint32) interface{}
- func Read(b []byte, byteStride uint32, data interface{}) error
- func SizeOfElement(c gltf.ComponentType, t gltf.AccessorType) uint32
- func Type(data interface{}) (c gltf.ComponentType, t gltf.AccessorType, count uint32)
- func Write(b []byte, stride uint32, data interface{}) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Byte byteComponent
Byte is the byte implementation of Component.
var Float floatComponent
Float is the float implementation of Component.
var Short shortComponent
Short is the byte short of Component.
var Ubyte ubyteComponent
Ubyte is the usnigned byte implementation of Component.
var Uint uintComponent
Uint is the unsigned int implementation of Component.
var Ushort ushortComponent
Ushort is the usnigned short implementation of Component.
Functions ¶
func MakeSlice ¶ added in v0.17.0
func MakeSlice(c gltf.ComponentType, t gltf.AccessorType, count uint32) interface{}
MakeSlice returns the slice type associated with c and t and with the given element count. For example, if c is gltf.ComponentFloat and t is gltf.AccessorVec3 then MakeSlice(c, t, 5) is equivalent to make([][3]float32, 5).
func Read ¶
Read reads structured binary data from b into data. byteStride can be zero for non-interleaved buffer views.
Data should be a slice of glTF predefined fixed-size types. If data length is greater than the length of b, Read returns io.ErrShortBuffer.
Example ¶
package main import ( "fmt" "github.com/qmuntal/gltf" "github.com/qmuntal/gltf/binary" ) func main() { // Allocate data indices := make([]uint8, 18) vertices := make([][3]float32, 5) // Define buffer b := []byte{0, 1, 2, 3, 1, 0, 0, 2, 3, 1, 4, 2, 4, 3, 2, 4, 1, 3, 0, 0, 44, 66, 0, 0, 44, 66, 0, 0, 0, 0, 0, 0, 166, 66, 0, 0, 44, 66, 0, 0, 0, 0, 0, 0, 124, 66, 0, 0, 124, 66, 0, 0, 32, 66, 0, 0, 44, 66, 0, 0, 166, 66, 0, 0, 0, 0, 0, 0, 166, 66, 0, 0, 166, 66, 0, 0, 0, 0} sizeIndices := uint32(len(indices)) * binary.SizeOfElement(gltf.ComponentUbyte, gltf.AccessorScalar) // Write binary.Read(b, 0, indices) binary.Read(b[sizeIndices:], 0, vertices) fmt.Println(indices) fmt.Println(vertices) }
Output: [0 1 2 3 1 0 0 2 3 1 4 2 4 3 2 4 1 3] [[43 43 0] [83 43 0] [63 63 40] [43 83 0] [83 83 0]]
func SizeOfElement ¶
func SizeOfElement(c gltf.ComponentType, t gltf.AccessorType) uint32
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 Type ¶ added in v0.13.0
func Type(data interface{}) (c gltf.ComponentType, t gltf.AccessorType, count uint32)
Type returns the associated glTF type data. It panics if data is not an slice.
func Write ¶
Write writes the binary representation of data into b. If stride is diferent than zero data will be interleaved.
Data must be a slice of glTF predefined fixed-size types, else it fallbacks to `encoding/binary.Write`.
Example ¶
package main import ( "fmt" "github.com/qmuntal/gltf" "github.com/qmuntal/gltf/binary" ) func main() { // Define data indices := []uint8{0, 1, 2, 3, 1, 0, 0, 2, 3, 1, 4, 2, 4, 3, 2, 4, 1, 3} vertices := [][3]float32{{43, 43, 0}, {83, 43, 0}, {63, 63, 40}, {43, 83, 0}, {83, 83, 0}} // Allocate buffer sizeIndices := uint32(len(indices)) * binary.SizeOfElement(gltf.ComponentUbyte, gltf.AccessorScalar) sizeVertices := uint32(len(vertices)) * binary.SizeOfElement(gltf.ComponentFloat, gltf.AccessorVec3) b := make([]byte, sizeIndices+sizeVertices) // Write binary.Write(b, 0, indices) binary.Write(b[sizeIndices:], 0, vertices) fmt.Print(b) }
Output: [0 1 2 3 1 0 0 2 3 1 4 2 4 3 2 4 1 3 0 0 44 66 0 0 44 66 0 0 0 0 0 0 166 66 0 0 44 66 0 0 0 0 0 0 124 66 0 0 124 66 0 0 32 66 0 0 44 66 0 0 166 66 0 0 0 0 0 0 166 66 0 0 166 66 0 0 0 0]
Types ¶
This section is empty.