WP3 和 3D 如何使用 VisualBrush 填充 Viewport3D?

发布于 2024-10-14 20:23:15 字数 3510 浏览 2 评论 0原文

我在 WPF 中定义了一个非常简单的 3D 空间,它定义了一个 3D 矩形和一个 3D 相机。 3D 矩形具有 VisualBrush 的材质,用于描绘窗口的内容。 当窗口尺寸大于 960x550 时,它工作正常(投影很好地填充了可用区域)。 但是,低于该数字(并且只要窗口的宽度远小于其高度),就会在屏幕右侧留下空白区域。我将不胜感激任何建议或指示...

这是我的 XAML:

<Grid x:Name="RootGrid">
        <!--UI Stuff here-->
    </Grid>
    <Viewport3D x:Name="MyAnimatedObject" ClipToBounds="False">
        <Viewport3D.Camera>
            <PerspectiveCamera x:Name="myPerspectiveCamera" FieldOfView="60" Position="0,0,1"/>
        </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <DirectionalLight Color="#FFFFFF" Direction="-0.5,-0.5,-0.5"/>
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <DirectionalLight Color="#FFFFFF" Direction="0.5,-0.5,-0.5"/>
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <GeometryModel3D x:Name="geometryModel3D">
                    <GeometryModel3D.Transform>
                        <Transform3DGroup>
                            <TranslateTransform3D/>
                            <ScaleTransform3D/>
                            <RotateTransform3D/>
                            <TranslateTransform3D/>
                            <TranslateTransform3D/>
                        </Transform3DGroup>
                    </GeometryModel3D.Transform>
                    <GeometryModel3D.Geometry>
                        <MeshGeometry3D
       TriangleIndices="0 3 2 0 2 1"
       TextureCoordinates="0,0 1,0 1,1 0,1"
                            Normals="0,0,1 0,0,1 0,0,1 0,0,1"
       Positions="-1,-1,0 1,-1,0 1,1,0 1,1,0 -1,1,0 -1,-1,0"/>
                    </GeometryModel3D.Geometry>
                    <GeometryModel3D.Material>
                        <MaterialGroup>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <VisualBrush Visual="{Binding ElementName=RootGrid}" Stretch="Fill"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </MaterialGroup>
                    </GeometryModel3D.Material>
                </GeometryModel3D>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>

这里是动态调整 3D 矩形大小的代码:

Private Sub MainWindow_SizeChanged(ByVal sender As Object, ByVal e As System.Windows.SizeChangedEventArgs) Handles Me.SizeChanged
    Dim AspectRatio As Double = RootGrid.ActualWidth / RootGrid.ActualHeight

    Dim CameraDistance As Double = AspectRatio / Math.Tan(30D * Math.PI / 180)
    myPerspectiveCamera.Position = New Media3D.Point3D(0, 0, CameraDistance)


    Dim points As New Media3D.Point3DCollection
    With points
        .Add(New Media3D.Point3D(0D - AspectRatio, 1, 0))
        .Add(New Media3D.Point3D(AspectRatio, 1, 0))
        .Add(New Media3D.Point3D(AspectRatio, -1, 0))
        .Add(New Media3D.Point3D(0D - AspectRatio, -1, 0))
    End With
    CType(geometryModel3D.Geometry, Media3D.MeshGeometry3D).Positions = points
End Sub

I have a VERY simple 3D space defined in WPF, which defines a single 3D rectangle and a 3D camera. The 3D Rect has a Material of a VisualBrush which depicts the contents of the window.
It works fine(the projection fill the available area nicely) when the window size is larger than 960x550.
BUT, below that number (and as long as the width of the window is considerably smaller than it's height) it leaves a white space on the right of the screen. I would appreciate any advice or directions...

Here is my XAML:

<Grid x:Name="RootGrid">
        <!--UI Stuff here-->
    </Grid>
    <Viewport3D x:Name="MyAnimatedObject" ClipToBounds="False">
        <Viewport3D.Camera>
            <PerspectiveCamera x:Name="myPerspectiveCamera" FieldOfView="60" Position="0,0,1"/>
        </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <DirectionalLight Color="#FFFFFF" Direction="-0.5,-0.5,-0.5"/>
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <DirectionalLight Color="#FFFFFF" Direction="0.5,-0.5,-0.5"/>
            </ModelVisual3D.Content>
        </ModelVisual3D>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <GeometryModel3D x:Name="geometryModel3D">
                    <GeometryModel3D.Transform>
                        <Transform3DGroup>
                            <TranslateTransform3D/>
                            <ScaleTransform3D/>
                            <RotateTransform3D/>
                            <TranslateTransform3D/>
                            <TranslateTransform3D/>
                        </Transform3DGroup>
                    </GeometryModel3D.Transform>
                    <GeometryModel3D.Geometry>
                        <MeshGeometry3D
       TriangleIndices="0 3 2 0 2 1"
       TextureCoordinates="0,0 1,0 1,1 0,1"
                            Normals="0,0,1 0,0,1 0,0,1 0,0,1"
       Positions="-1,-1,0 1,-1,0 1,1,0 1,1,0 -1,1,0 -1,-1,0"/>
                    </GeometryModel3D.Geometry>
                    <GeometryModel3D.Material>
                        <MaterialGroup>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <VisualBrush Visual="{Binding ElementName=RootGrid}" Stretch="Fill"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </MaterialGroup>
                    </GeometryModel3D.Material>
                </GeometryModel3D>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>

And the code behind for the dynamic resize of the 3D Rect here:

Private Sub MainWindow_SizeChanged(ByVal sender As Object, ByVal e As System.Windows.SizeChangedEventArgs) Handles Me.SizeChanged
    Dim AspectRatio As Double = RootGrid.ActualWidth / RootGrid.ActualHeight

    Dim CameraDistance As Double = AspectRatio / Math.Tan(30D * Math.PI / 180)
    myPerspectiveCamera.Position = New Media3D.Point3D(0, 0, CameraDistance)


    Dim points As New Media3D.Point3DCollection
    With points
        .Add(New Media3D.Point3D(0D - AspectRatio, 1, 0))
        .Add(New Media3D.Point3D(AspectRatio, 1, 0))
        .Add(New Media3D.Point3D(AspectRatio, -1, 0))
        .Add(New Media3D.Point3D(0D - AspectRatio, -1, 0))
    End With
    CType(geometryModel3D.Geometry, Media3D.MeshGeometry3D).Positions = points
End Sub

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文