Skip to content

Commit

Permalink
Fix for encoding non float attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed May 4, 2018
1 parent a0e8d89 commit 5bcd878
Showing 1 changed file with 54 additions and 57 deletions.
111 changes: 54 additions & 57 deletions lib/compressDracoMeshes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,33 @@ var removeUnusedElements = require('./removeUnusedElements');

var checkGreaterThanOrEquals = Cesium.Check.typeOf.number.greaterThanOrEquals;
var checkLessThan = Cesium.Check.typeOf.number.lessThan;
var clone = Cesium.clone;
var ComponentDatatype = Cesium.ComponentDatatype;
var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;
var DeveloperError = Cesium.DeveloperError;
var RuntimeError = Cesium.RuntimeError;
var arrayFill = Cesium.arrayFill;
var WebGLConstants = Cesium.WebGLConstants;

// Prepare encoder for compressing meshes.
var encoderModule = draco3d.createEncoderModule({});

module.exports = compressDracoMeshes;

function getNamedAttributeData(gltf, primitive, semantic) {
var accessorId = primitive.attributes[semantic];
var accessor = gltf.accessors[accessorId];
var componentsPerAttribute = numberOfComponentsForType(accessor.type);
var packed = readAccessorPacked(gltf, accessor);

return {
numberOfComponents: componentsPerAttribute,
numberOfVertices: accessor.count,
data: packed
};
}

function addCompressionExtensionToPrimitive(gltf, primitive, attributeToId, encodedLength, encodedData) {
// Remove properties from accessors.
// Remove indices bufferView.
var indicesAccessor = gltf.accessors[primitive.indices];
var newIndicesAccessor = {
componentType: indicesAccessor.componentType,
count: indicesAccessor.count,
max: indicesAccessor.max,
min: indicesAccessor.min,
type: indicesAccessor.type
};
var indicesAccessorId = addToArray(gltf.accessors, newIndicesAccessor);
primitive.indices = indicesAccessorId;
var indicesAccessor = clone(gltf.accessors[primitive.indices]);
delete indicesAccessor.bufferView;
delete indicesAccessor.byteOffset;
primitive.indices = addToArray(gltf.accessors, indicesAccessor);

// Remove attributes bufferViews.
/*eslint-disable no-unused-vars*/
ForEach.meshPrimitiveAttribute(primitive, function(accessorId, semantic) {
var attributeAccessor = gltf.accessors[primitive.attributes[semantic]];
var newAttributeAccessor = {
componentType: attributeAccessor.componentType,
count: attributeAccessor.count,
max: attributeAccessor.max,
min: attributeAccessor.min,
type: attributeAccessor.type
};
var attributeAccessorId = addToArray(gltf.accessors, newAttributeAccessor);
primitive.attributes[semantic] = attributeAccessorId;
var attributeAccessor = clone(gltf.accessors[accessorId]);
delete attributeAccessor.bufferView;
delete attributeAccessor.byteOffset;
primitive.attributes[semantic] = addToArray(gltf.accessors, attributeAccessor);
});

var buffer = {
Expand All @@ -84,11 +61,10 @@ function addCompressionExtensionToPrimitive(gltf, primitive, attributeToId, enco
extensions = {};
primitive.extensions = extensions;
}
var dracoExtension = {
extensions.KHR_draco_mesh_compression = {
bufferView: bufferViewId,
attributes: attributeToId
};
extensions.KHR_draco_mesh_compression = dracoExtension;
}

function copyCompressedExtensionToPrimitive(primitive, compressedPrimitive) {
Expand All @@ -103,11 +79,29 @@ function copyCompressedExtensionToPrimitive(primitive, compressedPrimitive) {
var dracoExtension = compressedPrimitive.extensions.KHR_draco_mesh_compression;
var extensions = {};
primitive.extensions = extensions;
var copiedExtension = {
extensions.KHR_draco_mesh_compression = {
bufferView: dracoExtension.bufferView,
attributes: dracoExtension.attributes
};
extensions.KHR_draco_mesh_compression = copiedExtension;
}

function getAddAttributeFunctionName(componentType) {
switch (componentType) {
case WebGLConstants.UNSIGNED_BYTE:
return 'AddUInt8Attribute';
case WebGLConstants.BYTE:
return 'AddInt8Attribute';
case WebGLConstants.UNSIGNED_SHORT:
return 'AddUInt16Attribute';
case WebGLConstants.SHORT:
return 'AddInt16Attribute';
case WebGLConstants.UNSIGNED_INT:
return 'AddUInt32Attribute';
case WebGLConstants.INT:
return 'AddInt32Attribute';
case WebGLConstants.FLOAT:
return 'AddFloatAttribute';
}
}

/**
Expand Down Expand Up @@ -163,7 +157,7 @@ function compressDracoMeshes(gltf, options) {
ForEach.accessorWithSemantic(gltf, 'POSITION', function(accessorId) {
var accessor = accessors[accessorId];
if (accessor.type !== 'VEC3') {
throw new DeveloperError('Could not perform unified quantization. Input contains position accessor with an unsupported number of components.');
throw new RuntimeError('Could not perform unified quantization. Input contains position accessor with an unsupported number of components.');
}
var accessorMin = accessor.min;
var accessorMax = accessor.max;
Expand Down Expand Up @@ -210,30 +204,33 @@ function compressDracoMeshes(gltf, options) {

// Add attributes to mesh.
var attributeToId = {};
/*eslint-disable no-unused-vars*/
ForEach.meshPrimitiveAttribute(primitive, function(accessorId, semantic) {
var attributeData = getNamedAttributeData(gltf, primitive, semantic);
var numberOfPoints = attributeData.numberOfVertices;
var accessor = gltf.accessors[accessorId];
var componentType = accessor.componentType;
var numberOfPoints = accessor.count;
var numberOfComponents = numberOfComponentsForType(accessor.type);
var packed = readAccessorPacked(gltf, accessor);
var addAttributeFunctionName = getAddAttributeFunctionName(componentType);
var data = ComponentDatatype.createTypedArray(componentType, packed);

var attributeName = semantic;
if (semantic.indexOf('_') !== -1) {
if (semantic.indexOf('_') > 0) { // Skip user-defined semantics prefixed with underscore
attributeName = attributeName.substring(0, semantic.indexOf('_'));
}
var data = new Float32Array(attributeData.data);
var attributeId = -1;
if (attributeName === 'POSITION' || attributeName === 'NORMAL' ||
attributeName === 'COLOR' ) {
attributeId = meshBuilder.AddFloatAttributeToMesh(newMesh, encoderModule[attributeName],
numberOfPoints, attributeData.numberOfComponents, data);
} else if (semantic === 'TEXCOORD_0') {
attributeId = meshBuilder.AddFloatAttributeToMesh(newMesh, encoderModule.TEX_COORD,
numberOfPoints, attributeData.numberOfComponents, data);

var attributeEnum;
if (attributeName === 'POSITION' || attributeName === 'NORMAL' || attributeName === 'COLOR') {
attributeEnum = encoderModule[attributeName];
} else if (attributeName === 'TEXCOORD') {
attributeEnum = encoderModule.TEX_COORD;
} else {
attributeId = meshBuilder.AddFloatAttributeToMesh(newMesh, encoderModule.GENERIC,
numberOfPoints, attributeData.numberOfComponents, data);
attributeEnum = encoderModule.GENERIC;
}

var attributeId = meshBuilder[addAttributeFunctionName](newMesh, attributeEnum, numberOfPoints, numberOfComponents, data);

if (attributeId === -1) {
throw new DeveloperError('Error: Failed adding attribute ' + semantic);
throw new RuntimeError('Error: Failed adding attribute ' + semantic);
} else {
attributeToId[semantic] = attributeId;
}
Expand Down Expand Up @@ -268,7 +265,7 @@ function compressDracoMeshes(gltf, options) {

var encodedLength = encoder.EncodeMeshToDracoBuffer(newMesh, encodedDracoDataArray);
if (encodedLength <= 0) {
throw new DeveloperError('Error: Encoding failed.');
throw new RuntimeError('Error: Encoding failed.');
}
var encodedData = Buffer.alloc(encodedLength);
for (var i = 0; i < encodedLength; ++i) {
Expand Down

0 comments on commit 5bcd878

Please sign in to comment.