在此 WPF C# 项目中,一个目标是制作对象在 3D 空间中从一个位置到另一个位置的运动动画。 GeometryModel 的 Transform 属性设置为 TranslateTransform3D 对象实例,并设置其 OffsetX、OffsetY 和 OffsetZ - 以将对象动画到其新位置。
当动画发生时,它似乎会分别为 OffsetX、OffsetY 和 OffsetZ 制作动画。当对象重新定位时,会产生视觉阶梯效果。有没有办法沿着新旧位置之间的矢量进行动画处理,以实现更平滑的视觉效果?
我在各种论坛上读到很多内容,WPF 确实首先对 X 进行动画处理,然后对 Y 进行动画处理,然后对 Z 进行动画处理。可能有人知道一种将动画步骤沿着矢量或其他方式组合起来的方法吗?
这是当前尝试的 C# 片段:
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
namespace SimWPF2
{
public class Object
{
#region Properties
public GeometryModel3D ObjectModel { get; set; }
private Point3D CurrLocation = new(), NewLocation = new(); // Universe coords
private TranslateTransform3D TranslateTransform3D { get; } = new();
#endregion
public Object()
{
CurrLocation.X = CurrLocation.Y = CurrLocation.Z = 0D;
// Reticle model
MeshGeometry3D SphereMesh = new();
Sphere.AddSphere(SphereMesh, CurrLocation, .5D, 10, 10);
ObjectModel = new()
{
Geometry = SphereMesh,
Material = new DiffuseMaterial(new SolidColorBrush(Colors.Red)),
Transform = TranslateTransform3D
};
}
/// <summary>
/// Position object to new location
/// </summary>
/// <param name="camera"></param>
/// <param name="newLocation"></param>
public void Position(SimCamera camera, Point3D newLocation)
{
// Reposition object
//
// **************************
// This causes WPF to animate and reposition the object. But it animates X, then Y, then Z
// giving the animation a stairstep effect as it moves on the screen.
// Is there a way to maybe animate along a vector between the old and new positions to achieve a smoother visual effect?
// **************************
//
newLocation = newLocation;
TranslateTransform3D.OffsetX = NewLocation.X;
TranslateTransform3D.OffsetY = NewLocation.Y;
TranslateTransform3D.OffsetZ = NewLocation.Z;
}
}
}
CompositionTarget.Rendering
在帧渲染期间沿着任何向量将 GeometryModel 从一个位置平移/动画到另一个位置是一件简单的事情。基本上不使用WPF内置的动画支持。