简单的相机动画有很多闪烁

发布于 2024-11-09 02:19:50 字数 2271 浏览 0 评论 0原文

我是 WPF 中的 3D 和动画新手。我一直在尝试创建一个由地板和 2 个立方体(好吧,3D 矩形、矩形棱柱,无论它叫什么)组成的非常简单的场景。这个场景有一个透视相机,我正在尝试为其设置动画(我基本上希望它在场景中飞行)。

代码非常简单,我相信大部分情况下都采用了很好的方法。请在下面找到相关摘录,并且可以在此处下载示例项目

XAML:

    <Viewport3D ClipToBounds="false" IsHitTestVisible="false">
        <!-- Camera -->
        <Viewport3D.Camera>
            <PerspectiveCamera x:Name="ViewportCamera"
                               Position="0 0 20" 
                               LookDirection="40 40 0" 
                               FarPlaneDistance="600" 
                               UpDirection="0 0 1" 
                               NearPlaneDistance="0.01" 
                               FieldOfView="75" />
        </Viewport3D.Camera>
        <!-- Light -->
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <DirectionalLight Direction="1 0.5 1" />
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D>
            <!-- Floor -->
            <local:CubeControl Color="Black" Size="300,300,1" Center="0,0,0" />
            <local:CubeControl Color="LightGray" Size="15,25,40" Center="40,40,20" />
            <local:CubeControl Color="LightGray" Size="25,20,60" Center="40,-40,30" />
        </ModelVisual3D>
    </Viewport3D>

VB:

Dim tAnimation As New Vector3DAnimation(ViewportCamera.LookDirection, New Vector3D(40, -40, -20), New Duration(TimeSpan.FromMilliseconds(1000)))
ViewportCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, tAnimation)
Dim tAnimation2 As New Point3DAnimation(ViewportCamera.Position, New Point3D(-10, -10, 30), TimeSpan.FromMilliseconds(1000))
ViewportCamera.BeginAnimation(PerspectiveCamera.PositionProperty, tAnimation2)

当窗口非常小(大约 320x240)时,动画按预期运行。随着窗口大小的增加,动画会闪烁,整个帧似乎被跳过,导致白屏。在大窗口 (1920x1080) 中,动画几乎根本不可见(在相机设置到新位置之前,动画都是空白的)。

不知怎的,它看起来与这个问题非常相似,但这对我没有帮助。

这是一个非常基本的动画,我不敢相信 WPF 执行此操作有任何问题,所以我认为我做错了什么。

有人能帮我弄清楚为什么吗?我在这方面已经花费了足够多的时间。

提前致谢!

I am new to 3D and animations in WPF. I have been trying to create a very simple scene made of a floor and 2 cubes (well, 3D rectangles, rectangular prisms, whatever that is called). This scene has a perspective camera which I am trying to animate (I basically want it to fly around the scene).

The code is very simple and I believe takes a good approach for the most part. Please find relevant extracts below and the sample project can be downloaded here.

XAML:

    <Viewport3D ClipToBounds="false" IsHitTestVisible="false">
        <!-- Camera -->
        <Viewport3D.Camera>
            <PerspectiveCamera x:Name="ViewportCamera"
                               Position="0 0 20" 
                               LookDirection="40 40 0" 
                               FarPlaneDistance="600" 
                               UpDirection="0 0 1" 
                               NearPlaneDistance="0.01" 
                               FieldOfView="75" />
        </Viewport3D.Camera>
        <!-- Light -->
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <DirectionalLight Direction="1 0.5 1" />
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D>
            <!-- Floor -->
            <local:CubeControl Color="Black" Size="300,300,1" Center="0,0,0" />
            <local:CubeControl Color="LightGray" Size="15,25,40" Center="40,40,20" />
            <local:CubeControl Color="LightGray" Size="25,20,60" Center="40,-40,30" />
        </ModelVisual3D>
    </Viewport3D>

VB:

Dim tAnimation As New Vector3DAnimation(ViewportCamera.LookDirection, New Vector3D(40, -40, -20), New Duration(TimeSpan.FromMilliseconds(1000)))
ViewportCamera.BeginAnimation(PerspectiveCamera.LookDirectionProperty, tAnimation)
Dim tAnimation2 As New Point3DAnimation(ViewportCamera.Position, New Point3D(-10, -10, 30), TimeSpan.FromMilliseconds(1000))
ViewportCamera.BeginAnimation(PerspectiveCamera.PositionProperty, tAnimation2)

The animation runs as expected when the window is very small (around 320x240). As the size of the window increases, the animation flickers and entire frames seem to be skipped, resulting in a white screen. In a large window (1920x1080), the animation is almost not visible at all (it's all blank until the camera is set to its new position).

Somehow it seems very similar to this problem but this has not helped me.

This is a very basic animation and I can't believe WPF has any problem doing this, so I'm thinking I am doing something wrong.

can anybody help me figure out why? I have spent more than enough time on this.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2024-11-16 02:19:50

您有独立的显卡还是使用主板的视频输出?听起来你的显卡上没有硬件加速。

如果你不这样做,那么你就会得到你所看到的效果。在小窗口尺寸下,软件渲染可以以合理的帧速率跟上并显示场景。但是,随着窗口大小的增加,显示每一帧所需的时间会越来越长 - 直到显示时间大于您在场景中移动相机所需的时间。只有当您停止移动相机时它才会赶上。

解决方案是坚持使用小窗口或使用具有 3D 图形硬件加速功能的显卡。

Do you have a separate graphics card or are you running off the motherboard's video output? It sounds like you don't have hardware acceleration on your graphics card.

If you don't then you would get the effect you see. At small window sizes the software rendering can keep up and display the scene at a reasonable frame rate. However, as you increase the size of the window it takes longer and longer to display each frame - until the display time is greater than the time it takes you to move the camera around the scene. It only catches up when you stop moving the camera.

The solution is to stick to small windows or get a graphics card that does hardware acceleration of 3D graphics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文