【问题标题】:How to insert 2d control in the wpf 3d cube如何在 wpf 3d 立方体中插入 2d 控件
【发布时间】:2018-11-07 04:25:59
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    你在这里做错了几件事。 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>
    

    【讨论】:

    • 谢谢...但是我想要立方体表面上的这个按钮,这样每当用户旋转立方体时,这个按钮就会粘在立方体表面并旋转
    猜你喜欢
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 2010-12-27
    • 2014-10-13
    • 1970-01-01
    • 2013-02-04
    相关资源
    最近更新 更多