/
writer.go
750 lines (612 loc) · 18.2 KB
/
writer.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
package gltf
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"image/color"
"io"
"math"
"github.com/EliCDavis/bitlib"
"github.com/EliCDavis/iter"
"github.com/EliCDavis/polyform/modeling"
"github.com/EliCDavis/polyform/modeling/animation"
"github.com/EliCDavis/vector/vector2"
"github.com/EliCDavis/vector/vector3"
"github.com/EliCDavis/vector/vector4"
)
type Writer struct {
buf *bytes.Buffer
bitW *bitlib.Writer
bytesWritten int
accessors []Accessor
bufferViews []BufferView
meshes []Mesh
nodes []Node
materials []Material
skins []Skin
animations []Animation
textures []Texture
images []Image
samplers []Sampler
textureInfos []TextureInfo
scene []int
// Extension Stuff
lights []KHR_LightsPunctual
extensionsUsed map[string]bool
}
func NewWriter() *Writer {
buf := &bytes.Buffer{}
return &Writer{
buf: buf,
bitW: bitlib.NewWriter(buf, binary.LittleEndian),
bufferViews: make([]BufferView, 0),
accessors: make([]Accessor, 0),
nodes: make([]Node, 0),
meshes: make([]Mesh, 0),
materials: make([]Material, 0),
skins: make([]Skin, 0),
animations: make([]Animation, 0),
// Extensions
lights: make([]KHR_LightsPunctual, 0),
extensionsUsed: make(map[string]bool),
}
}
func (w Writer) WriteVector4AsFloat32(v vector4.Float64) {
w.bitW.Float32(float32(v.X()))
w.bitW.Float32(float32(v.Y()))
w.bitW.Float32(float32(v.Z()))
w.bitW.Float32(float32(v.W()))
}
func (w Writer) WriteVector4AsByte(v vector4.Float64) {
w.bitW.Byte(uint8(v.X()))
w.bitW.Byte(uint8(v.Y()))
w.bitW.Byte(uint8(v.Z()))
w.bitW.Byte(uint8(v.W()))
}
func (w Writer) WriteVector3AsFloat32(v vector3.Float64) {
w.bitW.Float32(float32(v.X()))
w.bitW.Float32(float32(v.Y()))
w.bitW.Float32(float32(v.Z()))
}
func (w Writer) WriteVector3AsByte(v vector3.Float64) {
w.bitW.Byte(uint8(v.X()))
w.bitW.Byte(uint8(v.Y()))
w.bitW.Byte(uint8(v.Z()))
}
func (w Writer) WriteVector2AsFloat32(v vector2.Float64) {
w.bitW.Float32(float32(v.X()))
w.bitW.Float32(float32(v.Y()))
}
func (w Writer) WriteVector2AsByte(v vector2.Float64) {
w.bitW.Byte(uint8(v.X()))
w.bitW.Byte(uint8(v.Y()))
}
func (w *Writer) WriteVector4(accessorComponentType AccessorComponentType, data *iter.ArrayIterator[vector4.Float64]) {
accessorType := AccessorType_VEC4
min := vector4.Fill(math.MaxFloat64)
max := vector4.Fill(-math.MaxFloat64)
if accessorComponentType == AccessorComponentType_FLOAT {
for i := 0; i < data.Len(); i++ {
v := data.At(i)
min = vector4.Min(min, v)
max = vector4.Max(max, v)
w.WriteVector4AsFloat32(v)
}
}
if accessorComponentType == AccessorComponentType_UNSIGNED_BYTE {
for i := 0; i < data.Len(); i++ {
v := data.At(i)
min = vector4.Min(min, v)
max = vector4.Max(max, v)
w.WriteVector4AsByte(v)
}
}
minArr := []float64{min.X(), min.Y(), min.Z(), min.W()}
maxArr := []float64{max.X(), max.Y(), max.Z(), max.W()}
datasize := data.Len() * 4 * accessorComponentType.Size()
w.accessors = append(w.accessors, Accessor{
BufferView: ptrI(len(w.bufferViews)),
ComponentType: accessorComponentType,
Type: accessorType,
Count: data.Len(),
Min: minArr,
Max: maxArr,
})
w.bufferViews = append(w.bufferViews, BufferView{
Buffer: 0,
ByteOffset: w.bytesWritten,
ByteLength: datasize,
Target: ARRAY_BUFFER,
})
w.bytesWritten += datasize
}
func (w *Writer) WriteVector3(accessorComponentType AccessorComponentType, data *iter.ArrayIterator[vector3.Float64]) {
accessorType := AccessorType_VEC3
min := vector3.Fill(math.MaxFloat64)
max := vector3.Fill(-math.MaxFloat64)
if accessorComponentType == AccessorComponentType_FLOAT {
for i := 0; i < data.Len(); i++ {
v := data.At(i)
w.WriteVector3AsFloat32(v)
// Don't contaminate min/max with NaNs
if v.ContainsNaN() {
continue
}
min = vector3.Min(min, v)
max = vector3.Max(max, v)
}
}
if accessorComponentType == AccessorComponentType_UNSIGNED_BYTE {
for i := 0; i < data.Len(); i++ {
v := data.At(i)
w.WriteVector3AsByte(v)
// Don't contaminate min/max with NaNs
if v.ContainsNaN() {
continue
}
min = vector3.Min(min, v)
max = vector3.Max(max, v)
}
}
minArr := []float64{min.X(), min.Y(), min.Z()}
maxArr := []float64{max.X(), max.Y(), max.Z()}
datasize := data.Len() * 3 * accessorComponentType.Size()
w.accessors = append(w.accessors, Accessor{
BufferView: ptrI(len(w.bufferViews)),
ComponentType: accessorComponentType,
Type: accessorType,
Count: data.Len(),
Min: minArr,
Max: maxArr,
})
w.bufferViews = append(w.bufferViews, BufferView{
Buffer: 0,
ByteOffset: w.bytesWritten,
ByteLength: datasize,
Target: ARRAY_BUFFER,
})
w.bytesWritten += datasize
}
func (w *Writer) WriteVector2(accessorComponentType AccessorComponentType, data *iter.ArrayIterator[vector2.Float64]) {
accessorType := AccessorType_VEC2
min := vector2.Fill(math.MaxFloat64)
max := vector2.Fill(-math.MaxFloat64)
if accessorComponentType == AccessorComponentType_FLOAT {
for i := 0; i < data.Len(); i++ {
v := data.At(i)
w.WriteVector2AsFloat32(v)
// Don't contaminate min/max with NaNs
if v.ContainsNaN() {
continue
}
min = vector2.Min(min, v)
max = vector2.Max(max, v)
}
}
if accessorComponentType == AccessorComponentType_UNSIGNED_BYTE {
for i := 0; i < data.Len(); i++ {
v := data.At(i)
w.WriteVector2AsByte(v)
// Don't contaminate min/max with NaNs
if v.ContainsNaN() {
continue
}
min = vector2.Min(min, v)
max = vector2.Max(max, v)
}
}
minArr := []float64{min.X(), min.Y()}
maxArr := []float64{max.X(), max.Y()}
datasize := data.Len() * 2 * accessorComponentType.Size()
w.accessors = append(w.accessors, Accessor{
BufferView: ptrI(len(w.bufferViews)),
ComponentType: accessorComponentType,
Type: accessorType,
Count: data.Len(),
Min: minArr,
Max: maxArr,
})
w.bufferViews = append(w.bufferViews, BufferView{
Buffer: 0,
ByteOffset: w.bytesWritten,
ByteLength: datasize,
Target: ARRAY_BUFFER,
})
w.bytesWritten += datasize
}
func (w *Writer) WriteIndices(indices *iter.ArrayIterator[int], attributeSize int) {
indiceSize := indices.Len()
componentType := AccessorComponentType_UNSIGNED_INT
if attributeSize > math.MaxUint16 {
for i := 0; i < indices.Len(); i++ {
w.bitW.UInt32(uint32(indices.At(i)))
}
indiceSize *= 4
} else {
for i := 0; i < indices.Len(); i++ {
w.bitW.UInt16(uint16(indices.At(i)))
}
indiceSize *= 2
componentType = AccessorComponentType_UNSIGNED_SHORT
}
w.accessors = append(w.accessors, Accessor{
BufferView: ptrI(len(w.bufferViews)),
ComponentType: componentType,
Type: AccessorType_SCALAR,
Count: indices.Len(),
})
w.bufferViews = append(w.bufferViews, BufferView{
Buffer: 0,
ByteOffset: w.bytesWritten,
ByteLength: indiceSize,
Target: ELEMENT_ARRAY_BUFFER,
})
w.bytesWritten += indiceSize
}
func rgbaToFloatArr(c color.Color) [4]float64 {
r, g, b, a := c.RGBA()
return [4]float64{
float64(r) / math.MaxUint16,
float64(g) / math.MaxUint16,
float64(b) / math.MaxUint16,
float64(a) / math.MaxUint16,
}
}
func rgbToFloatArr(c color.Color) [3]float64 {
r, g, b, _ := c.RGBA()
return [3]float64{
float64(r) / math.MaxUint16,
float64(g) / math.MaxUint16,
float64(b) / math.MaxUint16,
}
}
func (w *Writer) AddTexture(mat PolyformTexture) *TextureInfo {
newTex := &TextureInfo{Index: len(w.textures)}
w.textures = append(w.textures, Texture{
Sampler: ptrI(len(w.samplers)),
Source: ptrI(len(w.images)),
})
w.images = append(w.images, Image{
URI: mat.URI,
})
w.samplers = append(w.samplers, Sampler{})
return newTex
}
func (w *Writer) AddMaterial(mat PolyformMaterial) *int {
var pbr *PbrMetallicRoughness = &PbrMetallicRoughness{
BaseColorFactor: &[4]float64{1, 1, 1, 1},
}
extensions := make(map[string]any)
if mat.PbrMetallicRoughness != nil {
polyPBR := mat.PbrMetallicRoughness
pbr.MetallicFactor = polyPBR.MetallicFactor
pbr.RoughnessFactor = polyPBR.RoughnessFactor
if polyPBR.BaseColorFactor != nil {
factor := rgbaToFloatArr(polyPBR.BaseColorFactor)
pbr.BaseColorFactor = &factor
}
if polyPBR.BaseColorTexture != nil {
pbr.BaseColorTexture = w.AddTexture(*polyPBR.BaseColorTexture)
}
if polyPBR.MetallicRoughnessTexture != nil {
pbr.MetallicRoughnessTexture = w.AddTexture(*polyPBR.MetallicRoughnessTexture)
}
}
for _, ext := range mat.Extensions {
id := ext.ExtensionID()
extensions[id] = ext.ToExtensionData(w)
w.extensionsUsed[id] = true
}
m := Material{
ChildOfRootProperty: ChildOfRootProperty{
Name: mat.Name,
Property: Property{
Extras: mat.Extras,
Extensions: extensions,
},
},
PbrMetallicRoughness: pbr,
}
if mat.NormalTexture != nil {
m.NormalTexture = &NormalTexture{
TextureInfo: *w.AddTexture(mat.NormalTexture.PolyformTexture),
Scale: mat.NormalTexture.Scale,
}
}
w.materials = append(w.materials, m)
return ptrI(len(w.materials) - 1)
}
func (w *Writer) AddSkin(skeleton animation.Skeleton) (*int, int) {
skeletonNodes := flattenSkeletonToNodes(1, skeleton, w.buf)
w.scene = append(w.scene, len(w.nodes))
w.nodes = append(w.nodes, skeletonNodes...)
jointIndices := make([]int, len(skeletonNodes))
for i := 0; i < len(skeletonNodes); i++ {
jointIndices[i] = i + 1 // +1 because we're offsetting from mesh node
}
w.accessors = append(w.accessors, Accessor{
BufferView: ptrI(len(w.bufferViews)),
ComponentType: AccessorComponentType_FLOAT,
Type: AccessorType_MAT4,
Count: len(skeletonNodes),
// Min: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// Max: []float64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
})
inverseBindMAtrixLen := len(skeletonNodes) * 4 * 16
w.bufferViews = append(w.bufferViews, BufferView{
Buffer: 0,
ByteOffset: w.bytesWritten,
ByteLength: inverseBindMAtrixLen,
// Target: ARRAY_BUFFER,
})
w.bytesWritten += inverseBindMAtrixLen
w.skins = []Skin{
{
Joints: jointIndices,
InverseBindMatrices: len(w.accessors) - 1,
},
}
return ptrI(len(w.skins) - 1), w.scene[len(w.scene)-1]
}
func (w *Writer) AddAnimations(animations []animation.Sequence, skeleton animation.Skeleton, skeletonNode int) {
for i, animation := range animations {
min := vector3.New(math.MaxFloat64, math.MaxFloat64, math.MaxFloat64)
max := vector3.New(-math.MaxFloat64, -math.MaxFloat64, -math.MaxFloat64)
for _, frame := range animation.Frames() {
v := frame.Val()
min = vector3.Min(min, v)
max = vector3.Max(max, v)
w.WriteVector3AsFloat32(v)
}
datasize := len(animation.Frames()) * 3 * 4
animationDataBufferView := BufferView{
Buffer: 0,
ByteOffset: w.bytesWritten,
ByteLength: datasize,
}
animationDataBufferViewIndex := len(w.bufferViews)
animationDataAccessor := Accessor{
BufferView: ptrI(animationDataBufferViewIndex),
ComponentType: AccessorComponentType_FLOAT,
Type: AccessorType_VEC3,
Count: len(animation.Frames()),
Min: []float64{min.X(), min.Y(), min.Z()},
Max: []float64{max.X(), max.Y(), max.Z()},
}
animationDataAccessorIndex := len(w.accessors)
w.accessors = append(w.accessors, animationDataAccessor)
w.bufferViews = append(w.bufferViews, animationDataBufferView)
w.bytesWritten += datasize
// Time Data ============================================================
minTime := math.MaxFloat64
maxTime := -math.MaxFloat64
for _, frame := range animation.Frames() {
minTime = math.Min(minTime, frame.Time())
maxTime = math.Max(maxTime, frame.Time())
w.bitW.Float32(float32(frame.Time()))
}
datasize = len(animation.Frames()) * 4
timeBufferView := BufferView{
Buffer: 0,
ByteOffset: w.bytesWritten,
ByteLength: datasize,
}
timeBufferViewIndex := len(w.bufferViews)
timeAccessor := Accessor{
BufferView: ptrI(timeBufferViewIndex),
ComponentType: AccessorComponentType_FLOAT,
Type: AccessorType_SCALAR,
Count: len(animation.Frames()),
Min: []float64{minTime},
Max: []float64{maxTime},
}
timeAccessorIndex := len(w.accessors)
w.accessors = append(w.accessors, timeAccessor)
w.bufferViews = append(w.bufferViews, timeBufferView)
w.bytesWritten += datasize
w.animations = append(w.animations, Animation{
Samplers: []AnimationSampler{
{
Interpolation: AnimationSamplerInterpolation_LINEAR,
Input: timeAccessorIndex,
Output: animationDataAccessorIndex,
},
},
Channels: []AnimationChannel{
{
Target: AnimationChannelTarget{
Path: AnimationChannelTargetPath_TRANSLATION,
Node: skeleton.Lookup(animation.Joint()) + skeletonNode,
},
Sampler: i,
},
},
})
}
}
func (w *Writer) AddLight(light KHR_LightsPunctual) {
nodeIndex := len(w.nodes)
w.scene = append(w.scene, nodeIndex)
lightIndex := len(w.lights)
w.lights = append(w.lights, light)
var translation = [3]float64{
light.Position.X(),
light.Position.Y(),
light.Position.Z(),
}
w.nodes = append(w.nodes, Node{
ChildOfRootProperty: ChildOfRootProperty{
Property: Property{
Extensions: map[string]any{
"KHR_lights_punctual": map[string]any{
"light": lightIndex,
},
},
},
},
Translation: &translation,
})
w.extensionsUsed["KHR_lights_punctual"] = true
}
func (w *Writer) AddMesh(model PolyformModel) {
primitiveAttributes := make(map[string]int)
for _, val := range model.Mesh.Float4Attributes() {
primitiveAttributes[polyformToGLTFAttribute(val)] = len(w.accessors)
w.WriteVector4(attributeType(val), model.Mesh.Float4Attribute(val))
}
for _, val := range model.Mesh.Float3Attributes() {
primitiveAttributes[polyformToGLTFAttribute(val)] = len(w.accessors)
w.WriteVector3(attributeType(val), model.Mesh.Float3Attribute(val))
}
for _, val := range model.Mesh.Float2Attributes() {
primitiveAttributes[polyformToGLTFAttribute(val)] = len(w.accessors)
w.WriteVector2(attributeType(val), model.Mesh.Float2Attribute(val))
}
indiceIndex := len(w.accessors)
w.WriteIndices(model.Mesh.Indices(), model.Mesh.AttributeLength())
meshIndex := len(w.meshes)
nodeIndex := len(w.nodes)
w.scene = append(w.scene, nodeIndex)
w.nodes = append(w.nodes, Node{
Mesh: &meshIndex,
})
var skinNode = 0
// Skins nodes in the scene always have to come after the mesh itself, the
// mesh node must act as the root node for the skinning to work
if model.Skeleton != nil {
var skinIndex *int
skinIndex, skinNode = w.AddSkin(*model.Skeleton)
w.nodes[nodeIndex].Skin = skinIndex
}
if len(model.Animations) > 0 {
w.AddAnimations(model.Animations, *model.Skeleton, skinNode)
}
var materialIndex *int
if model.Material != nil {
materialIndex = w.AddMaterial(*model.Material)
}
var mode *PrimitiveMode = nil
if model.Mesh.Topology() == modeling.PointTopology {
p := PrimitiveMode_POINTS
mode = &p
}
w.meshes = append(w.meshes, Mesh{
ChildOfRootProperty: ChildOfRootProperty{
Name: model.Name,
},
Primitives: []Primitive{
{
Indices: &indiceIndex,
Attributes: primitiveAttributes,
Material: materialIndex,
Mode: mode,
},
},
})
}
type BufferEmbeddingStrategy int
const (
BufferEmbeddingStrategy_Base64Encode BufferEmbeddingStrategy = iota
BufferEmbeddingStrategy_GLB
)
func (w Writer) ToGLTF(embeddingStrategy BufferEmbeddingStrategy) Gltf {
buffer := Buffer{
ByteLength: w.bytesWritten,
}
if embeddingStrategy == BufferEmbeddingStrategy_Base64Encode {
buffer.URI = "data:application/octet-stream;base64," + base64.StdEncoding.EncodeToString(w.buf.Bytes())
}
exnesionsArr := make([]string, 0, len(w.extensionsUsed))
for ext := range w.extensionsUsed {
exnesionsArr = append(exnesionsArr, ext)
}
extensions := make(map[string]any)
if len(w.lights) > 0 {
arr := make([]map[string]any, 0)
for _, l := range w.lights {
arr = append(arr, l.ToExtension())
}
extensions["KHR_lights_punctual"] = map[string]any{
"lights": arr,
}
}
return Gltf{
Asset: defaultAsset(),
Buffers: []Buffer{buffer},
BufferViews: w.bufferViews,
Accessors: w.accessors,
// Skins: skins,
Scene: 0,
Scenes: []Scene{
{
Nodes: w.scene,
},
},
Skins: w.skins,
Animations: w.animations,
Nodes: w.nodes,
Meshes: w.meshes,
Materials: w.materials,
Textures: w.textures,
Images: w.images,
Samplers: w.samplers,
ExtensionsUsed: exnesionsArr,
Property: Property{
Extensions: extensions,
},
}
}
func (w Writer) WriteGLB(out io.Writer) error {
jsonBytes, err := json.Marshal(w.ToGLTF(BufferEmbeddingStrategy_GLB))
if err != nil {
return err
}
jsonByteLen := len(jsonBytes)
jsonPadding := (4 - (jsonByteLen % 4)) % 4
jsonByteLen += jsonPadding
binBytes := w.buf.Bytes()
binByteLen := len(binBytes)
binPadding := (4 - (binByteLen % 4)) % 4
binByteLen += binPadding
bitWriter := bitlib.NewWriter(out, binary.LittleEndian)
// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.pdf
// magic MUST be equal to equal 0x46546C67. It is ASCII string glTF and can
// be used to identify data as Binary glTF
bitWriter.UInt32(0x46546C67)
// GLB version
bitWriter.UInt32(2)
// Length of entire document
totalLen := jsonByteLen + binByteLen + 12 + 8
if binByteLen > 0 {
totalLen += 8
}
bitWriter.UInt32(uint32(totalLen))
// JSON CHUNK =============================================================
// Chunk Length
bitWriter.UInt32(uint32(jsonByteLen))
// JSON Chunk Identifier
bitWriter.UInt32(0x4E4F534A)
// JSON data
bitWriter.ByteArray(jsonBytes)
// Padding to make it align to a 4 byte boundary
for i := 0; i < jsonPadding; i++ {
bitWriter.Byte(0x20)
}
// OPTIONAL BIN CHUNK =====================================================
// Don't write anything if the bin data is empty
if binByteLen == 0 {
return bitWriter.Error()
}
// Chunk Length
bitWriter.UInt32(uint32(binByteLen))
// BIN Chunk Identifier
bitWriter.UInt32(0x004E4942)
// Bin data
bitWriter.ByteArray(binBytes)
// Padding to make it align to a 4 byte boundary
for i := 0; i < binPadding; i++ {
bitWriter.Byte(0x00)
}
return bitWriter.Error()
}