English 中文(简体)
示范法 试图移动物体时的“变”
原标题:ModelVisual3D Transform null when trying to move a object
  • 时间:2011-11-10 10:45:05
  •  标签:
  • c#
  • wpf
  • 3d

我正试图移动一只 click子,点击它并跟踪 mo。 我的多瑙河就是这样:

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> 

I have a TrackballDecorator around my viewport, which holds the events:

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

我的第一次活动,OnMouseLeftButton Down, I taken from , example Mouse Tracking, which is:

 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?

事先感谢,如果你需要更多的信息帮助我,仅问:D

问题回答

<代码>vis3d. Transform set to any any?

如果它不提出这一问题,但如果加以核对,则实际上是一个<条码>翻译3D。 如果有的话,<代码>as的换算率则有效,并将返回<代码>null<>。

You ve说,它实际上是一个Matrix Transform3D,这是一个总体矩阵转变。 1. 修改以下案文:

transTracking = vis3d.Transform as MatrixTransform3D;

它应当发挥作用。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签