KhronosGroup / glTF-CSharp-Loader

C# Reference Loader for glTF
Other
219 stars 58 forks source link

Change Buffer.Uri type from string to byte[] #47

Open Nickpofig opened 1 year ago

Nickpofig commented 1 year ago

C# char is ALWAYS 2 bytes long (UTF-16). Therefore, in cases of loading GLTF files with embedded data (buffer's URI cares model data within it), when you treat the buffer as an array of bytes/ints/floats/etc - the data you read is wrong. The reason is that every second byte's value that you read from such a buffer is zero. Not only data is ill-formatted, but the buffer itself is twice the necessary size.

Please fix it.

P.s. I know it is annoying :) #blame_microsoft. This bug will always haunt people who port C/C++ code to C#.

bghgary commented 1 year ago

I assume this is about base64 encoded strings. Are you saying base64 encoded URIs are not working?

when you treat the buffer as an array of bytes/ints/floats/etc - the data you read is wrong

I don't know exactly what you mean by this. Are you casting the string to a byte array directly? C# strings are two-bytes per character to handle Unicode (to a certain extent). If you want to decode the string to a byte array, you can call Convert.FromBase64String to do it.

bikemurt commented 1 year ago

I have no issues using Convert.FromBase64String. Just need to chop off the front data:application/octet-stream;base64, part.

byte[] bufferBytes = Convert.FromBase64String(buffer.Uri.Substring(37));

Then just use System.Buffer.BlockCopy to get the chunks you need (indices are added to the end so you typically won't want that in your VBO).