Skip to content

Commit

Permalink
Draco compress meshes without indices
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Sep 8, 2018
1 parent 1de7258 commit c3b7602
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/compressDracoMeshes.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function compressDracoMeshes(gltf, options) {
return;
}
if (!defined(primitive.indices)) {
return;
addIndices(gltf, primitive);
}

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

function addIndices(gltf, primitive) {
// Reserve the 65535 index for primitive restart
var length = gltf.accessors[primitive.attributes.POSITION].count;
var componentType = length < 65535 ? WebGLConstants.UNSIGNED_SHORT : WebGLConstants.UNSIGNED_INT;
var array = ComponentDatatype.createTypedArray(componentType, length);
for (var i = 0; i < length; ++i) {
array[i] = i;
}
var buffer = Buffer.from(array.buffer);
var bufferView = addBuffer(gltf, buffer);
var 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"
}
}
23 changes: 23 additions & 0 deletions specs/lib/compressDracoMeshesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var clone = Cesium.clone;
var boxPath = 'specs/data/2.0/box-textured-embedded/box-textured-embedded.gltf';
var boxMorphPath = 'specs/data/2.0/box-morph/box-morph.gltf';
var multipleBoxesPath = 'specs/data/2.0/multiple-boxes/multiple-boxes.gltf';
var triangleWithoutIndicesPath = 'specs/data/2.0/triangle-without-indices/triangle-without-indices.gltf';

var gltf;
var gltfOther;
Expand Down Expand Up @@ -77,6 +78,28 @@ describe('compressDracoMeshes', function() {
expect(texcoordAccessor.byteLength).toBeUndefined();
});

it('compresses mesh without indices', function(done) {
expect(readGltf(triangleWithoutIndicesPath)
.then(function(gltf) {
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

var 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);

var positionAccessor = gltf.accessors[dracoExtension.attributes.POSITION];
expect(positionAccessor.bufferView).toBeUndefined();
expect(positionAccessor.byteLength).toBeUndefined();
}), done).toResolve();
});

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

0 comments on commit c3b7602

Please sign in to comment.