I want to click on a model in a Viewport3D and find the 3D coordinates of a model. I want the transformed coordinates.
Suppose you have this in a Viewport3D:
<ModelVisual3D x:Name="yellowTriangle">
<ModelVisual3D.Content>
<GeometryModel3D>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="-1,0,0 0,1,0 1,0,0" TriangleIndices="0,2,1"/>
</GeometryModel3D.Geometry>
<GeometryModel3D.Transform>
<Transform3DGroup>
<TranslateTransform3D OffsetX="10" OffsetY="20" OffsetZ="-10"/>
<ScaleTransform3D ScaleX="1" ScaleY="1.5" ScaleZ="1"/>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D Angle="20" Axis="1 0 0"/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
<TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
</Transform3DGroup>
</GeometryModel3D.Transform>
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Yellow"/>
</GeometryModel3D.Material>
</GeometryModel3D>
</ModelVisual3D.Content>
Using a VisualTreehelper.HitTest I can get to the MeshGeometry3D and the Point3D Positions. However, I can only get the original Point3D's : -1,0,0 etc. How do I get the transformed Point3D's ?
According to Ericsink's autozoom remarks I should:
walk up the visual tree from every MeshGeometry3D and stop to apply every Transform >object it finds along the way
But I don't succeed. Anyone has a way to reach to the ModelVisual3D of a MeshGeometry3D ?
Or a faster way to reach those transformed Point3D's ?
Adding the hittest code:
private void Viewport3D_MouseDown(object sender, MouseButtonEventArgs e)
{
RayMeshGeometry3DHitTestResult rayMeshResult = (RayMeshGeometry3DHitTestResult)
VisualTreeHelper.HitTest(mainViewport, e.GetPosition(mainViewport));
if (rayMeshResult.MeshHit is MeshGeometry3D)
{
MeshGeometry3D mesh = (MeshGeometry3D)rayMeshResult.MeshHit;
foreach (Point3D point3d in mesh.Positions)
{
MessageBox.Show("original point: " + point3d.ToString());
Point3D p = new Point3D();
// Using the Modelhit now because the meshhit doesn't contain a transform
Transform3D trans = rayMeshResult.ModelHit.Transform;
p = trans.Transform(point3d);
MessageBox.Show("transformed point: " + p.ToString());
}
}
}
I cleaned up my hittest code and it works with the "yellowTriangle" above. The problem only occurs with my derived Rectangle3D code based on Daniel Lehenbauer's extending Visual3d: "http://blogs.msdn.com/danlehen/archive/2005/10/16/481597.aspx"
Something like this doesn't give me the correct transformations:
<primitive3D:Rectangle3D>
<primitive3D:Rectangle3D.Material>
<DiffuseMaterial Brush="Green"/>
</primitive3D:Rectangle3D.Material>
<primitive3D:Rectangle3D.Transform>
<Transform3DGroup>
<TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
<ScaleTransform3D ScaleX="1" ScaleY="1" ScaleZ="10"/>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D Angle="30" Axis="0 1 0"/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
<TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
</Transform3DGroup>
</primitive3D:Rectangle3D.Transform>
</primitive3D:Rectangle3D>
I guess my derived rectangle3D should have a DependencyProperty for the Transformations ?