Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Apr 2, 2018
1 parent 6d2b0f8 commit 6405fa5
Show file tree
Hide file tree
Showing 34 changed files with 588 additions and 74 deletions.
43 changes: 43 additions & 0 deletions lib/ForEach.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ ForEach.accessor = function(gltf, handler) {
ForEach.topLevel(gltf, 'accessors', handler);
};

// TODO : also loop over ForEach.meshPrimitiveTarget?
ForEach.accessorWithSemantic = function(gltf, semantic, handler) {
ForEach.mesh(gltf, function(mesh) {
ForEach.meshPrimitive(mesh, function(primitive) {
Expand All @@ -57,6 +58,32 @@ ForEach.accessorWithSemantic = function(gltf, semantic, handler) {
});
};

ForEach.accessorContainingVertexAttributeData = function(gltf, handler) {
ForEach.mesh(gltf, function(mesh) {
ForEach.meshPrimitive(mesh, function (primitive) {
ForEach.meshPrimitiveAttribute(primitive, function(accessorId) {
handler(accessorId);
});
ForEach.meshPrimitiveTarget(primitive, function(target) {
ForEach.meshPrimitiveTargetAttribute(target, function(accessorId) {
handler(accessorId);
});
});
});
});
};

ForEach.accessorContainingIndexData = function(gltf, handler) {
ForEach.mesh(gltf, function (mesh) {
ForEach.meshPrimitive(mesh, function (primitive) {
var indices = primitive.indices;
if (defined(indices)) {
handler(indices);
}
});
});
};

ForEach.animation = function(gltf, handler) {
ForEach.topLevel(gltf, 'animations', handler);
};
Expand All @@ -75,6 +102,10 @@ ForEach.buffer = function(gltf, handler) {
ForEach.topLevel(gltf, 'buffers', handler);
};

ForEach.bufferLegacy = function(gltf, handler) {
ForEach.objectLegacy(gltf.buffers, handler);
};

ForEach.bufferView = function(gltf, handler) {
ForEach.topLevel(gltf, 'bufferViews', handler);
};
Expand All @@ -87,6 +118,10 @@ ForEach.image = function(gltf, handler) {
ForEach.topLevel(gltf, 'images', handler);
};

ForEach.imageLegacy = function(gltf, handler) {
ForEach.objectLegacy(gltf.images, handler);
};

ForEach.compressedImage = function(image, handler) {
if (defined(image.extras)) {
var compressedImages = image.extras.compressedImage3DTiles;
Expand Down Expand Up @@ -200,6 +235,14 @@ ForEach.shader = function(gltf, handler) {
ForEach.topLevel(gltf, 'shaders', handler);
};

ForEach.shaderLegacy = function(gltf, handler) {
if (defined(gltf.shaders)) {
ForEach.objectLegacy(gltf.shaders, handler);
} else {
ForEach.shader(gltf, handler);
}
};

ForEach.skin = function(gltf, handler) {
ForEach.topLevel(gltf, 'skins', handler);
};
Expand Down
69 changes: 21 additions & 48 deletions lib/addDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,25 @@ function addDefaults(gltf) {
});
});

addVertexBuffersDefaults(gltf);
addIndexBuffersDefaults(gltf);
ForEach.accessorContainingVertexAttributeData(gltf, function(accessorId) {
var accessor = gltf.accessors[accessorId];
var bufferViewId = accessor.bufferView;
accessor.normalized = defaultValue(accessor.normalized, false);
if (defined(bufferViewId)) {
var bufferView = gltf.bufferViews[bufferViewId];
bufferView.byteStride = getAccessorByteStride(gltf, accessor);
bufferView.target = WebGLConstants.ARRAY_BUFFER;
}
});

ForEach.accessorContainingIndexData(gltf, function(accessorId) {
var accessor = gltf.accessors[accessorId];
var bufferViewId = accessor.bufferView;
if (defined(bufferViewId)) {
var bufferView = gltf.bufferViews[bufferViewId];
bufferView.target = WebGLConstants.ELEMENT_ARRAY_BUFFER;
}
});

ForEach.material(gltf, function(material) {
var extensions = defaultValue(material.extensions, defaultValue.EMPTY_OBJECT);
Expand All @@ -61,7 +78,7 @@ function addDefaults(gltf) {
return;
}

// TODO : this hasExtension check can be removed once https://github.com/KhronosGroup/glTF/pull/1296 is finished
// TODO KHR_technique_webgl: this hasExtension check can be removed once https://github.com/KhronosGroup/glTF/pull/1296 is finished
if (!hasExtension(gltf, 'KHR_technique_webgl')) {
material.emissiveFactor = defaultValue(material.emissiveFactor, [0.0, 0.0, 0.0]);
material.alphaMode = defaultValue(material.alphaMode, 'OPAQUE');
Expand Down Expand Up @@ -122,7 +139,7 @@ function addDefaults(gltf) {
}

if (hasExtension(gltf, 'KHR_technique_webgl')) {
// TODO : setting these defaults will need to move to the 1.0->2.0(KHR_technique_webgl) code in updateVersion
// TODO KHR_technique_webgl: setting these defaults will need to move to the 1.0->2.0 code in updateVersion
// glTF 1.0 style materials
addDefaultMaterial(gltf);
addDefaultTechnique(gltf);
Expand Down Expand Up @@ -271,47 +288,3 @@ function addTextureDefaults(texture) {
texture.texCoord = defaultValue(texture.texCoord, 0);
}
}

function addIndexBuffersDefaults(gltf) {
ForEach.mesh(gltf, function (mesh) {
ForEach.meshPrimitive(mesh, function (primitive) {
var indices = primitive.indices;
if (defined(indices)) {
var accessor = gltf.accessors[indices];
var bufferViewId = accessor.bufferView;
if (defined(bufferViewId)) {
var bufferView = gltf.bufferViews[bufferViewId];
bufferView.target = WebGLConstants.ELEMENT_ARRAY_BUFFER;
}
}
});
});
}

function addVertexBufferDefaults(gltf, accessorId) {
var accessor = gltf.accessors[accessorId];
var bufferViewId = accessor.bufferView;

accessor.normalized = defaultValue(accessor.normalized, false);

if (defined(bufferViewId)) {
var bufferView = gltf.bufferViews[bufferViewId];
bufferView.byteStride = getAccessorByteStride(gltf, accessor);
bufferView.target = WebGLConstants.ARRAY_BUFFER;
}
}

function addVertexBuffersDefaults(gltf) {
ForEach.mesh(gltf, function(mesh) {
ForEach.meshPrimitive(mesh, function (primitive) {
ForEach.meshPrimitiveAttribute(primitive, function(accessorId) {
addVertexBufferDefaults(gltf, accessorId);
});
ForEach.meshPrimitiveTarget(primitive, function(target) {
ForEach.meshPrimitiveTargetAttribute(target, function(accessorId) {
addVertexBufferDefaults(gltf, accessorId);
});
});
});
});
}
13 changes: 7 additions & 6 deletions lib/addPipelineExtras.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var Cesium = require('cesium');
var ForEach = require('./ForEach');

var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;

module.exports = addPipelineExtras;

Expand All @@ -16,13 +16,14 @@ module.exports = addPipelineExtras;
* @private
*/
function addPipelineExtras(gltf) {
ForEach.objectLegacy(gltf.shaders, function(shader) {
// TODO KHR_technique_webgl
ForEach.shaderLegacy(gltf, function(shader) {
addExtras(shader);
});
ForEach.objectLegacy(gltf.buffers, function(buffer) {
ForEach.bufferLegacy(gltf, function(buffer) {
addExtras(buffer);
});
ForEach.objectLegacy(gltf.images, function (image) {
ForEach.imageLegacy(gltf, function (image) {
addExtras(image);
ForEach.compressedImage(image, function(compressedImage) {
addExtras(compressedImage);
Expand All @@ -35,6 +36,6 @@ function addPipelineExtras(gltf) {
}

function addExtras(object) {
object.extras = defaultValue(object.extras, {});
object.extras._pipeline = defaultValue(object.extras._pipeline, {});
object.extras = defined(object.extras) ? object.extras : {};
object.extras._pipeline = defined(object.extras._pipeline) ? object.extras._pipeline : {};
}
4 changes: 2 additions & 2 deletions lib/dataUriToBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module.exports = dataUriToBuffer;
* @private
*/
function dataUriToBuffer(dataUri) {
var data = dataUri.slice(dataUri.indexOf(','));
if (dataUri.indexOf('base64')) {
var data = dataUri.slice(dataUri.indexOf(',') + 1);
if (dataUri.indexOf('base64') >= 0) {
return Buffer.from(data, 'base64');
}
return Buffer.from(data, 'utf8');
Expand Down
1 change: 1 addition & 0 deletions lib/getJsonBufferPadded.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function getJsonBufferPadded(json) {
var byteLength = Buffer.byteLength(string);
var remainder = byteLength % boundary;
var padding = (remainder === 0) ? 0 : boundary - remainder;
// TODO more efficient if concatting buffers
var whitespace = '';
for (var i = 0; i < padding; ++i) {
whitespace += ' ';
Expand Down
2 changes: 1 addition & 1 deletion lib/hasExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ module.exports = hasExtension;
* @private
*/
function hasExtension(gltf, extension) {
return defined(gltf.extensionsUsed) && (gltf.extensionsUsed.indexOf(extension) > -1);
return defined(gltf.extensionsUsed) && (gltf.extensionsUsed.indexOf(extension) >= 0);
}
21 changes: 8 additions & 13 deletions lib/parseGlb.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ var removeExtensionsUsed = require('./removeExtensionsUsed');

var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;
var DeveloperError = Cesium.DeveloperError;
var getMagic = Cesium.getMagic;
var getStringFromTypedArray = Cesium.getStringFromTypedArray;
var RuntimeError = Cesium.RuntimeError;

var sizeOfUint32 = 4;

Expand All @@ -16,11 +16,9 @@ module.exports = parseGlb;
/**
* Convert a binary glTF to glTF.
*
* This file is bundled in Cesium and does not require the Node standard library or any third party libraries.
* The returned glTF has pipeline extras included. The binary data is stored in gltf.buffers[0].extras._pipeline.source
* as a Uint8Array.
* The returned glTF has pipeline extras included. The binary data is stored in gltf.buffers[0].extras._pipeline.source.
*
* @param {Uint8Array} glb The glb data to parse.
* @param {Buffer} glb The glb data to parse.
* @returns {Object} A javascript object containing a glTF asset with pipeline extras included.
*
* @private
Expand All @@ -29,13 +27,13 @@ function parseGlb(glb) {
// Check that the magic string is present
var magic = getMagic(glb);
if (magic !== 'glTF') {
throw new DeveloperError('File is not valid binary glTF');
throw new RuntimeError('File is not valid binary glTF');
}

var header = readHeader(glb, 0, 5);
var version = header[1];
if (version !== 1 && version !== 2) {
throw new DeveloperError('Binary glTF version is not 1 or 2');
throw new RuntimeError('Binary glTF version is not 1 or 2');
}

if (version === 1) {
Expand All @@ -61,14 +59,15 @@ function parseGlbVersion1(glb, header) {

// Check that the content format is 0, indicating that it is JSON
if (contentFormat !== 0) {
throw new DeveloperError('Binary glTF scene format is not JSON');
throw new RuntimeError('Binary glTF scene format is not JSON');
}

var jsonStart = 20;
var binaryStart = jsonStart + contentLength;

var contentString = getStringFromTypedArray(glb, jsonStart, contentLength);
var gltf = JSON.parse(contentString);
addPipelineExtras(gltf);

var binaryBuffer = glb.subarray(binaryStart, length);

Expand All @@ -77,11 +76,7 @@ function parseGlbVersion1(glb, header) {
// In some older models, the binary glTF buffer is named KHR_binary_glTF
var binaryGltfBuffer = defaultValue(buffers.binary_glTF, buffers.KHR_binary_glTF);
if (defined(binaryGltfBuffer)) {
binaryGltfBuffer.extras = {
_pipeline: {
source : binaryBuffer
}
};
binaryGltfBuffer.extras._pipeline.source = binaryBuffer;
}
}
// Remove the KHR_binary_glTF extension
Expand Down
1 change: 0 additions & 1 deletion lib/processGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var updateVersion = require('./updateVersion');
var writeResources = require('./writeResources');

var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;

module.exports = processGltf;

Expand Down
8 changes: 5 additions & 3 deletions lib/readResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var fsExtra = require('fs-extra');
var Jimp = require('jimp');
var path = require('path');
var Promise = require('bluebird');
var addPipelineExtras = require('./addPipelineExtras');
var dataUriToBuffer = require('./dataUriToBuffer');
var ForEach = require('./ForEach');
var getImageExtension = require('./getImageExtension');
Expand All @@ -30,24 +31,25 @@ module.exports = readResources;
* @private
*/
function readResources(gltf, options) {
addPipelineExtras(gltf);
options = defaultValue(options, {});
options.decodeImages = defaultValue(options.decodeImages, false);
options.checkTransparency = defaultValue(options.checkTransparency, false);

var bufferPromises = [];
var resourcePromises = [];

ForEach.objectLegacy(gltf.buffers, function(buffer) {
ForEach.bufferLegacy(gltf, function(buffer) {
bufferPromises.push(readBuffer(gltf, buffer, options));
});

// Buffers need to be read first because images and shader may resolve to bufferViews
return Promise.all(bufferPromises)
.then(function() {
ForEach.objectLegacy(gltf.shaders, function (shader) {
ForEach.shaderLegacy(gltf, function (shader) {
resourcePromises.push(readShader(gltf, shader, options));
});
ForEach.objectLegacy(gltf.images, function (image) {
ForEach.imageLegacy(gltf, function (image) {
resourcePromises.push(readImage(gltf, image, options));
ForEach.compressedImage(image, function(compressedImage) {
resourcePromises.push(readImage(gltf, compressedImage, options));
Expand Down
8 changes: 8 additions & 0 deletions specs/lib/ForEachSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
var ForEach = require('../../lib/ForEach');

// TODO :
describe('ForEach', function() {
it('ForEach', function() {
});
});
Loading

0 comments on commit 6405fa5

Please sign in to comment.