2013-08-02 62 views
0

我有一个包含一个图像一个的Viewport3D,这里是我的代码:的Viewport3D内容大小

<Window> 
    <Window.Resources> 
    <MaterialGroup x:Key="mat1" > 
      <DiffuseMaterial > 
       <DiffuseMaterial.Brush > 
        <VisualBrush > 
         <VisualBrush.Visual > 
          <Grid Width="270" Height="638" Background="Red"> 
           <Image Width="270" Height="638" Source="screenshot-1.Png"></Image> 
          </Grid> 
         </VisualBrush.Visual > 
        </VisualBrush > 
       </DiffuseMaterial.Brush > 
      </DiffuseMaterial > 
     </MaterialGroup> 
    </Window.Resources > 
    <Grid> 
     <Viewport3D x:Name="vp" UIElement.ClipToBounds ="False" Height="638" UIElement.IsHitTestVisible ="False" RenderOptions.EdgeMode ="Unspecified" > 
      <Viewport3D.Camera > 
       <PerspectiveCamera x:Name="camera" ProjectionCamera.Position="0,0,5" ProjectionCamera.LookDirection ="0,0,-1" PerspectiveCamera.FieldOfView ="28" /> 
      </Viewport3D.Camera> 
      <Viewport3D.Children > 
       <ModelVisual3D > 
        <ModelVisual3D.Content > 
         <SpotLight PointLightBase.Position="0,0,30" SpotLight.Direction ="0,0,-1" Light.Color ="white" /> 
        </ModelVisual3D.Content> 
       </ModelVisual3D> 

       <ModelVisual3D > 
        <ModelVisual3D.Content > 
         <GeometryModel3D x:Name="gm3d" Material="{StaticResource mat1}" > 
          <GeometryModel3D.Geometry > 
           <MeshGeometry3D x:Name="plane3d" MeshGeometry3D.Normals ="0,0,1 0,0,1 0,0,1 0,0,1 0,0,1 0,0,1" MeshGeometry3D.TextureCoordinates ="0,1 1,1 1,0 1,0 0,0 0,1" MeshGeometry3D.Positions ="-1,-1,1 1,-1,1 1,1,1 1,1,1 -1,1,1 -1,-1,1" /> 
          </GeometryModel3D.Geometry> 
          <Model3D.Transform > 
           <RotateTransform3D > 
            <RotateTransform3D.Rotation > 
             <AxisAngleRotation3D AxisAngleRotation3D.Axis="0,1,0" AxisAngleRotation3D.Angle ="0" x:Name ="rAngle" /> 
            </RotateTransform3D.Rotation> 
           </RotateTransform3D> 
          </Model3D.Transform> 
         </GeometryModel3D> 
        </ModelVisual3D.Content> 
       </ModelVisual3D> 
      </Viewport3D.Children> 
     </Viewport3D> 

     <Button Height="20" Width="30" VerticalAlignment="Top" HorizontalAlignment="Left" Click="Button_Click_2"></Button> 
    </Grid> 
</Window> 

的问题是,图像尺寸应该是这样的:

actual image 但它看起来像这种使用的Viewport3D时:

viewport3D image

是否有任何人能帮助我,好吗?

回答

0

对此的简单解释是,您要在方脸上放置一个矩形图像。

稍微详细的解释是,您的MeshGeometry3D.TextureCoordinates定义了两个三角形,它们覆盖您的矩形图像,并将它们挤压到由MeshGeometry3D.Positions中的值定义的正方形区域中。

现在有很多解决方案,取决于你想达到什么。让我们从一个简单的开始:

改变你的画笔有一个方形图像。这将保留图像的纵横比,因为默认情况下,Image.StretchUniform。请注意,我也已将背景更改为Transparent

<MaterialGroup x:Key="mat1" > 
     <DiffuseMaterial > 
      <DiffuseMaterial.Brush > 
       <VisualBrush > 
        <VisualBrush.Visual > 
         <Grid Width="800" Height="800" Background="Transparent"> 
          <Image Source="screenshot-1.Png"></Image> 
         </Grid> 
        </VisualBrush.Visual > 
       </VisualBrush > 
      </DiffuseMaterial.Brush > 
     </DiffuseMaterial > 
    </MaterialGroup>