Search This Blog

Wednesday, November 13, 2013

Create Cube XAML WPF Viewport3D

 WPF > Graphics > 3-D > Create Cube

In this example you will create a cube like this:


XAML:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Viewport3D x:Name="vp" RenderTransformOrigin="0.505,0.49" Margin="0,10,0,-10">
            <Viewport3D.Camera>
                <PerspectiveCamera x:Name="pc" Position="10 10 10" LookDirection="-10 -10 -10"/>
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <DirectionalLight x:Name="dirMain" Direction="-1,-1,-1">
                        <DirectionalLight.Transform>
                            <Transform3DGroup>
                                <TranslateTransform3D OffsetZ="0" OffsetX="0" OffsetY="0"/>
                                <ScaleTransform3D ScaleZ="1" ScaleY="1" ScaleX="1"/>
                                <RotateTransform3D d:EulerAngles="0,0,0">
                                    <RotateTransform3D.Rotation>
                                        <AxisAngleRotation3D Axis="0,1,0" Angle="0"/>
                                    </RotateTransform3D.Rotation>
                                </RotateTransform3D>
                                <TranslateTransform3D OffsetZ="0" OffsetX="0" OffsetY="0"/>
                                <TranslateTransform3D OffsetX="-1.309" OffsetZ="1.856" OffsetY="0.556"/>
                            </Transform3DGroup>
                        </DirectionalLight.Transform>
                    </DirectionalLight>
                </ModelVisual3D.Content>
                <ModelVisual3D.Transform>
                    <Transform3DGroup>
                        <TranslateTransform3D OffsetZ="0" OffsetX="0" OffsetY="0"/>
                        <ScaleTransform3D ScaleZ="1" ScaleY="1" ScaleX="1"/>
                        <RotateTransform3D d:EulerAngles="-8.693,-2.029,4.185">
                            <RotateTransform3D.Rotation>
                                <AxisAngleRotation3D Axis="-0.88,-0.239,0.411" Angle="9.791"/>
                            </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                        <TranslateTransform3D OffsetZ="0" OffsetX="0" OffsetY="0"/>
                        <TranslateTransform3D OffsetZ="0" OffsetX="0" OffsetY="0"/>
                    </Transform3DGroup>
                </ModelVisual3D.Transform>
            </ModelVisual3D>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D x:Name="mg"
                                                       Positions="0,0,0 2,0,0 0,2,0 2,2,0 0,0,2 2,0,2 0,2,2 2,2,2"
                                                       TriangleIndices="2 3 1  2 1 0  7 1 3  7 5 1  6 5 7  6 4 5  6 2 0  2 0 4  2 7 3  2 6 7  0 1 5  0 5 4"/>
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Blue" Opacity="1.0" />
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                    </GeometryModel3D>
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D>
    </Grid>
</Window>