Skip to content

Commit

Permalink
Merge pull request #356 from AnalyticalGraphicsInc/node-trs-default
Browse files Browse the repository at this point in the history
Add default translation, rotation, scale to node targeted for animation
  • Loading branch information
lilleyse authored Mar 20, 2018
2 parents 23f0082 + f2dce28 commit 6177109
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/ForEach.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ ForEach.animation = function(gltf, handler) {
ForEach.topLevel(gltf, 'animations', handler);
};

ForEach.animationChannel = function(animation, handler) {
var channels = animation.channels;
ForEach.object(channels, handler);
};

ForEach.animationSampler = function(animation, handler) {
var samplers = animation.samplers;
if (defined(samplers)) {
Expand Down
29 changes: 29 additions & 0 deletions lib/addDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,34 @@ function addDefaultsFromTemplate(object, template) {
}
}

function getAnimatedNodes(gltf) {
var nodes = {};
ForEach.animation(gltf, function(animation) {
ForEach.animationChannel(animation, function(channel) {
var target = channel.target;
var nodeId = target.node;
var path = target.path;
// Ignore animations that target 'weights'
if (path === 'translation' || path === 'rotation' || path === 'scale') {
nodes[nodeId] = true;
}
});
});
return nodes;
}

function addDefaultTransformToAnimatedNodes(gltf) {
var animatedNodes = getAnimatedNodes(gltf);
ForEach.node(gltf, function(node, id) {
if (defined(animatedNodes[id])) {
delete node.matrix;
node.translation = defaultValue(node.translation, [0.0, 0.0, 0.0]);
node.rotation = defaultValue(node.rotation, [0.0, 0.0, 0.0, 1.0]);
node.scale = defaultValue(node.scale, [1.0, 1.0, 1.0]);
}
});
}

var defaultMaterial = {
values : {
emission : [
Expand Down Expand Up @@ -475,6 +503,7 @@ function inferBufferViewTargets(gltf) {
function addDefaults(gltf, options) {
options = defaultValue(options, {});
addDefaultsFromTemplate(gltf, gltfTemplate);
addDefaultTransformToAnimatedNodes(gltf);
addDefaultMaterial(gltf);
addDefaultTechnique(gltf);
addDefaultByteOffsets(gltf);
Expand Down
33 changes: 33 additions & 0 deletions specs/lib/addDefaultsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,37 @@ describe('addDefaults', function() {
expect(gltf.bufferViews[2].target).not.toBeDefined();
expect(gltf.bufferViews[3].target).toEqual(WebGLConstants.ARRAY_BUFFER);
});

it('Adds animation properties', function() {
var gltf = {
animations: [{
channels: [
{
sampler: 0,
target: {
node: 0,
path: 'rotation'
}
}
],
samplers: [{
input: 0,
output: 1
}]
}],
nodes: [
{
mesh: 0
}
]
};

addDefaults(gltf);
var node = gltf.nodes[0];
expect(node.translation).toEqual([0, 0, 0]);
expect(node.rotation).toEqual([0, 0, 0, 1]);
expect(node.scale).toEqual([1, 1, 1]);
expect(node.matrix).toBeUndefined();
});

});

0 comments on commit 6177109

Please sign in to comment.