Choose camera Distance to fit scene into WPF window
#1
I have a WPF app that shows a simple scene, just a PlaneVisual3D and I want this shape to almost fill the entire WPF window.  The height/width of the WPF window  is 600/1000, the height/width of the  PlaneVisual3D is 400/800 and the camera distance is 1500.   The executed app appears as shown in the attachment Test.gif.  

I would like to to know how to choose the camera Distance so that the width of the PlaneVisual3D takes up the width of the WPF window with a 100 pixel horizontal margin on each side (the vertical margin is not important).

In general, what I am trying to do is to figure out how to programmatically choose the camera Distance so that the scene I create fits within the current size of a WPF window.

Here is the code:

Code:
<Window x:Class="ChooseCameraDistance.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       xmlns:cameras="clr-namespace:Ab3d.Cameras;assembly=Ab3d.PowerToys"
       xmlns:ab3d="clr-namespace:Ab3d.Controls;assembly=Ab3d.PowerToys"  
       xmlns:visuals="clr-namespace:Ab3d.Visuals;assembly=Ab3d.PowerToys"
       xmlns:local="clr-namespace:ChooseCameraDistance"
       mc:Ignorable="d"
       Title="MainWindow" Height="600" Width="1000">
   <Grid Name="MainGrid">
       <Border Name="ViewportBorder">
           <Border.Background>
               <LinearGradientBrush StartPoint="0 0" EndPoint="0 1">
                   <GradientStop Offset="0" Color="#033C62"/>
                   <GradientStop Offset="1" Color="#01131F"/>
               </LinearGradientBrush>
           </Border.Background>

           <Viewport3D Name="MainViewport"  >
               <visuals:PlaneVisual3D CenterPosition="0,0,0" Size="800 400" HeightDirection="0 1 0"  
                                      Normal="0 0 1" Material="Emissive:Yellow" />
           </Viewport3D>
       </Border>

       <cameras:TargetPositionCamera Name="Camera1"
                                     TargetPosition="0 0 0"
                                     Heading="0" Attitude="0" Bank="0"
                                     Distance="1500"
                                     ShowCameraLight="Always"
                                     TargetViewport3D="{Binding ElementName=MainViewport}" />
   </Grid>
</Window>


Attached Files Thumbnail(s)
   
  


Messages In This Thread
Choose camera Distance to fit scene into WPF window - by HelloSupport - 10-22-2020, 10:40 AM

Forum Jump:


Users browsing this thread:
1 Guest(s)