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

Apply Draco compression to meshes without indices #424

Merged
merged 8 commits into from
Feb 11, 2019
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
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