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

Fix byte boundary issue #279

Merged
merged 4 commits into from
May 9, 2017
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
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

### 0.1.0-alpha14 - ???
* Fixed byte offset alignment issue when loading converting models in Cesium. [#279](https://github.com/AnalyticalGraphicsInc/gltf-pipeline/pull/279)
* Added case-insensitive regex checking for image extensions. [#278](https://github.com/AnalyticalGraphicsInc/gltf-pipeline/pull/278)
* Added `mergeVertices` option to merge duplicate vertices. This operation is now disabled by default. [#276](https://github.com/AnalyticalGraphicsInc/gltf-pipeline/pull/276)

### 0.1.0-alpha13 - 2017-04-27
* Fixed a bug in `processModelMaterialsCommon` that produced out-of-spec technique states. [#269](https://github.com/AnalyticalGraphicsInc/gltf-pipeline/pull/269)

Expand Down
11 changes: 10 additions & 1 deletion lib/uninterleaveAndPackBuffers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function packGltfBuffer(gltf, bufferId, packBufferViews) {
var buffers = gltf.buffers;
var buffer = buffers[bufferId];
var source = buffer.extras._pipeline.source;
var packBuffer = new Uint8Array(source.length);
var packBuffer = new Uint8Array(source.length + 6); // Account for extra padding between targets
var accessors = getAccessorsByTargetAndByteLength(gltf, bufferId);
var offset = 0;

Expand All @@ -59,6 +59,15 @@ function packGltfBuffer(gltf, bufferId, packBufferViews) {
var accessorsByByteLength = accessors[target];
if (defined(accessorsByByteLength)) {
offset = packAccessorsForTarget(gltf, bufferId, target, accessorsByByteLength, source, packBuffer, packBufferViews, offset);

// End this target on a 4-byte boundary so the byteOffset of the next target's first accessor is 0
// Don't add padding to the last target
for (var j = i + 1; j < targets.length; ++j) {
if (defined(accessors[targets[j]])) {
offset += offset % 4;
break;
}
}
}
}
packBuffer = new Buffer(new Uint8Array(packBuffer.buffer, 0, offset));
Expand Down
59 changes: 58 additions & 1 deletion specs/lib/uninterleaveAndPackBuffersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,51 @@ describe('uninterleaveAndPackBuffers', function() {
}
};

var bufferPacked = new Uint8Array(18);
var testGltfPadded = {
accessors : {
accessor_0 : {
bufferView : 'bufferView_0',
byteOffset : 0,
byteStride : 0,
componentType : 5123,
count : 3,
type : 'SCALAR'
},
accessor_1 : {
bufferView : 'bufferView_1',
byteOffset : 0,
byteStride : 0,
componentType : 5125,
count : 3,
type : 'SCALAR'
}
},
bufferViews : {
bufferView_0 : {
buffer : 'buffer',
byteLength : 6,
byteOffset : 0,
target : 34962
},
bufferView_1 : {
buffer : 'buffer',
byteLength : 12,
byteOffset : 6,
target : 34963
}
},
buffers : {
buffer : {
byteLength : bufferPacked.length,
type : 'arraybuffer',
extras : {
_pipeline : {}
}
}
}
};

it('doesn\'t remove any data if the whole buffer is used', function() {
var gltf = clone(testGltf);
gltf.buffers.buffer.extras._pipeline.source = buffer;
Expand Down Expand Up @@ -117,4 +162,16 @@ describe('uninterleaveAndPackBuffers', function() {
var bufferViewCount = Object.keys(gltf.bufferViews).length;
expect(bufferViewCount).toEqual(1);
});
});

it('adds padding between different targets', function() {
var gltf = clone(testGltfPadded);
gltf.buffers.buffer.extras._pipeline.source = bufferPacked;
packBuffers(gltf);

// Expect the index accessor to begin on a 4-byte boundary
expect(gltf.buffers.buffer.byteLength).toEqual(20); // Originally 18
expect(gltf.bufferViews.bufferView_1.byteOffset).toBe(8); // Originally 6
expect(gltf.accessors.accessor_0.byteOffset).toBe(0);
expect(gltf.accessors.accessor_1.byteOffset).toBe(0);
});
});