04-04-2013, 10:38 PM
I have checked the code again and found a very interesting cause of the problem - the MeshGeometry for the 3D lines is generated based on the camera position - actually a camera matrix is calculated from the current camera position - but we also need aspect ration of the viewport where we are showing the 3D scene - so we also need the size of the Viewport3D. By default Viewport3D's ActualWidth and ActualHeight are used, but when they are not set the Width and Height can be used as fallback.
So the only thing that was missing was to set the Width and Height for Viewport.
So with that knowledge we can create the geometry anywhere in the code (no need to show anything) - the following creates a 3D Arc:
After the lineArcVisual3D is added to viewport3D, the Content of the lineArcVisual3D will contain an actual GeometryModel3D.
IMPORTANT:
I used BeginInit and EndInit when setting properties on LineArcVisual3D. This can be a big performance optimization - without that the 3D geometry can be recreated multiple times (in our case we would get 6 GeometryModel3D). When using BeginInit and EndInit the geometry is recreated only once.
So the only thing that was missing was to set the Width and Height for Viewport.
So with that knowledge we can create the geometry anywhere in the code (no need to show anything) - the following creates a 3D Arc:
Code:
var viewport3D = new Viewport3D();
viewport3D.Width = 800;
viewport3D.Height = 600;
var targetPositionCamera = new TargetPositionCamera();
targetPositionCamera.TargetPosition = new Point3D(0, 0, 0);
targetPositionCamera.Distance = 200;
targetPositionCamera.Attitude = -30;
targetPositionCamera.TargetViewport3D = viewport3D;
targetPositionCamera.Refresh();
var lineArcVisual3D = new LineArcVisual3D();
lineArcVisual3D.BeginInit();
lineArcVisual3D.Radius = 30;
lineArcVisual3D.StartAngle = 0;
lineArcVisual3D.EndAngle = 90;
lineArcVisual3D.CircleNormal = new Vector3D(0, 0, 1);
lineArcVisual3D.ZeroAngleDirection = new Vector3D(1, 0, 0);
lineArcVisual3D.LineThickness = 3;
lineArcVisual3D.LineColor = Colors.Red;
lineArcVisual3D.EndInit();
viewport3D.Children.Add(lineArcVisual3D);After the lineArcVisual3D is added to viewport3D, the Content of the lineArcVisual3D will contain an actual GeometryModel3D.
IMPORTANT:
I used BeginInit and EndInit when setting properties on LineArcVisual3D. This can be a big performance optimization - without that the 3D geometry can be recreated multiple times (in our case we would get 6 GeometryModel3D). When using BeginInit and EndInit the geometry is recreated only once.
Andrej Benedik

