简体   繁体   English

尝试移动对象时,ModelVisual3D转换为null

[英]ModelVisual3D Transform null when trying to move a object

I am trying to move a cube clicking on it and tracking the mouse. 我正在尝试移动一个多维数据集,然后单击它并跟踪鼠标。 My cube is implemented like this: 我的多维数据集是这样实现的:

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 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> 

I have a TrackballDecorator around my viewport, which holds the events: 我的视口周围有一个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... 

And my first event, OnMouseLeftButtonDown, I took from http://www.charlespetzold.com/3D/ , example Mouse Tracking, which is: 我的第一个事件OnMouseLeftButtonDown是从http://www.charlespetzold.com/3D/摘录的,示例为Mouse Tracking,它是:

 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;
    } 

I updated it, removing the overrides and put a object sender in the parameters. 我对其进行了更新,删除了替代,并将对象发送者放入参数中。 Is there something very obvious which I am missing for my ModelVisual3D Transform being null? 我的ModelVisual3D Transform为null是否缺少一些非常明显的东西?

Thanks in advance, if you need any more information to help me, just ask :D 在此先感谢您,如果您需要更多信息以帮助我,请询问:D

Is vis3d.Transform set to anything? vis3d.Transform是否设置为任何值?

If it's not that's the problem, but if it is then check it is actually a TranslateTransform3D . 如果不是,那是问题,但是如果是,请检查它实际上是TranslateTransform3D If it's something else then the as conversion won't be valid and will return null . 如果还有其他原因,那么as转换将无效,并将返回null

You've said that it's actually a MatrixTransform3D which is a general matrix transformation. 您已经说过,它实际上是MatrixTransform3D ,它是常规的矩阵转换。 Change your line to: 将行更改为:

transTracking = vis3d.Transform as MatrixTransform3D;

and it should work. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM