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

Export public API via index.js #157

Merged
merged 21 commits into from
Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Change Log
### Next Release

* cacheOptimization no longer crashes on primitives without indices. [#154](https://github.com/AnalyticalGraphicsInc/gltf-pipeline/issues/154)
* Public API is exposed via index.js [#153](https://github.com/AnalyticalGraphicsInc/gltf-pipeline/issues/153)

### 0.1.0-alpha3 - 2016-07-25

Expand Down
63 changes: 58 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,59 @@
module.exports = {
gltfPipeline : require('./lib/gltfPipeline'),
readGltf : require('./lib/readGltf'),
writeBinaryGltf : require('./lib/writeBinaryGltf'),
writeGltf : require('./lib/writeGltf')
};
AccessorReader : require('./lib/AccessorReader.js'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you want to expose all of this? That is a lot of API surface area. We learned a very hard lesson in Cesium to only expose what is needed to keep the maintain cost way down.

Please only expose what is currently needed and make sure the public API has full reference doc that can be generated like we do for Cesium. Don't worry about the generated doc's styling or hosting right now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's still way too much API surface area here. @lilleyse and @lasalvavida can you please take a pass over this?

I can help if needed, but I would rather you guys get the experience working with APIs. At this point, I would only expose top-level stages and helpers that @likangning93 needs. Anything else is a high-cost loan for money we don't need.

addCesiumRTC : require('./lib/addCesiumRTC.js'),
addDefaults : require('./lib/addDefaults.js'),
addExtensionsUsed : require('./lib/addExtensionsUsed.js'),
addPipelineExtras : require('./lib/addPipelineExtras.js'),
bakeAmbientOcclusion : require('./lib/bakeAmbientOcclusion.js'),
byteLengthForComponentType : require('./lib/byteLengthForComponentType.js'),
cacheOptimization : require('./lib/cacheOptimization.js'),
combineMeshes : require('./lib/combineMeshes.js'),
combineNodes : require('./lib/combineNodes.js'),
compressIntegerAccessors : require('./lib/compressIntegerAccessors.js'),
compressTextureCoordinates : require('./lib/compressTextureCoordinates.js'),
convertDagToTree : require('./lib/convertDagToTree.js'),
encodeImages : require('./lib/encodeImages.js'),
findAccessorMinMax : require('./lib/findAccessorMinMax.js'),
generateNormals : require('./lib/generateNormals.js'),
getAccessorByteStride : require('./lib/getAccessorByteStride.js'),
getBinaryGltf : require('./lib/getBinaryGltf.js'),
getUniqueId : require('./lib/getUniqueId.js'),
gltfPipeline : require('./lib/gltfPipeline.js'),
loadGltfUris : require('./lib/loadGltfUris.js'),
mergeBuffers : require('./lib/mergeBuffers.js'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed that there isn't doc for this one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind...

mergeDuplicateAccessors : require('./lib/mergeDuplicateAccessors.js'),
mergeDuplicateVertices : require('./lib/mergeDuplicateVertices.js'),
NodeHelpers : require('./lib/NodeHelpers.js'),
numberOfComponentsForType : require('./lib/numberOfComponentsForType.js'),
octEncodeNormals : require('./lib/octEncodeNormals.js'),
packArray : require('./lib/packArray.js'),
parseBinaryGltf : require('./lib/parseBinaryGltf.js'),
quantizeAttributes : require('./lib/quantizeAttributes.js'),
readAccessor : require('./lib/readAccessor.js'),
readBufferComponentType : require('./lib/readBufferComponentType.js'),
readGltf : require('./lib/readGltf.js'),
removeDuplicatePrimitives : require('./lib/removeDuplicatePrimitives.js'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And doc for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind...

removePipelineExtras : require('./lib/removePipelineExtras.js'),
removeUnused : require('./lib/removeUnused.js'),
removeUnusedAccessors : require('./lib/removeUnusedAccessors.js'),
removeUnusedBuffers : require('./lib/removeUnusedBuffers.js'),
removeUnusedBufferViews : require('./lib/removeUnusedBufferViews.js'),
removeUnusedCameras : require('./lib/removeUnusedCameras.js'),
removeUnusedImages : require('./lib/removeUnusedImages.js'),
removeUnusedMaterials : require('./lib/removeUnusedMaterials.js'),
removeUnusedMeshes : require('./lib/removeUnusedMeshes.js'),
removeUnusedNodes : require('./lib/removeUnusedNodes.js'),
removeUnusedPrimitiveAttributes : require('./lib/removeUnusedPrimitiveAttributes.js'),
removeUnusedPrograms : require('./lib/removeUnusedPrograms.js'),
removeUnusedSamplers : require('./lib/removeUnusedSamplers.js'),
removeUnusedShaders : require('./lib/removeUnusedShaders.js'),
removeUnusedSkins : require('./lib/removeUnusedSkins.js'),
removeUnusedTechniques : require('./lib/removeUnusedTechniques.js'),
removeUnusedTextures : require('./lib/removeUnusedTextures.js'),
removeUnusedVertices : require('./lib/removeUnusedVertices.js'),
uninterleaveAndPackBuffers : require('./lib/uninterleaveAndPackBuffers.js'),
writeAccessor : require('./lib/writeAccessor.js'),
writeBinaryGltf : require('./lib/writeBinaryGltf.js'),
writeBufferComponentType : require('./lib/writeBufferComponentType.js'),
writeGltf : require('./lib/writeGltf.js')
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main problem with exposing a lot of these functions is that they may rely on previous steps, like addPipelineExtras. Either we need to handle that in the functions themselves, or expect users to call things like addPipelineExtras, loadGltfUris, etc on their own.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be enough to just put the dependencies in the doc until we work out custom pipelines?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that's ok for now.

37 changes: 37 additions & 0 deletions lib/AccessorReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ var writeBufferComponentType = require('./writeBufferComponentType');

module.exports = AccessorReader;

/**
* Reads an accessor incrementally. This is useful to keep overhead low when
* memory consumption is important.
*
* @param {Object} gltf A javascript object holding a glTF hierarchy.
* @param {Object} accessor The accessor object from glTF to read .
* @constructor
*/
function AccessorReader(gltf, accessor) {
var bufferViews = gltf.bufferViews;
var buffers = gltf.buffers;
Expand All @@ -30,6 +38,14 @@ function AccessorReader(gltf, accessor) {
this.source = buffer.extras._pipeline.source;
}

/**
* Read data at the current index into components.
* Data will be read into components starting at index 0.
* Components will be grown if the number of components for the accessor is greater than the array size.
*
* @param {Array} components The array to read the data into.
* @returns {Array} components with data written into it.
*/
AccessorReader.prototype.read = function(components) {
if (this.index >= this.count) {
return undefined;
Expand All @@ -49,6 +65,16 @@ AccessorReader.prototype.read = function(components) {
return components;
};

/**
* Write data at the current index into the accessor data.
* Specifying a componentType different from the accessor's componentType can be done for a use case like attribute compression.
* The data is being read, compressed, and then written back as a different, smaller data type. This is done in place
* and then the gaps can be removed using uninterleaveAndPackBuffers.
*
* @param {Array} data The data to write into the accessor.
* @param {Number} [componentType=this.componentType] The component type to use for writing the data.
* @param {Number} [dataOffset=0] Read starting from a different position in the data array.
*/
AccessorReader.prototype.write = function(data, componentType, dataOffset) {
componentType = defaultValue(componentType, this.componentType);
dataOffset = defaultValue(dataOffset, 0);
Expand All @@ -66,14 +92,25 @@ AccessorReader.prototype.write = function(data, componentType, dataOffset) {
}
};

/**
* Get if the AccessorReader is at the end of the accessor data.
*
* @returns {Boolean} true if there is more data to read, false if not.
*/
AccessorReader.prototype.hasNext = function() {
return this.index < this.count;
};

/**
* Increment AccessorReader.index by one.
*/
AccessorReader.prototype.next = function() {
this.index++;
};

/**
* Set AccessorReader.index to zero.
*/
AccessorReader.prototype.reset = function() {
this.index = 0;
};
58 changes: 51 additions & 7 deletions lib/NodeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ function flattenTransform(parameters, node, parent) {
node.extras._pipeline.flatTransform = Matrix4.multiply(parentTransform, localTransform, new Matrix4());
}

// computes flattened Matrix4 transforms for every node in the scene
// store these matrices for each node in extras._pipeline.flatTransform
/**
* Computes flattened Matrix4 transforms for every node in the scene.
* Store these matrices for each node in extras._pipeline.flatTransform.
*
* @param {Object} scene The glTF scene to traverse.
* @param {Object} nodes glTF top-level nodes object.
*/
function computeFlatTransformScene(scene, nodes) {
var rootNodeIDs = scene.nodes;
var parameters = {
Expand All @@ -56,6 +61,16 @@ function addNodeToArray(parameters, node) {
}

// get all the nodes in a scene

/**
* Get all the nodes in a scene.
* If resultArray is undefined, a new array will be created.
*
* @param {scene} The glTF scene to traverse.
* @param {Object} nodes glTF top-level nodes object.
* @param {Array} [resultArray] Store the nodes into resultArray.
* @returns {Array} An array of all nodes in the scene.
*/
function getAllNodesInScene(scene, nodes, resultArray) {
var rootNodeIDs = scene.nodes;
if (!defined(resultArray)) {
Expand All @@ -73,9 +88,16 @@ function getAllNodesInScene(scene, nodes, resultArray) {
return resultArray;
}

// Perform a stack based iterative depth first traversal on the specified node.
// Apply functionChildParent to all of its children.
// functionChildParent functions take this node and the parent as arguments
/**
* Perform a stack based iterative depth first traversal on the specified node.
* Apply functionChildParent to all of its children.
* functionChildParent has the signature: function(parameters, childNode, parentNode).
*
* @param {Object} rootNode The glTF node to traverse.
* @param {Object} allNodes glTF top-level nodes object.
* @param {Function} functionChildParent Function to apply to the child and parent.
* @param {Object} parameters Parameters to pass as the first argument to functionChildParent.
*/
function depthFirstTraversal(rootNode, allNodes, functionChildParent, parameters) {
// process the root node
functionChildParent(parameters, rootNode);
Expand All @@ -102,6 +124,15 @@ function depthFirstTraversal(rootNode, allNodes, functionChildParent, parameters
}

// gets the local matrix of a node, or computes it from TRS

/**
* Gets the local matrix of a node, or computes it from TRS.
* If result is undefined, creates a new Matrix4.
*
* @param {Object} node glTF node with a transform.
* @param {Matrix4} [result] Store the transform matrix into result.
* @returns {Matrix4} The node transform matrix.
*/
function getLocalMatrix4(node, result) {
if (!defined(result)) {
result = new Matrix4();
Expand Down Expand Up @@ -139,8 +170,15 @@ var packedParametersScratch = {
primitiveFunction : undefined
};

// Perform an operation on each primitive in a scene.
// PrimitiveFunction should expect primitive, a meshPrimitiveID, some parameters, and the node itself
/**
* Perform an operation on each primitive in a scene.
* primitiveFunction has the signature: function(primitive, meshPrimitiveId, parameters, node).
*
* @param {Object} gltf A javascript object holding a glTF hierarchy.
* @param {Object} scene The glTF scene to traverse.
* @param {Function} primitiveFunction The function to apply to each primitive.
* @param {Object} parameters Parameters to pass to primitiveFunction.
*/
function forEachPrimitiveInScene(gltf, scene, primitiveFunction, parameters) {
var rootNodeNames = scene.nodes;
var allNodes = gltf.nodes;
Expand Down Expand Up @@ -176,6 +214,12 @@ function forEachPrimitiveInNode(packedParameters, node) {
}
}

/**
* Generates a mapping for each meshId to the nodeIds of the nodes that use that mesh.
*
* @param {Object} gltf A javascript object holding a glTF hierarchy.
* @returns {Object} An object with meshIds as keys with arrays of corresponding nodeIds as values.
*/
function mapMeshesToNodes(gltf) {
var meshesToNodes = {};
var nodes = gltf.nodes;
Expand Down
98 changes: 12 additions & 86 deletions lib/addDefaults.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict';
var Cesium = require('cesium');

var Cartesian3 = Cesium.Cartesian3;
var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;
var Quaternion = Cesium.Quaternion;
var WebGLConstants = Cesium.WebGLConstants;
var modelMaterialsCommon = require('./modelMaterialsCommon');

var addExtensionsUsed = require('./addExtensionsUsed');
var findAccessorMinMax = require('./findAccessorMinMax');
var modelMaterialsCommon = require('./modelMaterialsCommon');

module.exports = addDefaults;

Expand Down Expand Up @@ -121,82 +124,6 @@ function imageDefaults(gltf) {
}
}

// TODO: move lightDefaults() for KHR_materials_common elsewhere?
function lightDefaults(gltf) {
if (!defined(gltf.extensions)) {
gltf.extensions = {};
}
var extensions = gltf.extensions;

if (!defined(extensions.KHR_materials_common)) {
extensions.KHR_materials_common = {};
}
var khrMaterialsCommon = extensions.KHR_materials_common;

if (defined(gltf.lights)) {
// glTF 0.8 backward compatibility
khrMaterialsCommon.lights = gltf.lights;
delete gltf.lights;
}
else if (!defined(khrMaterialsCommon.lights)) {
khrMaterialsCommon.lights = {};
}
var lights = khrMaterialsCommon.lights;

for (var name in lights) {
if (lights.hasOwnProperty(name)) {
var light = lights[name];
if (light.type === 'ambient') {
if (!defined(light.ambient)) {
light.ambient = {};
}
var ambientLight = light.ambient;

if (!defined(ambientLight.color)) {
ambientLight.color = [1.0, 1.0, 1.0];
}
} else if (light.type === 'directional') {
if (!defined(light.directional)) {
light.directional = {};
}
var directionalLight = light.directional;

if (!defined(directionalLight.color)) {
directionalLight.color = [1.0, 1.0, 1.0];
}
} else if (light.type === 'point') {
if (!defined(light.point)) {
light.point = {};
}
var pointLight = light.point;

if (!defined(pointLight.color)) {
pointLight.color = [1.0, 1.0, 1.0];
}

pointLight.constantAttenuation = defaultValue(pointLight.constantAttenuation, 1.0);
pointLight.linearAttenuation = defaultValue(pointLight.linearAttenuation, 0.0);
pointLight.quadraticAttenuation = defaultValue(pointLight.quadraticAttenuation, 0.0);
} else if (light.type === 'spot') {
if (!defined(light.spot)) {
light.spot = {};
}
var spotLight = light.spot;

if (!defined(spotLight.color)) {
spotLight.color = [1.0, 1.0, 1.0];
}

spotLight.constantAttenuation = defaultValue(spotLight.constantAttenuation, 1.0);
spotLight.fallOffAngle = defaultValue(spotLight.fallOffAngle, 3.14159265);
spotLight.fallOffExponent = defaultValue(spotLight.fallOffExponent, 0.0);
spotLight.linearAttenuation = defaultValue(spotLight.linearAttenuation, 0.0);
spotLight.quadraticAttenuation = defaultValue(spotLight.quadraticAttenuation, 0.0);
}
}
}
}

function checkIfFloatVec4(value) {
if (defined(value) && value.length === 4) {
if (typeof(value[0]) === 'number' &&
Expand Down Expand Up @@ -280,14 +207,7 @@ function materialDefaults(gltf, options) {
if (!defined(material.technique)) {
if (!defined(material.extensions)) {
guessExtensionTechnique(gltf, material, options);

if (!defined(gltf.extensionsUsed)) {
gltf.extensionsUsed = [];
}
var extensionsUsed = gltf.extensionsUsed;
if (extensionsUsed.indexOf('KHR_materials_common') === -1) {
extensionsUsed.push('KHR_materials_common');
}
addExtensionsUsed(gltf, 'KHR_materials_common');
}
}
else if (!defined(material.values)) {
Expand Down Expand Up @@ -584,6 +504,13 @@ function textureDefaults(gltf) {
}
}

/**
* Adds default glTF values if they don't exist.
*
* @param {Object} gltf A javascript object holding a glTF hierarchy.
* @param {Object} [options={}]
* @returns {Object} The modified glTF.
*/
function addDefaults(gltf, options) {
options = defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);

Expand All @@ -604,7 +531,6 @@ function addDefaults(gltf, options) {
bufferViewDefaults(gltf);
cameraDefaults(gltf);
imageDefaults(gltf);
lightDefaults(gltf);
techniqueDefaults(gltf);
materialDefaults(gltf, options);
meshDefaults(gltf);
Expand Down
Loading