Skip to content

Commit

Permalink
Merge pull request #13220 from webprofusion-chrisc/patch-1
Browse files Browse the repository at this point in the history
GLTFExporter: implement 4-byte data alignment for buffer
  • Loading branch information
mrdoob authored Feb 4, 2018
2 parents 1279c1d + 69d2c7d commit b4c6726
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions examples/js/exporters/GLTFExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ THREE.GLTFExporter.prototype = {

}

/**
* Get the required size + padding for a buffer, rounded to the next 4-byte boundary.
* https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#data-alignment
*
* @param {Integer} bufferSize The size the original buffer.
* @returns {Integer} new buffer size with required padding.
*
*/
function getPaddedBufferSize( bufferSize ) {

return Math.ceil( bufferSize / 4 ) * 4;

}

/**
* Process a buffer to append to the default one.
* @param {THREE.BufferAttribute} attribute Attribute to store
Expand Down Expand Up @@ -208,6 +222,10 @@ THREE.GLTFExporter.prototype = {

// Create a new dataview and dump the attribute's array into it
var byteLength = count * attribute.itemSize * componentSize;

// adjust required size of array buffer with padding
// to satisfy gltf requirement that the length is divisible by 4
byteLength = getPaddedBufferSize( byteLength );

var dataView = new DataView( new ArrayBuffer( byteLength ) );

Expand Down

0 comments on commit b4c6726

Please sign in to comment.