glTF-CSharp-Loader icon indicating copy to clipboard operation
glTF-CSharp-Loader copied to clipboard

Change Buffer.Uri type from string to byte[]

Open Nickpofig opened this issue 2 years ago • 2 comments

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#.

Nickpofig avatar Mar 06 '23 19:03 Nickpofig

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.

bghgary avatar Mar 06 '23 21:03 bghgary

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).

bikemurt avatar Apr 28 '23 19:04 bikemurt