3D 未在 WPF 应用程序中呈现

发布于 2024-09-26 18:38:00 字数 4264 浏览 7 评论 0原文

我正在尝试使用下面的代码渲染 3D 对象。 但是当我运行该应用程序时,什么也没有显示。似乎一片空白。我错过了什么吗?

<Page x:Class="SampleWpfApplication.DemoPage3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DemoPage3" xmlns:my="clr-namespace:SampleWpfApplication">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="126*" />
        <RowDefinition Height="126*" />
        <RowDefinition Height="66" />
    </Grid.RowDefinitions>
    <Viewport3D x:Name="theView3D">
        <Viewport3D.Camera>
            <PerspectiveCamera Position="6,6,6" LookDirection="-4,-4,-4"
UpDirection="0,1,0" />
        </Viewport3D.Camera>         
        <ModelVisual3D x:Name="theModel">
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <GeometryModel3D x:Name="theGeometry">
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D Positions="0,1,0 1,-1,1 -1,-1,1 1,-1,-1 -1,-1,-1"
        Normals="0,1,0 -1,0,1 1,0,1 -1,0,-1 1,0,-1"
        TriangleIndices="0,2,1 0,3,1 0,3,4 0,2,4" />
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Red" Opacity="0.9"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                        <GeometryModel3D.BackMaterial>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Green" Opacity="0.9"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.BackMaterial>
                    </GeometryModel3D>
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <DirectionalLight Direction="0,-5,-2" />
                    <DirectionalLight Direction="3,2,2" />
                    <GeometryModel3D x:Name="theGeometry2">
                        <GeometryModel3D.Transform>
                            <ScaleTransform3D ScaleX="2" ScaleY="2" ScaleZ="2"></ScaleTransform3D>
                        </GeometryModel3D.Transform>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D Positions="0,1,0 1,-1,1 -1,-1,1 1,-1,-1 -1,-1,-1"
        Normals="0,1,0 -1,0,1 1,0,1 -1,0,-1 1,0,-1"
        TriangleIndices="0,2,1 0,3,1 0,3,4 0,2,4" />
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="#7FB0C4DE" Opacity="0.9"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                        <GeometryModel3D.BackMaterial>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="#7FB0C4DE" Opacity="0.9"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.BackMaterial>
                    </GeometryModel3D>
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>       
</Grid>
</Page>

I am trying to render 3D object using the below code.
But when I run the application, nothing is displayed. It seems to be blank. Am I missing anything?

<Page x:Class="SampleWpfApplication.DemoPage3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DemoPage3" xmlns:my="clr-namespace:SampleWpfApplication">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="126*" />
        <RowDefinition Height="126*" />
        <RowDefinition Height="66" />
    </Grid.RowDefinitions>
    <Viewport3D x:Name="theView3D">
        <Viewport3D.Camera>
            <PerspectiveCamera Position="6,6,6" LookDirection="-4,-4,-4"
UpDirection="0,1,0" />
        </Viewport3D.Camera>         
        <ModelVisual3D x:Name="theModel">
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <GeometryModel3D x:Name="theGeometry">
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D Positions="0,1,0 1,-1,1 -1,-1,1 1,-1,-1 -1,-1,-1"
        Normals="0,1,0 -1,0,1 1,0,1 -1,0,-1 1,0,-1"
        TriangleIndices="0,2,1 0,3,1 0,3,4 0,2,4" />
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Red" Opacity="0.9"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                        <GeometryModel3D.BackMaterial>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Green" Opacity="0.9"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.BackMaterial>
                    </GeometryModel3D>
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <DirectionalLight Direction="0,-5,-2" />
                    <DirectionalLight Direction="3,2,2" />
                    <GeometryModel3D x:Name="theGeometry2">
                        <GeometryModel3D.Transform>
                            <ScaleTransform3D ScaleX="2" ScaleY="2" ScaleZ="2"></ScaleTransform3D>
                        </GeometryModel3D.Transform>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D Positions="0,1,0 1,-1,1 -1,-1,1 1,-1,-1 -1,-1,-1"
        Normals="0,1,0 -1,0,1 1,0,1 -1,0,-1 1,0,-1"
        TriangleIndices="0,2,1 0,3,1 0,3,4 0,2,4" />
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="#7FB0C4DE" Opacity="0.9"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                        <GeometryModel3D.BackMaterial>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="#7FB0C4DE" Opacity="0.9"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.BackMaterial>
                    </GeometryModel3D>
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>       
</Grid>
</Page>

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

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

发布评论

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

评论(2

三五鸿雁 2024-10-03 18:38:00

它似乎在 Kaxaml 中对我来说渲染得很好。

也许您应该尝试不同的机器或使用 Kaxaml 来查看是否得到任何结果。此时,可能有很多事情。

编辑:我注意到我没有复制Grid.RowDefinitions。当我添加它们时,它会剪辑视口。如果你把它们去掉,它还能工作吗?

It appears to render fine for me in Kaxaml.

Perhaps you should try a different machine or use Kaxaml to see if you get any result at all. At this point, it could be any number of things.

EDIT: I noticed that I didn't copy the Grid.RowDefinitions. When I add them, it clips the viewport. If you remove those, does it work?

猫性小仙女 2024-10-03 18:38:00

我尝试在 WPF 应用程序中渲染 Viewport3D,并且已成功渲染。它向我展示了一个金字塔网格。

从上面的代码中,您会看到一个空白屏幕,因为您没有定义页面高度和宽度

这是我为您的页面提供的解决方案(请添加您的页面高度和宽度属性,如下所示):

<Page x:Class="SampleWpfApplication.DemoPage3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300"
    Title="DemoPage3" xmlns:my="clr-namespace:SampleWpfApplication">

I try to render your Viewport3D in WPF application and it's successfully rendered. It shows me a pyramid mesh.

From your code above, you get a blank screen because you are not defining your page height and width.

Here is my solution for your page (Please add your page height and width properties as follow) :

<Page x:Class="SampleWpfApplication.DemoPage3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300"
    Title="DemoPage3" xmlns:my="clr-namespace:SampleWpfApplication">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文