当尝试移动一个对象时,将ModelVisual3D转换为null

本文关键字:ModelVisual3D 转换 null 移动 一个对象 | 更新日期: 2023-09-27 18:13:57

我正在尝试移动一个立方体,点击它并跟踪鼠标。我的多维数据集是这样实现的:

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在我的viewport,它保存事件:

<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

当尝试移动一个对象时,将ModelVisual3D转换为null

vis3d.Transform是否设置为任何值?

如果它不是那就是问题,但如果它是然后检查它实际上是一个TranslateTransform3D。如果它是其他东西,那么as转换将无效,并将返回null

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

transTracking = vis3d.Transform as MatrixTransform3D;