Skip to content

Commit

Permalink
Infer missing bufferView targets
Browse files Browse the repository at this point in the history
  • Loading branch information
lasalvavida committed Apr 28, 2017
1 parent 1406b32 commit c43c6d8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/addDefaults.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var Cesium = require('cesium');
var addToArray = require('./addToArray');
var ForEach = require('./ForEach');

var clone = Cesium.clone;
var defaultValue = Cesium.defaultValue;
Expand Down Expand Up @@ -428,6 +429,47 @@ function optimizeForCesium(gltf) {
}
}

function inferBufferViewTargets(gltf) {
// If bufferView elements are missing targets, we can infer their type from their use
var needsTarget = {};
var shouldTraverse = false;
ForEach.bufferView(gltf, function(bufferView, bufferViewId) {
if (!defined(bufferView.target)) {
needsTarget[bufferViewId] = true;
shouldTraverse = true;
}
});
if (shouldTraverse) {
var accessors = gltf.accessors;
var bufferViews = gltf.bufferViews;
ForEach.mesh(gltf, function (mesh) {
ForEach.meshPrimitive(mesh, function (primitive) {
var indices = primitive.indices;
if (defined(indices)) {
var accessor = accessors[indices];
var bufferViewId = accessor.bufferView;
if (needsTarget[bufferViewId]) {
var bufferView = bufferViews[bufferViewId];
if (defined(bufferView)) {
bufferView.target = WebGLConstants.ELEMENT_ARRAY_BUFFER;
}
}
}
ForEach.meshPrimitiveAttribute(primitive, function (accessorId) {
var accessor = accessors[accessorId];
var bufferViewId = accessor.bufferView;
if (needsTarget[bufferViewId]) {
var bufferView = bufferViews[bufferViewId];
if (defined(bufferView)) {
bufferView.target = WebGLConstants.ARRAY_BUFFER;
}
}
});
});
});
}
}

/**
* Adds default glTF values if they don't exist.
*
Expand All @@ -448,6 +490,7 @@ function addDefaults(gltf, options) {
addDefaultTechnique(gltf);
enableDiffuseTransparency(gltf);
selectDefaultScene(gltf);
inferBufferViewTargets(gltf);
if (options.optimizeForCesium) {
optimizeForCesium(gltf);
}
Expand Down
35 changes: 35 additions & 0 deletions specs/lib/addDefaultsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,39 @@ describe('addDefaults', function() {

expect(gltf.scene).toEqual(0);
});

it('infers missing bufferView targets', function() {
var gltf = {
accessors: [
{
bufferView: 0
}, {
bufferView: 1
}, {
bufferView: 2
}
],
bufferViews: [
{},
{},
{}
],
meshes: [
{
primitives: [
{
attributes: {
POSITION: 0
},
indices: 1
}
]
}
]
};
addDefaults(gltf);
expect(gltf.bufferViews[0].target).toEqual(WebGLConstants.ARRAY_BUFFER);
expect(gltf.bufferViews[1].target).toEqual(WebGLConstants.ELEMENT_ARRAY_BUFFER);
expect(gltf.bufferViews[2].target).not.toBeDefined();
});
});

0 comments on commit c43c6d8

Please sign in to comment.