Skip to content

Commit

Permalink
Merge pull request #424 from AnalyticalGraphicsInc/compress-without-i…
Browse files Browse the repository at this point in the history
…ndices

Apply Draco compression to meshes without indices
  • Loading branch information
bagnell authored Feb 11, 2019
2 parents 826093e + 72caf62 commit d9bd056
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Change Log
==========

### 2.1.1 - 2019-??-??

* Added ability to apply Draco compression to meshes without indices. [#424](https://github.com/AnalyticalGraphicsInc/gltf-pipeline/pull/424)

### 2.1.0 - 2019-01-28

* Fixed a bug where nodes containing extensions or extras where being removed in the glTF 1.0 to 2.0 upgrade stage. [#431](https://github.com/AnalyticalGraphicsInc/gltf-pipeline/pull/431)
Expand Down
24 changes: 23 additions & 1 deletion lib/compressDracoMeshes.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function compressDracoMeshes(gltf, options) {
return;
}
if (!defined(primitive.indices)) {
return;
addIndices(gltf, primitive);
}

addedExtension = true;
Expand Down Expand Up @@ -236,6 +236,28 @@ function compressDracoMeshes(gltf, options) {
return gltf;
}

function addIndices(gltf, primitive) {
// Reserve the 65535 index for primitive restart
const length = gltf.accessors[primitive.attributes.POSITION].count;
const componentType = length < 65535 ? WebGLConstants.UNSIGNED_SHORT : WebGLConstants.UNSIGNED_INT;
const array = ComponentDatatype.createTypedArray(componentType, length);
for (let i = 0; i < length; ++i) {
array[i] = i;
}
const buffer = Buffer.from(array.buffer);
const bufferView = addBuffer(gltf, buffer);
const accessor = {
bufferView: bufferView,
byteOffset: 0,
componentType: componentType,
count: length,
type: 'SCALAR',
min: [0],
max: [length - 1]
};
primitive.indices = addToArray(gltf.accessors, accessor);
}

function addCompressionExtensionToPrimitive(gltf, primitive, attributeToId, dracoEncodedBuffer, uncompressedFallback) {
if (!uncompressedFallback) {
// Remove properties from accessors.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"scenes" : [
{
"nodes" : [ 0 ]
}
],

"nodes" : [
{
"mesh" : 0
}
],

"meshes" : [
{
"primitives" : [ {
"attributes" : {
"POSITION" : 0
}
} ]
}
],

"buffers" : [
{
"uri" : "data:application/octet-stream;base64,AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAgD8AAAAA",
"byteLength" : 36
}
],
"bufferViews" : [
{
"buffer" : 0,
"byteOffset" : 0,
"byteLength" : 36,
"target" : 34962
}
],
"accessors" : [
{
"bufferView" : 0,
"byteOffset" : 0,
"componentType" : 5126,
"count" : 3,
"type" : "VEC3",
"max" : [ 1.0, 1.0, 0.0 ],
"min" : [ 0.0, 0.0, 0.0 ]
}
],

"asset" : {
"version" : "2.0"
}
}
21 changes: 21 additions & 0 deletions specs/lib/compressDracoMeshesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const compressDracoMeshes = require('../../lib/compressDracoMeshes');
const boxPath = 'specs/data/2.0/box-textured-embedded/box-textured-embedded.gltf';
const boxMorphPath = 'specs/data/2.0/box-morph/box-morph.gltf';
const multipleBoxesPath = 'specs/data/2.0/multiple-boxes/multiple-boxes.gltf';
const triangleWithoutIndicesPath = 'specs/data/2.0/triangle-without-indices/triangle-without-indices.gltf';

let gltf;
let gltfOther;
Expand Down Expand Up @@ -69,6 +70,26 @@ describe('compressDracoMeshes', () => {
expect(texcoordAccessor.byteLength).toBeUndefined();
});

it('compresses mesh without indices', async () => {
const gltf = await readGltf(triangleWithoutIndicesPath);
expect(gltf.accessors.length).toBe(1); // positions
expect(gltf.bufferViews.length).toBe(1); // positions

compressDracoMeshes(gltf);

expect(gltf.accessors.length).toBe(2); // positions + indices
expect(gltf.bufferViews.length).toBe(1); // draco

const dracoExtension = gltf.meshes[0].primitives[0].extensions.KHR_draco_mesh_compression;
expect(dracoExtension.bufferView).toBeDefined();
expect(gltf.extensionsUsed.indexOf('KHR_draco_mesh_compression') >= 0).toBe(true);
expect(gltf.extensionsRequired.indexOf('KHR_draco_mesh_compression') >= 0).toBe(true);

const positionAccessor = gltf.accessors[dracoExtension.attributes.POSITION];
expect(positionAccessor.bufferView).toBeUndefined();
expect(positionAccessor.byteLength).toBeUndefined();
});

it('throws if quantize bits is out of range', () => {
expectOutOfRange(gltf, 'compressionLevel', -1);
expectOutOfRange(gltf, 'compressionLevel', 11);
Expand Down

0 comments on commit d9bd056

Please sign in to comment.