diff --git a/lib/quantizeAttributes.js b/lib/quantizeAttributes.js index fc1bb91d..db144484 100644 --- a/lib/quantizeAttributes.js +++ b/lib/quantizeAttributes.js @@ -121,7 +121,7 @@ function quantizeAttributes(gltf, options) { var components = []; while (defined(accessorReader.read(components))) { for (i = 0; i < numberOfComponents; i++) { - components[i] = Math.ceil((components[i] - min[i]) * range / (max[i] - min[i])); + components[i] = Math.round((components[i] - min[i]) * range / (max[i] - min[i])); } accessorReader.write(components, newComponentType); accessorReader.next(); diff --git a/specs/lib/quantizeAttributesSpec.js b/specs/lib/quantizeAttributesSpec.js index 0717f5fd..c19570bd 100644 --- a/specs/lib/quantizeAttributesSpec.js +++ b/specs/lib/quantizeAttributesSpec.js @@ -71,6 +71,16 @@ describe('quantizeAttributes', function() { min : [0], max : [1], type : 'SCALAR' + }, + // floating-point rounding test + accessor_5 : { + bufferView : 'bufferView_2', + byteOffset : 0, + componentType : 5126, + count : 1, + min : [-82.39035034179688], + max : [0.0026710000820457935], + type : 'SCALAR' } }, bufferViews : { @@ -85,6 +95,12 @@ describe('quantizeAttributes', function() { byteLength : 72, byteOffset : 48, target : 34962 + }, + bufferView_2 : { + buffer : 'buffer_float', + byteLength : 4, + byteOffset : 0, + target: 34962 } }, buffers : { @@ -94,6 +110,15 @@ describe('quantizeAttributes', function() { extras : { _pipeline : {} } + }, + buffer_float : { + byteLength : 4, + type : 'array_buffer', + extras : { + _pipeline : { + source : new Buffer(new Float32Array([0.0026710000820457935]).buffer) + } + } } }, meshes : { @@ -111,6 +136,11 @@ describe('quantizeAttributes', function() { TEXCOORD : 'accessor_3', SCALAR_TEST : 'accessor_4' } + }, + { + attributes : { + FLOAT_TEST : 'accessor_5' + } } ] } @@ -127,7 +157,7 @@ describe('quantizeAttributes', function() { it('Doesn\'t quantize excluded semantics', function() { var gltf = clone(testGltf); gltf.buffers.buffer.extras._pipeline.source = buffer; - quantizeAttributes(gltf, {exclude: ['POSITION', 'NORMAL', 'TEXCOORD', 'SCALAR_TEST']}); + quantizeAttributes(gltf, {exclude: ['POSITION', 'NORMAL', 'TEXCOORD', 'SCALAR_TEST', 'FLOAT_TEST']}); expect(gltf.buffers.buffer.byteLength).toEqual(buffer.length); });