Пример #1
0
        public List<animation> ExportTransform(IList<Keyframe> keyframes, string name, string target)
        {
            var anims = new List<animation>();
            var inputs = new List<InputLocal>();

            var outputs = new List<float>(keyframes.Count * 16);
            foreach (var keyframe in keyframes)
            {
                var transform = Matrix4.Identity;
                if (keyframe.hasRotation)
                    transform *= Matrix4.CreateFromQuaternion(keyframe.rotation.Inverted());

                if (keyframe.hasScaleShear)
                {
                    var scaleShear = Matrix4.Identity;
                    for (int i = 0; i < 3; i++)
                    {
                        for (int j = 0; j < 3; j++)
                            scaleShear[i, j] = keyframe.scaleShear[i, j];
                    }

                    transform *= scaleShear;
                }

                if (keyframe.hasTranslation)
                {
                    transform[0, 3] += keyframe.translation[0];
                    transform[1, 3] += keyframe.translation[1];
                    transform[2, 3] += keyframe.translation[2];
                }

                for (int i = 0; i < 4; i++)
                {
                    for (int j = 0; j < 4; j++)
                        outputs.Add(transform[i, j]);
                }
            }

            var interpolations = new List<string>(keyframes.Count);
            for (int i = 0; i < keyframes.Count; i++)
            {
                // TODO: Add control point estimation code and add in/out tangents for Bezier
                //interpolations.Add("BEZIER");
                interpolations.Add("LINEAR");
            }

            var knots = new List<float>(keyframes.Count);
            foreach (var keyframe in keyframes)
            {
                knots.Add(keyframe.time);
            }

            /*
             * Fix up animations that have only one keyframe by adding another keyframe at
             * the end of the animation.
             * (This mainly applies to DaIdentity and DnConstant32f)
             */
            if (keyframes.Count == 1)
            {
                knots.Add(ParentAnimation.Duration);
                for (int i = 0; i < 16; i++)
                    outputs.Add(outputs[i]);
                interpolations.Add(interpolations[0]);
            }

            var knotsSource = ColladaUtils.MakeFloatSource(name, "inputs", new string[] { "TIME" }, knots.ToArray());
            var knotsInput = new InputLocal();
            knotsInput.semantic = "INPUT";
            knotsInput.source = "#" + knotsSource.id;
            inputs.Add(knotsInput);

            var outSource = ColladaUtils.MakeFloatSource(name, "outputs", new string[] { "TRANSFORM" }, outputs.ToArray(), 16, "float4x4");
            var outInput = new InputLocal();
            outInput.semantic = "OUTPUT";
            outInput.source = "#" + outSource.id;
            inputs.Add(outInput);

            var interpSource = ColladaUtils.MakeNameSource(name, "interpolations", new string[] { "INTERPOLATION" }, interpolations.ToArray());

            var interpInput = new InputLocal();
            interpInput.semantic = "INTERPOLATION";
            interpInput.source = "#" + interpSource.id;
            inputs.Add(interpInput);

            var sampler = new sampler();
            sampler.id = name + "_sampler";
            sampler.input = inputs.ToArray();

            var channel = new channel();
            channel.source = "#" + sampler.id;
            channel.target = target;

            var animation = new animation();
            animation.id = name;
            animation.name = name;
            var animItems = new List<object>();
            animItems.Add(knotsSource);
            animItems.Add(outSource);
            animItems.Add(interpSource);
            animItems.Add(sampler);
            animItems.Add(channel);
            animation.Items = animItems.ToArray();
            anims.Add(animation);
            return anims;
        }
Пример #2
0
        protected void CreateChannel(List<object> listAnimationObjects, uint caseIndex, string sData, string sDataType, string sTarget, List<double> lValues)
        {
            string sTime = string.Format("sTime_{0}_ID", caseIndex);
            string sInterp = string.Format("sInterp_{0}_ID", caseIndex);

            // source
            source source_ = new source()
            {
                id = string.Format("sValue_{0}_{1}_ID", caseIndex, sData),
                name = string.Format("sValue_{0}_{1}", caseIndex, sData),
                Item = new float_array() { id = string.Format("fa_{0}_{1}_ID", caseIndex, sData), count = (ulong)lValues.Count, Values = lValues.ToArray() },
                technique_common = new sourceTechnique_common()
                {
                    accessor = new accessor()
                    {
                        source = string.Format("#fa_{0}_{1}_ID", caseIndex, sData),
                        count = (ulong)lValues.Count,
                        stride = 1,
                        param = new param[]
                        {
                            new param() { name=sDataType, type="float" }
                        }
                    }
                }
            };
            listAnimationObjects.Add(source_);

            // sampler
            sampler sampler_ = new sampler()
            {
                id = string.Format("sampler_{0}_{1}_ID", caseIndex, sData),
                input = new InputLocal[]
                {
                    new InputLocal() { semantic="INPUT", source="#" + sTime },
                    new InputLocal() { semantic="OUTPUT", source="#" + source_.id },
                    new InputLocal() { semantic="INTERPOLATION", source="#" + sInterp }
                }
            };
            listAnimationObjects.Add(sampler_);

            // channel
            channel channel_ = new channel()
            {
                source = "#" + sampler_.id,
                target = sTarget
            };
            listAnimationObjects.Add(channel_);
        }