简体   繁体   English

如何在WPF 3D立方体中插入2D控件

[英]How to insert 2d control in the wpf 3d cube

I have created one 3d cube using Viewport3D in WPF.I have created 6 GeometryModel3D inside Viewport since I want to show the differnt contents on each face.I want to insert one userinteractice buttone control on one of the face(front face) of cube.I know we should use Viewport2DVisual3D for this purpose.But I am not able to find a solution how I can insert the 2ndd control on the face using Viewport2DVisual3D.Please help me .Below is my Xamal code I have written sofar: 我在WPF中使用Viewport3D创建了一个3d多维数据集。由于要在每个面上显示不同的内容,所以在Viewport中创建了6个GeometryModel3D。我想在多维数据集的一个面(正面)上插入一个用户交互按钮控件。我知道我们应该为此使用Viewport2DVisual3D,但是我找不到解决方案,可以使用Viewport2DVisual3D在脸部插入第二个控件,请帮帮我,下面是我写的Xamal代码:

<Grid>
        <Viewport3D>
            <Viewport3D.Camera>
                <PerspectiveCamera Position="0,0,25" LookDirection="15,6,-50" />
            </Viewport3D.Camera>

            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <!--<AmbientLight Color="White" />-->
                    <SpotLight Color="White" InnerConeAngle="20" OuterConeAngle="60" Direction="15,6,-50" Position="0,0,25" />
                </ModelVisual3D.Content>
            </ModelVisual3D>

            <ModelVisual3D>

                <ModelVisual3D.Content>

                    <Model3DGroup>


                                <!-- front -->
                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D
                                Positions="0 0 0, 10 0 0, 0 16 0, 10 16 0, 10 0 0"
                                TriangleIndices="0, 1, 2, 2, 4, 3"
                                TextureCoordinates="0 1, 1 1, 0 0, 1 0, 1 1"
                                />
                            </GeometryModel3D.Geometry>

                            <GeometryModel3D.Material>
                                <DiffuseMaterial Brush="White"/>

                            </GeometryModel3D.Material>

                            <Viewport2DVisual3D>

                                <StackPanel>   
                                <Button>Hello, 3D</Button>
                                 </StackPanel>
                            </Viewport2DVisual3D>

                        </GeometryModel3D>


                        <!-- left -->
                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D
                            Positions="0 0 -.5, 0 0 0, 0 14 -.5, 0 14 0, 0 0 0"
                            TriangleIndices="0, 1, 2, 2, 4, 3" />
                            </GeometryModel3D.Geometry>

                            <GeometryModel3D.Material>
                                <MaterialGroup>
                                    <DiffuseMaterial Brush="{StaticResource side}" />
                                </MaterialGroup>
                            </GeometryModel3D.Material>
                            <GeometryModel3D.BackMaterial>
                                <DiffuseMaterial Color="Blue" />
                            </GeometryModel3D.BackMaterial>
                        </GeometryModel3D>

                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D
                            Positions="0 14 -4, 0 14 0, 0 16 -4, 0 16 0, 0 14 0"
                            TriangleIndices="0, 1, 2, 2, 4, 3" />
                            </GeometryModel3D.Geometry>

                            <GeometryModel3D.Material>
                                <MaterialGroup>
                                    <DiffuseMaterial Brush="{StaticResource top}" />
                                </MaterialGroup>
                            </GeometryModel3D.Material>
                            <GeometryModel3D.BackMaterial>
                                <DiffuseMaterial Color="Blue" />
                            </GeometryModel3D.BackMaterial>
                        </GeometryModel3D>

                        <!-- right  -->
                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D
                            Positions="10 0 0, 10 0 -.5, 10 16 0, 10 16 -.5, 10 0 -.5"
                            TriangleIndices="0, 1, 2, 2, 4, 3" />
                            </GeometryModel3D.Geometry>

                            <GeometryModel3D.Material>
                                <MaterialGroup>
                                    <DiffuseMaterial Brush="{StaticResource top}" />
                                </MaterialGroup>
                            </GeometryModel3D.Material>
                            <GeometryModel3D.BackMaterial>
                                <DiffuseMaterial Color="Blue" />
                            </GeometryModel3D.BackMaterial>
                        </GeometryModel3D>


                        <!-- top side -->
                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D
                            Positions="0 16 0, 0 16 -.5, 10 16 0, 10 16 -.5, 0 16 -.5"
                            TriangleIndices="0, 2, 1, 2, 3, 4"
                                TextureCoordinates="0 1, 0 0, 1 1, 1 0, 0 0"
                                />
                            </GeometryModel3D.Geometry>

                            <GeometryModel3D.Material>
                                <MaterialGroup>
                                    <DiffuseMaterial Brush="{StaticResource pages}" />
                                </MaterialGroup>

                            </GeometryModel3D.Material>
                            <GeometryModel3D.BackMaterial>
                                <DiffuseMaterial Color="Blue" />
                            </GeometryModel3D.BackMaterial>
                        </GeometryModel3D>

                        <!-- bottom -->
                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D
                            Positions="0 0 0, 0 0 -.5, 10 0 0, 10 0 -.5, 0 0 -.5"
                            TriangleIndices="0, 1, 2, 2, 4, 3"
                                TextureCoordinates="0 1, 0 0, 1 1, 1 0, 0 0"
                                />
                            </GeometryModel3D.Geometry>

                            <GeometryModel3D.Material>
                                <DiffuseMaterial Brush="{StaticResource pages}" />
                            </GeometryModel3D.Material>
                            <GeometryModel3D.BackMaterial>
                                <DiffuseMaterial Color="Blue" />
                            </GeometryModel3D.BackMaterial>
                        </GeometryModel3D>

                        <!-- back -->
                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D
                            Positions="0 0 -.5, 10 0 -.5, 0 16 -.5, 10 16 -.5, 10 0 -.5"
                            TriangleIndices="0, 2, 1, 2, 3, 4" />
                            </GeometryModel3D.Geometry>

                            <GeometryModel3D.Material>
                                <MaterialGroup>
                                    <DiffuseMaterial Brush="{StaticResource back}" />
                                </MaterialGroup>
                            </GeometryModel3D.Material>
                            <GeometryModel3D.BackMaterial>
                                <DiffuseMaterial Color="Blue" />
                            </GeometryModel3D.BackMaterial>
                        </GeometryModel3D>




                    </Model3DGroup>

                </ModelVisual3D.Content>
            </ModelVisual3D>

        </Viewport3D>
    </Grid>

You're doing a couple of things wrong here. 您在这里做错了几件事。 The Viewport2DVisual3D doesn't go inside a GeometryModel3D, it replaces it. Viewport2DVisual3D不在GeometryModel3D内,而是将其替换。 This also means that you have to pull it outside the Model3DGroup. 这也意味着您必须将其拉到Model3DGroup之外。 Finally, you need to set Viewport2DVisual3D.IsVisualHostMaterial so that WPF knows to use the brush for the 2D elements as well. 最后,您需要设置Viewport2DVisual3D.IsVisualHostMaterial以便WPF知道也将画笔用于2D元素。 This should be enough to get you started: 这应该足以让您入门:

<Grid>
    <Viewport3D>
        <Viewport3D.Camera>
            <PerspectiveCamera Position="-10,0,50" LookDirection="15,10,-50" />
        </Viewport3D.Camera>

        <ModelVisual3D>
            <ModelVisual3D.Content>
                <!--<AmbientLight Color="White" />-->
                <SpotLight Color="White" InnerConeAngle="20" OuterConeAngle="60" Direction="15,6,-50" Position="0,0,25" />
            </ModelVisual3D.Content>
        </ModelVisual3D>


        <!-- front -->
        <Viewport2DVisual3D>
            <Viewport2DVisual3D.Geometry>
                <MeshGeometry3D
                            Positions="0 0 0, 10 0 0, 0 16 0, 10 16 0, 10 0 0"
                            TriangleIndices="0, 1, 2, 2, 4, 3"
                            TextureCoordinates="0 1, 1 1, 0 0, 1 0, 1 1"
                            />
            </Viewport2DVisual3D.Geometry>

            <Viewport2DVisual3D.Material>
                <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="White"/>
            </Viewport2DVisual3D.Material>

            <StackPanel>
                <Button>Hello, 3D</Button>
            </StackPanel>

        </Viewport2DVisual3D>

    </Viewport3D>
</Grid>

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM