Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow referencing additional chunks. #1065

Merged
merged 1 commit into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions Assets/UniGLTF/Runtime/UniGLTF/Format/glbTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,28 @@ public static int TryParse(ArraySegment<Byte> bytes, out int pos, out Exception

public struct GlbChunk
{
public GlbChunkType ChunkType;
public GlbChunkType ChunkType => ChunkTypeString.ToChunkType();

public string ChunkTypeString;
public ArraySegment<Byte> Bytes;

public GlbChunk(string json) : this(
GlbChunkType.JSON,
GlbChunkType.JSON.ToChunkTypeString(),
new ArraySegment<byte>(Encoding.UTF8.GetBytes(json))
)
{
}

public GlbChunk(ArraySegment<Byte> bytes) : this(
GlbChunkType.BIN,
GlbChunkType.BIN.ToChunkTypeString(),
bytes
)
{
}

public GlbChunk(GlbChunkType type, ArraySegment<Byte> bytes)
public GlbChunk(string chunkTypeString, ArraySegment<Byte> bytes)
{
ChunkType = type;
ChunkTypeString = chunkTypeString;
Bytes = bytes;
}

Expand All @@ -82,12 +84,12 @@ public static GlbChunk CreateJson(string json)

public static GlbChunk CreateJson(ArraySegment<byte> bytes)
{
return new GlbChunk(GlbChunkType.JSON, bytes);
return new GlbChunk(GlbChunkType.JSON.ToChunkTypeString(), bytes);
}

public static GlbChunk CreateBin(ArraySegment<Byte> bytes)
{
return new GlbChunk(GlbChunkType.BIN, bytes);
return new GlbChunk(GlbChunkType.BIN.ToChunkTypeString(), bytes);
}

byte GetPaddingByte()
Expand Down Expand Up @@ -152,7 +154,7 @@ public int WriteTo(Stream s)
}

/// <summary>
/// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
/// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
/// </summary>
public struct Glb
{
Expand Down Expand Up @@ -267,12 +269,11 @@ public static bool TryParse(ArraySegment<Byte> bytes, out Glb glb, out Exception
//var type = (GlbChunkType)BitConverter.ToUInt32(bytes, pos);
var chunkTypeBytes = bytes.Slice(pos, 4).Where(x => x != 0).ToArray();
var chunkTypeStr = Encoding.ASCII.GetString(chunkTypeBytes);
var type = ToChunkType(chunkTypeStr);
pos += 4;

chunks.Add(new GlbChunk
{
ChunkType = type,
ChunkTypeString = chunkTypeStr,
Bytes = bytes.Slice(pos, chunkDataSize)
});

Expand Down
9 changes: 8 additions & 1 deletion Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void ParseGlb(Byte[] bytes)
{
var chunks = glbImporter.ParseGlbChunks(bytes);

if (chunks.Count != 2)
if (chunks.Count < 2)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allow

{
throw new Exception("unknown chunk count: " + chunks.Count);
}
Expand All @@ -106,6 +106,8 @@ public void ParseGlb(Byte[] bytes)
var jsonBytes = chunks[0].Bytes;
ParseJson(Encoding.UTF8.GetString(jsonBytes.Array, jsonBytes.Offset, jsonBytes.Count),
new SimpleStorage(chunks[1].Bytes));

ParseExternalChunks(bytes, chunks);
}
catch (StackOverflowException ex)
{
Expand All @@ -117,6 +119,11 @@ public void ParseGlb(Byte[] bytes)
}
}

protected virtual void ParseExternalChunks(byte[] bytes, IReadOnlyList<GlbChunk> chunks)
{

}

public virtual void ParseJson(string json, IStorage storage)
{
Json = json;
Expand Down
18 changes: 15 additions & 3 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/glbImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class glbImporter
public const string GLB_MAGIC = "glTF";
public const float GLB_VERSION = 2.0f;

public static GlbChunkType ToChunkType(string src)
public static GlbChunkType ToChunkType(this string src)
{
switch(src)
{
Expand All @@ -27,6 +27,19 @@ public static GlbChunkType ToChunkType(string src)
}
}

public static string ToChunkTypeString(this GlbChunkType type)
{
switch (type)
{
case GlbChunkType.JSON:
return "JSON";
case GlbChunkType.BIN:
return "BIN";
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}

[Obsolete("Use ParseGlbChunks(bytes)")]
public static List<GlbChunk> ParseGlbChanks(Byte[] bytes)
{
Expand Down Expand Up @@ -67,12 +80,11 @@ public static List<GlbChunk> ParseGlbChunks(Byte[] bytes)
//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,
ChunkTypeString = chunkTypeStr,
Bytes = new ArraySegment<byte>(bytes, (int)pos, (int)chunkDataSize)
});

Expand Down