如何在wpf 3d立方体中插入2d控件

时间:2018-11-07 04:25:59

标签: c# .net wpf vb.net xaml

我在WPF中使用Viewport3D创建了一个3d立方体。由于要在每个面上显示不同的内容,所以我在Viewport中创建了6个GeometryModel3D。我想在一个面(正面)上插入一个用户交互按钮控件我知道我们应该为此使用Viewport2DVisual3D。但是我找不到解决方案,可以使用Viewport2DVisual3D在脸部插入第二个控件,请帮帮我。以下是我编写的Xamal代码sofer:< / p>

<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>

1 个答案:

答案 0 :(得分:0)

您在这里做错了几件事。 Viewport2DVisual3D不在GeometryModel3D内,而是将其替换。这也意味着您必须将其拉到Model3DGroup之外。最后,您需要设置Viewport2DVisual3D.IsVisualHostMaterial,以便WPF知道也将画笔用于2D元素。这应该足以让您入门:

<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>

enter image description here