ModelVisual3D尝试移动对象时变换null

时间:2011-11-10 10:45:05

标签: c# wpf 3d

我正在尝试移动一个立方体点击它并跟踪鼠标。我的立方体实现如下:

的App.xaml

<MeshGeometry3D x:Key="solidCube" 
Positions="2, 2, 2 -2, 2, 2 -2, -2, 2 2, -2, 2 2, 2, -2 -2, 2, -2 -2, -2, -2 2, -2, -2"
TriangleIndices="0,1,2 2,3,0 3,4,0 7,4,3 5,4,7 7,6,5 1,5,6 6,2,1 2,6,7 7,3,2 1,0,4 4,5,1"/> 

MainWindow.xaml

<ModelVisual3D.Content>
    <GeometryModel3D x:Name="solidCubeGeometryModel3D" Geometry="{StaticResource solidCube}">
            <GeometryModel3D.Material>
                    <DiffuseMaterial>
                            <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Red" Opacity="1.0"/>
                            </DiffuseMaterial.Brush>
                    </DiffuseMaterial>
            </GeometryModel3D.Material>
            <GeometryModel3D.Transform>
                <TranslateTransform3D x:Name="myTranslateTransform3D" OffsetX="0" OffsetY="0" OffsetZ="0" />
            </GeometryModel3D.Transform>
    </GeometryModel3D>
</ModelVisual3D.Content> 

我的视口周围有一个TrackballDecorator,用于保存事件:

<custom:TrackballDecorator x:Name="trackballDecorator"         Grid.Column="1"MouseLeftButtonDown="OnMouseLeftButtonDown"
MouseMove="OnMouseMove"
MouseWheel="OnMouseWheel"
MouseLeftButtonUp="OnMouseLeftButtonUp"
PreviewTextInput="OnPreviewTextInput"
LostMouseCapture="OnLostMouseCapture"
Width="600" Height="600">

<custom:Interactive3DDecorator Grid.Column="1">

/// viewport etc... 

我的第一个事件OnMouseLeftButtonDown,我从http://www.charlespetzold.com/3D/获取了鼠标跟踪示例,其中包括:

 protected void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs args)
        {
        base.OnMouseLeftButtonDown(args);

        Point ptMouse = args.GetPosition(myViewport);
        HitTestResult result = VisualTreeHelper.HitTest(myViewport, ptMouse);

        // We're only interested in 3D hits.
        RayMeshGeometry3DHitTestResult result3d =
                            result as RayMeshGeometry3DHitTestResult;
        if (result3d == null)
            return;

        // We're only interested in ModelVisual3D hits.
        ModelVisual3D vis3d = result3d.VisualHit as ModelVisual3D;
        if (vis3d == null)
            return;

        // We're only interested in visuals with translate transforms.
        transTracking = vis3d.Transform as TranslateTransform3D; //NULL !!!!!!!!!!!
        if (transTracking == null)
            return;

        LineRange range;
        ViewportInfo.Point2DtoPoint3D(myViewport, ptMouse, out range);
        pointOriginal = range.PointFromZ(transTracking.OffsetZ);
        transOriginal = transTracking.Clone();
        isTracking = true;
        CaptureMouse();
        Focus();

        args.Handled = true;
    } 

我更新了它,删除了覆盖并在参数中放置了一个对象发送器。 我的ModelVisual3D转换是否为null,是否有一些非常明显的东西?

提前致谢,如果您需要更多信息来帮助我,请问:D

1 个答案:

答案 0 :(得分:0)

vis3d.Transform设置为什么?

如果不是那个问题,但如果是,那么检查它实际上是TranslateTransform3D。如果它是其他内容,那么as转换将无效并将返回null

你说它实际上是MatrixTransform3D,这是一般的矩阵变换。将您的行更改为:

transTracking = vis3d.Transform as MatrixTransform3D;

它应该有用。