我正试图移动一只 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