我正在编写一个WPF应用程序,遇到了一些奇怪的行为。当我在相机位置上运行动画(PerspectiveCamera.PositionProperty 上的 Point3DAnimation),我会在应用程序内部看到非常糟糕的闪烁伪影。3D 渲染对象似乎在某些帧中消失,并允许窗口的背景显示出来。
下面我编写了一个非常简单的示例应用程序来演示我的问题。要使用它,只需编译它并使用箭头向上和向下键进行缩放。该问题非常重复:每次尝试缩放时,对象都会在动画期间闪烁,然后在动画完成后再次变得“固定”。
我正在运行 Windows 7 32 位,并使用 NVIDIA GeForce 8600GT。以下是一些有趣的细节:
1)它似乎与硬件有关。我在 WPF 论坛上发布了一个帖子,其中一位用户回答说他一切正常。我请一些朋友尝试了一下,其中一位报告了与我经历的完全相同的闪烁,而另一位则说一切看起来都很好。
2)通过 NVIDIA 控制面板强制垂直同步并启用三重缓冲不会解决问题。
3)将所需的动画帧速率显着降低可以显著改善问题。在低帧速率(例如,5FPS)的情况下,闪烁会消失……但是动画看起来很可怕。我提供的示例应用程序仅显示映射到四边形上的单个图像,因此我认为这不应该是处理能力的问题!
4)问题似乎与多边形顶点超出可视窗口有关。如果我将程序中的closeDist值设置为4(这样,即使在“缩放”状态下,对象仍然完全适合窗口内),就不会有闪烁。但是,随着closeDist的增加,一旦顶点超出窗口,闪烁就会发生。随着closeDist的增加,闪烁似乎越来越严重。当 closeDist 的值为9.8(正好在相机的 NearPlaneDistance 将完全切断对象之前)时,闪烁最严重。
废话不多说,以下是示例代码!
MainWindow.xaml:
下面我编写了一个非常简单的示例应用程序来演示我的问题。要使用它,只需编译它并使用箭头向上和向下键进行缩放。该问题非常重复:每次尝试缩放时,对象都会在动画期间闪烁,然后在动画完成后再次变得“固定”。
我正在运行 Windows 7 32 位,并使用 NVIDIA GeForce 8600GT。以下是一些有趣的细节:
1)它似乎与硬件有关。我在 WPF 论坛上发布了一个帖子,其中一位用户回答说他一切正常。我请一些朋友尝试了一下,其中一位报告了与我经历的完全相同的闪烁,而另一位则说一切看起来都很好。
2)通过 NVIDIA 控制面板强制垂直同步并启用三重缓冲不会解决问题。
3)将所需的动画帧速率显着降低可以显著改善问题。在低帧速率(例如,5FPS)的情况下,闪烁会消失……但是动画看起来很可怕。我提供的示例应用程序仅显示映射到四边形上的单个图像,因此我认为这不应该是处理能力的问题!
4)问题似乎与多边形顶点超出可视窗口有关。如果我将程序中的closeDist值设置为4(这样,即使在“缩放”状态下,对象仍然完全适合窗口内),就不会有闪烁。但是,随着closeDist的增加,一旦顶点超出窗口,闪烁就会发生。随着closeDist的增加,闪烁似乎越来越严重。当 closeDist 的值为9.8(正好在相机的 NearPlaneDistance 将完全切断对象之前)时,闪烁最严重。
废话不多说,以下是示例代码!
MainWindow.xaml:
<Window x:Class="WPFFlickerTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
KeyDown="Window_KeyDown">
<Grid>
<Viewport3D Name="Viewport">
<Viewport3D.Camera>
<PerspectiveCamera LookDirection="0,0,1" FieldOfView="70" x:Name="viewportCam" />
</Viewport3D.Camera>
<ModelVisual3D>
<ModelVisual3D.Content>
<AmbientLight />
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>
</Grid>
</Window>
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
namespace WPFFlickerTest
{
public partial class MainWindow : Window
{
// time the camera animation takes to complete
private const double animTime = 0.25;
// path to an image to use (assuming it's 1920x1200 or 1.6 aspect ratio)
private const string imagePath = "C:/Windows/Web/Wallpaper/Windows/img0.jpg";
// far and close camera distances
private const double closeDist = 8, farDist = 10;
// chosen to align with aspect ratio of the image
private const double halfW = 4.8, halfH = 3;
public MainWindow()
{
InitializeComponent();
Model3DGroup modelGroup = new Model3DGroup();
// set up the mesh
MeshGeometry3D mesh = new MeshGeometry3D();
mesh.Positions.Add(new Point3D(-halfW, halfH, farDist));
mesh.Positions.Add(new Point3D(halfW, halfH, farDist));
mesh.Positions.Add(new Point3D(halfW, -halfH, farDist));
mesh.Positions.Add(new Point3D(-halfW, -halfH, farDist));
// set up triangle indices
mesh.TriangleIndices = (Int32Collection)new Int32CollectionConverter().ConvertFromString(
"0,1,2 2,3,0");
// set up texture coords
mesh.TextureCoordinates = (PointCollection)new PointCollectionConverter().ConvertFromString(
"1,0 0,0 0,1 1,1");
// set up the brush
ImageBrush brush = new ImageBrush(new BitmapImage(new Uri(imagePath, UriKind.Relative)));
// create a geometry model based on the mesh and give it a material based on an image
GeometryModel3D geom = new GeometryModel3D(mesh, new DiffuseMaterial(brush));
// add the object
modelGroup.Children.Add(geom);
// we should have filled in our objects now
// so we'll just add them to the viewport
ModelVisual3D modelVisual = new ModelVisual3D();
modelVisual.Content = modelGroup;
Viewport.Children.Add(modelVisual);
}
// react to keypresses
private void Window_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
// move the camera to the centre
case Key.Down: AnimateTo(new Point3D(0, 0, 0)); break;
// move the camera to the currently targeted image
case Key.Up: AnimateTo(new Point3D(0, 0, closeDist)); break;
}
}
// animate to a given position
void AnimateTo(Point3D position)
{
Point3DAnimation camPosAnim = new Point3DAnimation(position, TimeSpan.FromSeconds(animTime));
viewportCam.BeginAnimation(PerspectiveCamera.PositionProperty, camPosAnim);
}
}
}