我m trying to replicate a plane projection rotation in UWP using the composition api. But I'm having trouble with a projection matrix in my composition code. Here
就是我所拥有的:
private void Perspective(FrameworkElement element, double duration, float rotationDepth = 750f)
{
var parent = ElementCompositionPreview.GetElementVisual(element.Parent as FrameworkElement);
var width = (float)element.ActualWidth;
var height = (float)element.ActualHeight;
var halfWidth = (float)(width / 2.0);
var halfHeight = (float)(height / 2.0);
// Initialize the Compositor
var visual = ElementCompositionPreview.GetElementVisual(element);
// Create Scoped batch for animations
var batch = visual.Compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
// Rotation animation
var projectionMatrix = new Matrix4x4(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 1 / rotationDepth,
0, 0, 0, 1);
// To ensure that the rotation occurs through the center of the visual rather than the
// left edge, pre-multiply the rotation matrix with a translation that logically shifts
// the axis to the point of rotation, then restore the original location
parent.TransformMatrix = Matrix4x4.CreateTranslation(-halfWidth, -halfHeight, 0) *
projectionMatrix *
Matrix4x4.CreateTranslation(halfWidth, halfHeight, 0);
// Rotate along the Y-axis
visual.RotationAxis = new Vector3(0, 1, 0);
visual.CenterPoint = new Vector3(halfWidth, halfHeight, 0f);
visual.RotationAngleInDegrees = 0.0f;
var rotateAnimation = visual.Compositor.CreateScalarKeyFrameAnimation();
rotateAnimation.InsertKeyFrame(0.0f, 90);
rotateAnimation.InsertKeyFrame(1f, 0);
rotateAnimation.Duration = TimeSpan.FromMilliseconds(duration);
visual.StartAnimation("RotationAngleInDegrees", rotateAnimation);
// Batch is ended an no objects can be added
batch.End();
}
所以上面的这段代码将沿 Y 轴旋转。在下面的 gif 中,您会看到我在另一个由 PlaneProjection 驱动的动画之上制作了动画以进行比较:
两者的“视角”都很好,都在中间。现在让我们更改这行代码以将其切换为 X 轴上的旋转:
// Rotate along the Y-axis
visual.RotationAxis = new Vector3(0, 1, 0);
现在注意下面的 gif:
合成动画似乎以更向右而不是完全居中的视角旋转。我的投影矩阵需要改变吗?