glbImporter.cs
2.16 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace UniGLTF
{
public static class glbImporter
{
public const string GLB_MAGIC = "glTF";
public const float GLB_VERSION = 2.0f;
public static GlbChunkType ToChunkType(string src)
{
switch(src)
{
case "BIN":
return GlbChunkType.BIN;
case "JSON":
return GlbChunkType.JSON;
default:
throw new FormatException("unknown chunk type: " + src);
}
}
public static List<GlbChunk> ParseGlbChanks(Byte[] bytes)
{
if (bytes.Length == 0)
{
throw new Exception("empty bytes");
}
int pos = 0;
if (Encoding.ASCII.GetString(bytes, 0, 4) != GLB_MAGIC)
{
throw new Exception("invalid magic");
}
pos += 4;
var version = BitConverter.ToUInt32(bytes, pos);
if (version != GLB_VERSION)
{
Debug.LogWarningFormat("unknown version: {0}", version);
return null;
}
pos += 4;
//var totalLength = BitConverter.ToUInt32(bytes, pos);
pos += 4;
var chunks = new List<GlbChunk>();
while (pos < bytes.Length)
{
var chunkDataSize = BitConverter.ToInt32(bytes, pos);
pos += 4;
//var type = (GlbChunkType)BitConverter.ToUInt32(bytes, pos);
var chunkTypeBytes = bytes.Skip(pos).Take(4).Where(x => x != 0).ToArray();
var chunkTypeStr = Encoding.ASCII.GetString(chunkTypeBytes);
var type = ToChunkType(chunkTypeStr);
pos += 4;
chunks.Add(new GlbChunk
{
ChunkType = type,
Bytes = new ArraySegment<byte>(bytes, (int)pos, (int)chunkDataSize)
});
pos += chunkDataSize;
}
return chunks;
}
}
}