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

GLTFExporter: Fix JSON chunk padding issue. #13542

Merged
merged 9 commits into from
Mar 14, 2018

Conversation

takahirox
Copy link
Collaborator

This PR fixes padding logic in stringToArrayBuffer() of GLTFExporter for multi-byte strings. The current logic doesn't take account of the possibility that .name can be multi-byte strings.

@donmccurdy
Copy link
Collaborator

We only use this method once — could we just call getPaddedArrayBuffer() on the result? Conceptually converting a string to an arraybuffer does not necessarily require padding.

@takahirox
Copy link
Collaborator Author

So why does even the current stringToArrayBuffer() have padded option?

@donmccurdy
Copy link
Collaborator

😅 Oops. Let's remove that and just do padding when we're writing a bufferView or GLB chunk.

@mrdoob mrdoob added this to the r91 milestone Mar 9, 2018
@takahirox
Copy link
Collaborator Author

OK, I'll update,

@fernandojsg
Copy link
Collaborator

Yeah you're right :D I guess it was some duplicated use somewhere on the code that didn't need padding and it was removed after all :)

@takahirox
Copy link
Collaborator Author

takahirox commented Mar 9, 2018

Probably we misunderstood each other.

stringToArrayBuffer() is used once for converting from JSON string to ArrayBuffer for entire JSON chunk. And we can't use getPaddedArrayBuffer() for the JSON chunk array buffer because

https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#structured-json-content

This chunk must be padded with trailing Space chars (0x20) to satisfy alignment requirements.

If we pad with 0, JSON.parse( jsonChunk ) will fail when loading.

So padding in stringToArrayBuffer() is good. Adding an padding value (0 or 0x20) option to getPaddedArrayBuffer() would be another option.

@mrdoob mrdoob requested a review from fernandojsg March 9, 2018 20:37
@donmccurdy
Copy link
Collaborator

donmccurdy commented Mar 10, 2018

Hm I see what you mean :/ ...hoping we can still solve this with a smaller change to getPaddedArrayBuffer though, how about this?

function getPaddedArrayBuffer( arrayBuffer, paddingByte ) {

        paddingByte = paddingByte || 0;
	var paddedLength = getPaddedBufferSize( arrayBuffer.byteLength );

	if (paddedLength !== arrayBuffer.byteLength ) {

		var paddedBuffer = new ArrayBuffer( paddedLength );
		new Uint8Array( paddedBuffer )
                  .set( [ paddingByte, paddingByte, paddingByte ], paddedLength - 3 )
                  .set( new Uint8Array( arrayBuffer ) );
		return paddedBuffer;

	}

	return arrayBuffer;

}

EDIT: Sigh. .fill() isn't supported in IE11 or Safari, so edited above.

@takahirox
Copy link
Collaborator Author

That's the one I mentioned

Adding an padding value (0 or 0x20) option to getPaddedArrayBuffer() would be another option.

@takahirox
Copy link
Collaborator Author

Came to think adding that option would be better. We don't need two padding functions. I'll update.

@takahirox takahirox changed the title GLTFExporter: Fix padding in stringToArrayBuffer for multi-byte strings GLTFExporter: Fix JSON chunk padding issue for multi-byte characters. Mar 11, 2018
@takahirox
Copy link
Collaborator Author

Updated.

  • Added paddingByte option to getPaddedArrayBuffer()
  • Removed padded option from stringToArrayBuffer()
  • Cleaned up those two functions a bit

@donmccurdy

new Uint8Array( paddedBuffer )
    .set( [ paddingByte, paddingByte, paddingByte ], paddedLength - 3 )
    .set( new Uint8Array( arrayBuffer ) );

Just in case, .set doesn't return this.

@takahirox takahirox changed the title GLTFExporter: Fix JSON chunk padding issue for multi-byte characters. GLTFExporter: Fix JSON chunk padding issue. Mar 11, 2018
@mrdoob mrdoob merged commit 30f16d4 into mrdoob:dev Mar 14, 2018
@mrdoob
Copy link
Owner

mrdoob commented Mar 14, 2018

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants