public static void Import(FullModelData fmd, string path)
        {
            AnimationFile animationFile = new AnimationFile();

            animationFile.Read(path);

            foreach (AnimationFileObject animationObject in animationFile.Objects)
            {
                Object3D object3D = fmd.GetObject3DByHash(new HashName(animationObject.Name));

                Log.Default.Info("Trying to add animation to " + animationObject.Name);
                if (object3D != null)
                {
                    Log.Default.Info("Found " + animationObject.Name);

                    object3D.Animations.Clear(); // Kill the old anims.

                    if (animationObject.RotationKeyframes.Count > 0)
                    {
                        QuatLinearRotationController quatLinearRotationController = AddRotations(object3D, animationObject.RotationKeyframes);
                        fmd.AddSection(quatLinearRotationController);
                    }

                    if (animationObject.PositionKeyframes.Count > 0)
                    {
                        LinearVector3Controller linearVector3Controller = AddPositions(object3D, animationObject.PositionKeyframes);
                        fmd.AddSection(linearVector3Controller);
                    }
                }
                else
                {
                    Log.Default.Info("Not Found " + animationObject.Name);
                }
            }
        }
        public static string ExportFile(FullModelData data, string path)
        {
            AnimationFile animationFile = new AnimationFile();

            foreach (Object3D object3D in data.SectionsOfType <Object3D>())
            {
                if (object3D.Animations.Count > 0)
                {
                    AnimationFileObject animationFileObject = new AnimationFileObject(object3D.HashName.String);

                    foreach (IAnimationController animationController in object3D.Animations)
                    {
                        if (animationController is LinearVector3Controller)
                        {
                            LinearVector3Controller linearVector3Controller = (LinearVector3Controller)animationController;
                            animationFileObject.PositionKeyframes = new List <Keyframe <Vector3> >(linearVector3Controller.Keyframes);
                        }
                        else if (animationController is QuatLinearRotationController)
                        {
                            QuatLinearRotationController quatLinearRotationController = (QuatLinearRotationController)animationController;
                            animationFileObject.RotationKeyframes = new List <Keyframe <Quaternion> >(quatLinearRotationController.Keyframes);
                        }
                    }

                    animationFile.Objects.Add(animationFileObject);
                }
            }

            animationFile.Write(path);

            return(path);
        }
예제 #3
0
        public override void Execute(ScriptState state)
        {
            var filepath = state.ResolvePath(File);

            state.Log.Status("Writing animation script to {0}", filepath);

            var xws = new XmlWriterSettings()
            {
                Indent = true
            };

            using XmlWriter xw = XmlWriter.Create(filepath, xws);

            xw.WriteStartDocument(true);
            xw.WriteStartElement("modelscript");

            foreach (var obj in state.Data.SectionsOfType <Object3D>())
            {
                if (obj.Animations.Count == 0)
                {
                    continue;
                }

                xw.WriteStartElement("animate");
                xw.WriteAttributeString("object", obj.Name);

                foreach (var anim in obj.Animations)
                {
                    if (anim == null)
                    {
                        xw.WriteStartElement("null");
                        xw.WriteEndElement();
                        continue;
                    }

                    xw.WriteStartElement(anim switch
                    {
                        LinearFloatController _ => "float",
                        LinearVector3Controller _ => "vector3",
                        QuatLinearRotationController _ => "quaternion",
                        _ => throw new Exception($"Unrecognised animation controller type in {obj.Name}")
                    });
        public static QuatLinearRotationController AddRotations(Object3D targetObject, IList <Keyframe <Quaternion> > keyframes)
        {
            var quatLinearRotationController = new QuatLinearRotationController();

            quatLinearRotationController.Keyframes      = new List <Keyframe <Quaternion> >(keyframes);
            quatLinearRotationController.KeyframeLength = quatLinearRotationController.Keyframes.Max(kf => kf.Timestamp);

            if (targetObject.Animations.Count == 0)
            {
                targetObject.Animations.Add(quatLinearRotationController);
                targetObject.Animations.Add(null);
            }
            else if (targetObject.Animations.Count == 1 && (targetObject.Animations[0].GetType() == typeof(LinearVector3Controller)))
            {
                targetObject.Animations.Insert(0, quatLinearRotationController);
            }
            else
            {
                throw new Exception($"Failed to insert animation in {targetObject.Name}: unrecognised controller list shape");
            }

            return(quatLinearRotationController);
        }