Exemple #1
0
        private void ProcessAnimations(Node3D model)
        {
            if (Animations.Count == 0)
            {
                return;
            }
            var srcAnimation = model.DefaultAnimation;

            foreach (var animationData in Animations)
            {
                var animation = srcAnimation;
                if (animationData.Name != DefaultAnimationName)
                {
                    animation = new Lime.Animation {
                        Id = animationData.Name
                    };
                    foreach (var node in GetAnimationNodes(model, animationData))
                    {
                        OverrideAnimation(node, srcAnimation, animation);
                    }
                    model.Animations.Add(animation);
                }

                var animationBlending = new AnimationBlending()
                {
                    Option = animationData.Blending
                };

                foreach (var markerData in animationData.Markers)
                {
                    animation.Markers.AddOrdered(markerData.Marker.Clone());
                    if (markerData.Blending != null)
                    {
                        animationBlending.MarkersOptions.Add(
                            markerData.Marker.Id,
                            new MarkerBlending {
                            Option = markerData.Blending
                        });
                    }
                }

                foreach (var markersBlendings in animationData.MarkersBlendings)
                {
                    if (!animationBlending.MarkersOptions.ContainsKey(markersBlendings.DestMarkerId))
                    {
                        animationBlending.MarkersOptions.Add(markersBlendings.DestMarkerId, new MarkerBlending());
                    }
                    animationBlending.MarkersOptions[markersBlendings.DestMarkerId].SourceMarkersOptions
                    .Add(markersBlendings.SourceMarkerId, markersBlendings.Blending);
                }

                if (animationBlending.Option != null || animationBlending.MarkersOptions.Count > 0)
                {
                    animation.AnimationEngine = BlendAnimationEngine.Instance;
                    model.Components.GetOrAdd <AnimationBlender>().Options.Add(animation.Id ?? "", animationBlending);
                }
            }
        }
Exemple #2
0
        private void ProcessMaterialEffects(Node3D model)
        {
            var effectEngine = new MaterialEffectEngine();

            foreach (var effect in MaterialEffects)
            {
                try {
                    var effectPresenter = new MaterialEffectPresenter(effect.MaterialName, effect.Name, effect.Path);
                    model.CompoundPresenter.Add(effectPresenter);

                    if (effect.Blending != null)
                    {
                        effectPresenter.Animation.AnimationEngine = BlendAnimationEngine.Instance;
                        var blender           = effectPresenter.Scene.Components.GetOrAdd <AnimationBlender>();
                        var animationBlending = new AnimationBlending()
                        {
                            Option = effect.Blending
                        };
                        blender.Options.Add(effectPresenter.Animation.Id ?? "", animationBlending);
                    }

                    var animation = new Lime.Animation {
                        Id = effect.Name,
                        AnimationEngine = effectEngine
                    };

                    foreach (var marker in effectPresenter.Animation.Markers)
                    {
                        animation.Markers.Add(marker.Clone());
                    }

                    model.Animations.Add(animation);
                } catch (Lime.Exception e) {
                    if (Application.IsTangerine)
                    {
                        Console.WriteLine(e.Message);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
        private void ProcessAnimations(Node3D model)
        {
            if (Animations.Count == 0)
            {
                return;
            }

            var newAnimators       = new List <NodeAndAnimator>();
            var newAnimations      = new List <Lime.Animation>();
            var animationsToReduce = new Dictionary <string, (int, int)>();

            foreach (var animation in Animations)
            {
                if (!model.Animations.TryFind(animation.SourceAnimationId, out var srcAnimation))
                {
                    if (string.IsNullOrEmpty(animation.SourceAnimationId))
                    {
                        // If no source animation id is present, it means that model attachment has obsolete format.
                        var ac = model.Components.Get <AnimationComponent>();
                        srcAnimation = ac != null && ac.Animations.Count > 0 ? ac.Animations[0] : null;
                        // Check if the animation has been deleted but attachment hasn't been modified after that.
                        if (srcAnimation == null)
                        {
#if TANGERINE
                            Console.WriteLine($"Attachment3D Warning: skip '{ animation.Id }' animation applying. Source Fbx file have no animation data.");
#endif // TANGERINE
                            continue;
                        }
                    }
                    else
                    {
#if TANGERINE
                        Console.WriteLine($"Attachment3D Warning: source animation '{ animation.SourceAnimationId }' not found");
#endif // TANGERINE
                        continue;
                    }
                }

                var animationId = animation.Id;
                // TODO: Replace 'Default' animation with its source animation id in all referring to Lime projects.
                if (animationId == "Default")
                {
                    animationId = srcAnimation.Id;
                }

                var newAnimation = srcAnimation;
                if (animationId != animation.SourceAnimationId)
                {
                    newAnimation = new Lime.Animation {
                        Id = animationId
                    };
                    newAnimations.Add(newAnimation);
                    model.Animations.Add(newAnimation);
                    foreach (var node in GetAnimationNodes(model, animation))
                    {
                        foreach (var animator in node.Animators)
                        {
                            if (animator.AnimationId == srcAnimation.Id)
                            {
                                var newAnimator = Cloner.Clone(animator);
                                animator.AnimationId = newAnimation.Id;
                                CopyKeys(animator, newAnimator, animation.StartFrame, animation.LastFrame);
                                if (newAnimator.Keys.Count > 0)
                                {
                                    newAnimators.Add(new NodeAndAnimator {
                                        Node     = node,
                                        Animator = newAnimator
                                    });
                                }
                            }
                        }
                    }
                }
                else
                {
                    animationsToReduce.Add(newAnimation.Id, (animation.StartFrame, animation.LastFrame));
                }

                var animationBlending = new AnimationBlending()
                {
                    Option = animation.Blending
                };

                foreach (var data in animation.Markers)
                {
                    newAnimation.Markers.AddOrdered(data.Marker.Clone());
                    if (data.Blending != null)
                    {
                        animationBlending.MarkersOptions.Add(
                            data.Marker.Id,
                            new MarkerBlending {
                            Option = data.Blending
                        });
                    }
                }

                foreach (var markersBlendings in animation.MarkersBlendings)
                {
                    if (!animationBlending.MarkersOptions.ContainsKey(markersBlendings.DestMarkerId))
                    {
                        animationBlending.MarkersOptions.Add(markersBlendings.DestMarkerId, new MarkerBlending());
                    }
                    animationBlending.MarkersOptions[markersBlendings.DestMarkerId].SourceMarkersOptions
                    .Add(markersBlendings.SourceMarkerId, markersBlendings.Blending);
                }

                if (animationBlending.Option != null || animationBlending.MarkersOptions.Count > 0)
                {
                    model.Components.GetOrAdd <AnimationBlender>().Options.Add(newAnimation.Id ?? "", animationBlending);
                }

                foreach (var i in newAnimators)
                {
                    i.Node.Animators.Add(i.Animator);
                }
                newAnimators.Clear();
            }

            foreach (var animation in model.Animations.Except(newAnimations).ToList())
            {
                var srcAnimators = new List <IAnimator>();
                animation.FindAnimators(srcAnimators);
                if (animationsToReduce.Keys.Contains(animation.Id))
                {
                    srcAnimators.ForEach(a => ReduceKeys(a, animationsToReduce[animation.Id].Item1, animationsToReduce[animation.Id].Item2));
                    foreach (var animator in srcAnimators.Where(a => a.Keys.Count == 0))
                    {
                        animator.Owner.Animators.Remove(animator);
                    }
                }
                else
                {
                    foreach (var animator in srcAnimators)
                    {
                        animator.Owner.Animators.Remove(animator);
                    }
                    model.Animations.Remove(animation);
                }
            }
        }