1

我在 Viewport3D 中有一个模型,我想在它旁边绘制一个文本框,并且文本框可以将“常规”控件添加到 Viewport3D 吗?

4

2 回答 2

2

在下面的 XAML 中,我在水平堆栈面板中创建了一个带有一些 2D 控件、一个文本块和一个文本框的 Viewport3D,您要查找的控件是 Viewport2DVisual3D,您需要描述它的几何形状和材质,但之后它'将在世界空间中可用。

Viewport2DVisual3D 控件的最佳之处在于它将鼠标操作取消投影回世界空间,这意味着您放置在视口中的控件仍然可以使用,就像它们是在 2D 中绘制的一样。

<Viewport3D>

    <!-- To look in to a 3D world you need a camera just like a game.-->
    <Viewport3D.Camera>
        <!-- 
        I've positioned the camera at the center of the world and moved it back
        Along the Z axis, try changing the numbers here to see how it works.
    -->
        <PerspectiveCamera Position="0, 0, 3"/>
    </Viewport3D.Camera>

    <!-- All 3D worlds need to have a light, you can again play with the values
        here to see what they mean, Directional light is one that starts at a 
        location in the world and shines in a given direction.
-->
    <ModelVisual3D>
        <ModelVisual3D.Content>
            <DirectionalLight Color="White" Direction="0,0,-1"/>
        </ModelVisual3D.Content>
    </ModelVisual3D>

    <!-- 
    This is the wonder control that allows you to display WPF controls 
    in a 3D model.
-->
    <Viewport2DVisual3D >

        <!-- 
        The 3D model is made up of a vertex list, each grouping of 3 digits
        in the positions property is a point on a polygon, here I've used
        4 points to represent a flat square surface (2 triangles to make the square)
        and relate to a bitmap produced from your WPF controls 
        Triangle TriangleIndices map positions in to triangles, here I've got 
        1             2 imagine a triangle drawn through points 0, 1, 2
                        and another drawn from 0, 2 and 3 

        0             3
        The texture coords tell the 3D viewport how to map your controls visual to the 
        points in positions, texture coordinates range from 0,0 top left to 1,1 bottom right
    -->
        <Viewport2DVisual3D.Geometry>
            <MeshGeometry3D Positions="-1,1,0 -1,-1,0 1,-1,0 1,1,0" TriangleIndices="0 1 2 0 2 3" TextureCoordinates="0,0 0,1 1,1 1,0"/>
        </Viewport2DVisual3D.Geometry>
        <Viewport2DVisual3D.Material>
            <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="White"/>
        </Viewport2DVisual3D.Material>

        <!-- Here I'm doing a transform, it's the same as simply rotating a control using it's render transform
            in WPF
    -->
        <Viewport2DVisual3D.Transform>
            <RotateTransform3D>
                <RotateTransform3D.Rotation>
                    <!-- Play around with the Axis numbers to see what they mean.-->
                    <AxisAngleRotation3D Angle="-45" Axis="1, 0, 0" />
                </RotateTransform3D.Rotation>
            </RotateTransform3D>
        </Viewport2DVisual3D.Transform>

        <!-- This is the 2D bit, inside the visual property you can place any WPF controls. -->
        <Viewport2DVisual3D.Visual>
            <StackPanel Orientation="Horizontal">

                <!-- This bit is obvious. -->
                <TextBlock Text="3D user interface!"/>
                <TextBox />
            </StackPanel>
        </Viewport2DVisual3D.Visual>
    </Viewport2DVisual3D>

</Viewport3D>
于 2012-06-18T14:58:14.757 回答
0

您可以简单地在 Viewport3D 上方放置一个画布:

<Grid>
  <Viewport3D>
    [..]
  </Viewport3D>
  <Canvas>
    <TextBox />
  </Canvas>
</Grid>

这取决于你想做什么。

于 2015-02-07T16:04:00.850 回答