locked
How to make a 3d object as a button? RRS feed

  • Question

  • anyone here knows how to make a 3d object a button? i imported the 3d object from sketchup, when im trying to make a button the children of viewport3d i dont see the list of event. Now im confused on how can i make it. someone please help me. if it needs to code teach me how to code it using c#. thanks in advance.
    Sunday, October 16, 2011 2:37 AM

All replies

  • Paste this into any xaml view and you have the button with 3d content. Use the events of button.

     

      <Page.Resources>
              <MeshGeometry3D x:Key="plane" Normals="0,-1,0 0,-1,0 0,-1,0 0,-1,0" Positions="-0.5,0,0.5 0.5,0,-0.5 0.5,0,0.5 -0.5,0,-0.5" TextureCoordinates="0,1 1,0 1,1 0,0" TriangleIndices="0 1 2 1 0 3"/>

         <Style TargetType="{x:Type Button}">
          <Style.Resources>
            <Storyboard x:Key="Spin">
              <DoubleAnimation
                Storyboard.TargetName="CubeRotation"
                Storyboard.TargetProperty="Angle"
                Duration="0:0:5" From="0" To="360" />
            </Storyboard>
          </Style.Resources>
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate>
                <ControlTemplate.Triggers>
                  <Trigger Property="Button.IsPressed" Value="true">
                    <Trigger.EnterActions>
                      <BeginStoryboard Storyboard="{StaticResource Spin}"/>
                    </Trigger.EnterActions>
                  </Trigger>
                </ControlTemplate.Triggers>
                <Viewport3D>
                  <Viewport3D.Camera>
                    <PerspectiveCamera Position="4,4,4" LookDirection="-1,-1,-1" />
                  </Viewport3D.Camera>
                  <Viewport3D.Children>
                    <ModelVisual3D>
                      <ModelVisual3D.Content>
                        <DirectionalLight Direction="-0.3,-0.4,-0.5" />
                      </ModelVisual3D.Content>
                    </ModelVisual3D>
                    <ModelVisual3D x:Name="Cube">
                      <ModelVisual3D.Transform>
                        <RotateTransform3D>
                          <RotateTransform3D.Rotation>
                            <AxisAngleRotation3D x:Name="CubeRotation" Axis="0,1,0" Angle="0" />
                          </RotateTransform3D.Rotation>
                        </RotateTransform3D>
                      </ModelVisual3D.Transform>
                      <ModelVisual3D.Content>
                        <GeometryModel3D x:Name="OB_Cube">
                          <GeometryModel3D.Material>
                            <DiffuseMaterial>
                              <DiffuseMaterial.Brush>
                                <VisualBrush ViewportUnits="Absolute" Transform="1,0,0,-1,0,1">
                                  <VisualBrush.Visual>
                                    <Border Background="{Binding Path=Background, RelativeSource='{RelativeSource TemplatedParent}'}">
                                      <Label Content="{Binding Path=Content, RelativeSource='{RelativeSource TemplatedParent}'}" />
                                    </Border>
                                  </VisualBrush.Visual>
                                </VisualBrush>
                              </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                          </GeometryModel3D.Material>
                          <GeometryModel3D.Geometry>
                            <MeshGeometry3D x:Name="ME_Cube"
                              Positions="1,1,-1 1,-1,-1 -1,-1,-1 -1,1,-1 1,1,1 -1,1,1 -1,-1,1 1,-1,1 1,1,-1 1,1,1 1,-1,1 1,-1,-1 1,-1,-1 1,-1,1 -1,-1,1 -1,-1,-1 -1,-1,-1 -1,-1,1 -1,1,1 -1,1,-1 1,1,1 1,1,-1 -1,1,-1 -1,1,1"
                              TriangleIndices="0 1 2 0 2 3 4 5 6 4 6 7 8 9 10 8 10 11 12 13 14 12 14 15 16 17 18 16 18 19 20 21 22 20 22 23"
                              TextureCoordinates="0,1 0,0 1,0 1,1 1,1 -0,1 0,-0 1,0 1,1 -0,1 0,-0 1,0 1,0 1,1 -0,1 0,-0 -0,0 1,-0 1,1 0,1 1,-0 1,1 0,1 -0,0"/>
                          </GeometryModel3D.Geometry>
                        </GeometryModel3D>
                      </ModelVisual3D.Content>
                    </ModelVisual3D>
                  </Viewport3D.Children>
                </Viewport3D>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </Page.Resources>
      <Grid>
       <Button Width="100" Height="100" VerticalAlignment="Center" x:Name="zoomButton" >
              <Image Height="250" Source="large.png"/>
            </Button>
      </Grid>


    Creativity Flows
    • Edited by NatrajB Monday, October 17, 2011 5:03 PM
    • Proposed as answer by justin.m.chase Monday, October 17, 2011 10:29 PM
    Monday, October 17, 2011 4:52 PM