WP3 和 3D 如何使用 VisualBrush 填充 Viewport3D?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论