3D WPF 的问题

发布于 2024-08-24 10:12:51 字数 3129 浏览 10 评论 0原文

我今天开始学习 WPF 中的一些 3D,并将一些简单的示例(平面)复制到我的 XAML 中,它们都起作用了。然而,当我调整相机和飞机的坐标以满足我的需要时,我总是看不到任何东西。

我不知道我做错了什么,而且我也已经绘制了(非常简单的)3D 场景来验证我的数据,这对我来说似乎都是正确的。

任何人都可以检查以下 XAML 并告诉我我做错了什么吗?我只想在 xz 平面上创建一个平面,即某种“地板”,相机从顶部俯视它。

<Viewport3D>
    <Viewport3D.Camera>
        <PerspectiveCamera Position="0 1 0"
                           LookDirection="0 -1 0"
                           FieldOfView="60"/>
    </Viewport3D.Camera>
    <Viewport3D.Children>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <AmbientLight Color="White"/>
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <GeometryModel3D>
                    <GeometryModel3D.Geometry>
                        <MeshGeometry3D>
                            <MeshGeometry3D.Positions>
                                <Point3D X="-1" Y="0"   Z="1"/>
                                <Point3D X="1"  Y="0"   Z="1"/>
                                <Point3D X="1"  Y="0"   Z="-1"/>
                                <Point3D X="-1" Y="0"   Z="-1"/>
                            </MeshGeometry3D.Positions>
                            <MeshGeometry3D.TriangleIndices>
                                0 1 2  0 2 3
                            </MeshGeometry3D.TriangleIndices>
                            <MeshGeometry3D.Normals>
                                <Vector3D X="0" Y="1" Z="0"/>
                                <Vector3D X="0" Y="1" Z="0"/>
                                <Vector3D X="0" Y="1" Z="0"/>
                                <Vector3D X="0" Y="1" Z="0"/>
                                <Vector3D X="0" Y="1" Z="0"/>
                                <Vector3D X="0" Y="1" Z="0"/>
                            </MeshGeometry3D.Normals>
                        </MeshGeometry3D>
                    </GeometryModel3D.Geometry>
                    <GeometryModel3D.Material>
                        <MaterialGroup>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Red" Opacity="0.75"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </MaterialGroup>
                    </GeometryModel3D.Material>

                </GeometryModel3D>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D.Children>

</Viewport3D>

编辑:我刚刚尝试了相机的位置和LookDirection,当我将LookDirection更改为不垂直平面时,终于看到了一些东西,例如:

<PerspectiveCamera Position="0 2 0"
                   LookDirection="0 -2 1"
                   FieldOfView="60"/>

这是正常行为吗?

非常感谢!

哎呀。

I started to learn some 3D in WPF today and I copied some simple examples (planes) into my XAML, and they all worked. However, when I adjusted the camera's and the plane's coordinates to meet my needs, I always see nothing.

I do not know what I am doing wrong, and I also already sketched the (really simple) 3D scene to verify my data, and it all seems correct to me.

Could anyone please check the following XAML and tell me what I am doing wrong? I only want to create a plane in the x-z-plane, i.e. some sort of "floor" with the camera looking down on it from the top.

<Viewport3D>
    <Viewport3D.Camera>
        <PerspectiveCamera Position="0 1 0"
                           LookDirection="0 -1 0"
                           FieldOfView="60"/>
    </Viewport3D.Camera>
    <Viewport3D.Children>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <AmbientLight Color="White"/>
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <GeometryModel3D>
                    <GeometryModel3D.Geometry>
                        <MeshGeometry3D>
                            <MeshGeometry3D.Positions>
                                <Point3D X="-1" Y="0"   Z="1"/>
                                <Point3D X="1"  Y="0"   Z="1"/>
                                <Point3D X="1"  Y="0"   Z="-1"/>
                                <Point3D X="-1" Y="0"   Z="-1"/>
                            </MeshGeometry3D.Positions>
                            <MeshGeometry3D.TriangleIndices>
                                0 1 2  0 2 3
                            </MeshGeometry3D.TriangleIndices>
                            <MeshGeometry3D.Normals>
                                <Vector3D X="0" Y="1" Z="0"/>
                                <Vector3D X="0" Y="1" Z="0"/>
                                <Vector3D X="0" Y="1" Z="0"/>
                                <Vector3D X="0" Y="1" Z="0"/>
                                <Vector3D X="0" Y="1" Z="0"/>
                                <Vector3D X="0" Y="1" Z="0"/>
                            </MeshGeometry3D.Normals>
                        </MeshGeometry3D>
                    </GeometryModel3D.Geometry>
                    <GeometryModel3D.Material>
                        <MaterialGroup>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Red" Opacity="0.75"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </MaterialGroup>
                    </GeometryModel3D.Material>

                </GeometryModel3D>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D.Children>

</Viewport3D>

EDIT: I just experimented with the camera's Position and LookDirection, and finally saw something when I changed the LookDirection to be not perpendicular to the plane, for example:

<PerspectiveCamera Position="0 2 0"
                   LookDirection="0 -2 1"
                   FieldOfView="60"/>

Is this normal behavior?

Many thanks!

gehho.

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

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

发布评论

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

评论(1

鲜血染红嫁衣 2024-08-31 10:12:51

当您指向相机时,请务必注意向上矢量以及观看方向。

问题来自于向上向量 (PerspectiveCamera.UpDirection) 的默认值,即 [0,1,0]。相机的“Look”和“Up”向量一定不能平行,因为在这种情况下,将无法判断哪个方向是向上。

由于您的模型位于 XZ 平面,并且您想要俯视它,因此您可以简单地将 UpDirection 设置为 [0,0,1] 或 [1,0,0],以便向上指向分别为 Z 轴或 X 轴。但如果您打算让相机观察各个方向,您也应该注意向上向量。

When you are pointing the camera, it is important to watch out for the Up vector as well as the Look direction.

The problem comes from the default value for the Up vector (PerspectiveCamera.UpDirection), which is [0,1,0]. The camera's Look and Up vectors must not be parallel, because in that case it would be impossible to tell which way is up.

Since your model is in the XZ plane, and you want to look down over it, you can simply set UpDirection to [0,0,1] or [1,0,0], so that up points in the direction of either the Z or the X axis, respectively. But if you are planning on allowing the camera to look in all directions, you should be taking care of the Up vector too.

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