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

Add default translation, rotation, scale to node targeted for animation #356

Merged
merged 2 commits into from
Mar 20, 2018
Merged
Changes from all 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
5 changes: 5 additions & 0 deletions lib/ForEach.js
Original file line number Diff line number Diff line change
@@ -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)) {
29 changes: 29 additions & 0 deletions lib/addDefaults.js
Original file line number Diff line number Diff line change
@@ -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 : [
@@ -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);
33 changes: 33 additions & 0 deletions specs/lib/addDefaultsSpec.js
Original file line number Diff line number Diff line change
@@ -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();
});

});