diff --git a/lib/ForEach.js b/lib/ForEach.js index d4b3d8b8..2452b024 100644 --- a/lib/ForEach.js +++ b/lib/ForEach.js @@ -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)) { diff --git a/lib/addDefaults.js b/lib/addDefaults.js index 766028e4..0cbb6dca 100644 --- a/lib/addDefaults.js +++ b/lib/addDefaults.js @@ -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); diff --git a/specs/lib/addDefaultsSpec.js b/specs/lib/addDefaultsSpec.js index a9d128cc..01da6e5d 100644 --- a/specs/lib/addDefaultsSpec.js +++ b/specs/lib/addDefaultsSpec.js @@ -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(); + }); + });