Skip to content

Commit

Permalink
Merge branch '2.0' into ext-blend
Browse files Browse the repository at this point in the history
  • Loading branch information
ggetz committed Jul 11, 2018
2 parents 81a7f23 + 7586da1 commit 0a8988e
Show file tree
Hide file tree
Showing 9 changed files with 426 additions and 113 deletions.
199 changes: 141 additions & 58 deletions lib/ForEach.js

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions lib/compressDracoMeshes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var ForEach = require('./ForEach');
var numberOfComponentsForType = require('./numberOfComponentsForType');
var readAccessorPacked = require('./readAccessorPacked');
var removeUnusedElements = require('./removeUnusedElements');
var replaceWithDecompressedPrimitive = require('./replaceWithDecompressedPrimitive');
var splitPrimitives = require('./splitPrimitives');

var arrayFill = Cesium.arrayFill;
Expand Down Expand Up @@ -183,16 +184,19 @@ function compressDracoMeshes(gltf, options) {
encoder.SetEncodingMethod(encoderModule.MESH_SEQUENTIAL_ENCODING);
}

encoder.SetTrackEncodedProperties(true);
var encodedLength = encoder.EncodeMeshToDracoBuffer(mesh, encodedDracoDataArray);
if (encodedLength <= 0) {
throw new RuntimeError('Error: Draco encoding failed.');
}
var encodedPointsCount = encoder.GetNumberOfEncodedPoints();
var encodedFacesCount = encoder.GetNumberOfEncodedFaces();
var encodedData = Buffer.alloc(encodedLength);
for (var i = 0; i < encodedLength; ++i) {
encodedData[i] = encodedDracoDataArray.GetValue(i);
}

addCompressionExtensionToPrimitive(gltf, primitive, attributeToId, encodedLength, encodedData);
addCompressionExtensionToPrimitive(gltf, primitive, attributeToId, encodedLength, encodedData, encodedPointsCount, encodedFacesCount);

encoderModule.destroy(encodedDracoDataArray);
encoderModule.destroy(mesh);
Expand All @@ -209,7 +213,7 @@ function compressDracoMeshes(gltf, options) {
return gltf;
}

function addCompressionExtensionToPrimitive(gltf, primitive, attributeToId, encodedLength, encodedData) {
function addCompressionExtensionToPrimitive(gltf, primitive, attributeToId, encodedLength, encodedData, encodedPointsCount, encodedFacesCount) {
// Remove properties from accessors.
// Remove indices bufferView.
var indicesAccessor = clone(gltf.accessors[primitive.indices]);
Expand Down Expand Up @@ -246,6 +250,8 @@ function addCompressionExtensionToPrimitive(gltf, primitive, attributeToId, enco
bufferView: bufferViewId,
attributes: attributeToId
};

gltf = replaceWithDecompressedPrimitive(gltf, primitive, encodedPointsCount, encodedFacesCount);
}

function copyCompressedExtensionToPrimitive(primitive, compressedPrimitive) {
Expand Down Expand Up @@ -295,4 +301,4 @@ compressDracoMeshes.defaults = {
quantizeSkinBits: 12,
quantizeGenericBits: 12,
unifiedQuantization: false
};
};
2 changes: 1 addition & 1 deletion lib/readAccessorPacked.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
var Cesium = require('cesium');
var getAccessorByteStride = require('./getAccessorByteStride');
var getComponentReader = require('./getComponentReader.js');
var getComponentReader = require('./getComponentReader');
var numberOfComponentsForType = require('./numberOfComponentsForType');

var arrayFill = Cesium.arrayFill;
Expand Down
32 changes: 32 additions & 0 deletions lib/replaceWithDecompressedPrimitive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

module.exports = replaceWithDecompressedPrimitive;

/**
* Replace the accessor properties of the original primitive with the values from the decompressed primitive.
*
* @param {Object} gltf A javascript object containing a glTF asset.
* @param {Object} primitive A javascript object containing a glTF primitive.
* @param {Number} encodedPointsCount Number of points after the mesh is decompressed.
* @param {Number} encodedFacesCount Number of faces after the mesh is decompressed.
* @returns {Object} The glTF asset with the decompressed primitive.
*
* @private
*/
function replaceWithDecompressedPrimitive(gltf, primitive, encodedPointsCount, encodedFacesCount) {
// Add decompressed indices data to indices accessor.
var indicesAccessorId = primitive.indices;
var indicesAccessor = gltf.accessors[indicesAccessorId];
indicesAccessor.count = encodedFacesCount * 3;

var dracoAttributes = primitive.extensions.KHR_draco_mesh_compression.attributes;
for (var semantic in dracoAttributes) {
if (dracoAttributes.hasOwnProperty(semantic)) {
var gltfAttributeId = primitive.attributes[semantic];
var attributeAccessor = gltf.accessors[gltfAttributeId];
attributeAccessor.count = encodedPointsCount;
}
}

return gltf;
}
9 changes: 5 additions & 4 deletions lib/updateVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,9 @@ function moveByteStrideToBufferView(gltf) {
removeUnusedElements(gltf);
}

function requireAccessorMinMax(gltf) {
ForEach.accessor(gltf, function(accessor) {
function requirePositionAccessorMinMax(gltf) {
ForEach.accessorWithSemantic(gltf, 'POSITION', function(accessorId) {
var accessor = gltf.accessors[accessorId];
if (!defined(accessor.min) || !defined(accessor.max)) {
var minMax = findAccessorMinMax(gltf, accessor);
accessor.min = minMax.min;
Expand All @@ -830,8 +831,8 @@ function glTF10to20(gltf) {
requireByteLength(gltf);
// byteStride moved from accessor to bufferView
moveByteStrideToBufferView(gltf);
// accessor.min and accessor.max must be defined
requireAccessorMinMax(gltf);
// accessor.min and accessor.max must be defined for accessors containing POSITION attributes
requirePositionAccessorMinMax(gltf);
// buffer.type is unnecessary and should be removed
removeBufferType(gltf);
// Remove format, internalFormat, target, and type
Expand Down
6 changes: 2 additions & 4 deletions lib/writeResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,14 @@ function writeBufferView(gltf, object) {
}

function getProgram(gltf, shaderIndex) {
var programInfo;
ForEach.program(gltf, function(program, index) {
return ForEach.program(gltf, function(program, index) {
if (program.fragmentShader === shaderIndex || program.vertexShader === shaderIndex) {
programInfo = {
return {
program: program,
index: index
};
}
});
return programInfo;
}

function getName(gltf, object, index, extension, options) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"dependencies": {
"bluebird": "^3.5.1",
"cesium": "^1.43.0",
"draco3d": "^1.3.0",
"draco3d": "^1.3.3",
"fs-extra": "^5.0.0",
"jimp": "^0.2.28",
"mime": "^2.2.2",
Expand Down
Loading

0 comments on commit 0a8988e

Please sign in to comment.