旋转3d模型XAML c#

时间:2012-08-08 16:18:38

标签: c# xaml model 3d edit

我写了一个程序, 以XAML格式导入3d模型:

<Viewport3D x:Name="ZAM3DViewport3D" ClipToBounds="true" Width="400" Height="300" 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/interactivedesigner/2006" xmlns:c="http://schemas.openxmlformats.org/markup-compatibility/2006" c:Ignorable="d">
<Viewport3D.Resources>
    <ResourceDictionary>/**other Elements at the same Level**/
        <Transform3DGroup x:Key="root_boneOR13TR12" >
            <TranslateTransform3D OffsetX="-0" OffsetY="-0" OffsetZ="-0"/>
            <ScaleTransform3D ScaleX="1" ScaleY="1" ScaleZ="1"/>
            <RotateTransform3D>
                <RotateTransform3D.Rotation>
                    <AxisAngleRotation3D Angle="90.00000933" Axis="-1 0 0"/>
                </RotateTransform3D.Rotation>
            </RotateTransform3D>
            <TranslateTransform3D OffsetX="-0.00299196" OffsetY="0.00964678" OffsetZ="0.000916864"/>
        </Transform3DGroup>
    </ResourceDictionary>
</Viewport3D.Resources>

<Viewport3D.Camera>
    <PerspectiveCamera x:Name="FrontOR6" FarPlaneDistance="10" LookDirection="0,0,-1" UpDirection="0,1,0" NearPlaneDistance="0.1" Position="0,-0.0439422,0.729628" FieldOfView="39.5978" />
</Viewport3D.Camera>

<ModelVisual3D>
    <ModelVisual3D.Content>
        <Model3DGroup x:Name="Scene" Transform="{DynamicResource SceneTR7}"> <!-- Scene (XAML Path = ) -->
            <AmbientLight Color="#646464" />
            <DirectionalLight Color="#FFFFFF" Direction="-0.612372,-0.5,-0.612372" />
            <DirectionalLight Color="#FFFFFF" Direction="0.612372,-0.5,-0.612372" />
            <Model3DGroup x:Name="rootOR9" Transform="{DynamicResource rootOR9TR8}"> <!-- root (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3]) -->
                <Model3DGroup x:Name="glasses_meOR11" Transform="{DynamicResource glasses_meOR11TR10}"> <!-- glasses_me (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[0]) -->
                    <Model3DGroup x:Name="root_boneOR13" Transform="{DynamicResource root_boneOR13TR12}"> <!-- root_bone (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0]) -->
                        <Model3DGroup x:Name="main_boneOR15" Transform="{DynamicResource main_boneOR15TR14}"> <!-- main_bone (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0]) -->
                        </Model3DGroup>
                        <Model3DGroup x:Name="right_boneOR19" Transform="{DynamicResource right_boneOR19TR18}"> <!-- right_bone (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[1]) -->
                            <Model3DGroup x:Name="right_bon0OR21" Transform="{DynamicResource right_bon0OR21TR20}"> <!-- right_bon0 (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[1].(Model3DGroup.Children)[0]) -->
                            </Model3DGroup>
                        </Model3DGroup>
                        <Model3DGroup x:Name="left_bone_OR25" Transform="{DynamicResource left_bone_OR25TR24}"> <!-- left_bone_ (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[2]) -->
                            <Model3DGroup x:Name="left_boneOR27" Transform="{DynamicResource left_boneOR27TR26}"> <!-- left_bone (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[2].(Model3DGroup.Children)[0]) -->
                            </Model3DGroup>
                        </Model3DGroup>
                    </Model3DGroup>
                    <GeometryModel3D x:Name="glasses_meOR11GR12" Geometry="{DynamicResource glasses_meOR11GR12}" Material="{DynamicResource ER___Default_MaterialMR1}" BackMaterial="{DynamicResource ER___Default_MaterialMR1}"/>
                </Model3DGroup>
                <Model3DGroup x:Name="glasses_m0OR32" Transform="{DynamicResource glasses_m0OR32TR31}"> <!-- glasses_m0 (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[1]) -->
                    <Model3DGroup x:Name="root_bone1OR34" Transform="{DynamicResource root_bone1OR34TR33}"> <!-- root_bone1 (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[1].(Model3DGroup.Children)[0]) -->
                        <Model3DGroup x:Name="main_bone1OR36" Transform="{DynamicResource main_bone1OR36TR35}"> <!-- main_bone1 (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[1].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0]) -->
                            <Model3DGroup x:Name="main_eff1OR38" Transform="{DynamicResource main_eff1OR38TR37}"> <!-- main_eff1 (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[1].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0]) -->
                                <Model3DGroup x:Name="right_bon1OR40" Transform="{DynamicResource right_bon1OR40TR39}"> <!-- right_bon1 (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[1].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0]) -->
                                    <Model3DGroup x:Name="right_bon2OR42" Transform="{DynamicResource right_bon2OR42TR41}"> <!-- right_bon2 (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[1].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0]) -->
                                    </Model3DGroup>
                                </Model3DGroup>
                                <Model3DGroup x:Name="left_bone0OR46" Transform="{DynamicResource left_bone0OR46TR45}"> <!-- left_bone0 (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[1].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[1]) -->
                                    <Model3DGroup x:Name="left_bone1OR48" Transform="{DynamicResource left_bone1OR48TR47}"> <!-- left_bone1 (XAML Path = (Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[1].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0].(Model3DGroup.Children)[1].(Model3DGroup.Children)[0]) -->
                                    </Model3DGroup>
                                </Model3DGroup>
                            </Model3DGroup>
                        </Model3DGroup>
                    </Model3DGroup>
                </Model3DGroup>
            </Model3DGroup>
        </Model3DGroup>
    </ModelVisual3D.Content>
</ModelVisual3D>

我想通过C#访问Transform3DGroup x:Key =“root_boneOR13TR12”元素 并且如果需要,可以修改AxisAngelRotation属性。

此致 bbklol

1 个答案:

答案 0 :(得分:3)

让我们从定义故事板开始:当你运行它时,它会实际旋转你的对象!

然后,通过一些动画(同样DoubleAnimation效果很好),您可以设置RotateTransform3D的Rotation属性(见下文)。

在你的情况下,为了回答你的问题,我认为你应该使用非常接近的语法(我保留你的XAML路径,用[ ]签名....删除它们!!):

Storyboard.TargetProperty="[(Viewport3D.Children)[0].(ModelVisual3D.Content).(Model3DGroup.Children)[3].(Model3DGroup.Children)[0].(Model3DGroup.Children)[0])].(ModelVisual3D.Transform).(Transform3DGroup.Children)[2].(RotateTransform3D.Rotation).(AxisAngleRotation3D.Angle)"

我给你看一个例子,也许你可以用更简单的方式理解......

这是一个故事板,它将旋转下面ViewPort3D中的项目:

<Storyboard x:Key="RotationStoryboard">
    <!--This is an horizontal rotation-->
    <DoubleAnimation Storyboard.TargetProperty="(Viewport3D.Children)[0].(ModelVisual3D.Transform).(Transform3DGroup.Children)[0].(RotateTransform3D.Rotation).(AxisAngleRotation3D.Angle)"
        BeginTime="0:0:0.25"
        From="360"
        To="0"
        Duration="0:0:4.5"
        RepeatBehavior="Forever" />
    <!--This is an orbital horizontal rotation (I removed the other rotation on first transformation)-->
    <DoubleAnimation Storyboard.TargetProperty="(Viewport3D.Children)[1].(ModelVisual3D.Transform).(Transform3DGroup.Children)[3].(RotateTransform3D.Rotation).(AxisAngleRotation3D.Angle)"
        From="360"
        To="0"
        RepeatBehavior="Forever"
        Duration="0:0:3.5" />
</Storyboard>

这是对象的XAML代码...

<Viewport3D>
    <Viewport3D.Children>
        <!--First object-->
        <ModelVisual3D>
            <ModelVisual3D.Transform>
                <Transform3DGroup>
                    <RotateTransform3D>
                        <RotateTransform3D.Rotation>
                            <AxisAngleRotation3D Angle="360"
                                                 Axis="0,1,0" />
                            </RotateTransform3D.Rotation>
                    </RotateTransform3D>
                </Transform3DGroup>
            </ModelVisual3D.Transform>
            <model3d:Sphere3D />
        </ModelVisual3D>
        <!--Second object-->
        <ModelVisual3D>
            <ModelVisual3D.Transform>
                <Transform3DGroup>
                    <RotateTransform3D>
                        <RotateTransform3D.Rotation>
                            <AxisAngleRotation3D Angle="360" 
                                                 Axis="0,1,0" />
                        </RotateTransform3D.Rotation>
                    </RotateTransform3D>
                    <ScaleTransform3D ScaleZ="0"
                                      ScaleY="0"
                                      ScaleX="0"/>
                    <TranslateTransform3D OffsetX="3.5" />
                    <RotateTransform3D CenterX="0"
                                       CenterY="0"
                                       CenterZ="0">
                        <RotateTransform3D.Rotation>
                            <AxisAngleRotation3D Angle="360" 
                                                 Axis="0,1,0" />
                        </RotateTransform3D.Rotation>
                    </RotateTransform3D>
                </Transform3DGroup>
            </ModelVisual3D.Transform>
            <model3d:Sphere3D x:Name="SatelliteSphere">
                    <model3d:Sphere3D.Transform>
                        <RotateTransform3D CenterX="-1"
                                           CenterY="-1"
                                           CenterZ="-1" />
                    </model3d:Sphere3D.Transform>
            </model3d:Sphere3D>
        </ModelVisual3D>
    </Viewport3D.Children>
</Viewport3D>